diff options
author | bonmas14 <bonmas14@gmail.com> | 2025-08-02 23:20:37 +0000 |
---|---|---|
committer | bonmas14 <bonmas14@gmail.com> | 2025-08-02 23:20:37 +0000 |
commit | 1cf89852f951b59b89f2a8bd7b54a0b0b74d439c (patch) | |
tree | 884af08903beba5f0e1e8435df4a1c7015270487 /src/ungrateful.h | |
download | ungrateful-1cf89852f951b59b89f2a8bd7b54a0b0b74d439c.tar.gz ungrateful-1cf89852f951b59b89f2a8bd7b54a0b0b74d439c.zip |
memory manipulation, strings, allocators list and logger.
Diffstat (limited to 'src/ungrateful.h')
-rw-r--r-- | src/ungrateful.h | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/src/ungrateful.h b/src/ungrateful.h new file mode 100644 index 0000000..8d1e70e --- /dev/null +++ b/src/ungrateful.h @@ -0,0 +1,202 @@ +#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 <intrin.h> +# 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 <stddef.h> +#include <stdint.h> +#include <stdbool.h> +#include <stdarg.h> +#include <limits.h> +#include <assert.h> + +#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 void un_list_append(List *list, void *data); +extern void *un_list_get(List *list, u64 index); +extern void un_list_remove(List *list, u64 index); + +/* ---- no-wide string API ---- */ + +typedef struct { + s64 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, s64 start, s64 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); + +extern String un_string_format(Allocator alloc, String buffer, ...); +extern String un_string_tformat(String buffer, ...); + +/* ---- Logging API ---- */ + +typedef enum { + UN_LOG_TRACE, + UN_LOG_DEBUG, + UN_LOG_INFO, + UN_LOG_WARNING, + UN_LOG_ERROR, + UN_LOG_FATAL, + UN_LOG_RAW +} Log_Level; + +extern Log_Level un_current_log_level; + +extern void un_log_write(Log_Level level, String format, ...); +extern void un_log_write_cstring(Log_Level level, u8 *format, ...); + +#if defined(__cplusplus) +} +#endif + +#endif // UNGRATEFUL_H |