Contiki-NG
|
The heapmem module is a dynamic heap memory allocator similar to malloc() in standard C. More...
Files | |
file | heapmem.h |
Header file for the dynamic heap memory allocator. | |
Functions | |
void * | heapmem_alloc (size_t size) |
Allocate a chunk of memory in the heap. More... | |
void * | heapmem_realloc (void *ptr, size_t size) |
Reallocate a chunk of memory in the heap. More... | |
void | heapmem_free (void *ptr) |
Deallocate a chunk of memory. More... | |
void | heapmem_stats (heapmem_stats_t *stats) |
Obtain internal heapmem statistics regarding the allocated chunks. More... | |
The heapmem module is a dynamic heap memory allocator similar to malloc() in standard C.
The heap memory is managed in a block of static memory, whose size is determined at compile-time by setting HEAPMEM_CONF_ARENA_SIZE parameter. By default, the heap memory is only 1 bytes, which entails that this parameter must be set explicitly in order to be possible to use this module.
Each allocated memory object is referred to as a "chunk". The allocator manages free chunks in a double-linked list. While this adds some memory overhead compared to a single-linked list, it improves the performance of list management.
Internally, allocated chunks can be retrieved using the pointer to the allocated memory returned by heapmem_alloc() and heapmem_realloc(), because the chunk structure immediately precedes the memory of the chunk.
void* heapmem_alloc | ( | size_t | size | ) |
Allocate a chunk of memory in the heap.
size | The number of bytes to allocate. |
void heapmem_free | ( | void * | ptr | ) |
Deallocate a chunk of memory.
ptr | A pointer to a chunk that has been allocated using heapmem_alloc() or heapmem_realloc(). |
void* heapmem_realloc | ( | void * | ptr, |
size_t | size | ||
) |
Reallocate a chunk of memory in the heap.
ptr | A pointer to a chunk that has been allocated using heapmem_alloc() or heapmem_realloc(). |
size | The number of bytes to allocate. |
void heapmem_stats | ( | heapmem_stats_t * | stats | ) |
Obtain internal heapmem statistics regarding the allocated chunks.
stats | A pointer to an object of type heapmem_stats_t, which will be filled when calling this function. |
This function makes it possible to gain visibility into the internal structure of the heap. One can thus obtain information regarding the amount of memory allocated, overhead used for memory management, and the number of chunks allocated. By using this information, developers can tune their software to use the heapmem allocator more efficiently.