diff options
author | Omniscient <omniscient.oce@gmail.com> | 2024-05-17 09:39:57 +1000 |
---|---|---|
committer | Omniscient <omniscient.oce@gmail.com> | 2024-05-17 09:39:57 +1000 |
commit | 9df999df385b74be5096218d206dd39988784237 (patch) | |
tree | 9ade81031095c70b5efb162920393903797e7ed4 /src/std/mem.c | |
parent | e61a2e43947cebaafe4c3725414d33e092bb6fad (diff) |
starting on pool allocator
Diffstat (limited to 'src/std/mem.c')
-rw-r--r-- | src/std/mem.c | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/std/mem.c b/src/std/mem.c index 4886d72..7d768c9 100644 --- a/src/std/mem.c +++ b/src/std/mem.c @@ -8,6 +8,8 @@ #define DEFAULT_ALIGNMENT (2 * sizeof(void*)) #endif +// --- Arena + void* arena_alloc_align(arena* a, size_t size, size_t align) { ptrdiff_t padding = -(uintptr_t)a->curr & (align - 1); ptrdiff_t available = a->end - a->curr - padding; @@ -38,4 +40,21 @@ arena_save arena_savepoint(arena* a) { return savept; } -void arena_rewind(arena_save savepoint) { savepoint.arena->curr = savepoint.savepoint; }
\ No newline at end of file +void arena_rewind(arena_save savepoint) { savepoint.arena->curr = savepoint.savepoint; } + +// --- Pool + +void_pool void_pool_create(arena* a, u64 capacity, u64 entry_size) { + size_t memory_requirements = capacity * entry_size; + void* backing_buf = arena_alloc(a, memory_requirements); + + void_pool pool = { .capacity = capacity, + .entry_size = entry_size, + .count = 0, + .backing_buffer = backing_buf, + .free_list_head = NULL }; + + void_pool_free_all(&pool); + + return pool; +} |