cdsa
Arena allocators and friends
Loading...
Searching...
No Matches
arena.h File Reference
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

Data Structures

struct  Arena
 Represents an arena allocator. More...
 

Functions

static Arena arena_create (long capacity)
 Create a new arena.
 
static long arena_occupied (const Arena *self)
 Get the size of the occupied memory region.
 
static long arena_available (const Arena *self)
 Get the size of the available memory region.
 
static Arena arena_scratch_create (Arena *self, long capacity)
 Create a new scratch arena at the end of the available space of the parent arena.
 
static void * arena_malloc (Arena *self, long count, long size, long align)
 Allocate uninitialized memory from an arena.
 
static void * arena_calloc (Arena *self, long count, long size, long align)
 Allocate and zero-initialize memory from an arena.
 
static void * arena_realloc (Arena *self, void *ptr, long count, long size, long align)
 Reallocate memory from an arena.
 
static void * arena_memcpy (Arena *, void *dest, const void *src, long size)
 Copy memory from one location to another within an arena.
 
static void * arena_memdup (Arena *self, const void *src, long count, long size, long align)
 Duplicate memory within the arena.
 
static void arena_scratch_destroy (Arena *self, Arena scratch)
 Destroy a scratch arena and return its space to the parent arena.
 
static void arena_destroy (Arena *self)
 Destroy an arena and free its base memory region.
 

Function Documentation

◆ arena_create()

static Arena arena_create ( long  capacity)
static

Create a new arena.

Parameters
capacitySize of the base memory region in bytes
Returns
New arena instance
Note
The arena must be destroyed using arena_destroy() to prevent memory leaks

◆ arena_occupied()

static long arena_occupied ( const Arena self)
static

Get the size of the occupied memory region.

Parameters
selfPointer to an arena
Returns
Size of occupied memory in bytes

◆ arena_available()

static long arena_available ( const Arena self)
static

Get the size of the available memory region.

Parameters
selfPointer to an arena
Returns
Size of available memory in bytes

◆ arena_scratch_create()

static Arena arena_scratch_create ( Arena self,
long  capacity 
)
static

Create a new scratch arena at the end of the available space of the parent arena.

Parameters
selfPointer to the parent arena
capacitySize of the base memory region in bytes
Returns
New scratch arena instance

◆ arena_malloc()

static void * arena_malloc ( Arena self,
long  count,
long  size,
long  align 
)
static

Allocate uninitialized memory from an arena.

Parameters
selfPointer to an arena
countNumber of objects
sizeSize of a single object in bytes
alignRequested alignment in bytes
Returns
Pointer to the allocated memory
Note
This function will trigger an assertion failure if the arena runs out of memory

◆ arena_calloc()

static void * arena_calloc ( Arena self,
long  count,
long  size,
long  align 
)
static

Allocate and zero-initialize memory from an arena.

Parameters
selfPointer to an arena
countNumber of objects
sizeSize of a single object in bytes
alignRequested alignment in bytes
Returns
Pointer to the allocated memory

◆ arena_realloc()

static void * arena_realloc ( Arena self,
void *  ptr,
long  count,
long  size,
long  align 
)
static

Reallocate memory from an arena.

Parameters
selfPointer to an arena
ptrPointer to the previously allocated memory (optional)
countNumber of objects
sizeSize of a single object in bytes
alignRequested alignment in bytes
Returns
Pointer to the allocated memory
Note
If ptr is a null pointer, the function behaves like arena_malloc()
If ptr is the last allocated object of the arena, the alignment will be unchanged

◆ arena_memcpy()

static void * arena_memcpy ( Arena ,
void *  dest,
const void *  src,
long  size 
)
static

Copy memory from one location to another within an arena.

Parameters
destPointer to the destination
srcPointer to the source
sizeNumber of bytes to copy
Returns
Pointer to the destination

◆ arena_memdup()

static void * arena_memdup ( Arena self,
const void *  src,
long  count,
long  size,
long  align 
)
static

Duplicate memory within the arena.

Parameters
selfPointer to an arena
srcPointer to the source
countNumber of objects
sizeSize of a single object in bytes
alignRequested alignment in bytes
Returns
Pointer to the duplicated memory

◆ arena_scratch_destroy()

static void arena_scratch_destroy ( Arena self,
Arena  scratch 
)
static

Destroy a scratch arena and return its space to the parent arena.

Parameters
selfPointer to the parent arena
scratchScratch arena to destroy
Note
Scratch arenas must be destroyed in inverse creation order

◆ arena_destroy()

static void arena_destroy ( Arena self)
static

Destroy an arena and free its base memory region.

Parameters
selfPointer to an arena