A header-only C allocator library providing various memory allocation strategies through a unified interface. All allocators implement the same allocator interface with the state is also visible and controllable. There are no dependencies besides libc. Performance characteristics are predictable and the provided allocators are composable.
Standard malloc/free wrapper with alignment support.
allocator* alloc = c_allocator();
int* ptr = (int*)alloc_create(alloc, sizeof(int));
alloc_destroy(alloc, ptr, sizeof(int));
Fast bump allocator that allocates from a fixed buffer. Free all at once.
uint8_t buffer[4096];
arena_allocator arena;
arena_allocator_init(&arena, buffer, sizeof(buffer));
allocator alloc = arena_allocator_get(&arena);
int* data = (int*)alloc_create(&alloc, sizeof(int));
arena_allocator_reset(&arena);
Fixed-size chunk allocator with
uint8_t buffer[1024];
pool_allocator pool;
pool_allocator_init(&pool, buffer, 64, 16);
allocator alloc = pool_allocator_get(&pool);
void* chunk = alloc_alloc(&alloc, 64, 8);
alloc_free(&alloc, chunk, 64);
LIFO allocator with save/restore markers for scoped allocations.
uint8_t buffer[2048];
stack_allocator stack;
stack_allocator_init(&stack, buffer, sizeof(buffer));
allocator alloc = stack_allocator_get(&stack);
stack_marker mark = stack_allocator_mark(&stack);
int* temp = (int*)alloc_create(&alloc, sizeof(int));
stack_allocator_restore(&stack, mark);
Temporary allocator that tracks all allocations for bulk freeing.
scratch_allocator scratch;
scratch_allocator_init(&scratch, c_allocator());
allocator alloc = scratch_allocator_get(&scratch);
int* arr1 = (int*)alloc_create_array(&alloc, 100, sizeof(int));
int* arr2 = (int*)alloc_create_array(&alloc, 200, sizeof(int));
scratch_allocator_reset(&scratch);
scratch_allocator_destroy(&scratch);
General-purpose allocator managing free blocks with coalescing.
uint8_t buffer[8192];
freelist_allocator freelist;
freelist_allocator_init(&freelist, buffer, sizeof(buffer));
allocator alloc = freelist_allocator_get(&freelist);
void* ptr = alloc_alloc(&alloc, 256, 8);
alloc_free(&alloc, ptr, 256);
void* alloc_alloc(allocator* a, size_t size, size_t alignment);
void* alloc_realloc(allocator* a, void* ptr, size_t old_size, size_t new_size, size_t alignment);
void alloc_free(allocator* a, void* ptr, size_t size);
void* alloc_create(allocator* a, size_t size);
void* alloc_create_array(allocator* a, size_t count, size_t elem_size);
void alloc_destroy(allocator* a, void* ptr, size_t size);
Include the alloc.h header in your program.
Apache v2.0 License