CBuild wiki
Arena allocator.
License: GPL-3.0-or-later.
typedef struct cbuild_arena_t {
void* base;
size_t capacity;
size_t pointer;
#ifdef CBUILD_PROFILER
size_t max_pointer;
#endif // CBUILD_PROFILER
} cbuild_arena_t;Arena allocator state.
Arena automatically pad things to 2 * sizeof(void*).
This is done to be a little more cache-friendly. Padding is done after
allocation so it is always placed after the data.
void*
base Base pointer for allocated
memory.size_t capacity Capacity of this arena.size_t pointer Pointer to first unallocated
byte.Reallocate arena base to new size.
If old base is NULL then new memory will be
allocated.
This is just a direct call to realloc plus some metadata
management.
Allocate new arena base.
cbuild_arena_t*
arena Arena object.size_t capacity Capacity of arena.Free arena base.
Allocate inside of arena.
cbuild_arena_t*
arena Arena object.size_t size Number of bytes to allocate.void* Pointer into
arena. NULL if out-of-memory.
realloc for arena. Will not free old memory.
It sizesize_t
is bigger than old size then memory from adjacent allocations will be
used to initialize new memory block. This is safe access (no
segmentation fault possible), but can expose unrelated memory trough new
allocation.
Save checkpoint into arena.
Reset arena to some checkpoint.
Free arena. Set pointer to 0.
cbuild_arena_t*
arena Arena object.strdup that uses arena as allocator.
memdup that uses arena as its allocator.
sprintf that uses arena as its allocator.
vsprintf that uses arena as its allocator.
This adds one size_t field to each arena that track maximum allocated size. This makes each allocation a little slower.
cbuild_arena_profiler Just prints usage as
TRACE log.