cbase 1.46.11
C/C++ Static Template
Loading...
Searching...
No Matches
mem_allocator.h File Reference

Generic allocator interface (vtable) for polymorphic memory allocation. More...

#include "base/base_macros.h"
#include "base/base_types.h"
#include "memory/mem_arena.h"
Include dependency graph for mem_allocator.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

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

Generic allocator interface (vtable) for polymorphic memory allocation.

Defines a lightweight Allocator struct whose function pointers can be backed by any allocator implementation (Arena, heap, pool, etc.). Code that accepts an Allocator by value is automatically compatible with every allocator in the project without a recompile.

Design rationale
Rather than threading a concrete Arena * through every API, you pass an Allocator. The caller decides which backing allocator to use at the call site making library code allocator-agnostic.
Usage
// Wrap an arena in the generic interface:
Arena arena = arena_create(MB(64));
Allocator ally = arena_allocator(&arena);
// Allocate through the interface (zero-initialized):
MyStruct *s = (MyStruct *)allocator_alloc(&ally, sizeof(MyStruct));
// Free is a no-op for arena-backed allocators, but works generically:
allocator_free(&ally, s, sizeof(MyStruct));
// The same function works with any allocator:
void build_thing(Allocator *a) {
Node *n = (Node *)allocator_alloc(a, sizeof(Node));
}
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 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
Extending with a new allocator
static void *my_alloc_fn(void *ctx, usize size) { ... }
static void my_free_fn (void *ctx, void *ptr, usize size) { ... }
static void *my_realloc_fn(void *ctx, void *ptr, usize old_size, usize new_size) { ... }
Allocator my_allocator = {
.ctx = my_ctx_ptr,
.alloc = my_alloc_fn,
.free = my_free_fn,
.realloc = my_realloc_fn,
};
size_t usize
Definition base_types.h:43

Definition in file mem_allocator.h.