50#ifndef MEM_ALLOCATOR_H
51#define MEM_ALLOCATOR_H
70typedef void *(*AllocFn)(
void *ctx,
usize size);
97typedef void *(*ReallocFn)(
void *ctx,
void *ptr,
usize old_size,
usize new_size);
126 ASSERT(a != NULL && a->
alloc != NULL &&
"allocator_alloc: invalid allocator");
142 ASSERT(a != NULL &&
"allocator_free: invalid allocator");
143 if (a->
free != NULL) {
162 ASSERT(a != NULL && a->
alloc != NULL &&
"allocator_realloc: invalid allocator");
164 return a->
realloc(a->
ctx, ptr, old_size, new_size);
167 void *new_ptr = a->
alloc(a->
ctx, new_size);
168 if (new_ptr != NULL && ptr != NULL) {
179_arena_alloc_fn(
void *ctx,
usize size)
185_arena_free_fn(
void *ctx,
void *ptr,
usize size)
193_arena_realloc_fn(
void *ctx,
void *ptr,
usize old_size,
usize new_size)
196 if (new_ptr != NULL && ptr != NULL) {
223 a.
ctx = (
void *)arena;
224 a.
alloc = _arena_alloc_fn;
225 a.
free = _arena_free_fn;
Context detection, utilities, compiler abstractions, and data structures.
Core type definitions and fixed-width aliases.
function void * allocator_realloc(Allocator *a, void *ptr, usize old_size, usize new_size)
Resizes a previously allocated block through the allocator.
function void * allocator_alloc(Allocator *a, usize size)
Allocates size zero-initialized bytes through the allocator.
function void allocator_free(Allocator *a, void *ptr, usize size)
Frees a previously allocated block through the allocator.
void *(* ReallocFn)(void *ctx, void *ptr, usize old_size, usize new_size)
Function pointer type for a realloc operation.
void *(* AllocFn)(void *ctx, usize size)
Function pointer type for an allocation operation.
void(* FreeFn)(void *ctx, void *ptr, usize size)
Function pointer type for a free operation.
function Allocator arena_allocator(Arena *arena)
Constructs an Allocator interface backed by an existing Arena.
void * arena_push_zero(Arena *arena, usize size)
Allocates size bytes from the arena, zeroed, aligned to ARENA_DEFAULT_ALIGN.
#define ASSERT(expr)
Standard runtime assertion. Triggers a debug break on failure.
#define function
File-scoped function (invisible to the linker).
#define C_LINKAGE_END
Closes a C linkage block for C++ compilers (Empty in C).
#define C_LINKAGE_BEGIN
Opens a C linkage block for C++ compilers (Empty in C).
#define MEM_COPY(dst, src, size)
Wraps memmove for standard memory copying.
#define MIN(a, b)
Returns the minimum of two values (Standard C fallback).
High-performance Virtual Memory Linear (Arena) Allocator.
A generic allocator backed by function pointers (vtable).
Represents a linear memory arena backed by OS virtual memory.