Subastra
Loading...
Searching...
No Matches
arena.h
Go to the documentation of this file.
1#ifndef __H__ARENA__
2#define __H__ARENA__
3
4#include <string.h>
5
6#include "defs.h"
7#include "memory.h"
8
9#define ARENA_DEFAULT_SIZE 0x10000
10
17typedef struct {
22 byte *root;
27} arena_t;
28
32
35static void arena_init(arena_t *arena, allocator_t fallback, sz size) {
36 arena->fallback = fallback;
37 arena->root = allocator_alloc_ty(byte, fallback, size);
38 memset(arena->root, 0xCC, size);
39 poison_memory_region(arena->root, size);
40
41 arena->size = size;
42 arena->offset = 0;
43}
44
47static void arena_init_default(arena_t *arena) {
48 arena_init(arena, allocator_new_malloc(), ARENA_DEFAULT_SIZE);
49}
50
52static void *arena_alloc(arena_t *arena, sz size) {
53 ASSERT_(arena->root != 0, "Expected arena to be initialized");
54
55 ASSERT(
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);
59
60 void *ptr = arena->root + arena->offset;
61 arena->offset += size;
62
63 unpoison_memory_region(ptr, size);
64
65 // Align the pointer correctly.
66 // TODO: test if it actually speeds anything up
67 arena->offset = (arena->offset + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
68
69 return ptr;
70}
71
75static void arena_clear(arena_t *arena) {
76 poison_memory_region(arena, arena->size);
77 arena->offset = 0;
78}
79
82static void arena_cleanup(arena_t *arena) {
83 ASSERT_(arena->root != 0, "Attempt to free an uninitialized arena");
84
85 allocator_free(arena->fallback, arena->root);
86 arena->root = 0;
87 arena->offset = 0;
88 arena->size = 0;
89}
90
92static void *allocator_arena__alloc(void *arena, sz size) {
93 return arena_alloc((arena_t *)arena, size);
94}
95
97static void *allocator_arena__realloc(void *self, void *ptr, sz size) {
98 arena_t *arena = (arena_t *)self;
99 void *new_ptr = arena_alloc(arena, size);
100 sz copy_size = size;
101
102 memcpy(new_ptr, ptr, copy_size);
103 return new_ptr;
104}
105
107static void allocator_arena__free(void *self, void *ptr) {
108 // TODO: poison the memory region
109 return;
110}
111
116 allocator_t ret;
117 ret.allocator = (void *)arena;
118 ret.alloc = allocator_arena__alloc;
119 ret.realloc = allocator_arena__realloc;
120 ret.free = allocator_arena__free;
121
122 return ret;
123}
124
127#define arena_alloc_ty(ty, arena, sz) (ty *)arena_alloc(arena, sz * sizeof(ty))
128
129#endif
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