13#define MEM_ARENA_COMMIT_SIZE KB(64)
18 ASSERT(
IS_POW2(alignment) &&
"alignment must be a non-zero power of 2");
22 if (size > arena->
reserve - current_pos) {
23 ASSERT(0 &&
"Arena OOM: allocation exceeds reserved capacity");
27 usize next_pos = current_pos + size;
29 if (next_pos > arena->
commit) {
36 ASSERT(0 &&
"Arena OOM: OS refused to commit physical RAM");
39 arena->
commit += cmt_clamped;
42 void *result = arena->
base + current_pos;
43 arena->
pos = next_pos;
52 if (arena.
base != NULL) {
61 if (arena->
base != NULL) {
89 void *result = mem_arena_push_internal(arena, size, alignment);
99 ASSERT(pos <= arena->pos &&
"mem_arena_pop_to: pos is ahead of current position");
101 arena->
pos = clamped;
#define ALIGN_UP_POW2(x, p)
Aligns a value UP to the nearest multiple of p (must be power of 2).
#define IS_POW2(x)
Checks if a given integer is a strictly positive power of 2.
#define ASSERT(expr)
Standard runtime assertion. Triggers a debug break on failure.
MemAllocMode
Describes the type of memory operation being requested.
@ MEM_ALLOC_MODE_FREE_ALL
void mem_arena_clear(MemArena *arena)
Resets the arena pointer to zero, freeing all allocations in O(1).
void mem_arena_temp_end(MemArenaTemp temp)
Restores the arena to the position saved in temp.
void mem_arena_release(MemArena *arena)
Returns all virtual memory back to the operating system.
#define MEM_ARENA_ALIGNMENT
The default byte alignment for memory pushed onto the arena.
void * mem_arena_push_zero(MemArena *arena, usize size)
Allocates bytes and explicitly zero-initializes them.
void mem_arena_clear_and_decommit(MemArena *arena)
Clears the arena and returns all committed physical RAM to the OS.
void * mem_arena_push_aligned(MemArena *arena, usize size, usize alignment)
Allocates bytes with a specific custom alignment.
void * mem_arena_push(MemArena *arena, usize size)
Allocates bytes sequentially on the arena without zeroing.
void mem_arena_pop_to(MemArena *arena, usize pos)
Rewinds the arena's pointer to a specific offset.
MemAllocator mem_arena_allocator(MemArena *arena)
Wraps a MemArena in the generic MemAllocator interface.
char * mem_arena_push_cstr(MemArena *arena, const char *str, usize len)
Copies a C string into the arena and appends a NUL terminator.
MemArenaTemp mem_arena_temp_begin(MemArena *arena)
Temporary Memory API.
MemArena mem_arena_create(usize reserve_size)
Initializes a new virtual memory Arena.
void mem_os_release(void *ptr, usize size)
Releases a reserved virtual address block entirely back to the OS.
b32 mem_os_commit(void *ptr, usize size)
Commits physical RAM to a previously reserved virtual address block.
void * mem_os_reserve(usize size)
Reserves a contiguous block of virtual address space.
void mem_os_decommit(void *ptr, usize size)
Decommits physical RAM, returning it to the OS.
#define MEM_COPY(dst, src, size)
Copies memory using memmove (overlap-safe).
#define MEM_ZERO(ptr, size)
Zeroes a block of memory. Wraps memset.
#define MIN(a, b)
Returns the minimum of two values (Standard C fallback).
Generic interface for abstracting memory allocators.
static void * mem_arena_alloc_impl(MemAllocMode mode, void *ctx, usize size, void *old_ptr)
Generic Allocator Interface Implementation.
High-performance virtual memory Arena Allocator.
Cross-platform wrappers for OS-level virtual memory management.
An abstract allocator interface.
A virtual memory-backed linear allocator.
A temporary snapshot of an arena's state.