diff options
author | Omniscient <omniscient.oce@gmail.com> | 2024-11-04 21:38:09 +1100 |
---|---|---|
committer | Omniscient <omniscient.oce@gmail.com> | 2024-11-04 21:38:09 +1100 |
commit | 81eb8d48703f12391f1e19ab813005ebe922fabb (patch) | |
tree | 8df5c407fecb5847685d09a2508d4e2cac691fdd | |
parent | c2812d1a79a1d790b2c549abdfe9344d4fb4f4d2 (diff) | |
parent | 66021fc4573face1b4bbae909cc368197fda6f02 (diff) |
Merge branch 'master' into vk
-rw-r--r-- | include/celeritas.h | 61 | ||||
-rw-r--r-- | src/mem.c | 4 |
2 files changed, 53 insertions, 12 deletions
diff --git a/include/celeritas.h b/include/celeritas.h index 420a63b..4a75abd 100644 --- a/include/celeritas.h +++ b/include/celeritas.h @@ -120,19 +120,22 @@ void _cel_push_error(int error_code); // --- Memory facilities: Allocators, helpers +/** @brief Fixed-size arena allocator + @note Inspired by https://nullprogram.com/blog/2023/09/27/ +*/ typedef struct arena { - u8* storage; + u8* begin; u8* curr; u8* end; - size_t alignment; - bool can_grow; } arena; -/** @brief Fixed-size arena allocator */ -typedef struct arena fixed_arena; +typedef struct arena_save { + arena* arena; + u8* savepoint; +} arena_save; -/** @brief Create a new fixed size arena with the provided storage. */ -fixed_arena fixed_arena_new(void* backing_buffer, size_t size, size_t alignment); +/** @brief Create a new fixed arena with the provided storage. */ +arena arena_create(void* backing_buffer, size_t capacity); /** * @brief Allocates memory on the arena. @@ -141,12 +144,22 @@ fixed_arena fixed_arena_new(void* backing_buffer, size_t size, size_t alignment) * @param size Number of bytes to reserve * @return Pointer to the allocated memory or NULL if there's not enough space */ -void* fixed_arena_alloc(fixed_arena* arena, size_t size); +void* arena_alloc(arena* a, size_t size); + +void* arena_alloc_align(arena* a, size_t size, size_t alignment); /** @brief Clear the arena thereby resetting the index to the start. */ -void fixed_arena_clear(fixed_arena* a); +void arena_clear(arena* a); + +// TODO +arena_save arena_savepoint(arena* a); +// TODO +void arena_rewind(arena_save savepoint); -// TODO: grow arena +/** @brief Growable arena allocator based on OS-level memory address space overcommit */ +typedef struct grow_arena { + // TODO: grow arena +} grow_arena; // Pool typedef struct void_pool_header void_pool_header; // TODO: change name of this @@ -203,13 +216,39 @@ u32 void_pool_insert(void_pool* pool, void* item); * @note when using `printf` you must use %s.*s length, string until our own modified print routines are written. alternatively wrap in `cstr()` and pass to `%s`. */ -typedef struct { +typedef struct str8 { u8* buf; size_t len; } str8; + +/** @brief Take a string literal and turn it into a `str8` */ +#define str8(s) \ + (Str8) { (u8*)s, ((sizeof(s) / sizeof(*(s)) - 1)) } + +// Comparisons + +/** @brief Compare two strings for exact equality */ bool str8_equals(str8 a, str8 b); +// Subviews + +str8 Str8_substr(str8 s, u64 min, u64 max); +/** @brief Keeps only the `first_n` chars of `s` */ +str8 Str8_take(str8 s, u64 first_n); +/** @brief Keeps only the `last_n` chars of `s` */ +str8 Str8_drop(str8 s, u64 last_n); +/** @brief Keeps everything after the first `n` chars of `s` */ +str8 Str8_skip(str8 s, u64 n); +/** @brief Keeps everything before the last `n` chars of `s` */ +str8 Str8_chop(str8 s, u64 n); + +// Misc + +static inline bool Str8_is_null_term(str8 a) { + return a.buf[a.len] == 0; // This doesn't seem safe. YOLO +} + // --- Logging // Log levels @@ -1,6 +1,8 @@ #include <celeritas.h> -fixed_arena fixed_arena_new(void* backing_buffer, size_t size, size_t alignment){ TODO("") } +arena arena_create(void* backing_buffer, size_t size) { + TODO("") +} void_pool void_pool_create(void* storage, const char* debug_label, u64 capacity, u64 entry_size) { size_t _memory_requirements = capacity * entry_size; |