cbase 1.46.11
C/C++ Static Template
Loading...
Searching...
No Matches
Generic Allocator Interface

Classes

struct  Allocator
 A generic allocator backed by function pointers (vtable). More...

Typedefs

typedef void *(* AllocFn) (void *ctx, usize size)
 Function pointer type for an allocation operation.
typedef void(* FreeFn) (void *ctx, void *ptr, usize size)
 Function pointer type for a free operation.
typedef void *(* ReallocFn) (void *ctx, void *ptr, usize old_size, usize new_size)
 Function pointer type for a realloc operation.
typedef struct Allocator Allocator
 A generic allocator backed by function pointers (vtable).

Functions

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.
function void * allocator_realloc (Allocator *a, void *ptr, usize old_size, usize new_size)
 Resizes a previously allocated block through the allocator.
function Allocator arena_allocator (Arena *arena)
 Constructs an Allocator interface backed by an existing Arena.

Detailed Description

Typedef Documentation

◆ Allocator

typedef struct Allocator Allocator

A generic allocator backed by function pointers (vtable).

Pass by pointer to any function that needs to allocate memory without caring about the concrete allocator type.

Note
Passing by pointer is preferred over by value to allow the callee to observe state changes (e.g., arena pos advancing).

◆ AllocFn

typedef void *(* AllocFn) (void *ctx, usize size)

Function pointer type for an allocation operation.

Parameters
ctxOpaque allocator context (e.g., a pointer to an Arena).
sizeNumber of bytes to allocate.
Returns
Pointer to zero-initialized memory, or NULL on OOM.

Definition at line 70 of file mem_allocator.h.

◆ FreeFn

typedef void(* FreeFn) (void *ctx, void *ptr, usize size)

Function pointer type for a free operation.

For allocators that do not support individual frees (e.g., Arena), this may be a no-op. size is provided so implementations can track usage without storing it internally.

Parameters
ctxOpaque allocator context.
ptrPointer previously returned by AllocFn.
sizeThe size that was originally requested.

Definition at line 83 of file mem_allocator.h.

◆ ReallocFn

typedef void *(* ReallocFn) (void *ctx, void *ptr, usize old_size, usize new_size)

Function pointer type for a realloc operation.

For arena allocators, this always performs a fresh allocation (the old block is not freed).

Parameters
ctxOpaque allocator context.
ptrExisting allocation to resize (may be NULL for a fresh alloc).
old_sizeSize of the existing allocation.
new_sizeDesired size of the new allocation.
Returns
Pointer to the resized allocation, or NULL on OOM.

Definition at line 97 of file mem_allocator.h.

Function Documentation

◆ allocator_alloc()

function void * allocator_alloc ( Allocator * a,
usize size )
inline

Allocates size zero-initialized bytes through the allocator.

Parameters
aPointer to the Allocator.
sizeNumber of bytes to allocate.
Returns
Pointer to zero-initialized memory, or NULL on OOM.

Definition at line 124 of file mem_allocator.h.

◆ allocator_free()

function void allocator_free ( Allocator * a,
void * ptr,
usize size )
inline

Frees a previously allocated block through the allocator.

No-op for arena-backed allocators. Always safe to call.

Parameters
aPointer to the Allocator.
ptrPointer to free.
sizeThe size originally requested.

Definition at line 140 of file mem_allocator.h.

Here is the caller graph for this function:

◆ allocator_realloc()

function void * allocator_realloc ( Allocator * a,
void * ptr,
usize old_size,
usize new_size )
inline

Resizes a previously allocated block through the allocator.

Falls back to fresh alloc + memcpy + free if .realloc == NULL.

Parameters
aPointer to the Allocator.
ptrExisting allocation to resize (may be NULL).
old_sizeSize of the existing allocation.
new_sizeDesired new size.
Returns
Pointer to resized allocation, or NULL on OOM.

Definition at line 160 of file mem_allocator.h.

Here is the call graph for this function:

◆ arena_allocator()

function Allocator arena_allocator ( Arena * arena)
inline

Constructs an Allocator interface backed by an existing Arena.

The returned Allocator holds a pointer to arena the arena must outlive any use of the returned Allocator.

Example:

Arena arena = arena_create(MB(64));
Allocator ally = arena_allocator(&arena);
build_scene(&ally);
function Allocator arena_allocator(Arena *arena)
Constructs an Allocator interface backed by an existing Arena.
Arena arena_create(usize reserve_size)
Creates and initializes a new arena, reserving virtual address space.
Definition mem_arena.c:60
#define MB(x)
Definition base_types.h:84
A generic allocator backed by function pointers (vtable).
Represents a linear memory arena backed by OS virtual memory.
Definition mem_arena.h:83
Parameters
arenaPointer to an initialized Arena.
Returns
An Allocator whose alloc/free/realloc functions delegate to arena.

Definition at line 220 of file mem_allocator.h.