X_ARENA_VERSION_MAJOR
#define X_ARENA_VERSION_MAJOR 1
STDX - Arena Allocator Part of the STDX General Purpose C Library by marciovmf License: MIT https://github.com/marciovmf/stdx
Minimal, cache-friendly bump allocator with chunk growth, alignment, fast O(1) steady-state allocations, optional marks (scoped rewinds), and trimming APIs to keep memory bounded after spikes.
To compile the implementation define X_IMPL_ARENA
in one source file before including this header.
To customize how this module allocates memory, define
X_ARENA_ALLOC / X_ARENA_FREE before including.
#define X_ARENA_VERSION_MAJOR 1
#define X_ARENA_VERSION_MINOR 0
#define X_ARENA_VERSION_PATCH 0
#define X_ARENA_VERSION(X_ARENA_VERSION_MAJOR *10000+X_ARENA_VERSION_MINOR *100+X_ARENA_VERSION_PATCH)
typedef struct XArenaChunk XArenaChunk;
Arena
typedef struct XArena{
size_t chunk_size;
XArenaChunk *chunks;
XArenaChunk *current;
}XArena;
A mark is a lightweight snapshot for scoped unwinds.
typedef struct XArenaMark{
XArenaChunk *chunk;
size_t used;
}XArenaMark;
Create a new arena with the given default chunk size.
X_ARENA_API XArena * x_arena_create(size_t chunk_size);
pointer to the new arena or NULL if creation fails
Destroy the arena and free all memory.
X_ARENA_API void x_arena_destroy(XArena *arena);
XArena *arenaReset the arena: keep all chunks, set used = 0.
X_ARENA_API void x_arena_reset(XArena *arena);
XArena *arenaKeep only the head chunk, free the rest.
X_ARENA_API void x_arena_reset_keep_head(XArena *arena);
XArena *arenaKeep the first `keep_n` chunks, free the rest.
X_ARENA_API void x_arena_trim(
XArena *arena,
size_t keep_n
);
XArena *arenasize_t keep_nAllocate memory from the arena (aligned to X_ARENA_ALIGN).
X_ARENA_API void * x_arena_alloc(
XArena *arena,
size_t size
);
XArena *arenasize_t sizePointer to the allocated memory region
Allocate zero-initialized memory from the arena.
X_ARENA_API void * x_arena_alloc_zero(
XArena *arena,
size_t size
);
XArena *arenasize_t sizePointer to the allocated memory region
Duplicate a C-string into the arena.
X_ARENA_API char * x_arena_strdup(
XArena *arena,
const char *cstr
);
XArena *arenaconst char *cstrPointer to the allocated memory region
Duplicate a slice into the arena.
X_ARENA_API char * x_arena_slicedup(
XArena *arena,
const char *ptr,
size_t len,
bool null_terminate
);
XArena *arenaconst char *ptrsize_t lenPointer to the allocated memory region
Makes a snapshot of the arena state.
X_ARENA_API XArenaMark x_arena_mark(XArena *arena);
XArena *arenaA snapshot of the current arena state.
Rewind the arena to a previous mark (frees newer chunks).
X_ARENA_API void x_arena_release(
XArena *arena,
XArenaMark mark
);
XArena *arenaXArenaMark markchecks if a pointer points to arena owned memory
X_ARENA_API bool x_arena_has_pointer(
const XArena *arena,
void *ptr
);
const XArena *arenavoid *ptrtrue if ptr points to an address owned by the arena.
Internal macro for allocating memory. To override how this header allocates memory, define this macro with a different implementation before including this header.
#define X_ARENA_ALLOC(sz) malloc((sz))
szInternal macro for freeing memory. To override how this header frees memory, define this macro with a different implementation before including this header.
#define X_ARENA_FREE(p) free((p))
p#define X_ARENA_ALIGN(sizeof(void *)>sizeof(double)?sizeof(void *):sizeof(double))
#define X_ARENA_DEBUG_FILL 0xDD
size_t x_arena_chunk_count(const XArena *a);
size_t x_arena_head_capacity(const XArena *a);
size_t x_arena_head_used(const XArena *a);