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

A vtable structure for passing allocators dynamically. More...

Classes

struct  MemAllocator
 An abstract allocator interface. More...

Typedefs

typedef enum MemAllocMode MemAllocMode
 Describes the type of memory operation being requested.
typedef void *(* MemAllocFn) (MemAllocMode mode, void *ctx, usize size, void *old_ptr)
 The core function signature for an abstract allocator.
typedef struct MemAllocator MemAllocator
 An abstract allocator interface.

Enumerations

enum  MemAllocMode { MEM_ALLOC_MODE_ALLOC , MEM_ALLOC_MODE_FREE , MEM_ALLOC_MODE_REALLOC , MEM_ALLOC_MODE_FREE_ALL }
 Describes the type of memory operation being requested. More...

Functions

function void * mem_alloc (MemAllocator *alloc, usize size)
 Allocator Helper Functions.
function void * mem_realloc (MemAllocator *alloc, void *old_ptr, usize old_size, usize new_size)
 Resizes a previously allocated block through the allocator.
function void mem_free (MemAllocator *alloc, void *ptr, usize size)
 Frees a previously allocated block through the allocator.
function void mem_free_all (MemAllocator *alloc)
 Clears the entire allocator in one shot (e.g. arena reset, pool reset).

Detailed Description

A vtable structure for passing allocators dynamically.

Typedef Documentation

◆ MemAllocFn

typedef void *(* MemAllocFn) (MemAllocMode mode, void *ctx, usize size, void *old_ptr)

The core function signature for an abstract allocator.

  • Parameters
    modeThe memory operation being requested.
    ctxThe internal context/state of the allocator (e.g., pointer to an Arena).
    sizeThe size of the requested allocation.
    old_ptrA pointer to existing memory (used during FREE and REALLOC modes).
    Returns
    A pointer to the newly allocated memory, or NULL on failure or FREE.

Definition at line 80 of file mem_allocator.h.

Enumeration Type Documentation

◆ MemAllocMode

Describes the type of memory operation being requested.

Enumerator
MEM_ALLOC_MODE_ALLOC 

Request new memory.

MEM_ALLOC_MODE_FREE 

Free existing memory.

MEM_ALLOC_MODE_REALLOC 

Resize existing memory.

MEM_ALLOC_MODE_FREE_ALL 

Clear the entire allocator (if supported).

Definition at line 65 of file mem_allocator.h.

Function Documentation

◆ mem_alloc()

function void * mem_alloc ( MemAllocator * alloc,
usize size )
inline

Allocator Helper Functions.

Allocates size zero-initialized bytes through the allocator.

Example:

int *nums = (int *)mem_alloc(&alloc, sizeof(int) * 64);
// nums[0..63] are all zero
function void * mem_alloc(MemAllocator *alloc, usize size)
Allocator Helper Functions.
struct MemAllocator mem_arena_allocator(MemArena *arena)
Wraps a MemArena in the generic MemAllocator interface.
Definition mem_arena.c:172
An abstract allocator interface.
Parameters
allocPointer to the allocator.
sizeNumber of bytes to allocate.
Returns
Pointer to zero-initialized memory, or NULL on OOM.

Definition at line 105 of file mem_allocator.h.

Here is the caller graph for this function:

◆ mem_free()

function void mem_free ( MemAllocator * alloc,
void * ptr,
usize size )
inline

Frees a previously allocated block through the allocator.

No-op for arena allocators. Always safe to call regardless of allocator type.

Example:

MyStruct *s = MAKE_STRUCT(MyStruct, &alloc);
// ... use s ...
mem_free(&alloc, s, sizeof(MyStruct));
#define MAKE_STRUCT(type, alloc)
Allocates and zero-initialises a single instance of type.
function void mem_free(MemAllocator *alloc, void *ptr, usize size)
Frees a previously allocated block through the allocator.
MemAllocator mem_gpalloc(void)
Retrieves the generic allocator interface for the GP Allocator.
Parameters
allocPointer to the allocator.
ptrPointer to free.
sizeThe size originally requested.

Definition at line 166 of file mem_allocator.h.

◆ mem_free_all()

function void mem_free_all ( MemAllocator * alloc)
inline

Clears the entire allocator in one shot (e.g. arena reset, pool reset).

For arena allocators this resets pos to 0. For heap allocators this is a no-op. Use this instead of manually calling mem_arena_clear() so that code stays allocator-agnostic.

Parameters
allocPointer to the allocator to clear.

Definition at line 181 of file mem_allocator.h.

◆ mem_realloc()

function void * mem_realloc ( MemAllocator * alloc,
void * old_ptr,
usize old_size,
usize new_size )
inline

Resizes a previously allocated block through the allocator.

For arena allocators this always allocates a fresh block and copies the old data — the old block is not freed since arenas don't support individual frees. For heap allocators this maps to realloc.

Example:

int *buf = (int *)mem_alloc(&alloc, sizeof(int) * 4);
buf[0] = 1; buf[3] = 4;
// Grow to 8 elements — old data is preserved:
buf = (int *)mem_realloc(&alloc, buf, sizeof(int) * 4, sizeof(int) * 8);
// buf[0] == 1, buf[3] == 4, buf[4..7] == 0
function void * mem_realloc(MemAllocator *alloc, void *old_ptr, usize old_size, usize new_size)
Resizes a previously allocated block through the allocator.
Parameters
allocPointer to the allocator.
old_ptrExisting allocation to resize.
old_sizeSize of the existing allocation in bytes.
new_sizeDesired new size in bytes.
Returns
Pointer to the resized allocation, or NULL on OOM.

Definition at line 135 of file mem_allocator.h.