#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
|
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.
|
|
◆ arena_create()
static Arena arena_create |
( |
long |
capacity | ) |
|
|
static |
Create a new arena.
- Parameters
-
capacity | Size 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
-
- 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
-
- 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
-
self | Pointer to the parent arena |
capacity | Size 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
-
self | Pointer to an arena |
count | Number of objects |
size | Size of a single object in bytes |
align | Requested 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
-
self | Pointer to an arena |
count | Number of objects |
size | Size of a single object in bytes |
align | Requested 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
-
self | Pointer to an arena |
ptr | Pointer to the previously allocated memory (optional) |
count | Number of objects |
size | Size of a single object in bytes |
align | Requested 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
-
dest | Pointer to the destination |
src | Pointer to the source |
size | Number 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
-
self | Pointer to an arena |
src | Pointer to the source |
count | Number of objects |
size | Size of a single object in bytes |
align | Requested 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
-
self | Pointer to the parent arena |
scratch | Scratch 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
-