88#define HEAPMEM_DEBUG 0
92#if __STDC_VERSION__ >= 201112L
94#define HEAPMEM_DEFAULT_ALIGNMENT alignof(max_align_t)
96#define HEAPMEM_DEFAULT_ALIGNMENT sizeof(size_t)
99#ifdef HEAPMEM_CONF_ALIGNMENT
100#define HEAPMEM_ALIGNMENT HEAPMEM_CONF_ALIGNMENT
102#define HEAPMEM_ALIGNMENT HEAPMEM_DEFAULT_ALIGNMENT
105typedef struct heapmem_stats {
110 size_t max_heap_usage;
128typedef struct heapmem_zone {
133 size_t max_heap_usage;
134 struct heapmem_chunk *free_list;
151#define HEAPMEM_ZONE_DEFINE(varname, bufsize) \
152 static char varname##_buf_[bufsize] CC_ALIGN(HEAPMEM_ALIGNMENT); \
153 static heapmem_zone_t varname = { \
155 .heap_base = varname##_buf_, \
156 .arena_size = bufsize, \
221void *heapmem_zone_alloc_debug(heapmem_zone_t *zone,
size_t size,
222 const char *file,
unsigned line);
223bool heapmem_zone_free_debug(heapmem_zone_t *zone,
void *ptr,
224 const char *file,
unsigned line);
225void *heapmem_zone_realloc_debug(heapmem_zone_t *zone,
void *ptr,
size_t size,
226 const char *file,
unsigned line);
227void *heapmem_zone_calloc_debug(heapmem_zone_t *zone,
size_t nmemb,
size_t size,
228 const char *file,
unsigned line);
229#define heapmem_zone_alloc(zone, size) \
230 heapmem_zone_alloc_debug((zone), (size), __FILE__, __LINE__)
231#define heapmem_zone_free(zone, ptr) \
232 heapmem_zone_free_debug((zone), (ptr), __FILE__, __LINE__)
233#define heapmem_zone_realloc(zone, ptr, size) \
234 heapmem_zone_realloc_debug((zone), (ptr), (size), __FILE__, __LINE__)
235#define heapmem_zone_calloc(zone, nmemb, size) \
236 heapmem_zone_calloc_debug((zone), (nmemb), (size), __FILE__, __LINE__)
258#ifdef HEAPMEM_CONF_ARENA_SIZE
260#define heapmem_alloc(size) \
261 heapmem_zone_alloc(NULL, (size))
262#define heapmem_free(ptr) \
263 heapmem_zone_free(NULL, (ptr))
264#define heapmem_realloc(ptr, size) \
265 heapmem_zone_realloc(NULL, (ptr), (size))
266#define heapmem_calloc(nmemb, size) \
267 heapmem_zone_calloc(NULL, (nmemb), (size))
268#define heapmem_stats(stats) \
269 heapmem_zone_stats(NULL, (stats))
270#define heapmem_print_debug_info(print_chunks) \
271 heapmem_zone_print_debug_info(NULL, (print_chunks))
Default definitions of C compiler quirk work-arounds.
void * heapmem_zone_alloc(heapmem_zone_t *zone, size_t size)
Allocate a chunk of memory 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_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_print_debug_info(heapmem_zone_t *zone, bool print_chunks)
Print debugging information for a heapmem zone.
bool heapmem_zone_free(heapmem_zone_t *zone, void *ptr)
Deallocate a chunk of memory in the specified zone.