#if !defined(UNGRATEFUL_H) # define UNGRATEFUL_H /* Ungrateful - standard library for game development. LICENSE: Copyright (C) 2025 Bogdan Masyutin (bonmas14) This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Bogdan Masyutin - bonmas14@gmail.com */ #if defined(__clang__) || defined(__GNUC__) # define CLANG_COMPILER # define __TRAP() __builtin_trap() #elif _MSC_VER >= 1939 # define MSVC_COMPILER # include # define __TRAP() __debugbreak() #else # error "Unknown compiler" #endif #if defined(__cplusplus) # define CLITERAL(type) type #else # define CLITERAL(type) (type) #endif #if defined(_WIN32) # define OS_WINDOWS #elif defined(__linux__) # define OS_LINUX #else # error "unknown platform!" #endif #define UN_TEMP_SIZE UN_MB(50) #include #include #include #include #include #include #define UNUSED(x) (void)(x) #define UN_KB(s) ((u64)(s) * 1024LL) #define UN_MB(s) (UN_KB(s) * 1024LL) #define UN_GB(s) (UN_MB(s) * 1024LL) #define UN_MAX(a, b) (a) > (b) ? (a) : (b) #define UN_MIN(a, b) (a) < (b) ? (a) : (b) #define UN_CSTR (u8*) #define UN_STR(cstr) un_string_from_cstring(UN_CSTR cstr) #define CSTR (char*) #define UN_CLEAR(var) un_memory_set((void*)&var, 0, sizeof(var)) #if defined(__cplusplus) extern "C" { #endif typedef uint64_t u64; typedef uint32_t u32; typedef uint16_t u16; typedef uint8_t u8; typedef int64_t s64; typedef int32_t s32; typedef int16_t s16; typedef int8_t s8; typedef int8_t b32; typedef int8_t b8; typedef float f32; typedef double f64; /* ---- Memory Allocators API ---- */ typedef enum { UN_ALLOC_MSG_ALLOCATE, UN_ALLOC_MSG_REALLOCATE, UN_ALLOC_MSG_FREE, UN_ALLOC_MSG_SELF_DELETE, } Allocator_Message; #define ALLOCATOR_PROC_SIGNATURE(name)\ void *name(void *p, u64 size, Allocator_Message message, void *data) typedef ALLOCATOR_PROC_SIGNATURE(Allocator_Proc); typedef struct { Allocator_Proc *proc; void *data; } Allocator; // extern Allocator un_allocator_create_heap(s64 chunk_size); /* ... for large things */ extern Allocator un_allocator_create_arena(u64 initial_size); /* Grouping allocator, that will recursively grow */ extern Allocator un_allocator_get_standard(void); extern Allocator un_allocator_get_temporary(void); extern void *un_memory_alloc(u64 size, Allocator alloc); extern void *un_memory_realloc(void *ptr, u64 size, Allocator alloc); extern void *un_memory_free(void *ptr, Allocator alloc); extern void un_memory_destroy(Allocator *alloc); extern void un_memory_set(u8 *dest, u8 value, u64 size); extern void un_memory_copy(u8 *dest, u8 *src, u64 size); extern void un_memory_move(u8 *dest, u8 *src, u64 size); extern s32 un_memory_compare(u8 *left, u8 *right, u64 size); /* checks for every byte in arrays for condition: if left is bigger that right. */ /* ---- Generic list structure ---- */ typedef struct { u64 count; u64 capacity; u64 element_size; Allocator alloc; void *data; } List; extern List un_list_create(u64 start_capacity, u64 element_size, Allocator alloc); extern void un_list_destroy(List *list); extern List un_list_clone(List *list, Allocator alloc); extern b32 un_list_append(List *list, void *data); /* Returns true if succeed. */ extern void *un_list_get(List *list, u64 index); extern void un_list_remove(List *list, u64 index); /* ---- no-wide string API ---- */ typedef struct { u64 size; u8 *data; } String; extern u64 un_string_get_length(u8 *cstring); extern String un_string_from_cstring(u8* cstring); extern u8* un_string_to_cstring(String string, Allocator alloc); extern String un_string_copy(String source, Allocator alloc); extern s32 un_string_compare(String left, String right); extern String un_string_concat(String left, String right, Allocator alloc); extern String un_string_swap(String input, u8 from, u8 to, Allocator alloc); extern List un_string_split(String input, String pattern, Allocator alloc); extern String un_string_join(List string_list, String separator, Allocator alloc); extern String un_string_substring(String input, u64 start, u64 max_size); extern s64 un_string_index_of(String input, u8 value, u64 skip_count); extern s64 un_string_index_of_last(String input, u8 value); /* Simplest formatter, supports: * * %% - % * %c - char (internally u32) * %u - u64 * %d - s64 * %s - String * */ extern String un_string_format(Allocator alloc, String buffer, ...); extern String un_string_vformat(Allocator alloc, String buffer, va_list args); #if defined(__cplusplus) } #endif #endif // UNGRATEFUL_H