-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.h
464 lines (356 loc) · 13.4 KB
/
base.h
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#pragma once
//---------- Helper macros -----------
#define Stmt(S) do { S; } while (0)
#define Stringify_(S) #S
#define Stringify(S) Stringify_(S)
#define Glue_(A, B) A##B
#define Glue(A, B) Glue_(A, B)
#define OffsetOfMember(T, m) ((usize)&((T *)0)->m)
#define ParentStartPtr(P, field_name, field_ptr) ((P *)((u8 *)field_ptr - OffsetOfMember(P, field_name)))
#define ArrayCount(a) (sizeof(a) / sizeof(*(a)))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define ClampTop(x, max) Min(x, max)
#define ClampBot(x, min) Max(x, min)
#define StaticAssert(expression, message) _Static_assert(expression, message)
#define global static
#define local static
#define function static
//---------- Context cracking ----------
// This section defines the following constants/macros:
// - ARCH (can be tested with IsArch(type))
// - OS (can be tested with IsOs(type))
// - OS_FLAGS (can be tested with HasOsFlags(type))
// - COMPILER (can be tested with IsCompiler(type))
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
# include <unistd.h>
# if defined(_POSIX_VERSION) && !defined(_POSIX_SOURCE)
# define _POSIX_SOURCE
# endif
#endif
#if defined(_POSIX_SOURCE)
# define _POSIX_C_SOURCE 200809L
#endif
#define OS_FLAGS_NONE 0
#define OS_FLAGS_UNIX 1
#define OS_FLAGS_POSIX 2
typedef enum {
OsFlags_None = OS_FLAGS_NONE,
OsFlags_Unix = OS_FLAGS_UNIX,
OsFlags_Posix = OS_FLAGS_POSIX,
} OsFlags;
#define OS_LINUX 0
#define OS_WINDOWS 1
#define OS_COUNT 2
typedef enum {
Os_Linux = OS_LINUX,
Os_Windows = OS_WINDOWS,
Os_COUNT = OS_COUNT,
} Os;
#define ARCH_X86 0
#define ARCH_X86_64 1
#define ARCH_COUNT 2
typedef enum {
Arch_X86 = ARCH_X86,
Arch_X86_64 = ARCH_X86_64,
Arch_COUNT = ARCH_COUNT,
} Arch;
#define COMPILER_GCC 0
#define COMPILER_CLANG 1
#define COMPILER_COUNT 2
typedef enum {
Compiler_Gcc = COMPILER_GCC,
Compiler_Clang = COMPILER_CLANG,
Compiler_COUNT = COMPILER_COUNT,
} Compiler;
#if defined(__linux__)
# define OS OS_LINUX
# define OS_FLAGS (OS_FLAGS_UNIX | OS_FLAGS_POSIX)
#elif defined(_WIN32) || defined(_WIN64)
# define OS OS_WINDOWS
#endif
#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
# define ARCH ARCH_X86_64
#elif defined(i386) || defined(__i386) || defined(__i386__) || defined(__IA32__) || defined(_M_I86) || defined(_M_IX86) || defined(__X86__) || defined(_X86) || defined(__THW_INTEL__) || defined(__I86__) || defined(__INTEL__) || defined(__386)
# define ARCH ARCH_X86
#endif
#if defined(__clang__)
# define COMPILER COMPILER_CLANG
#elif defined(__GNUC__)
# define COMPILER COMPILER_GCC
#endif
#if !defined(ARCH)
# error "Unsupported architecture."
#endif
#if !defined(COMPILER)
# error "Unsupported compiler."
#endif
#if !defined(OS)
# error "Unsupported operating system."
#endif
#if !defined(OS_FLAGS)
# define OS_FLAGS OS_FLAGS_NONE
#endif
#define IsArch(type) (ARCH == type)
#define IsCompiler(type) (COMPILER == type)
#define IsOs(type) (OS == type)
#define OsHasFlags(flags) ((OS_FLAGS & (flags)) == (flags))
//---------- libc (which we'll want to get rid of later) -----------
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
//---------- Integer types -----------
#if OsHasFlags(OS_FLAGS_POSIX)
# include <sys/types.h>
#endif
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef size_t usize;
typedef ssize_t ssize;
typedef float f32;
typedef double f64;
typedef s32 rune;
typedef void VoidFunc(void);
#define S8_MIN INT8_MIN
#define S8_MAX INT8_MAX
#define S16_MIN INT16_MIN
#define S16_MAX INT16_MAX
#define S32_MIN INT32_MIN
#define S32_MAX INT32_MAX
#define S64_MIN INT64_MIN
#define S64_MAX INT64_MAX
#define U8_MAX UINT8_MAX
#define U16_MAX UINT16_MAX
#define U32_MAX UINT32_MAX
#define U64_MAX UINT64_MAX
#define USIZE_MAX SIZE_MAX
// SSIZE_MIN and SSIZE_MAX are already defined
#define RUNE_MIN S32_MIN
#define RUNE_MAX S32_MAX
//---------- Booleans ----------
typedef u8 bool;
#define true 1
#define false 0
//----------- Memory management -----------
struct Mem_Base;
typedef void *Mem_ReserveFunc(void *ctx, usize size);
typedef void Mem_ChangeFunc(void *ctx, void *p, usize size);
typedef struct Mem_Base {
Mem_ReserveFunc *reserve;
Mem_ChangeFunc *commit;
Mem_ChangeFunc *decommit;
Mem_ChangeFunc *release;
void *ctx;
} Mem_Base;
function void *mem_reserve(Mem_Base *mb, usize size);
function void mem_commit(Mem_Base *mb, void *p, usize size);
function void mem_decommit(Mem_Base *mb, void *p, usize size);
function void mem_release(Mem_Base *mb, void *p, usize size);
function void *mem_reserve_commit(Mem_Base *mb, usize size);
function void mem_decommit_release(Mem_Base *mb, void *p, usize size);
function void mem_noop_mem_change(void *ctx, void *p, usize size);
function Mem_Base *mem_malloc_base(void);
typedef struct {
Mem_Base *mb;
void **pp;
usize size;
void (*call)(Mem_Base *mb, void *p, usize size);
} Mem_AutoChangeContext;
function void mem_auto_change(Mem_AutoChangeContext *ctx);
#define MemAutoChange_(mb_, pp_, size_, call_) Mem_AutoChangeContext Glue(Glue(marctx_, __LINE__), _) __attribute__((__cleanup__(mem_auto_change))) = { .mb = (mb_), .pp = (void **)(pp_), .size = (size_), .call = (call_) }
#define MemAutoDecommit(mb, pp, size) MemAutoChange_(mb, pp, size, mem_decommit)
#define MemAutoRelease(mb, pp, size) MemAutoChange_(mb, pp, size, mem_release)
#define MemReserveAutoRelease(mb, target_type, target, size) target_type target = mem_reserve(mb, size); MemAutoChange_(mb, &target, size, mem_release)
//----------- Byte ordering stuff -----------
typedef enum {
ByteOrder_BigEndian,
ByteOrder_LittleEndian,
} ByteOrder;
#define SYSTEM_BYTE_ORDER ((union { u8 b_[2]; u16 x_; }){ .b_ = { 0, 1 } }.x_ == 1 ? ByteOrder_BigEndian : ByteOrder_LittleEndian)
function u64 swap_byte_order_u64(u64 x);
function u32 swap_byte_order_u32(u32 x);
function u16 swap_byte_order_u16(u16 x);
#define SwapByteOrder(x, bits) Glue(swap_byte_order_u, bits)(x)
#define SystemToBo(x, bits, bo) ((SYSTEM_BYTE_ORDER == (bo)) ? (x) : SwapByteOrder(x, bits))
#define BoToSystem(x, bits, bo) ((SYSTEM_BYTE_ORDER == (bo)) ? (x) : SwapByteOrder(x, bits))
#define SystemToLe(x, bits) SystemToBo((x), bits, ByteOrder_LittleEndian)
#define SystemToBe(x, bits) SystemToBo((x), bits, ByteOrder_BigEndian)
#define LeToSystem(x, bits) BoToSystem((x), bits, ByteOrder_LittleEndian)
#define BeToSystem(x, bits) BoToSystem((x), bits, ByteOrder_BigEndian)
#define U64FromBo(x, bo) BoToSystem((x), 64, (bo))
#define U64FromBe(x) BeToSystem((x), 64)
#define U64FromLe(x) LeToSystem((x), 64)
#define U32FromBo(x, bo) BoToSystem((x), 32, (bo))
#define U32FromBe(x) BeToSystem((x), 32)
#define U32FromLe(x) LeToSystem((x), 32)
#define U16FromBo(x, bo) BoToSystem((x), 16, (bo))
#define U16FromBe(x) BeToSystem((x), 16)
#define U16FromLe(x) LeToSystem((x), 16)
#define U64ToBo(x, bo) SystemToBo((x), 64, (bo))
#define U64ToBe(x) SystemToBe((x), 64)
#define U64ToLe(x) SystemToLe((x), 64)
#define U32ToBo(x, bo) SystemToBo((x), 32, (bo))
#define U32ToBe(x) SystemToBe((x), 32)
#define U32ToLe(x) SystemToLe((x), 32)
#define U16ToBo(x, bo) SystemToBo((x), 16, (bo))
#define U16ToBe(x) SystemToBe((x), 16)
#define U16ToLe(x) SystemToLe((x), 16)
//----------- Strings -----------
typedef struct {
const u8 *buf;
usize len;
} String;
typedef struct StringList {
String cur;
struct StringList *next;
} StringList;
#define Str(str) ((String){ .buf = (const u8 *)(str), .len = sizeof(str) - 1 })
function String string_from_raw(const u8 *buf, usize len);
function void string_destroy(Mem_Base *mb, String str);
function usize cstring_length(const char *str);
function usize ststring_length(const char *str, char sentinel);
function String string_from_c(Mem_Base *mb, const char *str);
function String string_from_st(Mem_Base *mb, const char *str, char sentinel);
function char *string_to_c(Mem_Base *mb, String s);
function char *string_to_st(Mem_Base *mb, String s, char sentinel);
function s8 string_cmp(String a, String b);
#define StringCmp(a, op, b) (string_cmp(a, b) op 0)
function String string_slice(String s, usize start_at, usize len);
#define INVALID_RUNE ((rune)-1)
typedef struct {
ByteOrder bo;
const u16 *buf;
usize len;
} Utf16String;
function Utf16String utf16string_from_raw(u16 *buf, usize len, ByteOrder bo);
function void utf16string_destroy(Mem_Base *mb, Utf16String str);
function Utf16String utf8_to_utf16(Mem_Base *mb, String s, ByteOrder bo);
function String utf16_to_utf8(Mem_Base *mb, Utf16String s);
function rune utf8_next_codepoint(String s, u8 *len);
function usize utf8_rune_count(String s);
function u8 utf8_encoded_len(rune codepoint);
function u8 utf8_encode_codepoint(rune codepoint, u8 *s);
function rune utf16_next_codepoint(Utf16String s, u8 *len);
function usize utf16_rune_count(Utf16String s);
function u8 utf16_encoded_len(rune codepoint);
function u8 utf16_encode_codepoint(rune codepoint, u16 *s, ByteOrder bo);
// Used as follows: StringEachRune(Str("asdf"), ch) printf("%c", ch);
#define StringEachRune(s, ident) \
String Glue(Glue(utf8str_, __LINE__), _) = (s); \
u8 Glue(Glue(utf8cpl_, __LINE__), _); \
for (rune ident = utf8_next_codepoint(Glue(Glue(utf8str_, __LINE__), _), &Glue(Glue(utf8cpl_, __LINE__), _)); \
(ident != 0 || Glue(Glue(utf8cpl_, __LINE__), _) != 0) && ident != INVALID_RUNE; \
ident = utf8_next_codepoint(Glue(Glue(utf8str_, __LINE__), _), &Glue(Glue(utf8cpl_, __LINE__), _)), \
Glue(Glue(utf8str_, __LINE__), _).len -= Glue(Glue(utf8cpl_, __LINE__), _), \
Glue(Glue(utf8str_, __LINE__), _).buf += Glue(Glue(utf8cpl_, __LINE__), _))
#define Utf16StringEachRune(s, ident) \
Utf16String Glue(Glue(utf16str_, __LINE__), _) = (s); \
u8 Glue(Glue(utf16cpl_, __LINE__), _); \
for (rune ident = utf16_next_codepoint(Glue(Glue(utf16str_, __LINE__), _), &Glue(Glue(utf16cpl_, __LINE__), _)); \
(ident != 0 || Glue(Glue(utf16cpl_, __LINE__), _) != 0) && ident != INVALID_RUNE; \
ident = utf16_next_codepoint(Glue(Glue(utf16str_, __LINE__), _), &Glue(Glue(utf16cpl_, __LINE__), _)), \
Glue(Glue(utf16str_, __LINE__), _).len -= Glue(Glue(utf16cpl_, __LINE__), _), \
Glue(Glue(utf16str_, __LINE__), _).buf += Glue(Glue(utf16cpl_, __LINE__), _))
//------------- Intrinsics --------------
#if defined(__has_include)
# define CanInclude(header) __has_include(header)
#else
# define CanInclude(...) 0
#endif
#if (IsArch(ARCH_X86) || IsArch(ARCH_X86_64)) && CanInclude(<immintrin.h>)
# define INTEL_INTRINSICS_AVAILABLE 1
#endif
#if IsCompiler(COMPILER_GCC) || IsCompiler(COMPILER_CLANG)
# define Likely(x) __builtin_expect((s64)(x), 1l)
# define Unlikely(x) __builtin_expect((s64)(x), 0l)
#else
// TODO(rutgerbrf): find out if other compilers support these kind of things too
# define Likely(x) x
# define Unlikely(x) x
#endif
//------------- Synchronization primitives -------------
#if OsHasFlags(OS_FLAGS_POSIX)
# include <pthread.h>
#endif
typedef struct {
#if OsHasFlags(OS_FLAGS_POSIX)
pthread_mutex_t inner;
#else
# error "No mutex support for this OS"
#endif
} Mutex;
typedef struct {
Mutex *m;
} MutexGuard;
#define MUTEX_AUTO_UNLOCK __attribute__((__cleanup__(mutex_guard_unlock)))
#define MutexLockScoped(mutp) MutexGuard Glue(Glue(mlsguard_, __LINE__), _) MUTEX_AUTO_UNLOCK = mutex_lock(mutp)
function MutexGuard mutex_lock(Mutex *m);
// TODO(rutgerbrf): check m->m, afterwards do: m->m = NULL
function void mutex_guard_unlock(MutexGuard *m);
//--------------- I/O Base ---------------
typedef ssize Io_RwFunc(void *ctx, u8 *dest, usize n);
typedef s32 Io_CloseFunc(void *ctx);
typedef struct {
Io_RwFunc *read;
void *ctx;
} Io_Reader;
typedef struct {
Io_RwFunc *write;
void *ctx;
} Io_Writer;
typedef struct {
Io_CloseFunc *close;
void *ctx;
} Io_Closer;
function ssize io_read(Io_Reader *r, u8 *dest, usize n);
function ssize io_write(Io_Writer *w, u8 *dest, usize n);
function s32 io_close(Io_Closer *c);
function s32 io_read_all(Io_Reader *r, u8 *dest, usize n);
//------------- Files -------------
#if OsHasFlags(OS_FLAGS_UNIX)
# include <fcntl.h>
# include <sys/types.h>
# include <sys/stat.h>
#endif
typedef struct {
Mem_Base *mb;
#if OsHasFlags(OS_FLAGS_UNIX)
Mutex fd_lock;
int fd; // -1 means invalid
#endif
} File;
function s32 file_open(Mem_Base *mb, String path, File **f);
function s32 file_create(Mem_Base *mb, String path, File **f);
function s32 file_close(File *f);
function s32 file_get_size(File *f, usize *size);
function s32 file_cleanup_close(File **f);
function ssize file_read(File *f, u8 *dest, usize n);
#if 0 // TODO(rutgerbrf): implement
function Io_Reader file_reader(File *f);
function Io_Writer file_writer(File *f);
function Io_Closer file_closer(File *f);
#endif
#define FILE_AUTO_CLOSE __attribute__((__cleanup__(file_cleanup_close)))
//------------- Debugging -------------
#if !defined(AssertBreak)
# define AssertBreak() (*(int*)0 = 0)
#endif
#if ENABLE_ASSERT
# define Assert(c) Stmt(if (!(c)) { fputs("Assertion " Stringify(c) " failed\n", stderr); AssertBreak(); })
#else
# define Assert(c)
#endif
#if ENABLE_UNREACHABLE
function void *unreachable(String loc, String reason);
# define Unreachable(reason) unreachable(Str(__FILE__ ":" Stringify(__LINE__)), Str(reason))
#else
# define Unreachable(reason)
#endif