summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmniscient <omniscient.oce@gmail.com>2024-11-04 21:36:03 +1100
committerOmniscient <omniscient.oce@gmail.com>2024-11-04 21:36:03 +1100
commit66021fc4573face1b4bbae909cc368197fda6f02 (patch)
treed60d98a08218501e53a5627a9125b8689a2c8026
parent7577b6d51c5180ec23f93cec79a5c77e54130558 (diff)
copy string function defs. change fixed_arena -> arena
-rw-r--r--include/celeritas.h71
-rw-r--r--src/mem.c2
2 files changed, 61 insertions, 12 deletions
diff --git a/include/celeritas.h b/include/celeritas.h
index 3dc3761..8b75400 100644
--- a/include/celeritas.h
+++ b/include/celeritas.h
@@ -120,16 +120,22 @@ void _cel_push_error(int error_code);
// --- Memory facilities: Allocators, helpers
-/** @brief Fixed-size arena allocator */
-typedef struct fixed_arena {
- void* storage;
- size_t size;
- size_t alignment;
- int index;
-} fixed_arena;
+/** @brief Fixed-size arena allocator
+ @note Inspired by https://nullprogram.com/blog/2023/09/27/
+*/
+typedef struct arena {
+ u8* begin;
+ u8* curr;
+ u8* end;
+} arena;
+
+typedef struct arena_save {
+ arena* arena;
+ u8* savepoint;
+} arena_save;
/** @brief Create a new fixed arena with the provided storage. */
-fixed_arena fixed_arena_new(void* backing_buffer, size_t size, size_t alignment);
+arena arena_create(void* backing_buffer, size_t capacity);
/**
* @brief Allocates memory on the arena.
@@ -138,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
@@ -195,6 +211,39 @@ u32 void_pool_insert(void_pool* pool, void* item);
// --- Strings
+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
diff --git a/src/mem.c b/src/mem.c
index 84c90e6..ec2f81c 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -1,6 +1,6 @@
#include <celeritas.h>
-fixed_arena fixed_arena_new(void *backing_buffer, size_t size, size_t alignment) {
+arena arena_create(void* backing_buffer, size_t size) {
TODO("")
}