-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathna.h
7592 lines (6181 loc) · 190 KB
/
na.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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
na.h - v0.08
Nick Aversano's C/C++ helper library
This is a single header file with a bunch of useful stuff
to replace the C/C++ standard library.
===========================================================================
USAGE
Define this in your source file:
#define impl
#include "na.h"
LICENSE
This software is dual-licensed to the public domain and under the following
license: you are granted a perpetual, irrevocable license to copy, modify,
publish, and distribute this file as you see fit.
CREDITS
Written by Nick Aversano
Credits are much appreciated but not required.
VERSION HISTORY
0.09 - linux support
0.08 - bug fixes, fix arena alignment on MacOS ARM
0.07 - bug fixes
0.06 - added comparision helpers, improved stretchy arrays API, added Timing_f64,
actually seed random with os time
0.05 - fix problem with not including Win32 headers, add back #impl
0.04 - breaking API changes, lots of new stuff!
0.03 - arena improvements
0.02 - replace nja with na, arena and thread clean up
0.01 - Initial version
*/
#ifndef NA_H
#define NA_H
#ifndef BASE_CTX_CRACK_H
#define BASE_CTX_CRACK_H
//
// OS
//
#if defined(_WIN32)
#define OS_WINDOWS 1
#elif defined(__APPLE__)
#define OS_MACOS 1
#elif defined(__linux__)
#define OS_LINUX 1
#endif
#if !defined(OS_WINDOWS)
#define OS_WINDOWS 0
#endif
#if !defined(OS_LINUX)
#define OS_LINUX 0
#endif
#if !defined(OS_MACOS)
#define OS_MACOS 0
#endif
//
// Language
//
#if defined(__cplusplus)
#define LANG_CPP 1
#else
#define LANG_C 1
#endif
#if !defined(LANG_CPP)
#define LANG_CPP 0
#endif
#if !defined(LANG_C)
#define LANG_C 0
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202300L
#define C_VERSION_23 1
#else
#define C_VERSION_23 0
#endif
//
// Compiler
//
#if defined(__clang__)
#define COMPILER_CLANG 1
#elif defined(_MSC_VER)
#define COMPILER_MSVC 1
#elif defined(__GNUC__) || defined(__GNUG__)
#define COMPILER_GCC 1
#endif
#if !defined(COMPILER_MSVC)
#define COMPILER_MSVC 0
#endif
#if !defined(COMPILER_GCC)
#define COMPILER_GCC 0
#endif
#if !defined(COMPILER_CLANG)
#define COMPILER_CLANG 0
#endif
#if COMPILER_MSVC
#if _MSC_VER >= 1930
#define COMPILER_MSVC_YEAR 2022
#elif _MSC_VER >= 1920
#define COMPILER_MSVC_YEAR 2019
#elif _MSC_VER >= 1910
#define COMPILER_MSVC_YEAR 2017
#elif _MSC_VER >= 1900
#define COMPILER_MSVC_YEAR 2015
#elif _MSC_VER >= 1800
#define COMPILER_MSVC_YEAR 2013
#elif _MSC_VER >= 1700
#define COMPILER_MSVC_YEAR 2012
#elif _MSC_VER >= 1600
#define COMPILER_MSVC_YEAR 2010
#elif _MSC_VER >= 1500
#define COMPILER_MSVC_YEAR 2008
#elif _MSC_VER >= 1400
#define COMPILER_MSVC_YEAR 2005
#else
#define COMPILER_MSVC_YEAR 0
#endif
#endif
//
// Arch
//
#if defined(_WIN32)
#if defined(_M_AMD64)
#define ARCH_X64 1
#elif defined(_M_IX86)
#define ARCH_X86 1
#elif defined(_M_ARM64)
#define ARCH_ARM64 1
#elif defined(_M_ARM)
#define ARCH_ARM32 1
#endif
#else
#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64)
#define ARCH_X64 1
#elif defined(i386) || defined(__i386) || defined(__i386__)
#define ARCH_X86 1
#elif defined(__aarch64__)
#define ARCH_ARM64 1
#elif defined(__arm__)
#define ARCH_ARM32 1
#endif
#endif
#if !defined(ARCH_X64)
#define ARCH_X64 0
#endif
#if !defined(ARCH_X86)
#define ARCH_X86 0
#endif
#if !defined(ARCH_ARM64)
#define ARCH_ARM64 0
#endif
#if !defined(ARCH_ARM32)
#define ARCH_ARM32 0
#endif
#if defined(ARCH_X64) || defined(ARCH_ARM64)
#define ARCH_64BIT 1
#elif defined(ARCH_X86) || defined(ARCH_ARM32)
#define ARCH_32BIT 1
#endif
#if !defined(ARCH_64BIT)
#define ARCH_64BIT 0
#endif
#if !defined(ARCH_32BIT)
#define ARCH_32BIT 0
#endif
//
// Byte Order
//
static const int __arch_endian_check_num = 1;
#define ARCH_LITTLE_ENDIAN (*(char *)&__arch_endian_check_num == 1)
#define ARCH_BIG_ENDIAN (!ARCH_LITTLE_ENDIAN)
//
// Extensions
//
#if COMPILER_MSVC
#define __has_builtin(x) false
#define __has_feature(x) false
#endif
#if COMPILER_MSVC
#define __FormatString __format_string
#else
#define __FormatString
#endif
#if COMPILER_CLANG
// #define __PrintFunction(x,y) __attribute__((__format__ (printf, x, y)))
#define __PrintFunction(x,y)
#else
#define __PrintFunction(x,y)
#endif
#if __has_feature(address_sanitizer)
#ifndef __SANITIZE_ADDRESS__
#define __SANITIZE_ADDRESS__
#endif
#endif
#ifdef __SANITIZE_ADDRESS__
#if OS_MACOS
#include <sanitizer/asan_interface.h>
#endif
#ifndef AsanPoisonMemoryRegion
void __asan_poison_memory_region(void const volatile *addr, unsigned long size);
#define AsanPoisonMemoryRegion(addr, size) __asan_poison_memory_region((addr), (size))
#endif
#ifndef AsanUnpoisonMemoryRegion
void __asan_unpoison_memory_region(void const volatile *addr, unsigned long size);
#define AsanUnpoisonMemoryRegion(addr, size) __asan_unpoison_memory_region((addr), (size))
#endif
#else
#define AsanPoisonMemoryRegion(addr, size) ((void)(addr), (void)(size))
#define AsanUnpoisonMemoryRegion(addr, size) ((void)(addr), (void)(size))
#endif
#endif // BASE_CTX_CRACK_H
#ifndef BASE_TYPES_H
#define BASE_TYPES_H
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//
// Keywords
//
#define function static
#define local_persist static
#ifndef export_function
#define export_function static
#endif
#if LANG_CPP
#if OS_WINDOWS
#define import extern "C" __declspec(dllimport)
#define export extern "C" __declspec(dllexport)
#else
#define import extern "C"
#define export extern "C"
#endif
#else
#if OS_WINDOWS
#define import __declspec(dllimport)
#define export __declspec(dllexport)
#else
#define import
#define export
#endif
#endif
#if COMPILER_MSVC
#define thread_local __declspec(thread)
#elif COMPILER_CLANG
#define thread_local __thread
#elif COMPILER_GCC
#define thread_local __thread
#endif
#if COMPILER_MSVC && _MSC_VER < 1900 // COMPILER_MSVC_YEAR < 2015
#define __function__ "unknown"
#else
#define __function__ __func__
#endif
#define fallthrough
#if OS_WINDOWS
#pragma section(".roglob", read)
#define read_only __declspec(allocate(".roglob"))
#else
#define read_only
#endif
#ifndef force_inline
#if defined(__GNUC__) && (__GNUC__ >= 4)
#define force_inline __attribute__((always_inline)) inline
#elif defined(__llvm__)
#define force_inline __attribute__((always_inline)) inline
#elif defined(_MSC_VER)
#define force_inline __forceinline
#else
#define force_inline inline
#endif
#endif
//
// Helper Macros
//
#define MemoryCopy(d,s,size) memcpy(d,s,size)
#define MemoryMove(d,s,size) memmove(d,s,size)
#define MemorySet(ptr,x,size) memset(ptr,x,size)
#define MemoryCompare(a, b, s) memcmp(a, b, s)
#define MemoryEquals(a, b, s) (memcmp(a, b, s) == 0)
#define MemoryCopyStruct(d,s) do { assert((d)!=NULL && (s)!=NULL); Assert(sizeof(*(d))==sizeof(*(s))); MemoryCopy((d),(s),sizeof(*(d))); } while(0)
#define MemoryCopyArray(d,s) do{ assert((d)!=NULL && (s)!=NULL); Assert(sizeof(d)==sizeof(s)); MemoryCopy((d),(s),sizeof(s)); }while(0)
#define MemoryCopyString(d,s) memcpy(d,(s).data,(s).count)
#define MemoryZero(p,s) do { assert((p)!=NULL); MemorySet((p), 0, (s)); } while(0)
#define MemoryZeroStruct(p) MemoryZero((p), sizeof(*(p)))
#define MemoryZeroArray(a) MemoryZero((a), sizeof(a))
#define QuickSort(data, count, item_size, cmp) qsort(data, count, item_size, cmp)
#define BinarySearch(key, base, count, item_size, cmp) bsearch(key, base, count, item_size, cmp)
#define ArrayCount(a) (sizeof(a) / sizeof((a)[0]))
#define IntFromPtr(p) (u64)(((u8*)p) - 0)
#define PtrFromInt(i) (void*)(((u8*)0) + i)
#define Member(S,m) ((S*)0)->m
#define OffsetOf(S,m) IntFromPtr(&Member(S,m))
#define CastFromMember(S,m,p) (S*)(((u8*)p) - OffsetOf(S,m))
#define MemberFromOffset(ptr, off, type) *(type *)((u8 *)(ptr) + off)
#define Unused(name) ((void)(name))
#define Bytes(n) (n)
#define Kilobytes(n) ((n) << 10)
#define Megabytes(n) ((n) << 20)
#define Gigabytes(n) (((u64)n) << 30)
#define Terabytes(n) (((u64)n) << 40)
#define Thousand(x) ((x)*1000)
#define Million(x) ((x)*1000000)
#define Billion(x) ((x)*1000000000LL)
#define Miliseconds(x) ((x)/1000.0)
#define Seconds(x) ((x)*1.0)
#define Minutes(x) ((x)*60.0)
#define Hours(x) ((x)*60.0*60.0)
#define Days(x) ((x)*24.0*60.0*60.0)
#define HasFlag(fl,fi) ((fl)&(fi))
#define SetFlag(fl,fi) ((fl)|=(fi))
#define RemFlag(fl,fi) ((fl)&=~(fi))
#define ToggleFlag(fl,fi) ((fl)^=(fi))
#define SetFlagState(fl,fi,set) do { if (set) SetFlag(fl,fi); else RemFlag(fl,fi); } while (0)
#define Swap(T,a,b) do { T t__ = (a); (a) = (b); (b) = t__; } while(0)
#define FourCC(a, b, c, d) \
(((u32)(a) << 0) | ((u32)(b) << 8) | ((u32)(c) << 16) | ((u32)(d) << 24))
#define FourCCStr(s) FourCC(s[0], s[1], s[2], s[3])
#define InRange(x,lo,hi) (((x) >= (lo)) && ((x) <= (hi)))
#define GetBit(x,bit) (((x) >> (bit)) & 0x1)
#define count_of ArrayCount
#define offset_of OffsetOf
#if LANG_CPP
#define cast(T) (T)
#else
#define cast(T) (T)
// #define cast(T) static_cast<T>
#endif
#define array_of(x) {(x),count_of(x)}
#ifndef typeof
#ifdef __clang__
#define typeof(x) __typeof__(x)
#endif
#if LANG_CPP
#ifdef _MSC_VER
#define typeof(x) decltype(x)
#endif
#endif
#endif
#define DefineStruct(Type) typedef struct Type Type
#if LANG_C
#define Struct(Type) typedef struct Type Type; struct Type
#define StructLit(Type) (Type)
// #define Enum(Type) typedef enum Type Type; enum Type
#else
#define Struct(Type) struct Type
#define StructLit(Type) Type
// #define Enum(Type) enum Type
#endif
//
// Linked-List Macros
//
#define CheckNull(p) ((p)==0)
#define SetNull(p) ((p)=0)
#define QueuePush_NZ(f,l,n,next,zchk,zset) (zchk(f)?\
(((f)=(l)=(n)), zset((n)->next)):\
((l)->next=(n),(l)=(n),zset((n)->next)))
#define QueuePush_N(f,l,n,next) QueuePush_NZ(f,l,n,next,CheckNull,SetNull)
#define QueuePushFront_NZ(f,l,n,next,zchk,zset) (zchk(f) ? (((f) = (l) = (n)), zset((n)->next)) :\
((n)->next = (f)), ((f) = (n)))
#define QueuePop_NZ(f,l,next,zset) ((f)==(l)?\
(zset(f),zset(l)):\
(f)=(f)->next)
#define StackPush_N(f,n,next) ((n)->next=(f),(f)=(n))
#define StackPop_NZ(f,next,zchk) (zchk(f)?0:((f)=(f)->next))
#define DLLInsert_NPZ(f,l,p,n,next,prev,zchk,zset) \
(zchk(f) ? (((f) = (l) = (n)), zset((n)->next), zset((n)->prev)) :\
zchk(p) ? (zset((n)->prev), (n)->next = (f), (zchk(f) ? (0) : ((f)->prev = (n))), (f) = (n)) :\
((zchk((p)->next) ? (0) : (((p)->next->prev) = (n))), (n)->next = (p)->next, (n)->prev = (p), (p)->next = (n),\
((p) == (l) ? (l) = (n) : (0))))
#define DLLPushBack_NPZ(f,l,n,next,prev,zchk,zset) DLLInsert_NPZ(f,l,l,n,next,prev,zchk,zset)
#define DLLRemove_NPZ(f,l,n,next,prev,zchk,zset) (((f)==(n))?\
((f)=(f)->next, (zchk(f) ? (zset(l)) : zset((f)->prev))):\
((l)==(n))?\
((l)=(l)->prev, (zchk(l) ? (zset(f)) : zset((l)->next))):\
((zchk((n)->next) ? (0) : ((n)->next->prev=(n)->prev)),\
(zchk((n)->prev) ? (0) : ((n)->prev->next=(n)->next))))
#define QueuePush(f,l,n) QueuePush_NZ(f,l,n,next,CheckNull,SetNull)
#define QueuePushFront(f,l,n) QueuePushFront_NZ(f,l,n,next,CheckNull,SetNull)
#define QueuePop(f,l) QueuePop_NZ(f,l,next,SetNull)
#define StackPush(f,n) StackPush_N(f,n,next)
#define StackPop(f) StackPop_NZ(f,next,CheckNull)
#define DLLPushBack(f,l,n) DLLPushBack_NPZ(f,l,n,next,prev,CheckNull,SetNull)
#define DLLPushFront(f,l,n) DLLPushBack_NPZ(l,f,n,prev,next,CheckNull,SetNull)
#define DLLInsert(f,l,p,n) DLLInsert_NPZ(f,l,p,n,next,prev,CheckNull,SetNull)
#define DLLRemove(f,l,n) DLLRemove_NPZ(f,l,n,next,prev,CheckNull,SetNull)
#define DLLPopBack(f,l) (((f) && (l)) ? DLLRemove(f,l,l) : 0)
#define DLLPopFront(f,l) (((f) && (l)) ? DLLRemove(f,l,f) : 0)
#define ListEach(Type,it,list) Type *it = (list).first; it != NULL; it = it->next
//
// Clamps
//
#define Min(a, b) (((a) < (b)) ? (a) : (b))
#define Max(a, b) (((a) > (b)) ? (a) : (b))
#define Clamp(value, lower, upper) (Max(Min(value, upper), lower))
#define ClampTop(a, b) Min(a, b)
#define ClampBot(a, b) Max(a, b)
#define Sign(x) (((x) > 0) - ((x) < 0))
#define Abs(x) (((x) < 0) ? (0u - x) : (0u + x))
#define AlignUpPow2(x, p) (((x) + (p) - 1) & ~((p) - 1))
#define AlignDownPow2(x, p) ((x) & ~((p) - 1))
#define IsPow2(x) (((x) & ((x) - 1)) == 0)
//
// Defer
//
#ifndef CONCAT
#define CONCAT_HELPER(x, y) x##y
#define CONCAT(x, y) CONCAT_HELPER(x, y)
#endif
#define Defer(start, end) for (int CONCAT(__i, __LINE__) = ((start), 0); !CONCAT(__i, __LINE__); CONCAT(__i, __LINE__) += 1, (end))
#if LANG_CPP
// Defer macro/thing.
template<typename T>
struct ExitScope {
T lambda;
ExitScope(T lambda) : lambda(lambda) {}
~ExitScope() { lambda(); }
ExitScope(const ExitScope&);
private:
ExitScope& operator =(const ExitScope&);
};
class ExitScopeHelp {
public:
template<typename T>
ExitScope<T> operator+(T t) { return t; }
};
#define defer const auto& CONCAT(defer__, __LINE__) = ExitScopeHelp() + [&]()
#endif // LANG_CPP
//
// Types
//
#ifndef NULL
#define NULL 0
#endif
#if LANG_C
#ifdef __STDC_VERSION__ // >= C99
#define bool _Bool
#else
#define bool int
#endif
#define true 1
#define false 0
#endif
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef i8 b8;
typedef i16 b16;
typedef i32 b32;
typedef i64 b64;
typedef float f32;
typedef double f64;
typedef void VoidFunction(void);
#define U8_MAX ((u8)0xff)
#define I8_MAX ((i8)0x7f)
#define I8_MIN ((i8)0x80)
#define U16_MAX ((u16)0xffff)
#define I16_MAX ((i16)0x7fff)
#define I16_MIN ((i16)0x8000)
#define U32_MAX ((u32)0xffffffff)
#define I32_MAX ((i32)0x7fffffff)
#define I32_MIN ((i32)0x80000000)
#define U64_MAX ((u64)0xffffffffffffffff)
#define I64_MAX ((i64)0x7fffffffffffffff)
#define I64_MIN ((i64)0x8000000000000000)
#define F32_MIN ((f32)1.17549435e-38f)
#define F32_MAX ((f32)3.40282347e+38f)
#define F64_MIN ((f64)2.2250738585072014e-308)
#define F64_MAX ((f64)1.7976931348623157e+308)
//
// Language Helpers
//
typedef struct MemberOffset MemberOffset;
struct MemberOffset
{
u64 v;
};
#define MemberOff(S, member) (MemberOffset){OffsetOf(S, m)}
#define MemberFromOff(ptr, type, memoff) (*(type *)((u8 *)ptr + memoff.v))
//
// Assert
//
#if COMPILER_MSVC
#define Breakpoint() __debugbreak()
#elif COMPILER_CLANG || COMPILER_GCC
#define Breakpoint() __builtin_trap()
#endif
int na__assert(bool cond, const char *expr, const char *file, long int line, char *msg) {
if (!cond) {
printf("%s(%ld): %s: ", file, line, "Assertion Failed");
if (expr) {
printf( "`%s` ", expr);
}
if (msg) {
printf("- %s", msg);
}
printf("\n");
fflush(stdout);
#if DEBUG
Breakpoint();
#endif
*(volatile int *)0 = 0;
}
return 0;
}
#undef assert
#define assert(expr) na__assert(expr, #expr, __FILE__, __LINE__, NULL)
#define Assert(expr) na__assert(expr, #expr, __FILE__, __LINE__, NULL)
#if LANG_C
#ifdef _Static_assert
#define StaticAssert(c,label) _Static_assert(c, label)
#else
#define StaticAssert(c,label) u8 static_assert__failed_on_line_##__LINE__[(c)?(1):(-1)]
#endif
#else
#define StaticAssert(c,label) static_assert(c, #label)
#endif
#define NotImplemented assert(!"Not Implemented")
#define InvalidPath assert(!"Invalid Path")
//
// Variadic Macros
//
#define NameConcat2(A, B) A##B
#define NameConcat(A, B) NameConcat2(A, B)
#define ArgCount(...) _COUNTOF_CAT( _COUNTOF_A, ( 0, ##__VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ) )
#define _COUNTOF_CAT( a, b ) a b
#define _COUNTOF_A( a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, n, ... ) n
//
// NOTE(nick): you have to define the functions in reverse order, as in:
// #define Dump(...) ArgSelectHelper4((__VA_ARGS__, Dump4, Dump3, Dump2, Dump1))(__VA_ARGS__)
//
#define ArgSelectHelper4_(_1, _2, _3, _4, NAME, ...) NAME
#define ArgSelectHelper4(args) ArgSelectHelper4_ args
#define ArgSelectHelper2_(_1, _2, NAME, ...) NAME
#define ArgSelectHelper2(args) ArgSelectHelper2_ args
// TODO(nick): implement these
#define Likely(x) (x)
#define Unlikely(x) (x)
#define TestFunction(Name) bool Name()
#define TestAssert(Cond) do { if (!(Cond)) return false; } while(0)
#define TestAssertM(Cond, Message) do { if (!(Cond)) { print(Message); print("\n"); return false; } } while(0)
#define TestAssertF(Cond, Message, ...) do { if (!(Cond)) { print(Message, __VA_ARGS__); print("\n"); return false; } } while(0)
#define TestEntry(Proc) StructLit(TestFunction_Entry){Proc, #Proc}
typedef struct TestFunction_Entry TestFunction_Entry;
struct TestFunction_Entry
{
bool (*fn)();
const char *name;
};
#endif // BASE_TYPES_H
#ifndef BASE_MEMORY_H
#define BASE_MEMORY_H
typedef u64 Arena_Flags;
enum
{
ArenaFlag_NoChain = (1 << 0),
};
typedef struct Arena Arena;
struct Arena
{
Arena_Flags flags;
Arena *current;
Arena *prev;
u8 *data;
u64 pos;
u64 size;
u64 commit_pos;
u64 page_size;
};
typedef struct M_Temp M_Temp;
struct M_Temp
{
Arena *arena;
u64 pos;
};
//
// API
//
function Arena arena_make(u8 *data, u64 size);
function void arena_init(Arena *arena, u8 *data, u64 size);
function Arena *arena_alloc(u64 size);
function void arena_free(Arena *arena);
function void *arena_push_bytes(Arena *arena, u64 size);
function void arena_pop_to(Arena *arena, u64 pos);
function void arena_pop(Arena *arena, u64 size);
function void arena_set_pos(Arena *arena, u64 pos);
function void arena_reset(Arena *arena);
function void arena_push_aligner(Arena *arena, u64 pow2_align);
function void *arena_push(Arena *arena, u64 size);
function void *arena_push_zero(Arena *arena, u64 size);
function bool arena_write(Arena *arena, u8 *data, u64 size);
function M_Temp arena_begin_temp(Arena *arena);
function void arena_end_temp(M_Temp temp);
#define PushArray(a,T,c) (T*)arena_push_no_zero((a), sizeof(T)*(c))
#define PushArrayZero(a,T,c) (T*)arena_push((a), sizeof(T)*(c))
#define PushStruct(a, T) (T*)arena_push_no_zero((a), sizeof(T))
#define PushStructZero(a, T) (T*)arena_push((a), sizeof(T))
#define PopArray(a, T, c0, c1) if (c1 < c0) arena_pop((a), (c0 - c1) * sizeof(T))
function M_Temp arena_get_scratch(Arena **conflicts, u64 conflict_count);
#define GetScratch(conflicts, conflict_count) arena_get_scratch(conflicts, conflict_count)
#define ReleaseScratch(temp) arena_end_temp(temp)
function Arena *temp_arena();
// Helpers
typedef i32 Compare_Proc(void *a, void *b);
function void memory_swap(void *i, void *j, u64 size);
function void memory_sort(void *base_, u64 count, u64 size, Compare_Proc cmp);
function i64 memory_binary_search(void *base, u64 count, u64 size, void *key, Compare_Proc cmp);
//
// Allocator
//
#if !defined(ALLOCATOR_DEFAULT_ALIGNMENT)
#define ALLOCATOR_DEFAULT_ALIGNMENT 16
#endif
enum Allocator_Mode
{
AllocatorMode_Alloc = 1,
AllocatorMode_Resize = 2,
AllocatorMode_Free = 3,
AllocatorMode_FreeAll = 4,
};
typedef enum Allocator_Mode Allocator_Mode;
#define ALLOCATOR_PROC(name) void *name(Allocator_Mode mode, u64 requested_size, u64 old_size, void *old_memory_pointer, void *allocator_data, u32 alignment)
typedef ALLOCATOR_PROC(Allocator_Proc);
typedef struct Allocator Allocator;
struct Allocator
{
Allocator_Proc *proc;
void *data;
};
//
// API
//
function void *allocator_alloc(Allocator allocator, u64 size);
function void allocator_free(Allocator allocator, void *data, u64 old_size);
function void *allocator_realloc(Allocator allocator, void *data, u64 new_size, u64 old_size);
function void *allocator_alloc_aligned(Allocator allocator, u64 size, u32 alignment);
function void *allocator_realloc_aligned(Allocator allocator, void *data, u64 new_size, u64 old_size, u32 alignment);
function Allocator os_allocator();
function Allocator arena_allocator(Arena *arena);
#endif // BASE_MEMORY_H
#ifndef BASE_STRINGS_H
#define BASE_STRINGS_H
#define S_UTF8_INVALID (u32)(0xFFFD)
#define S_UTF8_BOM (u32)(0xFEFF)
#define S_UTF8_MAX (u32)(0x0010FFFF)
#define Str(x) #x
#if LANG_CPP
#define S(x) (String{(u8 *)(x), sizeof(x)-1})
#else
#define S(x) ((String){(u8 *)(x), sizeof(x)-1})
#endif
typedef struct String String;
struct String
{
u8 *data;
i64 count;
#if LANG_CPP
u8 &operator[](i64 i) {
assert(i >= 0 && i < count);
return data[i];
}
#endif
};
typedef struct String16 String16;
struct String16
{
u16 *data;
i64 count;
#if LANG_CPP
u16 &operator[](i64 i) {
assert(i >= 0 && i < count);
return data[i];
}
#endif
};
typedef struct String32 String32;
struct String32
{
u32 *data;
i64 count;
#if LANG_CPP
u32 &operator[](i64 i) {
assert(i >= 0 && i < count);
return data[i];
}
#endif
};
typedef struct String_Decode String_Decode;
struct String_Decode
{
u32 codepoint;
u8 advance; // 1 - 4
};
typedef struct String_Node String_Node;
struct String_Node
{
String_Node *next;
String string;
};
typedef struct String_List String_List;
struct String_List
{
String_Node *first;
String_Node *last;
u64 node_count;
u64 total_size;
};
typedef struct String_Join_Params String_Join_Params;
struct String_Join_Params
{
String pre;
String sep;
String post;
};
typedef struct String_Array String_Array;
struct String_Array
{
i64 count;
i64 capacity;
String *data;
};
typedef u32 Match_Flags;
enum
{
MatchFlag_IgnoreCase = 1 << 0,
MatchFlag_RightSideSloppy = 1 << 1,
MatchFlag_SlashInsensitive = 1 << 2,
MatchFlag_FindLast = 1 << 3,
};
typedef struct String_Time_Options String_Time_Options;
struct String_Time_Options
{
b32 show_miliseconds;
b32 show_hours;
b32 show_sign;
};
typedef struct CLI_Argument CLI_Argument;
struct CLI_Argument
{
String name;
String value;
b32 consumes_next_index;
};
// Char Functions
function b32 char_is_alpha(u8 c);
function b32 char_is_lower(u8 c);
function b32 char_is_upper(u8 c);
function b32 char_is_digit(u8 c);
function b32 char_is_space(u8 c);
function b32 char_is_whitespace(u8 c);
function b32 char_is_symbol(u8 c);
function b32 char_is_separator(u8 c);
function b32 char_is_slash(u8 c);
function u8 char_to_upper(u8 c);
function u8 char_to_lower(u8 c);
function u8 char_to_forward_slash(u8 c);
// C-Style Strings
function i64 cstr_length(const char *cstr);
// Constructors
function String string_make(u8 *data, i64 count);
function String string_range(u8 *at, u8 *end);
function String string_from_cstr(const char *cstr);
function char *string_to_cstr(Arena *arena, String str);
function String16 string16_make(u16 *data, i64 count);
function String16 string16_from_cstr(u16 *data);
function String32 string32_make(u32 *data, i64 count);
#define CStr(x) string_to_cstr(temp_arena(), (x))
#define Str8(data, count) string_make((u8 *)data, count)
#define Str16(data, count) string16_make((u16 *)data, count)
#define Str32(data, count) string32_make((u32 *)data, count)
// Print Helper
#define LIT(str) (int)str.count, (char *)str.data
// Substrings
function String string_slice(String str, i64 start_index, i64 end_index);
function String string_substr(String str, i64 offset, i64 count);
function String string_skip(String str, i64 offset);
function String string_chop(String str, i64 offset);
function String string_prefix(String str, i64 count);
function String string_suffix(String str, i64 count);
// Matching
function b32 string_equals(String a, String b);
function b32 string_match(String a, String b, Match_Flags flags);
function i64 string_find(String str, String search, i64 start_index, Match_Flags flags);
function b32 string_includes(String str, String search);
function b32 string_starts_with(String str, String prefix);
function b32 string_ends_with(String str, String postfix);
function i64 string_index(String str, String search, i64 start_index);
function i64 string_char_index(String str, u8 search, i64 start_index);
function i64 string_last_index(String str, String search);
function b32 string_contains(String str, String search);
function b32 string_in_bounds(String str, i64 at);
// Allocation
function String string_push(Arena *arena, String str);
function String string_alloc(String str);
function void string_free(String *str);
function String string_printv(Arena *arena, const char *fmt, va_list args);
function String string_print(Arena *arena, const char *fmt, ...);
#define PushStringCopy(arena, str) string_push(arena, str)
#define PushStringFV(arena, fmt, args) string_printv(arena, fmt, args)
#define PushStringF(arena, fmt, ...) string_print(arena, fmt, __VA_ARGS__)
#define sprint(fmt, ...) string_print(temp_arena(), fmt, __VA_ARGS__)
function String string_replace(Arena *arena, String str, String find, String replacer, u64 replace_limit);
// Unicode Conversions
function String_Decode string_decode_utf8(u8 *str, u64 capacity);
function u32 string_encode_utf8(u8 *dest, u32 codepoint);
function u32 string_seek_right_utf8(u8 *data, u64 capacity);
function u32 string_seek_left_utf8(u8 *data, u64 capacity);
function i64 string_seek_utf8(String text, i64 cursor, i64 amount);
// @Cleanup: move these to the UI layer
function i64 string_move_word(String text, i64 cursor, i32 direction);
function void string_select_word(String text, i64 cursor, i64 *left, i64 *right);
function String_Decode string_decode_utf16(u16 *str, u64 capacity);
function u32 string_encode_utf16(u16 *dest, u32 codepoint);
function String32 string32_from_string(Arena *arena, String str);
function String string_from_string32(Arena *arena, String32 str);
function String16 string16_from_string(Arena *arena, String str);
function String string_from_string16(Arena *arena, String16 str);
// Conversions
function u64 string_to_u64(String string, u32 radix);
function i64 string_to_i64(String str, u32 base);
function f64 string_to_f64(String str);
function b32 string_to_b32(String str);