libmemalloc  v4.0.00
Modern Memory Allocator
Loading...
Searching...
No Matches
libmemalloc

Contributors Forks Stargazers Issues ULicense LinkedIn

libmemalloc logo

by: Rafael V. Volkmer - @RafaelVVolkmer - rafae.nosp@m.l.v..nosp@m.volkm.nosp@m.er@g.nosp@m.mail..nosp@m.com

- Library Description

libmemalloc is a comprehensive, drop-in C memory management library that elevates your application’s heap to a fully introspectable, high-performance subsystem. It offers three tunable allocation strategies—First-Fit, Next-Fit (with optional “last allocated” pointer tracking), and Best-Fit—enabling you to balance speed, fragmentation, or memory footprint on each call. Small allocations are serviced via segregated free lists organized into configurable size classes for constant-time inserts and removals, while large requests (≥ 128 KiB) automatically bypass sbrk in favor of mmap to minimize fragmentation and leverage OS paging efficiencies. An optional background mark-and-sweep garbage collector can be activated to transparently reclaim unreachable blocks, eliminating manual free() calls in scenarios where automatic cleanup is preferred. Every block is protected by magic numbers and footer canaries to detect header corruption or buffer overflows at runtime. Internal locking guarantees thread safety in multithreaded environments, and seamless Valgrind integration annotates a dedicated MemPool for precise leak and fragmentation reporting. With configurable log verbosity (ERROR, WARN, INFO, DEBUG), live allocation maps, fragmentation metrics, and on-demand diagnostics, libmemalloc gives you unparalleled visibility into your program’s memory behavior. Integration couldn’t be simpler: link against the provided static (.a) or shared (.so) library, include libmemalloc.h, and replace your standard malloc/free calls with the intuitive MEM_* API to transform your heap—no other source modifications required.


- C Example

#include "libmemalloc.h"
int main(void)
{
int ret = EXIT_SUCESS;
mem_allocator_t allocator;
ret = MEM_allocatorInit(&allocator);
if (ret != EXIT_SUCESS)
return ret;
void *buf1 = MEM_allocMallocFirstFit(&allocator, 1024, "buffer1");
if (!buf1)
return ret;
MEM_allocFree(&allocator, buf1, "buffer1");
void *buf2 = MEM_allocMallocBestFit(&allocator, 2048, "buffer2");
if (!buf2)
return ret;
MEM_allocFree(&allocator, buf2, "buffer2");
void *buf3 = MEM_allocMallocNextFit(&allocator, 512, "buffer3");
if (!buf3)
return ret;
MEM_allocFree(&allocator, buf3, "buffer3");
MEM_enableGc(&allocator);
void *gc_buf = MEM_allocCalloc(&allocator, 4096, "gc_buffer", FIRST_FIT);
gc_buff = NULL;
MEM_disableGc(&allocator);
return ret;
}
int MEM_allocatorInit(mem_allocator_t *const allocator)
Initializes the memory allocator and its internal structures.
Definition libmemalloc.c:1587
int MEM_allocFree(mem_allocator_t *const allocator, void *const ptr, const char *const var)
Releases allocated memory back to the heap.
Definition libmemalloc.c:3778
void * MEM_allocCalloc(mem_allocator_t *const allocator, const size_t size, const char *const var, const allocation_strategy_t strategy)
Allocates and zero‐initializes memory using the specified strategy.
Definition libmemalloc.c:3705
void * MEM_allocMallocBestFit(mem_allocator_t *const allocator, const size_t size, const char *const var)
Allocates memory using the BEST_FIT strategy.
Definition libmemalloc.c:3609
void * MEM_allocMallocFirstFit(mem_allocator_t *const allocator, const size_t size, const char *const var)
Allocates memory using the FIRST_FIT strategy.
Definition libmemalloc.c:3577
void * MEM_allocMallocNextFit(mem_allocator_t *const allocator, const size_t size, const char *const var)
Allocates memory using the NEXT_FIT strategy.
Definition libmemalloc.c:3640
int MEM_disableGc(mem_allocator_t *const allocator)
Stop the garbage collector thread and perform a final collection.
Definition libmemalloc.c:3826
int MEM_enableGc(mem_allocator_t *const allocator)
Start or signal the garbage collector thread.
Definition libmemalloc.c:3806
@ FIRST_FIT
Definition libmemalloc.h:324
Manages dynamic memory allocation.
Definition libmemalloc.h:479

(back to top)