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

Generic interface for abstracting memory allocators. More...

#include "base/base_macros.h"
#include "base/base_types.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  MemAllocator
 An abstract allocator interface. More...

Macros

#define MAKE(type, count, alloc)
 Allocates and zero-initialises an array of count elements of type.
#define MAKE_STRUCT(type, alloc)
 Allocates and zero-initialises a single instance of type.
#define RELEASE(type, count, ptr, alloc)
 Frees a previously allocated block through the allocator.

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

Generic interface for abstracting memory allocators.

Allows data structures to accept a generic allocator interface rather than hardcoding a dependency on an Arena or General Purpose Allocator. Code that takes a MemAllocator works with any backing allocator without recompilation.

Usage
// Wrap an arena in the generic interface:
// Allocate through the interface (zero-initialized):
MyStruct *s = MAKE_STRUCT(MyStruct, &alloc);
s->x = 10;
// Allocate an array:
int *buf = MAKE(int, 256, &alloc);
// Free is a no-op for arenas, but portable for any allocator:
RELEASE(int, 256, buf, &alloc);
// Reset the allocator (arena clear, pool reset, etc.):
mem_free_all(&alloc);
#define MAKE(type, count, alloc)
Allocates and zero-initialises an array of count elements of type.
#define RELEASE(type, count, ptr, alloc)
Frees a previously allocated block through the allocator.
#define MAKE_STRUCT(type, alloc)
Allocates and zero-initialises a single instance of type.
function void mem_free_all(MemAllocator *alloc)
Clears the entire allocator in one shot (e.g. arena reset, pool reset).
struct MemAllocator mem_arena_allocator(MemArena *arena)
Wraps a MemArena in the generic MemAllocator interface.
Definition mem_arena.c:172
MemArena mem_arena_create(usize reserve_size)
Initializes a new virtual memory Arena.
Definition mem_arena.c:48
#define MB(x)
Definition base_types.h:120
An abstract allocator interface.
A virtual memory-backed linear allocator.
Definition mem_arena.h:70
Implementing a custom allocator
static void *
my_alloc_fn(MemAllocMode mode, void *ctx, usize size, void *old_ptr)
{
MyAllocState *state = (MyAllocState *)ctx;
switch (mode) {
case MEM_ALLOC_MODE_ALLOC: return my_alloc(state, size);
case MEM_ALLOC_MODE_FREE: my_free(state, old_ptr); return NULL;
case MEM_ALLOC_MODE_REALLOC: return my_realloc(state, old_ptr, size);
case MEM_ALLOC_MODE_FREE_ALL: my_reset(state); return NULL;
}
return NULL;
}
MemAllocator alloc = { .fn = my_alloc_fn, .ctx = &my_state };
MemAllocMode
Describes the type of memory operation being requested.
@ MEM_ALLOC_MODE_FREE_ALL
@ MEM_ALLOC_MODE_ALLOC
@ MEM_ALLOC_MODE_REALLOC
@ MEM_ALLOC_MODE_FREE
size_t usize
Definition base_types.h:68

Definition in file mem_allocator.h.