From be0a97ed2c47f4f4d95872245dfccbf56ddb7826 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Thu, 16 Dec 2021 12:29:35 -0500 Subject: [PATCH] move utility functions from arena.h to util.h --- src/asset.cpp | 72 --------------------------------- src/asset.h | 37 +---------------- src/util.h | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 108 deletions(-) diff --git a/src/asset.cpp b/src/asset.cpp index fdb69c7..62f621c 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -9,78 +9,6 @@ #include "dumbLog.h" -//----------------- -// Hashing - -uint64_t -utilFNV64a_str(const char* str, uint64_t hval) -{ - unsigned char* s = (unsigned char *)str; // unsigned string - - // FNV-1a hash each octet of the string - while (*s) { - // xor the bottom with the current octet - hval ^= (uint64_t)*s++; - // multiply by the 64 bit FNV magic prime mod 2^64 - hval *= FNV_64_PRIME; - } - - return hval; -} - -//----------------- -// Memory allocation - -memory_arena* -arenaInit(size_t initial_size) -{ - uint sz = sizeof(memory_arena); - memory_arena* arena = - (memory_arena*) std::calloc(initial_size + sz, sizeof(u8)); - arena->head = arena->next_free = (uint8_t*) arena + sz; - arena->max_size = initial_size; - return arena; -} - -void -arenaFree(memory_arena*& arena) -{ - if (arena != nullptr) { - std::free(arena); - arena = nullptr; - } -} - -uint -arenaGetFreeSize(memory_arena* arena) -{ - return (uint8_t*) arena->head - + arena->max_size - - (uint8_t*) arena->next_free; -} - -void* -arenaAllocateBlock(memory_arena* arena, size_t block_size) -{ - // TODO: resizable memory arena - assert(arenaGetFreeSize(arena) >= block_size); - assert(block_size > 0); - void* ret = arena->next_free; - arena->next_free = (uint8_t*) arena->next_free + block_size; - return ret; -} - -char* -arenaCopyCStr(memory_arena* arena, const char* input, u32 max_len) -{ - u32 name_len = std::strlen(input) + 1; - assert(name_len < max_len); - char* out = ARENA_ALLOC(arena, char, name_len); - std::strncpy(out, input, name_len); - return out; -} - - // forward declarations void dumpNodes(tinygltf::Model t_mdl); model* initModel(Assets* assets, diff --git a/src/asset.h b/src/asset.h index c6beb4e..7584e6c 100644 --- a/src/asset.h +++ b/src/asset.h @@ -5,43 +5,9 @@ #include #include "types.h" +#include "util.h" -//----------------- -// Hashing - -// NOTE: FNV1a hashing algorithm http://www.isthe.com/chongo/tech/comp/fnv/ -#define FNV1_64_INIT ((u64) 0xcbf29ce484222325ULL) -#define FNV_64_PRIME ((u64) 0x100000001b3ULL) -u64 -utilFNV64a_str(const char *str, u64 hval = FNV1_64_INIT); - -//----------------- -// Memory allocation - -struct memory_arena -{ - size_t max_size; - void* head; - void* next_free; -}; - -#define DEFAULT_ARENA_SIZE 10 * 1024 * 1024 // 10MB -memory_arena* arenaInit(size_t initial_size = DEFAULT_ARENA_SIZE); - -void arenaFree(memory_arena*& arena); - -uint arenaGetFreeSize(memory_arena* arena); - -#define ARENA_ALLOC(arena, type, count) \ - (type*) arenaAllocateBlock(arena, sizeof(type) * count) -void* arenaAllocateBlock(memory_arena* arena, size_t block_size); - -#define MAX_NAME_LENGTH 256 -char* arenaCopyCStr(memory_arena* arena, - const char* input, - u32 max_len = MAX_NAME_LENGTH); - // NOTE: wrapper for stb_image struct util_image { @@ -57,7 +23,6 @@ struct util_image char file_path[256]; }; - // NOTE: wrapper for tinygltf https://github.com/syoyo/tinygltf // https://github.com/KhronosGroup/glTF diff --git a/src/util.h b/src/util.h index 3fd505c..59cb9b5 100644 --- a/src/util.h +++ b/src/util.h @@ -1,16 +1,126 @@ +#ifndef UTIL_H +#define UTIL_H + #include #include #include "types.h" +//----------------- +// Hashing + +// NOTE: FNV1a hashing algorithm http://www.isthe.com/chongo/tech/comp/fnv/ +#define FNV1_64_INIT ((u64) 0xcbf29ce484222325ULL) +#define FNV_64_PRIME ((u64) 0x100000001b3ULL) +u64 +utilFNV64a_str(const char *str, u64 hval = FNV1_64_INIT); + +//----------------- +// Memory allocation + +struct memory_arena +{ + size_t max_size; + void* head; + void* next_free; +}; + +#define DEFAULT_ARENA_SIZE 10 * 1024 * 1024 // 10MB +memory_arena* arenaInit(size_t initial_size = DEFAULT_ARENA_SIZE); + +void arenaFree(memory_arena*& arena); +uint arenaGetFreeSize(memory_arena* arena); + +#define ARENA_ALLOC(arena, type, count) \ + (type*) arenaAllocateBlock(arena, sizeof(type) * count) +void* arenaAllocateBlock(memory_arena* arena, size_t block_size); + +#define MAX_NAME_LENGTH 256 +char* arenaCopyCStr(memory_arena* arena, + const char* input, + u32 max_len = MAX_NAME_LENGTH); + char* utilAllocateCStr(const char* str, u32 max_len = 256); void utilSafeFree(void* p); +#endif // UTIL_H + #ifdef UTIL_IMPLEMENTATION +//----------------- +// Hashing + +uint64_t +utilFNV64a_str(const char* str, uint64_t hval) +{ + unsigned char* s = (unsigned char *)str; // unsigned string + + // FNV-1a hash each octet of the string + while (*s) { + // xor the bottom with the current octet + hval ^= (uint64_t)*s++; + // multiply by the 64 bit FNV magic prime mod 2^64 + hval *= FNV_64_PRIME; + } + + return hval; +} + +//----------------- +// Memory allocation + +memory_arena* +arenaInit(size_t initial_size) +{ + uint sz = sizeof(memory_arena); + memory_arena* arena = + (memory_arena*) std::calloc(initial_size + sz, sizeof(u8)); + arena->head = arena->next_free = (uint8_t*) arena + sz; + arena->max_size = initial_size; + return arena; +} + +void +arenaFree(memory_arena*& arena) +{ + if (arena != nullptr) { + std::free(arena); + arena = nullptr; + } +} + +uint +arenaGetFreeSize(memory_arena* arena) +{ + return (uint8_t*) arena->head + + arena->max_size + - (uint8_t*) arena->next_free; +} + +void* +arenaAllocateBlock(memory_arena* arena, size_t block_size) +{ + // TODO: resizable memory arena + assert(arenaGetFreeSize(arena) >= block_size); + assert(block_size > 0); + void* ret = arena->next_free; + arena->next_free = (uint8_t*) arena->next_free + block_size; + return ret; +} + +char* +arenaCopyCStr(memory_arena* arena, const char* input, u32 max_len) +{ + u32 name_len = std::strlen(input) + 1; + assert(name_len < max_len); + char* out = ARENA_ALLOC(arena, char, name_len); + std::strncpy(out, input, name_len); + return out; +} + char* utilAllocateCStr(const char* str, u32 max_len) {