🚀 Library Description
libmemalloc is a focused, drop-in replacement for the standard C allocator that turns your heap into a high-visibility, high-reliability subsystem.
Instead of opaque malloc/free calls, you get a small, well-defined API under the MEM_ prefix:
- Core allocation:
MEM_alloc
MEM_calloc
MEM_realloc
MEM_free
- Custom memory operations:
- Strategy-specific helpers:
MEM_allocFirstFit
MEM_allocNextFit
MEM_allocBestFit
Under the hood, the allocator uses:
- Segregated free lists organized into size classes for (amortized) O(1) insert/remove.
- Automatic promotion of large blocks to **
mmap(2)** regions to reduce fragmentation.
- Dynamic heap growth / shrink using **
sbrk(2)** for regular blocks.
- Optional background mark-and-sweep GC mode to reclaim unreachable memory in GC-friendly workloads.
- Magic values and canaries around each block to detect overruns and header corruption.
- Internal locking to remain safe in multithreaded programs.
- Integration points for tools like Valgrind and external profilers.
The public surface is kept small and stable, and the shared library is exported through a linker version script (MEMALLOC_3.0) so that only the intended symbols are visible to consumers.
To use it, you simply link the .a or .so, include libmemalloc.h, and swap your malloc/free calls for MEM_* equivalents.
📦 C Example
A minimal example using the current public API (no explicit allocator struct or init):
#include <stdio.h>
#include <string.h>
int main(void)
{
int *values =
MEM_alloc(10 *
sizeof *values);
if (values == NULL) {
fprintf(stderr, "MEM_alloc failed\n");
return 1;
}
if (zeros == NULL) {
fprintf(stderr, "MEM_calloc failed\n");
return 1;
}
for (int i = 0; i < 10; ++i)
values[i] = 42 + i;
if (copy == NULL) {
fprintf(stderr, "MEM_alloc for copy failed\n");
return 1;
}
printf("copy[3] = %d\n", copy[3]);
if (!buf_ff || !buf_nf || !buf_bf) {
fprintf(stderr, "strategy-specific allocation failed\n");
return 1;
}
if (buf_bigger == NULL) {
fprintf(stderr, "MEM_realloc failed, keeping original buffer\n");
} else {
buf_bf = buf_bigger;
}
return 0;
}
void * MEM_calloc(const size_t size, const allocation_strategy_t strategy)
Allocates and zero‐initializes memory using the specified strategy.
Definition libmemalloc.c:4035
void * MEM_allocFirstFit(const size_t size)
Allocates memory using the FIRST_FIT strategy.
Definition libmemalloc.c:3877
int MEM_free(void *const ptr)
Releases allocated memory back to the heap.
Definition libmemalloc.c:4118
void * MEM_alloc(const size_t size, const allocation_strategy_t strategy)
Allocates memory using the specified strategy.
Definition libmemalloc.c:3995
void * MEM_memset(void *const source, const int value, const size_t size)
Fills a memory block with a specified byte value using optimized operations.
Definition libmemalloc.c:1204
void * MEM_allocBestFit(const size_t size)
Allocates memory using the BEST_FIT strategy.
Definition libmemalloc.c:3916
void * MEM_allocNextFit(const size_t size)
Allocates memory using the NEXT_FIT strategy.
Definition libmemalloc.c:3955
void * MEM_memcpy(void *const dest, const void *src, const size_t size)
Copies a memory block between buffers using optimized operations.
Definition libmemalloc.c:1287
void * MEM_realloc(void *const ptr, const size_t new_size, const allocation_strategy_t strategy)
Reallocates memory with safety checks using the specified strategy.
Definition libmemalloc.c:4076
Typical compile/link invocation (adjust include/library paths to your tree):
gcc -Iinc -o example example.c -Lbin/Release -lmemalloc
This example touches all nine symbols from the version script:
MEM_alloc / MEM_calloc / MEM_realloc / MEM_free
MEM_memcpy / MEM_memset
MEM_allocFirstFit / MEM_allocNextFit / MEM_allocBestFit
(back to top)