ArenaAllocator
Description
Create an arena allocator descriptor. Each AllocatorAlloc call bumps a cursor inside the current chunk; when a chunk is exhausted, a new one is mapped through PageAllocator. AllocatorFree is a no-op for arena memory - everything is released together when the arena is unbound.
The arena’s runtime state is created lazily on the first allocation and torn down by AllocatorUnbind. The arena does not call libc heap functions.
Usage example (from documentation)
Allocator scratch = ArenaAllocator();
Vec(Token) toks = VecInit(scratch);
// ... build many small allocations cheaply ...
AllocatorUnbind(&scratch); // frees everything
Success
Returns an arena allocator descriptor.
Failure
Function cannot fail at creation time. Out-of-memory shows up as a NULL from the first AllocatorAlloc call.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Arena.c:236:
}
Allocator ArenaAllocator(void) {
return (Allocator) {
.state = NULL,- In
Arena.c:252:
Allocator ArenaAllocatorAligned(size alignment) {
Allocator alloc = ArenaAllocator();
if (alignment) {
alloc.alignment = alignment;
static bool test_basic_bump(void) {
Allocator arena = ArenaAllocator();
char *a = (char *)AllocatorAlloc(&arena, 16, true);
char *b = (char *)AllocatorAlloc(&arena, 32, true);
static bool test_grow_last_in_place(void) {
Allocator arena = ArenaAllocator();
char *p = (char *)AllocatorAlloc(&arena, 16, true);
bool ok = (p != NULL);
static bool test_grow_non_last_relocates(void) {
Allocator arena = ArenaAllocator();
char *a = (char *)AllocatorAlloc(&arena, 16, true);
char *b = (char *)AllocatorAlloc(&arena, 16, true);
static bool test_vec_on_arena(void) {
Allocator arena = ArenaAllocator();
typedef Vec(int) IntVec;
IntVec v = VecInit(arena);
static bool test_reset(void) {
Allocator arena = ArenaAllocator();
char *a = (char *)AllocatorAlloc(&arena, 4096, true);
char *b = (char *)AllocatorAlloc(&arena, 4096, true);
Last updated on