diff options
Diffstat (limited to 'src/std')
-rw-r--r-- | src/std/mem.c | 21 | ||||
-rw-r--r-- | src/std/mem.h | 28 |
2 files changed, 47 insertions, 2 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; +} diff --git a/src/std/mem.h b/src/std/mem.h index bbfb852..eef97a0 100644 --- a/src/std/mem.h +++ b/src/std/mem.h @@ -10,6 +10,9 @@ #pragma once #include <stddef.h> +#include "defines.h" + +// --- Arena // Inspired by https://nullprogram.com/blog/2023/09/27/ typedef struct arena { @@ -30,4 +33,27 @@ void arena_free_all(arena* a); void arena_free_storage(arena* a); arena_save arena_savepoint(arena* a); void arena_rewind(arena_save savepoint); -// TODO: arena_resize
\ No newline at end of file +// TODO: arena_resize + +// --- Pool + +typedef struct void_pool_header void_pool_header; +struct void_pool_header { + void_pool_header* next; +}; + +typedef struct void_pool { + u64 capacity; + u64 entry_size; + u64 count; + void* backing_buffer; + void_pool_header* free_list_head; +} void_pool; + +void_pool void_pool_create(arena* a, u64 capacity, u64 entry_size); +void void_pool_free_all(void_pool* pool); +bool void_pool_is_empty(void_pool* pool); +bool void_pool_is_full(void_pool* pool); +void* void_pool_get(u32 raw_handle); + +// TODO: macro that lets us specialise |