![]() |
Contiki-NG
|
Files | |
| file | heapmem.h |
| Header file for the dynamic heap memory allocator. | |
Macros | |
| #define | HEAPMEM_ZONE_DEFINE(varname, bufsize) |
| Define a zone with its own static memory buffer. | |
Functions | |
| void * | heapmem_zone_alloc (heapmem_zone_t *zone, size_t size) |
| Allocate a chunk of memory in the specified zone. | |
| bool | heapmem_zone_free (heapmem_zone_t *zone, void *ptr) |
| Deallocate a chunk of memory in the specified zone. | |
| void * | heapmem_zone_realloc (heapmem_zone_t *zone, void *ptr, size_t size) |
| Reallocate a chunk of memory in the specified zone. | |
| void * | heapmem_zone_calloc (heapmem_zone_t *zone, size_t nmemb, size_t size) |
| Allocate memory for a zero-initialized array in the specified zone. | |
| void | heapmem_zone_stats (heapmem_zone_t *zone, heapmem_stats_t *stats) |
| Obtain internal statistics for a heapmem zone. | |
| void | heapmem_zone_print_debug_info (heapmem_zone_t *zone, bool print_chunks) |
| Print debugging information for a heapmem zone. | |
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 the HEAPMEM_CONF_ARENA_SIZE parameter.
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.
HeapMem zones provide independent heaps with their own static memory buffers. Each zone has its own free list and usage tracking, so fragmentation in one zone cannot affect another. Use HEAPMEM_ZONE_DEFINE() to create a zone with a dedicated buffer.
| #define HEAPMEM_ZONE_DEFINE | ( | varname, | |
| bufsize ) |
Define a zone with its own static memory buffer.
| varname | The variable name for the zone. |
| bufsize | The size of the zone's memory buffer in bytes. |
This macro creates a file-scoped zone with a statically allocated buffer. The zone provides an independent heap that is isolated from all other zones.
Example usage: HEAPMEM_ZONE_DEFINE(packet_zone, 4096); void *p = heapmem_zone_alloc(&packet_zone, 128); heapmem_zone_free(&packet_zone, p);
| void * heapmem_zone_alloc | ( | heapmem_zone_t * | zone, |
| size_t | size ) |
Allocate a chunk of memory in the specified zone.
| zone | A pointer to the zone in which to allocate the memory, or NULL to use the general zone. |
| size | The number of bytes to allocate. |
| void * heapmem_zone_calloc | ( | heapmem_zone_t * | zone, |
| size_t | nmemb, | ||
| size_t | size ) |
Allocate memory for a zero-initialized array in the specified zone.
| zone | A pointer to the zone in which to allocate the memory, or NULL to use the general zone. |
| nmemb | The number of elements to allocate. |
| size | The size of each element. |
| bool heapmem_zone_free | ( | heapmem_zone_t * | zone, |
| void * | ptr ) |
Deallocate a chunk of memory in the specified zone.
| zone | A pointer to the zone from which the memory was allocated, or NULL to use the general zone. |
| ptr | A pointer to a chunk that has been allocated using heapmem_zone_alloc() or heapmem_zone_realloc(). |
| void heapmem_zone_print_debug_info | ( | heapmem_zone_t * | zone, |
| bool | print_chunks ) |
Print debugging information for a heapmem zone.
| zone | A pointer to the zone to query. |
| print_chunks | Determines whether to print information about all allocated chunks. |
Definition at line 622 of file heapmem.c.
References heapmem_zone_stats().
| void * heapmem_zone_realloc | ( | heapmem_zone_t * | zone, |
| void * | ptr, | ||
| size_t | size ) |
Reallocate a chunk of memory in the specified zone.
| zone | A pointer to the zone in which to reallocate the memory, or NULL to use the general zone. |
| ptr | A pointer to a chunk that has been allocated using heapmem_zone_alloc() or heapmem_zone_realloc(). |
| size | The number of bytes to allocate. |
| void heapmem_zone_stats | ( | heapmem_zone_t * | zone, |
| heapmem_stats_t * | stats ) |
Obtain internal statistics for a heapmem zone.
| zone | A pointer to the zone to query. |
| stats | A pointer to an object of type heapmem_stats_t, which will be filled when calling this function. |
Definition at line 594 of file heapmem.c.
Referenced by heapmem_zone_print_debug_info().