cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
mem_arena.h File Reference

High-performance virtual memory Arena Allocator. More...

#include "base/base_macros.h"
#include "base/base_types.h"
Include dependency graph for mem_arena.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  MemArena
 A virtual memory-backed linear allocator. More...
struct  MemArenaTemp
 A temporary snapshot of an arena's state. More...

Macros

#define MEM_ARENA_ALIGNMENT   8
 The default byte alignment for memory pushed onto the arena.
#define PUSH_STRUCT(arena, type)
 Type-Safe Allocation Macros.
#define PUSH_ARRAY(arena, type, count)
 Pushes an array of structs of type T onto the arena.
#define PUSH_STRUCT_ALIGNED(arena, type, alignment)
 Pushes a struct with custom memory alignment.
#define PUSH_ARRAY_ALIGNED(arena, type, count, alignment)
 Pushes an array of structs with custom memory alignment.
#define PUSH_CSTR(arena, cstr)
 Copies a NUL-terminated C string into the arena.

Typedefs

typedef struct MemArena MemArena
 A virtual memory-backed linear allocator.
typedef struct MemArenaTemp MemArenaTemp
 A temporary snapshot of an arena's state.

Functions

MemArena mem_arena_create (usize reserve_size)
 Initializes a new virtual memory Arena.
void * mem_arena_push (MemArena *arena, usize size)
 Allocates bytes sequentially on the arena without zeroing.
void * mem_arena_push_zero (MemArena *arena, usize size)
 Allocates bytes and explicitly zero-initializes them.
void * mem_arena_push_aligned (MemArena *arena, usize size, usize alignment)
 Allocates bytes with a specific custom alignment.
char * mem_arena_push_cstr (MemArena *arena, const char *str, usize len)
 Copies a C string into the arena and appends a NUL terminator.
void mem_arena_pop_to (MemArena *arena, usize pos)
 Rewinds the arena's pointer to a specific offset.
void mem_arena_clear (MemArena *arena)
 Resets the arena pointer to zero, freeing all allocations in O(1).
void mem_arena_clear_and_decommit (MemArena *arena)
 Clears the arena and returns all committed physical RAM to the OS.
void mem_arena_release (MemArena *arena)
 Returns all virtual memory back to the operating system.
struct MemAllocator mem_arena_allocator (MemArena *arena)
 Wraps a MemArena in the generic MemAllocator interface.
MemArenaTemp mem_arena_temp_begin (MemArena *arena)
 Temporary Memory API.
void mem_arena_temp_end (MemArenaTemp temp)
 Restores the arena to the position saved in temp.

Detailed Description

High-performance virtual memory Arena Allocator.

Reserves a massive block of virtual address space upfront but only commits physical RAM as the bump pointer advances. Eliminates individual malloc/free overhead and memory fragmentation entirely.

Allocation Model
This is a linear (bump) allocator. Individual allocations cannot be freed. Memory is reclaimed in bulk by resetting the position pointer via mem_arena_clear() or rolling back to a saved checkpoint via MemArenaTemp.
Basic Usage
// Reserve 1 GB of address space (no physical RAM used yet).
// Allocate a single zeroed struct:
MyStruct *s = PUSH_STRUCT(&arena, MyStruct);
s->x = 10;
// Allocate a zeroed array:
float *buf = PUSH_ARRAY(&arena, float, 64);
// Scoped temporary memory — save, allocate, restore:
char *scratch = PUSH_ARRAY(&arena, char, 4096);
// ... use scratch ...
mem_arena_temp_end(tmp); // arena snaps back to before tmp
// Per-frame reset — free everything at once, O(1):
// Return all virtual memory to the OS when done:
void mem_arena_clear(MemArena *arena)
Resets the arena pointer to zero, freeing all allocations in O(1).
Definition mem_arena.c:105
void mem_arena_temp_end(MemArenaTemp temp)
Restores the arena to the position saved in temp.
Definition mem_arena.c:141
void mem_arena_release(MemArena *arena)
Returns all virtual memory back to the operating system.
Definition mem_arena.c:59
#define PUSH_ARRAY(arena, type, count)
Pushes an array of structs of type T onto the arena.
Definition mem_arena.h:343
#define PUSH_STRUCT(arena, type)
Type-Safe Allocation Macros.
Definition mem_arena.h:331
MemArenaTemp mem_arena_temp_begin(MemArena *arena)
Temporary Memory API.
Definition mem_arena.c:132
MemArena mem_arena_create(usize reserve_size)
Initializes a new virtual memory Arena.
Definition mem_arena.c:48
#define GB(x)
Definition base_types.h:121
A virtual memory-backed linear allocator.
Definition mem_arena.h:70
A temporary snapshot of an arena's state.
Definition mem_arena.h:78
Using as a MemAllocator
// Now any API that takes MemAllocator works:
StringBuilder sb = {0};
sb_append(&sb, &alloc, STR8("hello"));
struct MemAllocator mem_arena_allocator(MemArena *arena)
Wraps a MemArena in the generic MemAllocator interface.
Definition mem_arena.c:172
#define MB(x)
Definition base_types.h:120
static void sb_append(StringBuilder *sb, MemAllocator *alloc, String8 str)
Appends a String8 slice to the builder.
Definition str_builder.h:75
#define STR8(s)
Wraps a C string literal into a String8 at compile time.
An abstract allocator interface.
An allocator-agnostic string builder.
Definition str_builder.h:56

Definition in file mem_arena.h.