From f95836c5d5035a0233cd03caac653b38bd403c5e Mon Sep 17 00:00:00 2001 From: doyle Date: Sat, 14 Mar 2020 10:32:25 +1100 Subject: [PATCH] Allocator: Rename data->user_context, File: Use allocators --- Code/Dqn.h | 25 ++++++--- Code/DqnHeader.h | 6 +-- Code/DqnHeader_Generated.h | 105 +++++++++++++++++++++++-------------- 3 files changed, 87 insertions(+), 49 deletions(-) diff --git a/Code/Dqn.h b/Code/Dqn.h index 3bc7c74..19d5cb8 100644 --- a/Code/Dqn.h +++ b/Code/Dqn.h @@ -611,7 +611,7 @@ typedef DQN_ALLOCATOR_FREE_PROC(Dqn_Allocator_FreeProc); struct Dqn_Allocator { Dqn_Allocator_Type type; - void *data; + void *user_context; isize bytes_allocated; isize total_bytes_allocated; @@ -712,7 +712,7 @@ DQN_HEADER_COPY_PROTOTYPE(Dqn_Allocator, inline Dqn_Allocator_Arena(Dqn_MemArena { Dqn_Allocator result = {}; result.type = Dqn_Allocator_Type::Arena; - result.data = arena; + result.user_context = arena; return result; } @@ -863,6 +863,10 @@ DQN_HEADER_COPY_PROTOTYPE(template void, Dqn_StringBuilder_BuildIn Dqn_StringBuilder__BuildOutput(builder, dest, dest_size); } +void *Dqn_Allocator_Allocate(Dqn_Allocator *allocator, Dqn_usize size); +void * Dqn_MemArena_Alloc(Dqn_MemArena *arena, Dqn_usize size DQN_DEBUG_ARGS); +Dqn_b32 Dqn_MemArena_Reserve(Dqn_MemArena *arena, Dqn_usize size DQN_DEBUG_ARGS); + DQN_HEADER_COPY_PROTOTYPE(template char *, Dqn_StringBuilder_Build(Dqn_StringBuilder *builder, Dqn_Allocator *allocator, Dqn_isize *len = nullptr)) { Dqn_isize len_w_null_terminator = Dqn_StringBuilder_BuildLen(builder); @@ -1347,6 +1351,12 @@ DQN_HEADER_COPY_PROTOTYPE(template Dqn_b32, Dqn_FixedString_App return result; } +DQN_HEADER_COPY_PROTOTYPE(template Dqn_String, Dqn_FixedString_ToString(Dqn_FixedString const *str)) +{ + Dqn_String result = { str->str, str->len }; + return result; +} + // @ ------------------------------------------------------------------------------------------------- // @ // @ NOTE: Dqn_U64Str @@ -1447,8 +1457,6 @@ DQN_HEADER_COPY_PROTOTYPE(Dqn_b32, Dqn_Log(Dqn_LogType type, char const *file, D return true; } -void * Dqn_MemArena_Alloc(Dqn_MemArena *arena, Dqn_usize size DQN_DEBUG_ARGS); -Dqn_b32 Dqn_MemArena_Reserve(Dqn_MemArena *arena, Dqn_usize size DQN_DEBUG_ARGS); DQN_HEADER_COPY_PROTOTYPE(void *, Dqn_Allocator_Allocate(Dqn_Allocator *allocator, Dqn_usize size)) { void *result = nullptr; @@ -1470,7 +1478,7 @@ DQN_HEADER_COPY_PROTOTYPE(void *, Dqn_Allocator_Allocate(Dqn_Allocator *allocato case Dqn_Allocator_Type::Arena: { - auto *arena = static_cast(allocator->data); + auto *arena = static_cast(allocator->user_context); result = DQN_MEM_ARENA_ALLOC(arena, size); } break; @@ -1516,7 +1524,7 @@ DQN_HEADER_COPY_PROTOTYPE(void *, Dqn_Allocator_Realloc(Dqn_Allocator *allocator case Dqn_Allocator_Type::Arena: { - auto *arena = static_cast(allocator->data); + auto *arena = static_cast(allocator->user_context); if (DQN_MEM_ARENA_RESERVE(arena, DQN_CAST(size_t)new_size)) { result = DQN_MEM_ARENA_ALLOC(arena, DQN_CAST(size_t)new_size); @@ -2463,9 +2471,10 @@ DQN_HEADER_COPY_PROTOTYPE(Dqn_String, Dqn_String_Copy(Dqn_Allocator *allocator, // @ NOTE: File // @ // @ ------------------------------------------------------------------------------------------------- -DQN_HEADER_COPY_PROTOTYPE(char *, Dqn_File_ReadWithArena(Dqn_MemArena *arena, char const *file, Dqn_isize *file_size)) +DQN_HEADER_COPY_PROTOTYPE(char *, Dqn_File_ReadAll(Dqn_Allocator *allocator, char const *file, Dqn_isize *file_size)) { FILE *file_handle = fopen(file, "rb"); + if (!file_handle) return nullptr; fseek(file_handle, 0, SEEK_END); Dqn_isize file_size_ = ftell(file_handle); if (DQN_CAST(long)file_size_ == -1L) @@ -2476,7 +2485,7 @@ DQN_HEADER_COPY_PROTOTYPE(char *, Dqn_File_ReadWithArena(Dqn_MemArena *arena, ch rewind(file_handle); - auto *result = (char *)DQN_MEM_ARENA_ALLOC(arena, DQN_CAST(Dqn_usize)(file_size_ + 1)); + auto *result = DQN_CAST(char *)Dqn_Allocator_Allocate(allocator, DQN_CAST(Dqn_usize)(file_size_ + 1)); DQN_ASSERT(result); result[file_size_] = 0; diff --git a/Code/DqnHeader.h b/Code/DqnHeader.h index 6da3a36..5904f7a 100644 --- a/Code/DqnHeader.h +++ b/Code/DqnHeader.h @@ -163,13 +163,14 @@ int main(int argc, char *argv[]) return 0; } - Dqn_MemArena arena = {}; + Dqn_MemArena arena = {}; + Dqn_Allocator allocator = Dqn_Allocator_Arena(&arena); DQN_MEM_ARENA_RESERVE(&arena, DQN_MEGABYTES(16)); for (isize arg_index = 1; arg_index < argc; ++arg_index) { char const *file = argv[arg_index]; isize buf_size = 0; - char *buf = Dqn_File_ReadWithArena(&arena, file, &buf_size); + char *buf = Dqn_File_ReadAll(&allocator, file, &buf_size); if (!buf) { fprintf(stderr, "Failed to read file: %s\n", file); @@ -229,7 +230,6 @@ int main(int argc, char *argv[]) isize func_name_len = 0; char const *func_name = ParseFunctionNameAndParameters(ptr, &func_name_len); - Dqn_Allocator allocator = Dqn_Allocator_Arena(&arena); entry->function_decl.return_val = Dqn_Asprintf(&allocator, "%.*s", func_type_len, func_type); entry->function_decl.name_and_args = Dqn_Asprintf(&allocator, "%.*s", func_name_len, func_name); ptr = func_name + func_name_len + 1; // Ptr is at macro closing paren, skip the paren diff --git a/Code/DqnHeader_Generated.h b/Code/DqnHeader_Generated.h index 6352462..7a19625 100644 --- a/Code/DqnHeader_Generated.h +++ b/Code/DqnHeader_Generated.h @@ -208,7 +208,7 @@ enum struct Dqn_Allocator_Type XHeap, // Malloc realloc, free, crash on failure Arena, Custom, - NullAllocator, + Null, }; #define DQN_ALLOCATOR_ALLOCATE_PROC(name) void *name(Dqn_usize size) @@ -220,7 +220,13 @@ typedef DQN_ALLOCATOR_FREE_PROC(Dqn_Allocator_FreeProc); struct Dqn_Allocator { Dqn_Allocator_Type type; - void *data; + void *user_context; + + isize bytes_allocated; + isize total_bytes_allocated; + + isize allocations; + isize total_allocations; // NOTE: Only required if type == Dqn_Allocator_Type::Custom Dqn_Allocator_AllocateProc *allocate; @@ -274,8 +280,8 @@ struct Dqn_MemArenaScopedRegion #define DQN_DEBUG_PARAMS #endif -#define DQN_MEM_ARENA_INIT_WITH_ALLOCATOR(arena, allocator, size) Dqn_MemArena_InitWithAllocator(arena, allocator, size DQN_DEBUG_PARAMS) -#define DQN_MEM_ARENA_INIT_MEMORY(arena, src, size) Dqn_MemArena_InitMemory(arena, src, size DQN_DEBUG_PARAMS) +#define DQN_MEM_ARENA_INIT_WITH_ALLOCATOR(allocator, size) Dqn_MemArena_InitWithAllocator(allocator, size DQN_DEBUG_PARAMS) +#define DQN_MEM_ARENA_INIT_MEMORY(src, size) Dqn_MemArena_InitMemory(src, size DQN_DEBUG_PARAMS) #define DQN_MEM_ARENA_ALLOC(arena, size) Dqn_MemArena_Alloc(arena, size DQN_DEBUG_PARAMS) #define DQN_MEM_ARENA_ALLOC_ARRAY(arena, T, num) (T *)Dqn_MemArena_Alloc(arena, sizeof(T) * num DQN_DEBUG_PARAMS) #define DQN_MEM_ARENA_ALLOC_STRUCT(arena, T) (T *)Dqn_MemArena_Alloc(arena, sizeof(T) DQN_DEBUG_PARAMS) @@ -287,10 +293,32 @@ struct Dqn_MemArenaScopedRegion // NOTE: Dqn_Allocator // // ------------------------------------------------------------------------------------------------- -Dqn_Allocator inline Dqn_Allocator_NullAllocator(); -Dqn_Allocator inline Dqn_Allocator_HeapAllocator(); -Dqn_Allocator inline Dqn_Allocator_XHeapAllocator(); -Dqn_Allocator inline Dqn_Allocator_ArenaAllocator(Dqn_MemArena *arena); +Dqn_Allocator inline Dqn_Allocator_Null(); +Dqn_Allocator inline Dqn_Allocator_Heap(); +Dqn_Allocator inline Dqn_Allocator_XHeap(); +Dqn_Allocator inline Dqn_Allocator_Arena(Dqn_MemArena *arena); +// ------------------------------------------------------------------------------------------------- +// +// NOTE: String +// +// ------------------------------------------------------------------------------------------------- +struct Dqn_String +{ + union { + // NOTE: To appease GCC, Clang can't assign C string literal to char * + // Only UB if you try modify a string originally declared const + char const *str_; + char *str; + }; + + Dqn_isize len; + char const *begin() const { return str; } + char const *end () const { return str + len; } + char *begin() { return str; } + char *end () { return str + len; } +}; +#define DQN_STRING_LITERAL(string) {string, Dqn_CharCountI(string)} + // ------------------------------------------------------------------------------------------------- // // NOTE: String Builder @@ -300,7 +328,7 @@ struct Dqn_StringBuilderBuffer { char *mem; Dqn_usize size; - usize used; + Dqn_usize used; Dqn_StringBuilderBuffer *next; }; @@ -319,11 +347,12 @@ struct Dqn_StringBuilder // The necessary length to build the string, it returns the length including the null-terminator template Dqn_isize Dqn_StringBuilder_BuildLen(Dqn_StringBuilder const *builder); template void Dqn_StringBuilder_BuildInBuffer(Dqn_StringBuilder const *builder, char *dest, Dqn_usize dest_size); -template char * Dqn_StringBuilder_BuildFromMalloc(Dqn_StringBuilder *builder, Dqn_isize *len = nullptr); -template char * Dqn_StringBuilder_BuildFromArena(Dqn_StringBuilder *builder, Dqn_MemArena *arena, Dqn_isize *len = nullptr); +template char * Dqn_StringBuilder_Build(Dqn_StringBuilder *builder, Dqn_Allocator *allocator, Dqn_isize *len = nullptr); +template Dqn_String Dqn_StringBuilder_BuildString(Dqn_StringBuilder *builder, Dqn_Allocator *allocator); template void Dqn_StringBuilder_VFmtAppend(Dqn_StringBuilder *builder, char const *fmt, va_list va); template void Dqn_StringBuilder_FmtAppend(Dqn_StringBuilder *builder, char const *fmt, ...); template void Dqn_StringBuilder_Append(Dqn_StringBuilder *builder, char const *str, Dqn_isize len = -1); +template void Dqn_StringBuilder_AppendString(Dqn_StringBuilder *builder, Dqn_String const string); template void Dqn_StringBuilder_AppendChar(Dqn_StringBuilder *builder, char ch); template void Dqn_StringBuilder_Free(Dqn_StringBuilder *builder); // ------------------------------------------------------------------------------------------------- @@ -338,15 +367,6 @@ template inline Dqn_Slice Dqn_Slice_Copy(Dqn_MemArena *arena, Dq template inline bool Dqn_Slice_Equals(Dqn_Slice const a, Dqn_Slice const b); // ------------------------------------------------------------------------------------------------- // -// NOTE: Dqn_Asprintf (Allocate Sprintf) -// -// ------------------------------------------------------------------------------------------------- -template Dqn_Slice Dqn_AsprintfSlice(T *arena, char const *fmt, va_list va); -template Dqn_Slice Dqn_AsprintfSlice(T *arena, char const *fmt, ...); -template char * Dqn_Asprintf(T *arena, int *len, char const *fmt, ...); -template char * Dqn_Asprintf(T *arena, char const *fmt, ...); -// ------------------------------------------------------------------------------------------------- -// // NOTE: Dqn_FixedArray // // ------------------------------------------------------------------------------------------------- @@ -376,22 +396,11 @@ T * Dqn_FixedArray_Make(DQN_FIXED_ARRAY_TE void Dqn_FixedArray_Clear(DQN_FIXED_ARRAY_TEMPLATE_DECL *a); void Dqn_FixedArray_EraseStable(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, Dqn_isize index); void Dqn_FixedArray_EraseUnstable(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, Dqn_isize index); -void Dqn_FixedArray_Pop(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, Dqn_isize num); +void Dqn_FixedArray_Pop(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, Dqn_isize num = 1); T * Dqn_FixedArray_Peek(DQN_FIXED_ARRAY_TEMPLATE_DECL *a); Dqn_isize Dqn_FixedArray_GetIndex(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, T const *entry); T * Dqn_FixedArray_Find(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, EqualityProc IsEqual); T * Dqn_FixedArray_Find(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, T *find); -// ------------------------------------------------------------------------------------------------- -// -// NOTE: Dqn_FixedStack -// -// ------------------------------------------------------------------------------------------------- -template using Dqn_FixedStack = DQN_FIXED_ARRAY_TEMPLATE_DECL; -template T Dqn_FixedStack_Pop (Dqn_FixedStack *array) { T result = *Dqn_FixedArray_Peek(array); Dqn_FixedArray_Pop(array, 1); return result; } -template T *Dqn_FixedStack_Peek (Dqn_FixedStack *array) { return Dqn_FixedArray_Peek(array); } -template T *Dqn_FixedStack_Push (Dqn_FixedStack *array, T item) { return Dqn_FixedArray_Add(array, item); } -template void Dqn_FixedStack_Clear(Dqn_FixedStack *array) { Dqn_FixedArray_Clear(array); } - // ------------------------------------------------------------------------------------------------- // // NOTE: Dqn_Array @@ -469,6 +478,7 @@ template void Dqn_FixedString_Clear(Dqn_FixedString< template Dqn_b32 Dqn_FixedString_AppendVFmt(Dqn_FixedString *str, char const *fmt, va_list va); template Dqn_b32 Dqn_FixedString_AppendFmt(Dqn_FixedString *str, char const *fmt, ...); template Dqn_b32 Dqn_FixedString_Append(Dqn_FixedString *str, char const *src, Dqn_isize len = -1); +template Dqn_String Dqn_FixedString_ToString(Dqn_FixedString const *str); // ------------------------------------------------------------------------------------------------- // // NOTE: Dqn_U64Str @@ -479,10 +489,11 @@ struct Dqn_U64Str // Points to the start of the str in the buffer, not necessarily buf since // we write into the buffer in reverse char *str; - char buf[27]; // NOTE(doyle): 27 is the maximum size of Dqn_u64 including commas - int len; + char buf[27]; // NOTE(doyle): 27 is the maximum size of Dqn_u64 including commas + int len; }; +char * Dqn_U64Str_ToStr(Dqn_u64 val, Dqn_U64Str *result, Dqn_b32 comma_sep); // ------------------------------------------------------------------------------------------------- // // NOTE: Logging @@ -502,12 +513,19 @@ void Dqn_Allocator_Free(Dqn_Allocator *allo void * Dqn_MemArena_Alloc(Dqn_MemArena *arena, Dqn_usize size DQN_DEBUG_ARGS); void Dqn_MemArena_Free(Dqn_MemArena *arena DQN_DEBUG_ARGS); Dqn_b32 Dqn_MemArena_Reserve(Dqn_MemArena *arena, Dqn_usize size DQN_DEBUG_ARGS); -void Dqn_MemArena_InitWithAllocator(Dqn_MemArena *arena, Dqn_Allocator allocator, Dqn_usize size DQN_DEBUG_ARGS); -void Dqn_MemArena_InitMemory(Dqn_MemArena *arena, void *memory, Dqn_usize size DQN_DEBUG_ARGS); +Dqn_MemArena Dqn_MemArena_InitWithAllocator(Dqn_Allocator allocator, Dqn_usize size DQN_DEBUG_ARGS); +Dqn_MemArena Dqn_MemArena_InitMemory(void *memory, Dqn_usize size DQN_DEBUG_ARGS); void Dqn_MemArena_ResetUsage(Dqn_MemArena *arena, Dqn_ZeroMem zero_mem); Dqn_MemArenaScopedRegion Dqn_MemArena_MakeScopedRegion(Dqn_MemArena *arena); // ------------------------------------------------------------------------------------------------- // +// NOTE: Dqn_Asprintf (Allocate Sprintf) +// +// ------------------------------------------------------------------------------------------------- +Dqn_String Dqn_Asprintf(Dqn_Allocator *allocator, char const *fmt, va_list va); +Dqn_String Dqn_Asprintf(Dqn_Allocator *allocator, char const *fmt, ...); +// ------------------------------------------------------------------------------------------------- +// // NOTE: Vectors // // ------------------------------------------------------------------------------------------------- @@ -530,6 +548,7 @@ Dqn_b32 Dqn_Rect_ContainsPoint(Dqn_Rect rect, Dqn_b32 Dqn_Rect_ContainsRect(Dqn_Rect a, Dqn_Rect b); Dqn_V2 Dqn_Rect_Size(Dqn_Rect rect); Dqn_Rect Dqn_Rect_Move(Dqn_Rect src, Dqn_V2 move_amount); +Dqn_Rect Dqn_Rect_Intersection(Dqn_Rect a, Dqn_Rect b); Dqn_Rect Dqn_Rect_Union(Dqn_Rect a, Dqn_Rect b); Dqn_Rect Dqn_Rect_FromRectI32(Dqn_RectI32 a); Dqn_V2I Dqn_RectI32_Size(Dqn_RectI32 rect); @@ -575,6 +594,7 @@ Dqn_i8 Dqn_Safe_TruncateISizeToI8(Dqn_isize v Dqn_u32 Dqn_Safe_TruncateUSizeToU32(Dqn_u64 val); int Dqn_Safe_TruncateUSizeToI32(Dqn_usize val); int Dqn_Safe_TruncateUSizeToInt(Dqn_usize val); +Dqn_isize Dqn_Safe_TruncateUSizeToISize(Dqn_usize val); // ------------------------------------------------------------------------------------------------- // // NOTE: Char Helpers @@ -584,6 +604,7 @@ Dqn_b32 Dqn_Char_IsAlpha(char ch); Dqn_b32 Dqn_Char_IsDigit(char ch); Dqn_b32 Dqn_Char_IsAlphaNum(char ch); Dqn_b32 Dqn_Char_IsWhitespace(char ch); +char Dqn_Char_ToLower(char ch); // ------------------------------------------------------------------------------------------------- // // NOTE: String Helpers @@ -610,13 +631,21 @@ Dqn_u64 Dqn_Str_ToU64(char const *buf, int len Dqn_i64 Dqn_Str_ToI64(char const *buf, int len = -1); // ------------------------------------------------------------------------------------------------- // +// NOTE: Dqn_String +// +// ------------------------------------------------------------------------------------------------- +Dqn_b32 Dqn_String_Compare(Dqn_String const lhs, Dqn_String const rhs); +Dqn_b32 Dqn_String_CompareCaseInsensitive(Dqn_String const lhs, Dqn_String const rhs); +Dqn_String Dqn_String_Copy(Dqn_Allocator *allocator, Dqn_String const src); +// ------------------------------------------------------------------------------------------------- +// // NOTE: File // // ------------------------------------------------------------------------------------------------- -char * Dqn_File_ReadWithArena(Dqn_MemArena *arena, char const *file, Dqn_isize *file_size); +char * Dqn_File_ReadAll(Dqn_Allocator *allocator, char const *file, Dqn_isize *file_size); // ------------------------------------------------------------------------------------------------- // // NOTE: Utils // // ------------------------------------------------------------------------------------------------- -char * Dqn_EpochTimeToDate(i64 timestamp, char *buf, isize buf_len); +char * Dqn_EpochTimeToDate(Dqn_i64 timestamp, char *buf, Dqn_isize buf_len);