From 4b9ab2ec5acf2ecf2e96c38733a835b964664171 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 11:02:46 +1100 Subject: add an arena allocator first impl --- src/log.h | 2 ++ src/maths/geometry.h | 2 +- src/std/mem.c | 26 ++++++++++++++++++++++++++ src/std/mem.h | 12 ++++++++++-- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/std/mem.c (limited to 'src') diff --git a/src/log.h b/src/log.h index e7a90ea..b0c355b 100644 --- a/src/log.h +++ b/src/log.h @@ -1,6 +1,8 @@ #pragma once #include +#include +#include #define ERROR_EXIT(...) \ { \ diff --git a/src/maths/geometry.h b/src/maths/geometry.h index 6f4797b..937c38a 100644 --- a/src/maths/geometry.h +++ b/src/maths/geometry.h @@ -4,7 +4,7 @@ * @brief Shapes and intersections between them * @version 0.1 * @date 2024-02-24 - * + * * @copyright Copyright (c) 2024 */ #pragma once diff --git a/src/std/mem.c b/src/std/mem.c new file mode 100644 index 0000000..c30f76d --- /dev/null +++ b/src/std/mem.c @@ -0,0 +1,26 @@ +#include "mem.h" +#include +#include +#include +#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->begin & (align - 1); + ptrdiff_t available = a->end - a->begin - 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, .end = backing_buffer + (ptrdiff_t)capacity }; +} \ No newline at end of file diff --git a/src/std/mem.h b/src/std/mem.h index 74222a7..75b6d2a 100644 --- a/src/std/mem.h +++ b/src/std/mem.h @@ -9,6 +9,14 @@ */ #pragma once -#include "defines.h" +#include -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* 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); -- cgit v1.2.3-70-g09d2 From 80a734de03eaf3d534f95010653c47bf54389f48 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 12:50:39 +1100 Subject: new string function signatures just dropped --- examples/main_loop/ex_main_loop.c | 39 ++++++++++++++++++++------ src/platform/file.c | 31 ++++++++++++++++++++ src/platform/file.h | 10 ++++++- src/renderer/render_types.h | 3 +- src/std/str.c | 30 ++++++++++++++++++++ src/std/str.h | 59 +++++++++++++++++++++++++++++++++------ 6 files changed, 154 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/examples/main_loop/ex_main_loop.c b/examples/main_loop/ex_main_loop.c index 0ea9ded..de0caa6 100644 --- a/examples/main_loop/ex_main_loop.c +++ b/examples/main_loop/ex_main_loop.c @@ -1,22 +1,45 @@ #include #include "core.h" +#include "file.h" #include "render.h" +#include "str.h" int main() { core* core = core_bringup(); - // Main loop - while (!glfwWindowShouldClose(core->renderer.window)) { - input_update(&core->input); - threadpool_process_results(&core->threadpool, 1); + size_t arena_size = 1024; + arena scratch = arena_create(malloc(arena_size), arena_size); + arena* a = &scratch; + + str8 hello = str8lit("Hello World"); + + // this works but we should be careful because str8 is not *guaranteed* to point to + // a null-terminated string + printf("String before: '%s' (null-terminated: %s) \n ", hello.buf, + str8_is_null_term(hello) ? "true" : "false"); - render_frame_begin(&core->renderer); + char* c = str8_to_cstr(&scratch, hello); - // insert work here + printf("String after: %s\n", c); - render_frame_end(&core->renderer); + str8_opt test_file = str8_from_file(&scratch, str8lit("assets/shaders/ui_rect.vert")); + if (test_file.has_value) { + printf("Contents: %.*s \n", (int)test_file.contents.len, test_file.contents.buf); + printf("Null-terminated: %s\n", str8_is_null_term(test_file.contents) ? "true" : "false"); } + // Main loop + // while (!glfwWindowShouldClose(core->renderer.window)) { + // input_update(&core->input); + // threadpool_process_results(&core->threadpool, 1); + // + // render_frame_begin(&core->renderer); + // + // // insert work here + // + // render_frame_end(&core->renderer); + // } + return 0; -} \ No newline at end of file +} diff --git a/src/platform/file.c b/src/platform/file.c index 45c5b30..44aa9d0 100644 --- a/src/platform/file.c +++ b/src/platform/file.c @@ -7,6 +7,8 @@ #include #include "log.h" +#include "mem.h" +#include "str.h" const char *string_from_file(const char *path) { FILE *f = fopen(path, "rb"); @@ -29,4 +31,33 @@ const char *string_from_file(const char *path) { string[fsize] = '\0'; return string; +} + +str8_opt str8_from_file(arena *a, str8 path) { + char *p = cstr(a, path); + str8_opt result = { .has_value = false }; + + FILE *f = fopen(p, "rb"); + if (f == NULL) { + ERROR("Error reading file: %s. errno: %d", path, errno); + return result; + } + if (ferror(f)) { + ERROR("Error reading file: %s. errno: %d", path, errno); + return result; + } + fseek(f, 0, SEEK_END); + long fsize = ftell(f); + rewind(f); + + u8 *raw = arena_alloc(a, fsize + 1); + str8 contents = str8_create(raw, fsize); + contents.buf[contents.len] = '\0'; + + fread(raw, fsize, 1, f); + fclose(f); + result.contents = contents; + result.has_value = true; + + return result; } \ No newline at end of file diff --git a/src/platform/file.h b/src/platform/file.h index b965ceb..fa09891 100644 --- a/src/platform/file.h +++ b/src/platform/file.h @@ -6,6 +6,14 @@ */ #pragma once -#include +#include "defines.h" +#include "str.h" + +typedef struct str8_opt { + str8 contents; + bool has_value; +} str8_opt; const char* string_from_file(const char* path); + +str8_opt str8_from_file(arena* a, str8 path); \ No newline at end of file diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index a3639e7..e24fc24 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -9,6 +9,7 @@ #include "darray.h" #include "maths_types.h" +#include "str.h" struct GLFWwindow; @@ -65,7 +66,7 @@ KITC_DECL_TYPED_ARRAY(mesh) // creates "mesh_darray" #endif typedef struct model { - char name[256]; + str8 name; } model; #ifndef TYPED_MODEL_ARRAY 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 + +#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 3d3cb41..d2fa891 100644 --- a/src/std/str.h +++ b/src/std/str.h @@ -1,30 +1,73 @@ /** - * @brief - * + * @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) encoded string - * @note when using `printf` you must use %s.*s length, string + * @brief Fat pointer representing a UTF8 encoded string + * @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 { 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 -- cgit v1.2.3-70-g09d2 From d919e8cb785c165ee700907b74672eb586c55d29 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:31:17 +1100 Subject: add an arena reset to free everything --- src/std/mem.c | 10 +++++++--- src/std/mem.h | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/std/mem.c b/src/std/mem.c index c30f76d..419a712 100644 --- a/src/std/mem.c +++ b/src/std/mem.c @@ -9,8 +9,8 @@ #endif void* arena_alloc_align(arena* a, size_t size, size_t align) { - ptrdiff_t padding = -(uintptr_t)a->begin & (align - 1); - ptrdiff_t available = a->end - a->begin - padding; + 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"); @@ -22,5 +22,9 @@ void* arena_alloc_align(arena* a, size_t size, size_t align) { 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, .end = backing_buffer + (ptrdiff_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 75b6d2a..36d30bd 100644 --- a/src/std/mem.h +++ b/src/std/mem.h @@ -14,9 +14,11 @@ // 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_alloc(arena* a, size_t size); +void* arena_alloc_align(arena* a, size_t size, size_t align); +void arena_free_all(arena* a); \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 1d6be7975928e644e56422f3244aae5571f48ee0 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:31:44 +1100 Subject: chore: format --- src/std/mem.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/std/mem.c b/src/std/mem.c index 419a712..f5b92d4 100644 --- a/src/std/mem.c +++ b/src/std/mem.c @@ -22,9 +22,11 @@ void* arena_alloc_align(arena* a, size_t size, size_t align) { 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 }; + 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. +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 -- cgit v1.2.3-70-g09d2 From a1c3e27c53558fde411e63d8b3e3809c79789ea4 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:32:46 +1100 Subject: add TODO for future arena resize fn --- src/std/mem.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/std/mem.h b/src/std/mem.h index 36d30bd..c3ec61d 100644 --- a/src/std/mem.h +++ b/src/std/mem.h @@ -21,4 +21,5 @@ typedef struct 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); \ No newline at end of file +void arena_free_all(arena* a); +// TODO: arena_resize \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 938f745f052c14fccaa987b98ea4ac84e70c1fcc Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:49:12 +1100 Subject: chore: format --- examples/standard_lib/ex_std.c | 30 ++++++++++++++++++++++++++++++ src/platform/file.h | 4 ++-- src/std/str.h | 20 ++++++++++---------- 3 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 examples/standard_lib/ex_std.c (limited to 'src') diff --git a/examples/standard_lib/ex_std.c b/examples/standard_lib/ex_std.c new file mode 100644 index 0000000..9d474de --- /dev/null +++ b/examples/standard_lib/ex_std.c @@ -0,0 +1,30 @@ +#include +#include +#include "file.h" +#include "str.h" + +int main() { + // Examples of how to work with arenas and strings + size_t arena_size = 1024; + arena scratch = arena_create(malloc(arena_size), arena_size); + arena* a = &scratch; + + str8 hello = str8lit("Hello World"); + + // this works but we should be careful because str8 is not *guaranteed* to point to + // a null-terminated string + printf("String before: '%s' (null-terminated: %s) \n ", hello.buf, + str8_is_null_term(hello) ? "true" : "false"); + + char* c = str8_to_cstr(&scratch, hello); + + printf("String after: %s\n", c); + + str8_opt test_file = str8_from_file(&scratch, str8lit("assets/shaders/ui_rect.vert")); + if (test_file.has_value) { + printf("Contents: %.*s \n", (int)test_file.contents.len, test_file.contents.buf); + printf("Null-terminated: %s\n", str8_is_null_term(test_file.contents) ? "true" : "false"); + } + + return 0; +} diff --git a/src/platform/file.h b/src/platform/file.h index fa09891..8bb22c8 100644 --- a/src/platform/file.h +++ b/src/platform/file.h @@ -10,8 +10,8 @@ #include "str.h" typedef struct str8_opt { - str8 contents; - bool has_value; + str8 contents; + bool has_value; } str8_opt; const char* string_from_file(const char* path); diff --git a/src/std/str.h b/src/std/str.h index d2fa891..f6f8820 100644 --- a/src/std/str.h +++ b/src/std/str.h @@ -1,12 +1,12 @@ /** * @file str.h * @author your name (you@domain.com) - * @brief + * @brief * @version 0.1 * @date 2024-02-25 - * + * * @copyright Copyright (c) 2024 - * + * */ #pragma once @@ -19,22 +19,22 @@ 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))} +#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 +#define cstr(a, s) (str8_to_cstr(a, s)) // Shorthand // --- Comparisons @@ -43,8 +43,8 @@ bool str8_equals(str8 a, str8 b); /** * @brief Compare the first `first_nchars` of each string for equality - * @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. + * @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. */ @@ -69,5 +69,5 @@ 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 + return a.buf[a.len] == 0; // This doesn't seem safe. YOLO } \ No newline at end of file -- cgit v1.2.3-70-g09d2