diff options
author | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-17 15:00:53 +1100 |
---|---|---|
committer | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-17 15:00:53 +1100 |
commit | 51b4a3fc75351d6ecd2142c228d31a1f7ed52152 (patch) | |
tree | c28d0c11b71a56199a6308914a040e18a28b60ef /src | |
parent | 2b83174a87f5a1e4991cc9153309ad0f73450b44 (diff) |
fix a bug with dirname seg faulting when passed a string literal
it doesnt like things in readonly memory i guess.
now we create an arena for the obj load and create a dynamically allocated copy of the string
Diffstat (limited to 'src')
-rw-r--r-- | src/platform/path.c | 9 | ||||
-rw-r--r-- | src/platform/path.h | 2 | ||||
-rw-r--r-- | src/resources/obj.c | 9 | ||||
-rw-r--r-- | src/std/mem.c | 6 | ||||
-rw-r--r-- | src/std/mem.h | 1 |
5 files changed, 21 insertions, 6 deletions
diff --git a/src/platform/path.c b/src/platform/path.c index e67102b..9572941 100644 --- a/src/platform/path.c +++ b/src/platform/path.c @@ -1,12 +1,17 @@ #include "path.h" #include <libgen.h> +#include <stdlib.h> #include <string.h> +#include "mem.h" #include "str.h" #if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_MAC) -path_opt path_parent(const char* path) { - char* path_dirname = dirname(path); +path_opt path_parent(arena* a, const char* path) { + // Duplicate the string because dirname doesnt like const literals + char* path_copy = arena_alloc(a, strlen(path) + 1); + strcpy(path_copy, path); + char* path_dirname = dirname(path_copy); return (path_opt){ .path = str8_cstr_view(path_dirname), .has_value = true }; } #endif diff --git a/src/platform/path.h b/src/platform/path.h index 0ec6993..73063ea 100644 --- a/src/platform/path.h +++ b/src/platform/path.h @@ -13,4 +13,4 @@ typedef struct path_opt { bool has_value; } path_opt; -path_opt path_parent(const char* path); // TODO: convert to using str8
\ No newline at end of file +path_opt path_parent(arena* a, const char* path); // TODO: convert to using str8
\ No newline at end of file diff --git a/src/resources/obj.c b/src/resources/obj.c index 56f885f..c6e9fa6 100644 --- a/src/resources/obj.c +++ b/src/resources/obj.c @@ -15,6 +15,7 @@ #include "file.h" #include "log.h" #include "maths.h" +#include "mem.h" #include "path.h" #include "render.h" #include "render_types.h" @@ -40,8 +41,11 @@ bool model_load_obj_str(const char *file_string, str8 relative_path, model *out_ bool invert_textures_y); model_handle model_load_obj(core *core, const char *path, bool invert_textures_y) { + size_t arena_size = 1024; + arena scratch = arena_create(malloc(arena_size), arena_size); + TRACE("Loading model at Path %s\n", path); - path_opt relative_path = path_parent(path); + path_opt relative_path = path_parent(&scratch, path); if (!relative_path.has_value) { WARN("Couldnt get a relative path for the path to use for loading materials & textures later"); } @@ -61,6 +65,9 @@ model_handle model_load_obj(core *core, const char *path, bool invert_textures_y u32 index = model_darray_len(core->models); model_darray_push(core->models, model); + + arena_free_all(&scratch); + arena_free_storage(&scratch); return (model_handle){ .raw = index }; } diff --git a/src/std/mem.c b/src/std/mem.c index f5b92d4..d7c0f4c 100644 --- a/src/std/mem.c +++ b/src/std/mem.c @@ -16,7 +16,7 @@ void* arena_alloc_align(arena* a, size_t size, size_t align) { ERROR_EXIT("Arena ran out of memory\n"); } void* p = a->begin + padding; - a->begin += padding + size; + a->curr += padding + size; return memset(p, 0, size); } void* arena_alloc(arena* a, size_t size) { return arena_alloc_align(a, size, DEFAULT_ALIGNMENT); } @@ -29,4 +29,6 @@ arena arena_create(void* backing_buffer, size_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 +} + +void arena_free_storage(arena* a) { free(a->begin); }
\ No newline at end of file diff --git a/src/std/mem.h b/src/std/mem.h index c3ec61d..2f92894 100644 --- a/src/std/mem.h +++ b/src/std/mem.h @@ -22,4 +22,5 @@ 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); +void arena_free_storage(arena* a); // TODO: arena_resize
\ No newline at end of file |