-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprelude.h
4210 lines (3728 loc) · 113 KB
/
prelude.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
#ifndef PRELUDE_H
#define PRELUDE_H
#ifndef PRELUDE_NO_WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h> // PRI macros
#include <stdint.h> // int8_t, etc
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h> // printf
#include <string.h> // malloc, realloc, free
#include <assert.h>
#include <ctype.h> // isspace, isdigit etc
#include <math.h> // ceilf, etc
// Bring in SIMD headers
#if defined(_MSC_VER)
#include <intrin.h>
#elif defined(__GNUC__)
#if defined(__x86_64__) || defined(__i386__)
#include <x86intrin.h>
#elif defined(__ARM_NEON__)
#include <arm_neon.h>
#endif
#endif
// Force <windows.h> to not include definition for min() and max() macros
#define NOMINMAX
// If <windows.h> was already included explicitly remove those macros
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
#define PRELUDE_PRAGMA(X) _Pragma(#X)
#if defined(__clang__)
PRELUDE_PRAGMA(clang diagnostic push)
PRELUDE_PRAGMA(clang diagnostic ignored "-Wtautological-constant-out-of-range-compare")
#endif
#ifdef _MSC_VER
#define PRELUDE_NO_DISCARD _Must_inspect_result_
#else
#define PRELUDE_NO_DISCARD __attribute__ ((warn_unused_result))
#endif
#ifndef countof
#define countof(...)\
(sizeof((__VA_ARGS__)) / sizeof((__VA_ARGS__)[0]))
#endif
#define static_assert_type_alignment(_type_, _alignment_)\
static_assert(\
sizeof(_type_) % (_alignment_) == 0,\
#_type_ " expected to be aligned to " #_alignment_\
);
#define static_assert_type_size(_type_, _size_)\
static_assert(\
sizeof(_type_) == (_size_),\
#_type_ " expected to be " #_size_\
);
#ifndef __APPLE__
#define panic(_message_) assert(!(_message_))
#endif
#define value_swap(_type_, _a_, _b_)\
do {\
struct { _type_ *a; _type_ *b; _type_ temp; } value_swap = { .a = &(_a_), .b = &(_b_) };\
value_swap.temp = *value_swap.a;\
*value_swap.a = *value_swap.b;\
*value_swap.b = value_swap.temp;\
} while (0)
#if defined(__STDC_NO_THREADS__)
#ifdef _MSC_VER
#define thread_local __declspec(thread)
#else
#define thread_local __thread
#endif
#else
#define thread_local _Thread_local
#endif
//////////////////////////////////////////////////////////////////////////////
// Numbers
//////////////////////////////////////////////////////////////////////////////
// Check whether `long` type is 32 bits (Windows) or 64 bits (Linux, Mac, BSD, etc)
#if ((ULONG_MAX) == 4294967295l)
typedef signed long slong_t;
typedef unsigned long ulong_t;
#define slong_t_max_value INT32_MAX
#define slong_t_min_value INT32_MIN
#define ulong_t_max_value UINT32_MAX
#define ulong_t_min_value 0
#define PRIslong_t "li"
#define PRIulong_t "lu"
#else
typedef signed long long slong_t;
typedef unsigned long long ulong_t;
#define slong_t_max_value INT64_MAX
#define slong_t_min_value INT64_MIN
#define ulong_t_max_value UINT64_MAX
#define ulong_t_min_value 0
#define PRIslong_t "lli"
#define PRIulong_t "llu"
#endif
#define PRIs64 PRIi64
#define PRIxs64 PRIxi64
#define PRIs32 PRIi32
#define PRIxs32 PRIxi32
#define PRIs16 PRIi16
#define PRIxs16 PRIxi16
#define PRIs8 PRIi8
#define PRIxs8 PRIxi8
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;
#define u64_max_value UINT64_MAX
#define u64_min_value 0
#define u32_max_value UINT32_MAX
#define u32_min_value 0
#define u16_max_value UINT16_MAX
#define u16_min_value 0
#define u8_max_value UINT8_MAX
#define u8_min_value 0
#define s64_max_value INT64_MAX
#define s64_min_value INT64_MIN
#define s32_max_value INT32_MAX
#define s32_min_value INT32_MIN
#define s16_max_value INT16_MAX
#define s16_min_value INT16_MIN
#define s8_max_value INT8_MAX
#define s8_min_value INT8_MIN
typedef float f32;
#define PRIf32 "f"
typedef double f64;
#define PRIf64 "f"
static_assert(sizeof(void *) == sizeof(u64), "Prelude only supports 64bit architectures");
#define PRELUDE_MAP_SIGNED_INTEGER_TYPES(_FUNC_)\
_FUNC_(s8) _FUNC_(s16) _FUNC_(s32) _FUNC_(s64) _FUNC_(slong_t)
#define PRELUDE_MAP_UNSIGNED_INTEGER_TYPES(_FUNC_)\
_FUNC_(u8) _FUNC_(u16) _FUNC_(u32) _FUNC_(u64) _FUNC_(ulong_t)
#define PRELUDE_MAP_INTEGER_TYPES(_FUNC_)\
PRELUDE_MAP_SIGNED_INTEGER_TYPES(_FUNC_)\
PRELUDE_MAP_UNSIGNED_INTEGER_TYPES(_FUNC_)
#define PRELUDE_MAP_FLOAT_TYPES(_FUNC_)\
_FUNC_(f32) _FUNC_(f64)
#define PRELUDE_MAP_NUMERIC_TYPES(_FUNC_)\
PRELUDE_MAP_INTEGER_TYPES(_FUNC_)\
PRELUDE_MAP_FLOAT_TYPES(_FUNC_)
//////////////////////////////////////////////////////////////////////////////
// Atomics
//////////////////////////////////////////////////////////////////////////////
#ifndef __STDC_NO_ATOMICS__
#include <stdatomic.h>
#define PRELUDE_ATOMIC _Atomic
#else
#ifdef _MSC_VER
#include <windows.h>
#define PRELUDE_ATOMIC
#endif
#endif
#ifdef PRELUDE_ATOMIC
typedef struct {
PRELUDE_ATOMIC u64 raw;
} Atomic_u64;
static inline u64
atomic_u64_increment(
Atomic_u64 *value
) {
#if defined(_MSC_VER) && !defined(__clang__)
return InterlockedIncrement64(&value->raw);
#else
return ++value->raw;
#endif
}
static inline u64
atomic_u64_decrement(
Atomic_u64 *value
) {
#if defined(_MSC_VER) && !defined(__clang__)
return InterlockedDecrement64(&value->raw);
#else
return --value->raw;
#endif
}
static inline u64
atomic_u64_exchange(
Atomic_u64 *value,
u64 new_value
) {
#if defined(_MSC_VER) && !defined(__clang__)
return InterlockedExchange64(&value->raw, new_value);
#else
return atomic_exchange(&value->raw, new_value);
#endif
}
static inline u64
atomic_u64_load(
Atomic_u64 *value
) {
#if defined(_MSC_VER) && !defined(__clang__)
return InterlockedCompareExchange64(&value->raw, 0, 0);
#else
return atomic_load(&value->raw);
#endif
}
static inline u64
atomic_u64_compare_exchange(
Atomic_u64 *value,
u64 expected,
u64 desired
) {
#if defined(_MSC_VER) && !defined(__clang__)
return InterlockedCompareExchange64(&value->raw, desired, expected);
#else
return atomic_compare_exchange_strong(&value->raw, &expected, desired);
#endif
}
#endif
//////////////////////////////////////////////////////////////////////////////
// Bit Operations
//////////////////////////////////////////////////////////////////////////////
static inline u32
u64_count_leading_zeros(
u64 data
) {
if (!data) return 64;
#if defined(_MSC_VER) && !defined(__clang__)
u32 index;
_BitScanReverse64(&index, data);
return 63 - index;
#else
return (u32)__builtin_clzll((unsigned long long)data);
#endif
}
static inline u32
u64_count_trailing_zeros(
u64 data
) {
if (!data) return 64;
#if defined(_MSC_VER) && !defined(__clang__)
u32 index;
_BitScanForward64(&index, data);
return index;
#else
return (u32)__builtin_ctzll((unsigned long long)data);
#endif
}
static inline u32
u32_count_leading_zeros(
u32 data
) {
if (!data) return 32;
#if defined(_MSC_VER) && !defined(__clang__)
u32 index;
_BitScanReverse(&index, data);
return 31 - index;
#else
return (u32)__builtin_clz((unsigned long long)data);
#endif
}
static inline u32
u32_count_trailing_zeros(
u32 data
) {
if (!data) return 32;
#if defined(_MSC_VER) && !defined(__clang__)
u32 index;
_BitScanForward(&index, data);
return index;
#else
return (u32)__builtin_ctz((unsigned int)data);
#endif
}
static inline u32
u32_count_set_bits(
u32 data
) {
#if defined(_MSC_VER) && !defined(__clang__)
return (u32)__popcnt(data);
#else
return (u32)__builtin_popcount((unsigned int)data);
#endif
}
static inline u32
u64_count_set_bits(
u64 data
) {
#if defined(_MSC_VER) && !defined(__clang__)
return (u32)__popcnt64(data);
#else
return (u32)__builtin_popcountll((unsigned long long)data);
#endif
}
//////////////////////////////////////////////////////////////////////////////
// Integer Casts
//////////////////////////////////////////////////////////////////////////////
#define DEFINE_UNSIGNED_CAST(_source_type_, _target_type_)\
static inline bool _source_type_##_fits_into_##_target_type_(_source_type_ value) {\
return value <= _target_type_##_max_value;\
}\
static inline _target_type_ _source_type_##_to_##_target_type_(_source_type_ value) {\
assert(value <= _target_type_##_max_value);\
return (_target_type_)value;\
}
#define PRELUDE_PROCESS_TYPE(_type_)\
DEFINE_UNSIGNED_CAST(u8, _type_)\
DEFINE_UNSIGNED_CAST(u16, _type_)\
DEFINE_UNSIGNED_CAST(u32, _type_)\
DEFINE_UNSIGNED_CAST(u64, _type_)
PRELUDE_MAP_INTEGER_TYPES(PRELUDE_PROCESS_TYPE)
#undef PRELUDE_PROCESS_TYPE
#undef DEFINE_UNSIGNED_CAST
#define DEFINE_SIGNED_CAST(_source_type_, _target_type_)\
static inline bool _source_type_##_fits_into_##_target_type_(_source_type_ value) {\
return value >= _target_type_##_min_value && value <= _target_type_##_max_value;\
}\
static inline _target_type_ _source_type_##_to_##_target_type_(_source_type_ value) {\
assert(value >= _target_type_##_min_value);\
assert(value <= _target_type_##_max_value);\
return (_target_type_)value;\
}
#define PRELUDE_PROCESS_TYPE(_type_)\
DEFINE_SIGNED_CAST(s8, _type_)\
DEFINE_SIGNED_CAST(s16, _type_)\
DEFINE_SIGNED_CAST(s32, _type_)\
DEFINE_SIGNED_CAST(s64, _type_)
PRELUDE_MAP_INTEGER_TYPES(PRELUDE_PROCESS_TYPE)
#undef PRELUDE_PROCESS_TYPE
#undef DEFINE_UNSIGNED_CAST
//////////////////////////////////////////////////////////////////////////////
// Math
//////////////////////////////////////////////////////////////////////////////
#define PRELUDE_PROCESS_TYPE(_type_)\
static inline _type_ _type_##_min(_type_ x, _type_ y) { return x < y ? x : y; }\
static inline _type_ _type_##_max(_type_ x, _type_ y) { return x > y ? x : y; }
PRELUDE_MAP_NUMERIC_TYPES(PRELUDE_PROCESS_TYPE)
#undef PRELUDE_PROCESS_TYPE
#define min(x, y) _Generic((x) + (y),\
u8: u8_min, u16: u16_min, u32: u32_min, u64: u64_min,\
s8: s8_min, s16: s16_min, s32: s32_min, s64: s64_min,\
slong_t: slong_t_min, ulong_t: ulong_t_min,\
f32: f32_min, f64: f64_min)((x), (y))
#define max(x, y) _Generic((x) + (y),\
u8: u8_max, u16: u16_max, u32: u32_max, u64: u64_max,\
s8: s8_max, s16: s16_max, s32: s32_max, s64: s64_max,\
slong_t: slong_t_max, ulong_t: ulong_t_max,\
f32: f32_max, f64: f64_max)((x), (y))
#define PRELUDE_PROCESS_TYPE(_type_)\
static inline _type_ _type_##_abs(_type_ x) { return x < 0 ? (-x) : x; }
PRELUDE_MAP_SIGNED_INTEGER_TYPES(PRELUDE_PROCESS_TYPE)
PRELUDE_MAP_FLOAT_TYPES(PRELUDE_PROCESS_TYPE)
#undef PRELUDE_PROCESS_TYPE
#define abs(x) _Generic((x),\
s8: s8_abs, s16: s16_abs, s32: s32_abs, s64: s64_abs,\
slong_t: slong_t_abs,\
f32: f32_abs, f64: f64_abs)((x))
#define PRELUDE_ALIGN_INTERNAL(X, ALIGN)\
(((X) + (ALIGN) - 1) / (ALIGN)) * (ALIGN)
#define PRELUDE_PROCESS_TYPE(_type_)\
static inline _type_ _type_##_align(_type_ x, _type_ align) {\
switch(align) {\
case 1: return x;\
case 2: return PRELUDE_ALIGN_INTERNAL(x, 2);\
case 4: return PRELUDE_ALIGN_INTERNAL(x, 4);\
case 8: return PRELUDE_ALIGN_INTERNAL(x, 8);\
case 16: return PRELUDE_ALIGN_INTERNAL(x, 16);\
default: return PRELUDE_ALIGN_INTERNAL(x, align);\
}\
}
PRELUDE_MAP_INTEGER_TYPES(PRELUDE_PROCESS_TYPE)
#undef PRELUDE_PROCESS_TYPE
#undef PRELUDE_ALIGN_INTERNAL
static inline f32 f32_align(f32 x, f32 align) { return ceilf(x / align) * align; }
static inline f64 f64_align(f64 x, f64 align) { return ceil(x / align) * align; }
#define PRELUDE_PROCESS_TYPE(_type_)\
static inline int _type_##_comparator(const _type_ *x, const _type_ *y) {\
if (*x < *y) return -1;\
if (*x > *y) return 1;\
return 0;\
}
PRELUDE_MAP_NUMERIC_TYPES(PRELUDE_PROCESS_TYPE)
#undef PRELUDE_PROCESS_TYPE
#if defined(__clang__)
PRELUDE_PRAGMA(clang diagnostic pop)
#endif
//////////////////////////////////////////////////////////////////////////////
// Memory
//////////////////////////////////////////////////////////////////////////////
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
static inline s32
memory_page_size() {
static s32 size = -1;
if (size == -1) {
#ifdef _WIN32
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
size = system_info.dwPageSize;
#else
size = sysconf(_SC_PAGESIZE);
#endif
}
return size;
}
static inline s32
memory_allocation_granularity() {
static s32 size = -1;
if (size == -1) {
#ifdef _WIN32
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
size = system_info.dwAllocationGranularity;
#else
size = sysconf(_SC_PAGESIZE);
#endif
}
return size;
}
//////////////////////////////////////////////////////////////////////////////
// System
//////////////////////////////////////////////////////////////////////////////
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
static inline s32
system_logical_core_count() {
static s32 count = -1;
if (count == -1) {
#ifdef _WIN32
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
count = system_info.dwNumberOfProcessors;
#else
count = sysconf(_SC_NPROCESSORS_ONLN);
#endif
}
return count;
}
typedef union {
struct {
u64 start_time;
u64 frequency;
};
void *raw[2];
} Performance_Counter;
#ifdef _WIN32
static inline Performance_Counter
system_performance_counter_start() {
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
LARGE_INTEGER start_time;
QueryPerformanceCounter(&start_time);
return (const Performance_Counter) {
.frequency = frequency.QuadPart,
.start_time = start_time.QuadPart
};
}
static inline u64
system_performance_counter_end(
Performance_Counter *counter
) {
LARGE_INTEGER end_time;
QueryPerformanceCounter(&end_time);
u64 elapsed = end_time.QuadPart - counter->start_time;
return (elapsed * 1000000) / counter->frequency;
}
#elif __APPLE__
#include <mach/mach.h>
#include <mach/mach_time.h>
static inline Performance_Counter
system_performance_counter_start() {
return (const Performance_Counter) {
.start_time = mach_absolute_time(),
};
}
static inline u64
system_performance_counter_end(
Performance_Counter *counter
) {
static mach_timebase_info_data_t time_base_info;
u64 elapsed = mach_absolute_time() - counter->start_time;
if ( time_base_info.denom == 0 ) {
mach_timebase_info(&time_base_info);
}
// We might consider doing this math in double
// to avoid overflow with the cost of performance
u64 elapsed_micro =
elapsed * time_base_info.numer / time_base_info.denom / 1000;
return elapsed_micro;
}
#else
#include <sys/time.h>
static inline Performance_Counter
system_performance_counter_start() {
Performance_Counter result = {0};
gettimeofday((struct timeval *)result.raw, 0);
return result;
}
static inline u64
system_performance_counter_end(
Performance_Counter *counter
) {
struct timeval end;
gettimeofday(&end, 0);
struct timeval *start = (struct timeval *)counter->raw;
f64 microseconds_in_a_second = 1000.0 * 1000.0;
f64 elapsed = (end.tv_sec - start->tv_sec) * microseconds_in_a_second;
elapsed += (end.tv_usec - start->tv_usec);
return (u64)elapsed;
}
static_assert(
sizeof(struct timeval) <= sizeof(Performance_Counter), "unix timespec must fit into 16bytes"
);
#endif
//////////////////////////////////////////////////////////////////////////////
// Allocator
//////////////////////////////////////////////////////////////////////////////
typedef struct {
void *raw;
} Allocator_Handle;
typedef struct {
void *(*allocate)(Allocator_Handle handle, u64 size_in_bytes, u64 alignment);
void (*deallocate)(Allocator_Handle handle, void *address, u64 size_in_bytes);
void *(*reallocate)(
Allocator_Handle handle,
void *address,
u64 old_size_in_bytes,
u64 new_size_in_bytes,
u64 alignment
);
Allocator_Handle handle;
} Allocator;
// If the caller takes ownership of a dynamically allocated object it will call
// `deallocate` at some point on it. To be able to return a static object from
// you can use this allocator that simply ignores deallocate calls
static void allocator_static_deallocate(Allocator_Handle handle, void *address, u64 size){}
static const Allocator allocator_static = {
.allocate = 0,
.deallocate = allocator_static_deallocate,
.reallocate = 0,
.handle = 0,
};
static inline void *
allocator_default_allocate(
Allocator_Handle handle,
u64 size_in_bytes,
u64 alignment
) {
#ifdef _WIN32
return _aligned_malloc(size_in_bytes, alignment);
#else
return malloc(size_in_bytes);
#endif
}
static inline void
allocator_default_deallocate(
Allocator_Handle handle,
void *address,
u64 size_in_bytes
) {
#ifdef _WIN32
_aligned_free(address);
#else
free(address);
#endif
}
static inline void *
allocator_default_reallocate(
Allocator_Handle handle,
void *address,
u64 old_size_in_bytes,
u64 new_size_in_bytes,
u64 alignment
) {
#ifdef _WIN32
return _aligned_realloc(address, new_size_in_bytes, alignment);
#else
return realloc(address, new_size_in_bytes);
#endif
}
const Allocator *allocator_default = &(Allocator){
.allocate = allocator_default_allocate,
.deallocate = allocator_default_deallocate,
.reallocate = allocator_default_reallocate,
.handle = 0,
};
#ifdef _WIN32
static inline void *
allocator_system_allocate(
Allocator_Handle handle,
u64 size_in_bytes,
u64 alignment
) {
// More than page of alignment is probably not required anyway so can ignore
u64 granularity = s32_to_u64(memory_allocation_granularity());
(void)granularity;
assert(
alignment <= granularity &&
memory_allocation_granularity() % alignment == 0
);
return VirtualAlloc(0, size_in_bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
}
static inline void
allocator_system_deallocate(
Allocator_Handle handle,
void *address,
u64 size_in_bytes
) {
VirtualFree(address, 0, MEM_RELEASE);
}
#else
// __USE_MISC is required for MAP_ANONYMOUS to be defined on Ubuntu
#ifndef __USE_MISC
#define __USE_MISC 1
#endif
#include <sys/mman.h>
static inline void *
allocator_system_allocate(
Allocator_Handle handle,
u64 size_in_bytes,
u64 alignment
) {
// More than page of alignment is probably not required anyway so can ignore
void *address = 0; // system chosen
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
int fd = -1; // Required by MAP_ANONYMOUS
size_t offset = 0; // Required by MAP_ANONYMOUS
return mmap(0, size_in_bytes, prot, flags, fd, offset);
}
static inline void
allocator_system_deallocate(
Allocator_Handle handle,
void *address,
u64 size_in_bytes
) {
munmap(address, size_in_bytes);
}
#endif
static inline void *
allocator_system_reallocate(
Allocator_Handle handle,
void *address,
u64 old_size_in_bytes,
u64 new_size_in_bytes,
u64 alignment
) {
// More than page of alignment is probably not required anyway so can ignore
void *new_address = allocator_system_allocate(handle, new_size_in_bytes, alignment);
memcpy(new_address, address, old_size_in_bytes);
allocator_system_deallocate(handle, address, old_size_in_bytes);
return new_address;
}
const Allocator *allocator_system = &(Allocator){
.allocate = allocator_system_allocate,
.deallocate = allocator_system_deallocate,
.reallocate = allocator_system_reallocate,
.handle = 0,
};
static inline void *
allocator_allocate_bytes(
const Allocator *allocator,
u64 size_in_bytes,
u64 alignment
) {
return allocator->allocate(allocator->handle, size_in_bytes, alignment);
}
#define allocator_allocate(_allocator_, _type_)\
(_type_ *)allocator_allocate_bytes((_allocator_), sizeof(_type_), _Alignof(_type_))
#define allocator_allocate_array(_allocator_, _type_, _count_)\
(_type_ *)allocator_allocate_bytes((_allocator_), sizeof(_type_) * (_count_), _Alignof(_type_))
#define allocator_allocate_bulk(_ALLOCATOR_, _NAME_, ...)\
struct __VA_ARGS__ *_NAME_ = allocator_allocate_bytes(\
(_ALLOCATOR_), sizeof(struct __VA_ARGS__), _Alignof(struct __VA_ARGS__)\
)
static inline void
allocator_deallocate(
const Allocator *allocator,
void *address,
u64 size_in_bytes
) {
allocator->deallocate(allocator->handle, address, size_in_bytes);
}
static inline void *
allocator_reallocate(
const Allocator *allocator,
void *address,
u64 old_size_in_bytes,
u64 new_size_in_bytes,
u64 alignment
) {
return allocator->reallocate(
allocator->handle,
address,
old_size_in_bytes,
new_size_in_bytes,
alignment
);
}
#define allocator_make(_allocator_, _type_, ...)\
(_type_ *)memcpy(\
allocator_allocate((_allocator_), _type_),\
&(_type_){__VA_ARGS__},\
sizeof(_type_)\
)
////////////////////////////////////////////////////////////////////
// Threads
////////////////////////////////////////////////////////////////////
typedef struct {
void *native_handle;
} Thread;
typedef void (*Thread_Proc)(void *);
#ifdef _WIN32
struct Win32_Thread_Proc_And_Data {
Thread_Proc proc;
void *data;
};
DWORD
thread_make_win32_wrapper_proc(
struct Win32_Thread_Proc_And_Data *raw_proc_and_data
) {
Thread_Proc proc = raw_proc_and_data->proc;
void *data = raw_proc_and_data->data;
allocator_deallocate(
allocator_default, raw_proc_and_data, sizeof(struct Win32_Thread_Proc_And_Data)
);
proc(data);
ExitThread(0);
}
Thread
thread_make(
Thread_Proc proc,
void *data
) {
struct Win32_Thread_Proc_And_Data *proc_and_data =
allocator_allocate(allocator_default, struct Win32_Thread_Proc_And_Data);
proc_and_data->proc = proc;
proc_and_data->data = data;
HANDLE handle = CreateThread(0, 0, thread_make_win32_wrapper_proc, proc_and_data, 0, &(DWORD){0});
return (Thread){.native_handle = handle};
}
void
thread_join(
Thread thread
) {
WaitForSingleObject(thread.native_handle, INFINITE);
}
#else // POSIX
#include <pthread.h>
typedef void *(*Thread_Pthread_Proc)(void *);
static inline Thread
thread_make(
Thread_Proc proc,
void *data
) {
pthread_t handle;
int result = pthread_create(&handle, 0, (Thread_Pthread_Proc)proc, data);
return (Thread){.native_handle = (void *)handle};
}
static inline void
thread_join(Thread thread) {
pthread_join((pthread_t)thread.native_handle, 0);
}
static_assert(sizeof(pthread_t) <= sizeof(Thread), "TODO implement thread wrappers");
#endif
//////////////////////////////////////////////////////////////////////////////
// Range
//////////////////////////////////////////////////////////////////////////////
#define range_length(_r_)\
((_r_).to - (_r_).from)
#define range_equal(_a_, ...)\
((_a_).from == (__VA_ARGS__).from && (_a_).to == (__VA_ARGS__).to)
#define range_intersects(_a_, ...)\
((_a_).from < (__VA_ARGS__).to && (__VA_ARGS__).from < (_a_).to)
#define range_contains(_range_, ...)\
((__VA_ARGS__) >= (_range_).from && (__VA_ARGS__) < (_range_).to)
/// `from` is inclusive, `to` is exclusive
#define PRELUDE_PROCESS_TYPE(_type_)\
typedef struct { _type_ from; _type_ to; } Range_##_type_;\
\
Range_##_type_ range_##_type_##_intersection(Range_##_type_ *a, Range_##_type_ *b) {\
Range_##_type_ result = {0};\
if (range_intersects(*a, *b)) { \
result.from = _type_##_max(a->from, b->from);\
result.to = _type_##_min(a->to, b->to);\
} \
return result;\
}
PRELUDE_MAP_NUMERIC_TYPES(PRELUDE_PROCESS_TYPE)
#undef PRELUDE_PROCESS_TYPE
//////////////////////////////////////////////////////////////////////////////
// Bucket_Array
//////////////////////////////////////////////////////////////////////////////
#define BUCKET_ARRAY_INITIAL_TAIL_CAPACITY 10
#define bucket_array_bucket_struct(_type_, _count_) \
struct { \
u64 length; \
_type_ (* items)[_count_]; \
}
// Must match bucket_array_bucket_struct definition above
typedef struct {
u64 length;
s8 *memory;
} Bucket_Array_Bucket_Internal;
#define bucket_array_struct_internal(_bucket_type_) \
struct { \
const Allocator *allocator; \
u64 bucket_tail_length; \
u64 bucket_tail_capacity; \
_bucket_type_ bucket_tail[]; \
}
#define bucket_array_struct(_type_, _count_) \
bucket_array_struct_internal(\
bucket_array_bucket_struct(_type_, _count_)\
)
typedef bucket_array_struct_internal(Bucket_Array_Bucket_Internal) Bucket_Array_Internal;
#define bucket_array_type(_type_, _count_)\
union {\
Bucket_Array_Internal *internal;\
bucket_array_struct(_type_, _count_) *typed;\
}
#define bucket_array_type_page_size(_type_)\
bucket_array_type(_type_, (4096 / sizeof(_type_)))
#define PRELUDE_PROCESS_TYPE(_type_)\
typedef bucket_array_type_page_size(_type_) Bucket_Array_##_type_;
PRELUDE_MAP_NUMERIC_TYPES(PRELUDE_PROCESS_TYPE)
#undef PRELUDE_PROCESS_TYPE
struct Bucket_Array_Make_Options {
void *_count_reset;
const Allocator *allocator;
};
static inline Bucket_Array_Internal *
bucket_array_alloc_internal(
struct Bucket_Array_Make_Options *options,
u64 single_bucket_byte_size,
u64 item_byte_size,
u64 initial_bucket_tail_capacity
) {
const Allocator *allocator = options->allocator;
u64 allocation_size =
sizeof(Bucket_Array_Internal) +
sizeof(Bucket_Array_Bucket_Internal) * initial_bucket_tail_capacity;
Bucket_Array_Internal *result = allocator_allocate_bytes(
allocator,
allocation_size,
u64_max(_Alignof(Bucket_Array_Bucket_Internal), _Alignof(Bucket_Array_Internal))
);
*result = (Bucket_Array_Internal) {
.allocator = allocator,
.bucket_tail_length = 1,
.bucket_tail_capacity = initial_bucket_tail_capacity,
};
result->bucket_tail[0] = (Bucket_Array_Bucket_Internal) {