cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
Ergonomic Allocation Macros

Dylan Falconer-style typed allocation macros. No casts, no sizeof. 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.

Detailed Description

Dylan Falconer-style typed allocation macros. No casts, no sizeof.

Macro Definition Documentation

◆ MAKE

#define MAKE ( type,
count,
alloc )
Value:
((type *)mem_alloc((alloc), sizeof(type) * (count)))
function void * mem_alloc(MemAllocator *alloc, usize size)
Allocator Helper Functions.

Allocates and zero-initialises an array of count elements of type.

Example:

MemAllocator a = mem_allocator_create(ARENA, GB(1));
MyStruct *nodes = MAKE(MyStruct, 64, &a);
#define MAKE(type, count, alloc)
Allocates and zero-initialises an array of count elements of type.
#define GB(x)
Definition base_types.h:121
An abstract allocator interface.
Parameters
typeThe element type.
countNumber of elements.
allocPointer to a MemAllocator.
Returns
Correctly typed pointer to the zeroed allocation, or NULL on OOM.

Definition at line 208 of file mem_allocator.h.

◆ MAKE_STRUCT

#define MAKE_STRUCT ( type,
alloc )
Value:
((type *)mem_alloc((alloc), sizeof(type)))

Allocates and zero-initialises a single instance of type.

Example:

Node *n = MAKE_STRUCT(Node, &a);
n->value = 42;
#define MAKE_STRUCT(type, alloc)
Allocates and zero-initialises a single instance of type.
Parameters
typeThe struct type to allocate.
allocPointer to a MemAllocator.
Returns
Correctly typed pointer to the zeroed struct, or NULL on OOM.

Definition at line 223 of file mem_allocator.h.

◆ RELEASE

#define RELEASE ( type,
count,
ptr,
alloc )
Value:
mem_free((alloc), (ptr), sizeof(type) * (count))
function void mem_free(MemAllocator *alloc, void *ptr, usize size)
Frees a previously allocated block through the allocator.

Frees a previously allocated block through the allocator.

For arena allocators this is a no-op. For heap allocators it calls free.

Parameters
typeThe element type (used to compute the size).
countNumber of elements originally allocated.
ptrPointer to the allocation.
allocPointer to the MemAllocator.

Definition at line 235 of file mem_allocator.h.