__heapstats()
Defined in stdlib.h, the __heapstats() function displays statistics on the state of the storage allocation heap.
__heapvalid()
calloc(), free(), malloc(), realloc()
The heap functions are thread-safe if the _mutex_* functions are implemented.
All threads share a single heap and use mutexes to avoid data corruption when there is concurrent access.
Each heap implementation is responsible for doing its own locking. If you supply your own allocator, it must
also do its own locking. This enables it to do fine-grained locking if required, rather than protecting the entire
heap with a single mutex (coarse-grained locking).
How to ensure re-implemented mutex functions are called:
If your re-implemented _mutex_*() functions are within an object that is contained within a library file, the linker does not automatically include the object.
This can result in the _mutex_*() functions being excluded from the image you have built.
To avoid this problem, that is, to ensure that your _mutex_*() functions are called, you can either:
Place your mutex functions in a non-library object file. This helps to ensure that they are resolved at link time.
Place your mutex functions in a library object file, and arrange a non-weak reference to something in the object.
Place your mutex functions in a library object file, and have the linker explicitly extract the specific object from the library on the command line by writing libraryname.a(objectfilename.o) when you invoke the linker.
在 RTX_CM_LIB.H 里面实现了:
/*----------------------------------------------------------------------------
* Standard Library multithreading interface
*---------------------------------------------------------------------------*/
int _mutex_initialize (OS_ID *mutex) {
/* Allocate and initialize a system mutex. */
if (nr_mutex >= OS_MUTEXCNT) {
/* If you are here, you need to increase the number OS_MUTEXCNT. */
for (;;);
}
*mutex = &std_libmutex[nr_mutex++];
mutex_init (*mutex);
return (1);
}