|
cbase 1.50.0
C/C++ Static Template
|
Auto-committing linear allocator backed by OS virtual memory. More...
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. | |
Auto-committing linear allocator backed by OS virtual memory.
| #define MEM_ARENA_ALIGNMENT 8 |
The default byte alignment for memory pushed onto the arena.
Definition at line 67 of file mem_arena.h.
| #define PUSH_ARRAY | ( | arena, | |
| type, | |||
| count ) |
Pushes an array of structs of type T onto the arena.
Example:
Definition at line 343 of file mem_arena.h.
| #define PUSH_ARRAY_ALIGNED | ( | arena, | |
| type, | |||
| count, | |||
| alignment ) |
Pushes an array of structs with custom memory alignment.
Example:
Definition at line 366 of file mem_arena.h.
| #define PUSH_CSTR | ( | arena, | |
| cstr ) |
Copies a NUL-terminated C string into the arena.
Convenience wrapper around mem_arena_push_cstr() that calls strlen for you. The result is always NUL-terminated and safe to pass to C string APIs.
Example:
| arena | Pointer to the MemArena. |
| cstr | A NUL-terminated C string to copy. |
Definition at line 386 of file mem_arena.h.
| #define PUSH_STRUCT | ( | arena, | |
| type ) |
Type-Safe Allocation Macros.
Pushes a struct of type T onto the arena.
Example:
Definition at line 331 of file mem_arena.h.
| #define PUSH_STRUCT_ALIGNED | ( | arena, | |
| type, | |||
| alignment ) |
Pushes a struct with custom memory alignment.
Example:
Definition at line 354 of file mem_arena.h.
| struct MemAllocator mem_arena_allocator | ( | MemArena * | arena | ) |
Wraps a MemArena in the generic MemAllocator interface.
The returned MemAllocator holds a pointer to arena — the arena must outlive any use of the returned MemAllocator. Pass this to any API that takes a MemAllocator to use arena allocation without locking in the type.
Example:
| arena | The arena to bind. |
arena. Definition at line 172 of file mem_arena.c.

| void mem_arena_clear | ( | MemArena * | arena | ) |
Resets the arena pointer to zero, freeing all allocations in O(1).
Physical RAM remains committed and warm for subsequent allocations — the next allocation after a clear is just as fast as before. This is the idiomatic per-frame reset pattern.
Example:
| arena | The arena to clear. |
Definition at line 105 of file mem_arena.c.


| void mem_arena_clear_and_decommit | ( | MemArena * | arena | ) |
Clears the arena and returns all committed physical RAM to the OS.
Use instead of mem_arena_clear() when the arena held a large one-off allocation and you want those physical pages back. Subsequent allocations will re-commit RAM on demand as normal.
Example:
| arena | The arena to reset and decommit. |
Definition at line 111 of file mem_arena.c.

Initializes a new virtual memory Arena.
Example:
| reserve_size | The total virtual address space to reserve (e.g., GB(1)). |
Definition at line 48 of file mem_arena.c.

Rewinds the arena's pointer to a specific offset.
All memory at or above pos is considered freed. Committed physical RAM is not returned to the OS. Prefer MemArenaTemp for scoped rollbacks.
Example:
| arena | The arena to rewind. |
| pos | The offset to pop back to (must be <= arena->pos in debug builds). |
Definition at line 97 of file mem_arena.c.

Allocates bytes sequentially on the arena without zeroing.
Prefer mem_arena_push_zero() unless you are about to overwrite the entire allocation. Physical RAM is committed in 64 KB chunks as needed.
Example:
| arena | The arena to allocate from. |
| size | The number of bytes to allocate. |
Definition at line 71 of file mem_arena.c.
Allocates bytes with a specific custom alignment.
Use when the default 8-byte alignment is insufficient, e.g. for SIMD data that requires 16 or 32-byte alignment.
Example:
| arena | The arena to allocate from. |
| size | The number of bytes to allocate. |
| alignment | The alignment boundary (must be a power of 2). |
Definition at line 87 of file mem_arena.c.
Copies a C string into the arena and appends a NUL terminator.
The resulting buffer is always safe to pass to any C string API. Use the PUSH_CSTR macro as a convenience wrapper that calls strlen for you.
Example:
| arena | The arena to allocate from. |
| str | The source string (does not need to be NUL-terminated). |
| len | Number of characters to copy (excluding the NUL terminator). |
Definition at line 121 of file mem_arena.c.

Allocates bytes and explicitly zero-initializes them.
This is the preferred push variant. Use mem_arena_push() only when you are certain you will overwrite every byte before reading it.
Example:
| arena | The arena to allocate from. |
| size | The number of bytes to allocate. |
Definition at line 77 of file mem_arena.c.

| void mem_arena_release | ( | MemArena * | arena | ) |
Returns all virtual memory back to the operating system.
Every pointer previously returned by mem_arena_push*() becomes invalid. Call this when the arena will no longer be used.
Example:
| arena | The arena to release. |
Definition at line 59 of file mem_arena.c.

| MemArenaTemp mem_arena_temp_begin | ( | MemArena * | arena | ) |
Temporary Memory API.
Saves the current arena position as a rewind checkpoint.
Example:
| arena | The arena to checkpoint. |
Definition at line 132 of file mem_arena.c.
| void mem_arena_temp_end | ( | MemArenaTemp | temp | ) |
Restores the arena to the position saved in temp.
All allocations made after the corresponding mem_arena_temp_begin() are effectively freed. Physical RAM remains committed and reusable.
| temp | The checkpoint returned by mem_arena_temp_begin(). |
Definition at line 141 of file mem_arena.c.
