1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
|