summaryrefslogtreecommitdiff
path: root/src/std
diff options
context:
space:
mode:
authorOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-25 17:36:36 +1100
committerOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-25 17:36:36 +1100
commit06ed6edcedf30fd8f7e036fffe8f81e8ca89d4b0 (patch)
treef5ae75e431817a53ee5725b4c0d6f3e61b15a664 /src/std
parent109b6dd1881d90915e972f0d263a032bd262adb5 (diff)
parentb5b7cea24c46d28a4b72bc18ca8ccb2d532007d3 (diff)
Merge branch 'master' of github.com:omnisci3nce/celeritas-core
Diffstat (limited to 'src/std')
-rw-r--r--src/std/mem.c32
-rw-r--r--src/std/mem.h15
-rw-r--r--src/std/str.c30
-rw-r--r--src/std/str.h52
4 files changed, 122 insertions, 7 deletions
diff --git a/src/std/mem.c b/src/std/mem.c
new file mode 100644
index 0000000..f5b92d4
--- /dev/null
+++ b/src/std/mem.c
@@ -0,0 +1,32 @@
+#include "mem.h"
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+#include "log.h"
+
+#ifndef DEFAULT_ALIGNMENT
+#define DEFAULT_ALIGNMENT (2 * sizeof(void*))
+#endif
+
+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;
+ TRACE("Padding %td available %td", padding, available);
+ if (available < 0 || (ptrdiff_t)size > available) {
+ ERROR_EXIT("Arena ran out of memory\n");
+ }
+ void* p = a->begin + padding;
+ a->begin += padding + size;
+ return memset(p, 0, size);
+}
+void* arena_alloc(arena* a, size_t size) { return arena_alloc_align(a, size, DEFAULT_ALIGNMENT); }
+
+arena arena_create(void* backing_buffer, size_t capacity) {
+ return (arena){ .begin = backing_buffer,
+ .curr = backing_buffer,
+ .end = backing_buffer + (ptrdiff_t)capacity };
+}
+
+void arena_free_all(arena* a) {
+ a->curr = a->begin; // pop everything at once and reset to the start.
+} \ No newline at end of file
diff --git a/src/std/mem.h b/src/std/mem.h
index 74222a7..c3ec61d 100644
--- a/src/std/mem.h
+++ b/src/std/mem.h
@@ -9,6 +9,17 @@
*/
#pragma once
-#include "defines.h"
+#include <stddef.h>
-typedef void* (*alloc)(size_t amount); \ No newline at end of file
+// Inspired by https://nullprogram.com/blog/2023/09/27/
+typedef struct arena {
+ char* begin;
+ char* curr;
+ char* end;
+} arena;
+
+arena arena_create(void* backing_buffer, size_t capacity);
+void* arena_alloc(arena* a, size_t size);
+void* arena_alloc_align(arena* a, size_t size, size_t align);
+void arena_free_all(arena* a);
+// TODO: arena_resize \ No newline at end of file
diff --git a/src/std/str.c b/src/std/str.c
index 27f8f68..3b2770e 100644
--- a/src/std/str.c
+++ b/src/std/str.c
@@ -1,4 +1,9 @@
#include "str.h"
+#include <string.h>
+
+#include "mem.h"
+
+str8 str8_create(u8* buf, size_t len) { return (str8){ .buf = buf, .len = len }; }
bool str8_equals(str8 a, str8 b) {
if (a.len != b.len) {
@@ -11,4 +16,29 @@ bool str8_equals(str8 a, str8 b) {
}
}
return true;
+}
+
+char* str8_to_cstr(arena* a, str8 s) {
+ bool is_null_terminated = s.buf[s.len - 1] == 0;
+ size_t n_bytes = is_null_terminated ? s.len : s.len + 1;
+
+ u8* dest = arena_alloc(a, n_bytes);
+
+ memcpy(dest, s.buf, s.len);
+ if (is_null_terminated) {
+ dest[s.len] = '\0';
+ }
+ return (char*)dest;
+}
+
+str8 str8_concat(arena* a, str8 left, str8 right) {
+ size_t n_bytes = left.len + right.len + 1;
+
+ u8* dest = arena_alloc(a, n_bytes);
+ memcpy(dest, left.buf, left.len);
+ memcpy(dest + right.len, right.buf, right.len);
+
+ dest[n_bytes - 1] = '\0';
+
+ return str8_create(dest, n_bytes);
} \ No newline at end of file
diff --git a/src/std/str.h b/src/std/str.h
index f3ade5a..735b88e 100644
--- a/src/std/str.h
+++ b/src/std/str.h
@@ -1,10 +1,17 @@
/**
+ * @file str.h
+ * @author your name (you@domain.com)
* @brief
+ * @version 0.1
+ * @date 2024-02-25
+ *
+ * @copyright Copyright (c) 2024
*
*/
#pragma once
#include "defines.h"
+#include "mem.h"
/**
* @brief Fat pointer representing a UTF8 (TODO some APIs supporting utf8) encoded string
@@ -12,20 +19,55 @@
print routines are written. alternatively wrap in `cstr()` and pass to `%s`.
*/
typedef struct {
- u8 *buf;
+ u8* buf;
size_t len;
} str8;
+// --- Constructors
+
+/** @brief Take a string literal and turn it into a `str8` */
+#define str8lit(s) \
+ (str8) { (u8*)s, ((sizeof(s) / sizeof(*(s)) - 1)) }
+
+str8 str8_create(u8* buf, size_t len);
+
+/** @brief Return a null-terminated C string cloned onto an arena */
+char* str8_to_cstr(arena* a, str8 s);
+
+#define cstr(a, s) (str8_to_cstr(a, s)) // Shorthand
+
+// --- Comparisons
+
/** @brief Compare two strings for exact equality */
bool str8_equals(str8 a, str8 b);
/**
* @brief Compare the first `first_nchars` of each string for equality
- If either of the strings are shorter than the number only the characters up until the end
- of the shorter string will be compared.
+ * @details If either of the strings are shorter than the number only the characters up until the
+ end of the shorter string will be compared.
* @returns 0 if they are fully equal up until `first_nchars`, i.e they never differed, else it
- returns the index at which the first string differed from the second string.
+ returns the index at which the first string differed from the second string.
*/
size_t str8_nequals(str8 a, str8 b, size_t first_nchars);
-bool str8_ends_with(str8 input_str, str8 suffix); \ No newline at end of file
+bool str8_ends_with(str8 input_str, str8 suffix);
+
+/// --- 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);
+
+str8 str8_concat(arena* a, str8 left, str8 right);
+
+/// --- Misc
+
+static inline bool str8_is_null_term(str8 a) {
+ return a.buf[a.len] == 0; // This doesn't seem safe. YOLO
+} \ No newline at end of file