libmemalloc  v3.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 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:
    • MEM_memcpy
    • MEM_memset
  • 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> /* only for printf formats, not memcpy/memset */
#include "libmemalloc.h" /* provides MEM_* declarations */
int main(void)
{
/* 1) Default allocation using MEM_alloc */
int *values = MEM_alloc(10 * sizeof *values);
if (values == NULL) {
fprintf(stderr, "MEM_alloc failed\n");
return 1;
}
/* 2) Zero-initialized array with MEM_calloc */
int *zeros = MEM_calloc(5, sizeof *zeros);
if (zeros == NULL) {
fprintf(stderr, "MEM_calloc failed\n");
MEM_free(values);
return 1;
}
/* 3) Initialize memory using MEM_memset */
/* Set all 'values' entries to 42 in two steps:
* - fill with zero
* - then write the contents explicitly
*/
MEM_memset(values, 0, 10 * sizeof *values);
for (int i = 0; i < 10; ++i)
values[i] = 42 + i;
/* 4) Copy memory with MEM_memcpy */
int *copy = MEM_alloc(10 * sizeof *copy);
if (copy == NULL) {
fprintf(stderr, "MEM_alloc for copy failed\n");
MEM_free(zeros);
MEM_free(values);
return 1;
}
MEM_memcpy(copy, values, 10 * sizeof *copy);
printf("copy[3] = %d\n", copy[3]);
/* 5) Strategy-specific allocations */
char *buf_ff = MEM_allocFirstFit(1024); /* fast, simple placement */
char *buf_nf = MEM_allocNextFit(512); /* reuse last search position */
char *buf_bf = MEM_allocBestFit(2048); /* reduce fragmentation */
if (!buf_ff || !buf_nf || !buf_bf) {
fprintf(stderr, "strategy-specific allocation failed\n");
MEM_free(copy);
MEM_free(zeros);
MEM_free(values);
MEM_free(buf_ff);
MEM_free(buf_nf);
MEM_free(buf_bf);
return 1;
}
/* Use MEM_memset on these buffers as well */
MEM_memset(buf_ff, 0xAA, 1024);
MEM_memset(buf_nf, 0xBB, 512);
MEM_memset(buf_bf, 0xCC, 2048);
/* 6) Resize an allocation with MEM_realloc */
char *buf_bigger = MEM_realloc(buf_bf, 4096);
if (buf_bigger == NULL) {
fprintf(stderr, "MEM_realloc failed, keeping original buffer\n");
/* In this case buf_bf is still valid and must be freed */
MEM_free(buf_bf);
} else {
buf_bf = buf_bigger;
}
/* 7) Clean up everything with MEM_free */
MEM_free(buf_ff);
MEM_free(buf_nf);
MEM_free(buf_bf);
MEM_free(copy);
MEM_free(zeros);
MEM_free(values);
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)