9#define ARENA_DEFAULT_SIZE 0x10000
37 arena->
root = allocator_alloc_ty(
byte, fallback, size);
38 memset(arena->
root, 0xCC, size);
53 ASSERT_(arena->
root != 0,
"Expected arena to be initialized");
56 arena->
offset + size < arena->size,
57 "Expected enough space in the arena (capacity=%d, busy=%d, requested=%d)",
58 (
int)arena->
size, (
int)arena->
offset, (
int)size);
67 arena->
offset = (arena->
offset +
sizeof(
void *) - 1) & ~(
sizeof(
void *) - 1);
83 ASSERT_(arena->
root != 0,
"Attempt to free an uninitialized arena");
92static void *allocator_arena__alloc(
void *arena,
sz size) {
97static void *allocator_arena__realloc(
void *self,
void *ptr,
sz size) {
102 memcpy(new_ptr, ptr, copy_size);
107static void allocator_arena__free(
void *self,
void *ptr) {
118 ret.
alloc = allocator_arena__alloc;
119 ret.
realloc = allocator_arena__realloc;
120 ret.
free = allocator_arena__free;
127#define arena_alloc_ty(ty, arena, sz) (ty *)arena_alloc(arena, sz * sizeof(ty))
static void * arena_alloc(arena_t *arena, sz size)
Allocate size bytes data inside the arena. O(1)
Definition arena.h:52
static arena_t ARENA_GLOBAL
The single global arena. Declared here instead of specific files to prevent aliasing issues.
Definition arena.h:31
static void arena_init_default(arena_t *arena)
Initializes the memory arena using malloc with ARENA_DEFAULT_SIZE bytes of memory.
Definition arena.h:47
#define ARENA_DEFAULT_SIZE
Definition arena.h:9
#define ASSERT(expr, msg,...)
Definition defs.h:35
size_t sz
Definition defs.h:51
#define ASSERT_(expr, msg)
Definition defs.h:29
#define unpoison_memory_region(ptr, sz)
Makes previously poisoned memory safe again. Idemponent. Pair of poison_memory_region....
Definition memory.h:136
static allocator_t allocator_new_malloc()
Wrap malloc into an generic allocator interface.
Definition memory.h:99
#define poison_memory_region(ptr, sz)
If compiled with a memory sanitizer, any write to the brief selected memory will crash the program....
Definition memory.h:128
A generic allocator type passed by value. Contains a fallback allocator and a set of function pointer...
Definition memory.h:30
mem_alloc_f * alloc
Definition memory.h:33
mem_free_f * free
Definition memory.h:35
allocator_ptr allocator
A pointer to the actual underyling allocator, type-erased.
Definition memory.h:32
mem_realloc_f * realloc
Definition memory.h:34
The Arena allocator is a single continous block of memory.
Definition arena.h:17
static void arena_cleanup(arena_t *arena)
Release all the allocated memory.
Definition arena.h:82
sz offset
The next location to put the data at.
Definition arena.h:26
allocator_t fallback
The allocator that the arena will forward all the allocation requests to.
Definition arena.h:20
sz size
The total amount of bytes the arena can handle.
Definition arena.h:24
static allocator_t arena_as_allocator(arena_t *arena)
A generic function to type-erase the arena_t into an allocator_t.
Definition arena.h:115
byte * root
The starting address of the arena.
Definition arena.h:22
static void arena_clear(arena_t *arena)
Clears the memory arena entirely and marks all the memory as poison. Importantly, does not release me...
Definition arena.h:75
static void arena_init(arena_t *arena, allocator_t fallback, sz size)
Initializes the memory arena.
Definition arena.h:35