-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.h
More file actions
488 lines (441 loc) · 13.7 KB
/
Copy pathbase.h
File metadata and controls
488 lines (441 loc) · 13.7 KB
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#ifndef LJRE_BASE_H
#define LJRE_BASE_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <float.h>
#include <stdarg.h>
#ifndef API
# define API EXTERN_C
#endif
#if defined(__x86_64__) || defined(_M_X64)
# define CONFIG_ARCH_AMD64
# define CONFIG_ARCH_X86FAMILY
# define CONFIG_SIZEOF_PTR 8
# define CONFIG_CACHELINE_SIZE 64
#elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
# define CONFIG_ARCH_I686
# define CONFIG_ARCH_X86FAMILY
# define CONFIG_SIZEOF_PTR 4
# define CONFIG_CACHELINE_SIZE 64
#elif defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
# define CONFIG_ARCH_ARMV7A
# define CONFIG_ARCH_ARMFAMILY
# define CONFIG_SIZEOF_PTR 4
# define CONFIG_CACHELINE_SIZE 64
#elif defined(__aarch64__) || defined(_M_ARM64)
# define CONFIG_ARCH_AARCH64
# define CONFIG_ARCH_ARMFAMILY
# define CONFIG_SIZEOF_PTR 8
# define CONFIG_CACHELINE_SIZE 64
#else
# error "Could not detect target arch"
#endif
#ifdef CONFIG_ARCH_X86FAMILY
# if defined(__AVX2__)
# define CONFIG_ARCH_SSELEVEL 51
# elif defined(__AVX__)
# define CONFIG_ARCH_SSELEVEL 50
# elif defined(__SSE4_2__)
# define CONFIG_ARCH_SSELEVEL 42
# elif defined(__SSE4_1__)
# define CONFIG_ARCH_SSELEVEL 41
# elif defined(__SSSE3__)
# define CONFIG_ARCH_SSELEVEL 31
# elif defined(__SSE3__)
# define CONFIG_ARCH_SSELEVEL 30
# elif defined(__SSE2__) || defined(CONFIG_ARCH_AMD64)
# define CONFIG_ARCH_SSELEVEL 20
# endif
# if CONFIG_ARCH_SSELEVEL >= 41 || defined(__POPCNT__)
# define CONFIG_ARCH_INST_POPCNT
# define CONFIG_ARCH_INST_LZCNT
# endif
#endif //CONFIG_ARCH_X86FAMILY
#if defined(__cplusplus) || __STDC_VERSION__ >= 202000
# define CONFIG_HAS_ENHANCED_ENUMS 1
#endif
#ifdef CONFIG_HAS_ENHANCED_ENUMS
# define CONFIG_IF_ENHANCED_ENUMS(...) __VA_ARGS__
#else
# define CONFIG_IF_ENHANCED_ENUMS(...)
#endif
#define AlignUp(x, mask) (((x) + (mask)) & ~(mask))
#define AlignDown(x, mask) ((x) & ~(mask))
#define IsPowerOf2(x) ( ((x) & (x)-1) == 0 )
#define ArrayLength(x) ((intz)(sizeof(x) / sizeof(x)[0]))
#define Min(x,y) ((x) < (y) ? (x) : (y))
#define Max(x,y) ((x) > (y) ? (x) : (y))
#define ClampMax Min
#define ClampMin Max
#define Clamp(x,min,max) ((x) < (min) ? (min) : (x) > (max) ? (max) : (x))
#define SignedSizeof(x) ((intz)sizeof(x))
#define SizeofField(Type, field) ((intz)sizeof( ((Type*)0)->field ))
#define Deferred(begin, end) (int __i_ = ((void)(begin), 0); __i_ < 1; (void)(end), ++__i_)
#define Breakable() (int __i_ = 0; __i_ < 1; ++__i_)
#define AddrofField(ptr, field) ((ptr) ? &(ptr)->field : NULL)
#ifdef __cplusplus
# define StridedOffset(ptr, index, stride) ((decltype(ptr))( (char*)(ptr) + (index) * (stride) ))
# define StridedIndex(ptr, index, stride) StridedOffset(ptr, index, stride)[0]
#elif __STDC_VERSION__ >= 202000
# define StridedOffset(ptr, index, stride) ((typeof(ptr))( (char*)(ptr) + (index) * (stride) ))
# define StridedIndex(ptr, index, stride) StridedOffset(ptr, index, stride)[0]
#endif
#define StridedOffsetT(Type, ptr, index, stride) ((Type*)( (char*)(ptr) + (intz)(index) * (stride) ))
#define StridedIndexT(Type, ptr, index, stride) StridedOffsetT(Type, ptr, index, stride)[0]
#ifndef Trace
# define Trace() ((void)0)
# define TraceName(...) ((void)0)
# define TraceText(...) ((void)0)
# define TraceColor(...) ((void)0)
# define TraceF(sz, ...) ((void)0)
# define TraceFrameBegin() ((void)0)
# define TraceFrameEnd() ((void)0)
# define TraceInit() ((void)0)
# define TraceDeinit() ((void)0)
#endif
#if defined(__clang__)
# define Assume(...) __builtin_assume(__VA_ARGS__)
# define Debugbreak() __builtin_debugtrap()
# define Trap() __builtin_trap()
# define Likely(...) __builtin_expect(!!(__VA_ARGS__), 1)
# define Unlikely(...) __builtin_expect((__VA_ARGS__), 0)
# define Unreachable() __builtin_unreachable()
# define DisableWarnings() \
_Pragma("clang diagnostic push")\
_Pragma("clang diagnostic ignored \"-Weverything\"")
# define ReenableWarnings() \
_Pragma("clang diagnostic pop")
# define FORCE_INLINE __attribute__((always_inline))
# define FORCE_NOINLINE __attribute__((noinline))
# define Alignas(x) __attribute__((aligned(x)))
#elif defined(_MSC_VER)
# define Assume(...) __assume(__VA_ARGS__)
# define Debugbreak() __debugbreak()
# define Trap() __debugbreak()
# define Likely(...) (__VA_ARGS__)
# define Unlikely(...) (__VA_ARGS__)
# define Unreachable() __assume(0)
# define DisableWarnings() \
__pragma(warning(push, 0))
# define ReenableWarnings() \
__pragma(warning(pop))
# define FORCE_INLINE __forceinline
# define FORCE_INLINE __declspec(noinline)
# define Alignas(x) __declspec(align(x))
#elif defined(__GNUC__)
# define Assume(...) do { if (!(__VA_ARGS__)) __builtin_unreachable(); } while (0)
# define Debugbreak() __asm__ __volatile__ ("int $3")
# define Trap() __builtin_trap()
# define Likely(...) __builtin_expect(!!(__VA_ARGS__), 1)
# define Unlikely(...) __builtin_expect((__VA_ARGS__), 0)
# define Unreachable() __builtin_unreachable()
# define DisableWarnings() \
_Pragma("GCC diagnostic push")\
_Pragma("GCC diagnostic ignored \"-Wall\"")\
_Pragma("GCC diagnostic ignored \"-Wextra\"")
# define ReenableWarnings() \
_Pragma("GCC diagnostic pop")
# define FORCE_INLINE __attribute__((always_inline))
# define FORCE_NOINLINE __attribute__((noipa))
# define Alignas(x) __attribute__((aligned(x)))
#else
# define Assume(...) ((void)0)
# define Debugbreak() ((void)0)
# define Trap() (*(char*)0 = 0)
# define Likely(...) (__VA_ARGS__)
# define Unlikely(...) (__VA_ARGS__)
# define Unreachable() ((void)0)
# define DisableWarnings()
# define ReenableWarnings()
# define FORCE_INLINE
# define FORCE_NOINLINE
# define Alignas(x)
#endif
#ifdef __cplusplus
# define restrict
# define EXTERN_C extern "C"
# define NO_RETURN [[noreturn]]
#else
# define EXTERN_C extern
# define NO_RETURN _Noreturn
# define alignas _Alignas
# define alignof _Alignof
# define static_assert _Static_assert
# ifdef _MSC_VER
# define thread_local __declspec(thread)
# else
# define thread_local _Thread_local
# endif
#endif
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
typedef uintptr_t uintptr;
typedef size_t uintz;
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef intptr_t intptr;
typedef ptrdiff_t intz;
typedef float float32;
typedef double float64;
#define INTZ_MAX PTRDIFF_MAX
#define INTZ_MIN PTRDIFF_MIN
#define UINTZ_MAX SIZE_MAX
struct String
{
uint8 const* data;
intz size;
}
typedef String;
typedef String Buffer;
struct Range
{
intz start;
intz end;
}
typedef Range;
struct Arena typedef Arena;
typedef bool ArenaCommitMemoryProc(Arena* arena, intz needed_size);
struct Arena
{
intz size;
intz offset;
uint8* memory;
// NOTE(ljre): Commit-as-you-go arena style.
// commit_memory_proc() function should return true if the specified needed_size is commited
intz reserved;
ArenaCommitMemoryProc* commit_memory_proc;
};
struct ArenaSavepoint
{
Arena* arena;
intz offset;
}
typedef ArenaSavepoint;
enum AllocatorMode
{
// requires (size, alignment), returns a valid pointer
AllocatorMode_Alloc,
// requires (old_ptr, old_size), returns NULL
AllocatorMode_Free,
// requires (), returns NULL
AllocatorMode_FreeAll,
// requires (size, alignment, old_ptr, old_size), returns a valid pointer
AllocatorMode_Resize,
// requires(old_ptr), returns NULL
// NOTE(ljre): old_ptr needs to be a valid pointer to a uint32
AllocatorMode_QueryFeatures,
AllocatorMode_QueryInfo,
// same as Alloc
AllocatorMode_AllocNonZeroed,
// same as Resize
AllocatorMode_ResizeNonZeroed,
// requires (old_ptr), returns NULL
// NOTE(ljre): this is valid for arena allocators, where one can call Alloc(0, 0) to receive a pointer
// to the end of the arena and then call Pop(ptr) to free all allocations made after the
// call to Alloc.
AllocatorMode_Pop,
}
typedef AllocatorMode;
enum AllocatorError
{
AllocatorError_Ok = 0,
AllocatorError_OutOfMemory,
AllocatorError_InvalidPointer,
AllocatorError_InvalidArgument,
AllocatorError_ModeNotImplemented,
}
typedef AllocatorError;
struct Allocator typedef Allocator;
typedef void* AllocatorProc(void* instance, AllocatorMode mode, intz size, intz alignment, void* old_ptr, intz old_size, AllocatorError* out_err);
struct Allocator
{
AllocatorProc* proc;
void* instance;
};
Allocator typedef SingleAllocator; // Alloc; single allocation
Allocator typedef SingleResizingAllocator; // Alloc, Resize, Free; single allocation
Allocator typedef MultiAllocator; // Alloc
Allocator typedef MultiResizingAllocator; // Alloc, Resize, Free
Allocator typedef ArenaAllocator; // Alloc, Pop
enum
{
LOG_DEBUG = 0,
LOG_INFO = 10,
LOG_WARN = 20,
LOG_ERROR = 30,
LOG_FATAL = 40,
};
struct ThreadContextLogger typedef ThreadContextLogger;
typedef void ThreadContextLoggerProc(ThreadContextLogger* logger, int32 level, char const* fmt, va_list args);
struct ThreadContextLogger
{
ThreadContextLoggerProc* proc;
void* user_data;
int32 minimum_level;
};
typedef void ThreadContextAssertionFailureProc(String expr, String func, String file, int32 line);
struct ThreadContext
{
Arena scratch[2]; // 4 MiB each
ThreadContextLogger logger;
ThreadContextAssertionFailureProc* assertion_failure_proc;
}
typedef ThreadContext;
API ThreadContext* ThisThreadContext(void);
static inline Arena*
ScratchArena(intz conflict_count, Arena* const conflicts[])
{
ThreadContext* thread_context = ThisThreadContext();
for (intz i = 0; i < ArrayLength(thread_context->scratch); ++i)
{
Arena* arena = &thread_context->scratch[i];
bool ok = true;
for (intz j = 0; j < conflict_count; ++j)
{
if (arena == conflicts[j])
{
ok = false;
break;
}
}
if (ok)
return arena;
}
return NULL;
}
static inline void
Log(int32 level, char const* fmt, ...)
{
ThreadContext* thread_ctx = ThisThreadContext();
if (!thread_ctx->logger.proc || level < thread_ctx->logger.minimum_level)
return;
va_list args;
va_start(args, fmt);
thread_ctx->logger.proc(&thread_ctx->logger, level, fmt, args);
va_end(args);
}
NO_RETURN static inline void FORCE_NOINLINE
AssertionFailure(char const* expr, char const* func, char const* file, int32 line)
{
ThreadContext* thread_ctx = ThisThreadContext();
if (thread_ctx->assertion_failure_proc)
{
String expr_str = { (uint8 const*)expr };
for (; *expr++; ++expr_str.size);
String func_str = { (uint8 const*)func };
for (; *func++; ++func_str.size);
String file_str = { (uint8 const*)file };
for (; *file++; ++file_str.size);
thread_ctx->assertion_failure_proc(expr_str, func_str, file_str, line);
}
Trap();
}
// ===========================================================================
// ===========================================================================
// Buffers and strings
#ifndef __cplusplus
# define Buf(x) (Buffer) BufInit(x)
# define BUFNULL (Buffer) { 0 }
# define BufMake(size, data) (Buffer) { (const uint8*)(data), (intz)(size) }
# define BufRange(begin, end) (Buffer) BufInitRange(begin, end)
# define Str(x) (String) StrInit(x)
# define STRNULL (String) { 0 }
# define StrMake(size,data) (String) { (const uint8*)(data), (intz)(size) }
# define StrRange(begin, end) (String) StrInitRange(begin, end)
#else //__cplusplus
# define Buf(x) Buffer BufInit(x)
# define BUFNULL Buffer {}
# define BufMake(size, data) Buffer { (const uint8*)(data), (intz)(size) }
# define BufRange(begin, end) Buffer BufInitRange(begin, end)
# define Str(x) String StrInit(x)
# define STRNULL String {}
# define StrMake(size, data) String { (const uint8*)(data), (intz)(size) }
# define StrRange(begin, end) String StrInitRange(begin, end)
#endif //__cplusplus
#define BufInit(x) { (const uint8*)(x), SignedSizeof(x) }
#define BufInitRange(begin, end) { (const uint8*)(begin), (intz)((end) - (begin)) }
#define StrInit(x) { (const uint8*)(x), SignedSizeof(x) - 1 }
#define StrInitRange(begin, end) { (const uint8*)(begin), (intz)((end) - (begin)) }
#define StrFmt(x) (x).size, (char*)(x).data
#define StrMacro_(x) #x
#define StrMacro(x) StrMacro_(x)
#ifdef __cplusplus
template <typename T>
struct Slice
{
T* data;
intz count;
inline T& operator[](intz index) const
{
SafeAssert(index >= 0 && index < count);
return data[index];
}
inline bool operator==(Slice<T> other) const { return data == other.data && count == other.count; }
inline operator bool() const { return count > 0; }
inline operator Slice<T const>() const { return { data, count }; }
inline T* begin() const { return data; }
inline T* end() const { return data + count; }
inline intz Size() const { return count * SignedSizeof(T); }
inline Buffer Buffer() const
{
SafeAssert(count <= INTZ_MAX / sizeof(T));
return { (uint8 const*)data, count * SignedSizeof(T) };
}
inline constexpr Slice<T>
SliceRange(intz start, intz end = -1) const
{
if (!count)
return *this;
if (start < 0)
start = count + start + 1;
if (end < 0)
end = count + end + 1;
start = ClampMax(start, count);
end = Clamp(end, start, count);
return {
data + start,
end - start,
};
}
inline constexpr Slice<T>
Subslice(intz start, intz len = -1) const
{
if (len < 0)
len = count;
return this->SliceRange(start, start + len);
}
};
template <typename T, size_t N>
static inline Slice<T>
SliceFromArray(T (&array)[N])
{
return { array, N };
}
template <typename T>
static inline Slice<T>
SliceFromRange(T* begin, T* end)
{
return { begin, end - begin };
}
static inline Slice<uint8 const>
SliceFromBuffer(Buffer buf)
{
return { buf.data, buf.size };
}
template <typename T>
static inline Slice<T const>
SliceFromTypedBuffer(Buffer buf)
{
return {
buf.data,
buf.size / SignedSizeof(T),
};
}
#endif
#endif