-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteve.h
1163 lines (991 loc) · 39.7 KB
/
steve.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
// steve.h ..
// :. :--
// -------: ----
// ++++==----:--=+=--:-::---:
// #::#*+====--*@@@=-.------:====**###
// ####**====-=@@@@%-:-+@@@-.-==+*** #
// ####*+=++=-*@@@@@---@@@@%.-+=+**###
// ####*+++++-*@@@@@--+@@@@@--===**###
// +#####+++=+--@@@@+---@@@@@--++=*####
// -#####+++++=--@@#-*#-*@@@--*++=**###
// -####*+++==*=----#*+*-----**++=**##*
// -#####++++++*%%%#++++##*##*====####*
// =#**% =######*++++++++++++=++*+++===*####+
// =##**#%# =##################*##*##*##*######-
// -##*##%@# +##################################: %#*+
// %##**%%@# +################################## *@%**#*
// =%##**%@@= *################################## %%%#*##=
// %%%#*##%@ ########=:::::-----==+++*########## @@@##*##
// @@@%###%%#+ #######*::.:-::...........=######## *##@@@%###%
// *#%@@@@@%%@ #######*:::####...........+######## @@@@@#**#%
// %##%@@# #######+:::####::::::::...+######## +@##*##@
// +%##%@@% #######+:::###*:::::::::::*######## +%####@+
// +%#*%%@%%= ######%+::-###*:::::::::::*#######* %%@%%%@%
// +%#**#%%+ #######=::=#%#+:::::::::::########* :*%@@@@@
// @@@@@* =====++-::::::::::::::::::##*++###* *%#%@@@%
// -++++- --:::.....:- @@@@@-
// #%*+**#@%%%# *%*+-::.......::- :@@#
// =-:::.....:%@# %%*+=-::::::::.::-
// +=-::::....:=@% @#**==-:::::::::-==
// *=---::::::-+** @%#**=----:::::---+
// *++==-----=+** *###*+=--:::::::--=
// =**++======+**+ ##**+=--::::::-:--=-
// +#+=::::::::=+= -
//
// Single header file library for C.
// * Provides an arena allocator and some data structures that use it.
// * Requires c11. Tested on gcc and clang on macos, linux and windows.
//
// * Arena for allocating memory.
// Since all allocations are backed by arenas you don't have to free anything individually, just
// use an arena that matches the lifetime of the data.
// * Thread local scratch arena for cheap temporary allocations within functions.
// * Includes data structures like a dynamic array that work well with the arena.
// * Allows for some optimizations like extending instead of reallocing if the array is on the end
// of an arena.
// * Rules of thumb.
// * If a function returns something that needs to be allocated, it should take an arena to
// allocate the result on.
// * If a function needs to allocate temporary memory,
// it should acquire the scratch arena and release it before returning. p
// * You should never return something allocated on the scratch arena.
// * Things to add.
// * HashMap.
// * Pool
// * Handles
// * Other Ideas
// * Debug print macros that print to stdout on posix and the debug console on windows (for
// viewing in raddbg)
// * Helpers to draw stuff with sixel, supported in windows terminal now so cross platform as far
// as this lib cares.
// * A debug mode that tracks memory usage.
// * Helpers to view these data structures in the debugger.
// * @note(steve): in clion, you can view a pointer as an array like this.
// ptr @ len
// * View an array
// *(&(arr->e[0])) @ arr->len
// * View a slice
// *slc.e @ slc.len
// * @todo(steve): Document how to do this in raddbg.
#ifndef STEVE_H
#define STEVE_H
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
// This is a good way to check compilers and operating systems for
// the things this lib supports. Everything used in the header is normal c11 so this isn't
// needed here, but I keep it here as living docs.
// A switch on the OS is used in the implementation file.
#if defined(_MSC_VER)
// MSVC-specific code
#elif defined(__GNUC__) || defined(__clang__)
// GCC/Clang-specific code
#else
#error "Unsupported Compiler"
#endif
#if defined(_WIN64)
// Windows-specific code
#elif defined(__APPLE__)
// macOS-specific code
#elif defined(__linux__)
// Linux-specific code
#else
#error "Unsupported Platform"
#endif
// @todo(steve): Clean up this block.
// * Make it more obvious which compilers and c versions are supported.
// The current list is
// * Linux
// * gcc (c11 and c23)
// * clang (c11 and c23)
// * @todo(steve): Make sure works with musl as well as glibc.
// * MacOS
// * gcc (c11 and c23) (Does anybody use this?)
// * clang (c11 and c23)
// * Windows
// * mingw64 (not sure the c versions supported)
// * clang (c11 and c23)
// * @todo: Support msvc (c11 and c17)
// * @todo(steve): Support cosmocc.
// * @todo(steve): Support zigc.
// * @todo(steve): Support SDL instead of stdlib for all these platforms.
// * One block like this should figure out the compiler and set some easier to switch on defines.
// eg: STEVE_MACOS_CLANG_C23
// I can maybe look at other projects like sdl to see how they do this.
// Assert what these HAVE to be for the library to work.
_Static_assert(sizeof(float) == 4, "float must be 32 bits");
_Static_assert(sizeof(double) == 8, "double must be 64 bits");
_Static_assert(sizeof(size_t) == 8, "size_t must be 64 bits");
_Static_assert(sizeof(ptrdiff_t) == 8, "ptrdiff_t must be 64 bits");
_Static_assert(sizeof(unsigned char) == 1, "unsigned char must be 8 bits");
_Static_assert(sizeof(char) == 1, "char must be 8 bits");
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
#define MAX(x, y) ((x) >= (y) ? (x) : (y))
#define CLAMP_MAX(x, max) MIN(x, max)
#define CLAMP_MIN(x, min) MAX(x, min)
#define IS_POW2(x) (((x) != 0) && ((x) & ((x) - 1)) == 0)
#define ALIGN_DOWN(n, a) ((n) & ~((a) - 1))
#define ALIGN_UP(n, a) ALIGN_DOWN((n) + (a) - 1, (a))
#define ALIGN_DOWN_PTR(p, a) ((void *)ALIGN_DOWN((ptrdiff_t)(p), (a)))
#define ALIGN_UP_PTR(p, a) ((void *)ALIGN_UP((ptrdiff_t)(p), (a)))
#define ABS(x) ((x) < 0 ? -(x) : (x))
uint64_t pow2_next(uint64_t i);
// memcpy wrapper so we don't have to include sting.h in the header.
// @todo(steve): Tie in with future error handling.
void *xmemcpy(void *dest, const void *src, size_t n);
// Arena
// * An arena is a continuous block of memory that can grow up to a certain size.
// * They currently have a max capacity of 1024*1024 pages. On my m1 mac that's 64GB.
// This is probably too big. @todo(steve): Make this configurable.
// * Arenas are not thread safe so multiple threads should not use the same arena at the same time.
// * This only works on 64 bit systems where pointers are 64 bits.
typedef struct Arena Arena;
struct Arena {
uint8_t *begin;
uint8_t *pos;
uint8_t *commit;
uint64_t refcount;
};
// * The way an arena works is it allocates the big block of memory and starts allocations at
// begin + sizeof(Arena).
// * We want this begin to be aligned to 16 bytes so common things like an array (which is always
// aligned to 16 bytes here) can be allocated at the start of the arena.
// * Arena happens to be 4 pointers so this works well, static assert here to catch if that changes.
_Static_assert(sizeof(Arena) % 16 == 0, "Arena size must be a multiple of 16 bytes");
Arena *arena_new(void);
void arena_reset(Arena *arena);
void arena_free(Arena *arena);
_Thread_local static Arena *scratch__arena = NULL;
// Get a reference to the scratch arena.
// There is a single scratch arena per thread.
// The scratch arena keeps track of how many references there are to it.
// It is only reset after all the references have released it.
// This makes it's lifetime the lifetime of the outermost use of it.
// Depending on the program, this can be bad if you are using it in a long-lived context for a lot
// of temp memory. Consider manually creating and freeing an arena in that case.
Arena *scratch_acquire(void);
// Release reference to the scratch arena. Will be reset if there are no other references.
void scratch_release(void);
// Manually free all scratch memory.
// Useful if you know the allocation is big and nobody has a reference.
void scratch_free(void);
#define arena_alloc(arena, type) (type *)arena_alloc_size(arena, sizeof(type), _Alignof(type))
uint8_t *arena_alloc_size(Arena *arena, size_t size, ptrdiff_t align);
// Get the size of the arena (not including the arena struct itself).
size_t arena_size(Arena *arena);
// You can copy and restore all the data in an area by copying the memory buffer.
// This can be very powerful for cloning arbitrary data structures.
// If you have a data structure you want to clone, and it only uses relative pointers, then you
// can dedicate an arena to it and use arena_serialize and arena_deserialize to clone it.
// Buf must have space for arena_size(arena) bytes.
void arena_serialize(void *buf, Arena *arena);
void arena_deserialize(Arena *arena, void *buf, size_t size);
// Note that dest comes first which matches the order arena_serialize and memcpy use.
void arena_clone(Arena *dest, Arena *src);
// * It's nice to use relative pointers (offsets from the beginning of the arena) in data structures
// on an arena.
// * If you store offsets instead of pointers in all the data structures in an arena, and they all
// only point to other things in the arena, you should use relative pointers if you want to take
// advantage of the arena_serialize and arena_deserialize.
// * Since the type of the offset is always ptrdiff_t you can't know what this is pointing at from
// the type.
// * This is annoying, you have to know what it's pointing at from the context.
// @todo(steve): Is there a better way to do this in c that doesn't suck?
#define rel(arena, ptr) ((ptrdiff_t)(ptr) - (ptrdiff_t)(arena)->begin)
#define ptr(arena, off) (void *)((ptrdiff_t)(arena)->begin + (off))
// Slice
// * A slice is a pointer and a length. It's a view into an array.
// * It's a polymorphic type so you typically typedef it to specific types.
// eg: typedef Slice(uint8_t) uint8_tSlice;
#define Slice(T) \
struct { \
uint64_t len; \
T *e; \
}
typedef Slice(uint8_t) U8Slice;
// * If the slice is a view into data on an arena, and it's stored in a data structure in the arena,
// it's better to store a relative slice so the arena can be serialized and deserialized.
// * Since the type of the offset is always ptrdiff_t you can't know what this is pointing at from
// the type.
// This is annoying, you have to know what it's pointing at from the context.
// @todo(steve): Is there a better way to do this in c that doesn't suck?
typedef struct {
uint64_t len;
ptrdiff_t e;
} RelSlice;
#define slice_rel(arena, ptr_slice) {.len = (ptr_slice).len, .e = rel(arena, (ptr_slice).e)}
#define slice_ptr(arena, rel_slice) {.len = (rel_slice).len, .e = ptr(arena, (rel_slice).e)}
#define arena_clone_slice(arena, slice) \
{.len = (slice).len, \
.e = (__typeof__((slice).e))arena__clone_slice(arena, (U8Slice *)&(slice), \
sizeof(*((slice).e)))}
uint8_t *arena__clone_slice(Arena *arena, U8Slice *slice, size_t item_size);
// Array
// * A dynamic array that grows as needed.
// It's backed by an arena so you don't have to free it.
// * An array can be resized with helpers like append or setlen.
// If the new size is > the capacity it will reallocate the data (or extend the data
// allocation if the array is at the end of the arena).
// * There is no internal pointer in the array struct, so it works when referenced by other data
// structures
// on the arena using relative pointers.
// (of course if it reallocs it will move so you'll have to update the reference).
// * It's a polymorphic type so you typically typedef it to specific types.
// eg: typedef Array(uint8_t) uint8_tArray;
// * Since the type is polymorphic, most of the helper functions are macros.
#define Array(T) \
struct { \
uint64_t len; \
uint64_t cap; \
T e[]; \
}
typedef Array(uint8_t) U8Array;
#define arena_alloc_array(arena, type, item_type, cap) \
(type *)arena__alloc_array(arena, sizeof(item_type), cap)
U8Array *arena__alloc_array(Arena *arena, size_t item_size, uint64_t cap);
U8Array *arena__grow_array(Arena *arena, U8Array *array, size_t item_size, uint64_t amount);
#define arr_push(arena, array, val) \
arr__maybegrow(arena, array, 1); \
(array)->e[(array)->len++] = (val)
#define arr_push_array(arena, array, val) \
arr__maybegrow(arena, array, (val)->len); \
(xmemcpy(&(array)->e[(array)->len], (val)->e, (val)->len * sizeof((val)->e[0])), \
(array)->len += (val)->len)
#define arr_push_slice(arena, array, slice) \
arr__maybegrow(arena, array, (slice).len); \
(xmemcpy(&(array)->e[(array)->len], (slice).e, (slice).len * sizeof((slice).e[0])), \
(array)->len += (slice).len)
#define arr_setlen(arena, array, n) \
assert(n > 0); \
arr__maybegrow(arena, array, !(array) ? (n) : (n) - (array)->len); \
(array)->len = (n)
#define arr_slice(array) {.len = (array)->len, .e = (array)->e}
// This isn't strictly necessary for the array or arena arguments.
// * For array, you wouldn't push to the result of a function call,
// That would just throw away the result.
// eg: arr_push(arena, get_array(), 1);
// * For arena, it'd be bad to pass scratch_acquire() or arena_new() because you'd lose the arena
// reference and leak the whole thing.
// eg: arr_push(arena_new(), a, 1);
// * It is however important for the n argument in case it's something like ++i
// In that case we don't want to evaluate it twice.
#define arr__maybegrow(arena, array, n) \
do { \
Arena *_arena = (arena); \
__typeof__(array) _array = (array); \
size_t _n = (n); \
if (!_array || _array->len + _n > _array->cap) { \
_array = (__typeof__(_array))arena__grow_array(_arena, (U8Array *)_array, \
sizeof(_array->e[0]), _n); \
(array) = _array; \
} \
} while (0)
#define arena_clone_arr(arena, array) \
(__typeof__(array))arena__clone_arr(arena, (U8Array *)(array), sizeof((array)->e[0]))
U8Array *arena__clone_arr(Arena *arena, U8Array *array, size_t item_size);
// Notes on Array vs Slice
// * An array contains the array data, so it's a buffer with metadata. The buffer is almost always
// in the arena so you typically have pointers to arrays.
// * A slice is a pointer with some metadata. That makes it a fancy pointer so you usually use it as
// a value rather than a pointer to a slice.
// * Many times you'd want to pass an array to a function that takes a slice. Use the slice macro to
// do that.
// * You can't alter slices, so these functions treat them as const.
// * There's no slice -> array converter
// * There's no guarantees about where the data the slice points to is stored so you don't want
// to modify it.
// * If you want to pass an array and use it as an array, just pass an array pointer.
// * If you want to create an array from a slice (by copying the data), you can use
// arena_push_slice.
// A String is a slice of characters.
// * It's length doesn't include a null terminator and there's no guarantee that there is a null
// terminator.
// * When it's a view into another string like in a parser or something, there definitely won't be
// one.
// * If it's allocated in an arena with one of the functions here, there usually is a null
// terminator, because it's easier to inspect in the debugger. But this is not a property you can
// rely on in code!
// * To pass to stdlib and other functions that expect a null terminated string, you can use the
// cstr macro.
// * It will allocate a new buffer in the arena with a null terminator.
// @opt(steve): Don't allocate if the String already has a null terminator.
// * To get a String view of a c string, use the str macro.
typedef Slice(uint8_t) String;
#define str(c_string) ((String){.len = strlen(c_string), .e = (uint8_t *)c_string})
#define cstr(a, s) arena__alloc_cstring((a), (s))
typedef Array(String) StringArray;
typedef Slice(String) StringSlice;
String str_format(Arena *a, const char *fmt, ...);
StringSlice str_split(Arena *a, String s, char sep);
// @todo(steve): hashmaps.
// probably gonna base it on this either
// * jon blow's hashmap: https://gist.github.com/saolsen/25e22c9ec7445acf1a60d484ea357355
// * chris wellon's hashmap: https://nullprogram.com/blog/2023/09/30/
// @todo(steve): interned strings.
// @todo(steve): pool.
#endif // STEVE_H
#ifdef STEVE_IMPLEMENTATION
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN64)
// Windows
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// @note(steve): Must include Windows.h first.
#include <Memoryapi.h>
static size_t memory__page_size(void) {
SYSTEM_INFO sys_info;
GetSystemInfo(&sys_info);
return sys_info.dwPageSize;
}
static uint8_t *memory__reserve(size_t size) {
void *r = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_READWRITE);
if (r == NULL) {
// @todo(steve): Call GetLastError to get the error message.
perror("memory__reserve");
exit(1);
}
return r;
}
static void memory__commit(uint8_t *addr, size_t size) {
// addr should be the start of a page and size should be a multiple of the page size.
if (VirtualAlloc(addr, size, MEM_COMMIT, PAGE_READWRITE) == 0) {
perror("memory__commit");
exit(1);
}
}
static void memory__free(uint8_t *addr) {
if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
perror("memory__free");
exit(1);
}
}
#else
// Posix
#include <sys/mman.h>
#include <unistd.h>
static size_t memory__page_size(void) {
return (size_t)sysconf(_SC_PAGE_SIZE);
}
static uint8_t *memory__reserve(size_t size) {
void *addr = mmap(NULL, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (addr == MAP_FAILED) {
perror("memory__reserve");
exit(1);
}
return addr;
}
static void memory__commit(uint8_t *addr, size_t size) {
// addr should be the start of a page and size should be a multiple of the page size.
if (mprotect(addr, size, PROT_READ | PROT_WRITE) == -1) {
perror("memory__commit");
exit(1);
}
}
static void memory__free(uint8_t *addr) {
size_t pagesize = memory__page_size();
if (munmap(addr, pagesize * 4 * 1024 * 1024) == -1) {
perror("munmap");
exit(1);
}
}
#endif
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
#include <sanitizer/asan_interface.h>
#endif
#endif
uint64_t pow2_next(uint64_t i) {
if (i == 0) {
return 0;
}
i--;
i |= i >> 1;
i |= i >> 2;
i |= i >> 4;
i |= i >> 8;
i |= i >> 16;
i |= i >> 32;
i++;
return i;
}
void *xmemcpy(void *dest, const void *src, size_t n) {
return memcpy(dest, src, n);
}
Arena *arena_new(void) {
// Allocate a new arena.
size_t pagesize = memory__page_size(); // 16kb on my machine.
size_t cap =
pagesize * 4 * 1024 * 1024; // 64GB on my machine. @note(steve): probably way too much
uint8_t *addr = memory__reserve(cap);
memory__commit(addr, pagesize); // commit first page
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
ASAN_UNPOISON_MEMORY_REGION(addr, sizeof(Arena));
#endif
#endif
Arena *arena = (Arena *)addr;
arena->begin = addr;
arena->pos = addr + sizeof(*arena);
arena->commit = addr + pagesize;
arena->refcount = 0;
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
ASAN_POISON_MEMORY_REGION(arena->pos, arena->commit - arena->pos);
#endif
#endif
return arena;
}
void arena_reset(Arena *arena) {
if (arena->refcount > 0) {
perror("arena_reset: Arena is in use by other functions. If this is scratch call "
"scratch_release.");
exit(1);
}
// @todo(steve): On windows, decommit all the pages after the first one.
arena->pos = arena->begin + sizeof(*arena);
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
ASAN_POISON_MEMORY_REGION(arena->pos, arena->commit - arena->pos);
#endif
#endif
}
void arena_free(Arena *arena) {
if (arena->refcount > 0) {
perror("arena_free: Arena is in use by other functions. If this is scratch call "
"scratch_release");
exit(1);
}
arena_reset(arena);
void *addr = arena->begin;
memory__free(addr);
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
ASAN_POISON_MEMORY_REGION(addr, sizeof(Arena));
#endif
#endif
}
Arena *scratch_acquire(void) {
if (scratch__arena == NULL) {
scratch__arena = arena_new();
}
scratch__arena->refcount++;
return scratch__arena;
}
void scratch_release(void) {
if (scratch__arena == NULL) {
perror("scratch_release: No scratch arena to release.");
exit(1);
}
scratch__arena->refcount--;
if (scratch__arena->refcount == 0) {
arena_reset(scratch__arena);
}
}
void scratch_free(void) {
if (scratch__arena == NULL) {
return;
}
if (scratch__arena->refcount > 0) {
perror("scratch_free: Scratch arena is in use by other functions.");
exit(1);
}
arena_free(scratch__arena);
scratch__arena = NULL;
}
uint8_t *arena_alloc_size(Arena *arena, size_t size, ptrdiff_t align) {
// Align Pointer
uint8_t *start = (uint8_t *)ALIGN_UP_PTR(arena->pos, align);
uint8_t *new_pos = start + size;
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
ASAN_UNPOISON_MEMORY_REGION(start, new_pos - start);
#endif
#endif
// Commit new page if needed.
if (new_pos > arena->commit) {
uint8_t *new_commit = arena->commit;
size_t pagesize = memory__page_size();
while (new_pos > new_commit) {
new_commit += pagesize;
}
memory__commit(arena->commit, (size_t)(new_commit - arena->commit));
arena->commit = new_commit;
}
arena->pos = new_pos;
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
ASAN_POISON_MEMORY_REGION(arena->pos, arena->commit - arena->pos);
#endif
#endif
return start;
}
// The size of the data in the arena. This is the size of buffer you would need to call
// arena_serialize.
size_t arena_size(Arena *arena) {
ptrdiff_t buf_span = arena->pos - arena->begin;
assert((size_t)buf_span >= sizeof(Arena));
return (size_t)buf_span - sizeof(Arena);
}
void arena_serialize(void *buf, Arena *arena) {
if (arena->refcount > 0) {
perror(
"arena_serialize: Arena is in use by other functions. You cannot serialize scratch.");
exit(1);
}
xmemcpy(buf, arena->begin + sizeof(Arena), arena_size(arena));
}
// Note: This only works on an empty arena, if the arena passed in is not empty it will get reset.
// @todo(steve): Return an error or something instead of just resetting the arena.
void arena_deserialize(Arena *arena, void *buf, size_t size) {
if (arena_size(arena) > 0) {
perror("arena_deserialize: Arena is not empty. You must pass in an empty arena.");
exit(1);
}
uint8_t *data = arena_alloc_size(arena, size, 1);
xmemcpy(data, buf, size);
}
void arena_clone(Arena *dest, Arena *src) {
size_t size = arena_size(src);
arena_deserialize(dest, src->begin + sizeof(Arena), size);
}
uint8_t *arena__clone_slice(Arena *arena, U8Slice *slice, size_t item_size) {
uint8_t *buf = arena_alloc_size(arena, slice->len * item_size, 16);
xmemcpy(buf, slice->e, slice->len * item_size);
return buf;
}
U8Array *arena__alloc_array(Arena *arena, size_t item_size, uint64_t cap) {
U8Array *array = (U8Array *)arena_alloc_size(arena, sizeof(U8Array) + (item_size * cap), 16);
array->len = 0;
array->cap = cap;
return array;
}
U8Array *arena__grow_array(Arena *arena, U8Array *array, size_t item_size, uint64_t amount) {
if (array == NULL) {
return arena__alloc_array(arena, item_size, MAX(4, amount));
}
size_t new_cap = MAX(array->cap, 4);
while (MAX(array->cap, array->len) + amount > new_cap) {
new_cap *= 2;
}
if (new_cap > array->cap) {
// Grow the array
if (arena->pos == (uint8_t *)(array->e) + array->cap * item_size) {
// Array is on the end of the arena, we can just grow it.
arena_alloc_size(arena, (new_cap - array->cap) * item_size, 1);
array->cap = new_cap;
return array;
} else {
// Array is not on the end of the arena, we need a new allocation.
U8Array *new_array = arena__alloc_array(arena, item_size, new_cap);
new_array->len = array->len;
xmemcpy(new_array->e, array->e, new_array->len * item_size);
// Mark the old array as dead. This helps catch reads and writes from a non updated list
// pointer. There is one issue though which is that if you use serialize/deserialize
// on an arena that has a dead array, it will trigger asan.
// I actually like that though, because it means you need to be more explicit about what
// you put in an arena that you're serializing.
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
ASAN_POISON_MEMORY_REGION(array, sizeof(*array) + array->cap * item_size);
#endif
#endif
return new_array;
}
}
return array;
}
U8Array *arena__clone_arr(Arena *arena, U8Array *array, size_t item_size) {
U8Array *new_array = arena__alloc_array(arena, item_size, array->len);
new_array->len = array->len;
xmemcpy(new_array->e, array->e, new_array->len * item_size);
return new_array;
}
char *arena__alloc_cstring(Arena *a, String *s) {
char *c = (char *)arena_alloc_size(a, s->len + 1, _Alignof(char));
xmemcpy(c, s->e, s->len);
c[s->len] = '\0';
return c;
}
String str_format(Arena *a, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
va_list args_copy;
va_copy(args_copy, args);
size_t len = (size_t)vsnprintf(NULL, 0, fmt, args_copy);
va_end(args_copy);
char *c = (char *)arena_alloc_size(a, len + 1, _Alignof(char));
vsnprintf(c, len + 1, fmt, args);
va_end(args);
return (String){.len = len, .e = (uint8_t *)c};
}
StringSlice str_split(Arena *a, String s, char sep) {
// Skip leading separators.
uint32_t i = 0;
StringArray *parts = NULL;
while (i < s.len) {
uint32_t start = i;
while (i < s.len && s.e[i] != sep) {
i++;
}
if (i > start) {
String part = {.len = i - start, .e = s.e + start};
arr_push(a, parts, part);
}
while (i < s.len && s.e[i] == sep) {
i++;
}
}
if (parts == NULL) {
return (StringSlice){.len = 0, .e = NULL};
}
StringSlice result = arr_slice(parts);
return result;
}
#endif // STEVE_IMPLEMENTATION
#ifdef STEVE_TEST
static void test_helpers(void);
static void test_arena_acquire_release(void);
static void test_arena_reset(void);
static void test_arena_free(void);
static void test_arena_alloc_alignment(void);
static void test_arena_large_alloc(void);
static void test_rel_ptr(void);
static void test_slices(void);
static void test_rel_slices(void);
static void test_dynamic_arrays(void);
static void test_arena_serialize(void);
static void test_strings(void);
int main(void) {
test_helpers();
test_arena_acquire_release();
test_arena_reset();
test_arena_free();
test_arena_alloc_alignment();
test_arena_large_alloc();
test_rel_ptr();
test_slices();
test_rel_slices();
test_dynamic_arrays();
test_arena_serialize();
test_strings();
printf("steve.h: All tests passed!\n");
return 0;
}
static void test_helpers(void) {
assert(pow2_next(0) == 0);
assert(pow2_next(1) == 1);
assert(pow2_next(2) == 2);
assert(pow2_next(3) == 4);
assert(pow2_next(4) == 4);
assert(pow2_next(5) == 8);
assert(pow2_next(7) == 8);
assert(pow2_next(8) == 8);
assert(pow2_next(9) == 16);
assert(pow2_next(63) == 64);
assert(pow2_next(64) == 64);
assert(pow2_next(65) == 128);
assert(MIN(10, 20) == 10);
assert(MIN(-5, 3) == -5);
assert(MAX(10, 20) == 20);
assert(MAX(-5, 3) == 3);
assert(CLAMP_MAX(25, 20) == 20); // 25 clamped to 20
assert(CLAMP_MAX(15, 20) == 15); // within limit
assert(CLAMP_MIN(-1, 0) == 0); // -1 clamped to 0
assert(CLAMP_MIN(10, 0) == 10); // within limit
assert(IS_POW2(1) == true);
assert(IS_POW2(2) == true);
assert(IS_POW2(3) == false);
assert(IS_POW2(4) == true);
assert(IS_POW2(0) == false); // Edge case: 0 is not a power of 2
assert(ALIGN_UP(0, 8) == 0);
assert(ALIGN_UP(1, 8) == 8);
assert(ALIGN_UP(7, 8) == 8);
assert(ALIGN_UP(8, 8) == 8);
assert(ALIGN_UP(9, 8) == 16);
assert(ALIGN_DOWN(0, 8) == 0);
assert(ALIGN_DOWN(7, 8) == 0);
assert(ALIGN_DOWN(8, 8) == 8);
assert(ALIGN_DOWN(9, 8) == 8);
assert(ALIGN_DOWN(15, 8) == 8);
{
ptrdiff_t ptr_val = 15;
void *p1 = (void *)ptr_val;
void *aligned_up = ALIGN_UP_PTR(p1, 8);
void *aligned_down = ALIGN_DOWN_PTR(p1, 8);
assert((ptrdiff_t)aligned_up == 16);
assert((ptrdiff_t)aligned_down == 8);
}
}
static void test_arena_acquire_release(void) {
// Acquire a single arena and reset it
Arena *a = arena_new();
assert(a != NULL);
arena_reset(a);
// Acquire multiple arenas
Arena *scratch = scratch_acquire();
assert(scratch != NULL);
scratch_release();
// Acquire again to ensure same arena.
Arena *scratch_2 = scratch_acquire();
Arena *scratch_3 = scratch_acquire();
assert(scratch_2 == scratch_3);
assert(scratch_2->refcount == 2);
scratch_release();
assert(scratch_2->refcount == 1);
scratch_release();
assert(scratch__arena->refcount == 0);
scratch_free();
}
static void test_arena_reset(void) {
Arena *a = arena_new();
// Allocate some memory
int *x = arena_alloc(a, int);
*x = 42;
assert(*x == 42);
// Reset
arena_reset(a);
assert(arena_size(a) == 0);
// Allocate again after reset
int *y = arena_alloc(a, int);
*y = 2025;
assert(*y == 2025);
// Pointers should be to the same memory.
assert(y == x);
arena_free(a);
}
static void test_arena_free(void) {
Arena *a = arena_new();
int *x = arena_alloc(a, int);
*x = 123;
arena_free(a);
}
static void test_arena_alloc_alignment(void) {
Arena *a = arena_new();
// Test alignment for 1, 2, 4, 16
ptrdiff_t alignments[] = {1, 2, 4, 16};
for (int i = 0; i < 4; i++) {
ptrdiff_t align = alignments[i];
uint8_t *p = arena_alloc_size(a, 10, align);
assert(((ptrdiff_t)p % align) == 0 && "Pointer must be aligned");
}
// Cross page boundary test
size_t pagesize = memory__page_size();
(void)arena_alloc_size(a, pagesize + 1, 16);
arena_free(a);
}
static void test_arena_large_alloc(void) {
Arena *a = arena_new();
size_t pagesize = memory__page_size();
size_t big_size = pagesize * 5;
void *big_block = arena_alloc_size(a, big_size, 16);
assert(big_block != NULL);
// Mke sure we can write to it.
memset(big_block, 0xAB, big_size);
arena_free(a);
}
static void test_rel_ptr(void) {
Arena *a = arena_new();
int *x = arena_alloc(a, int);
*x = 55;
ptrdiff_t offset = rel(a, x);
int *y = (int *)ptr(a, offset);
assert(x == y);
assert(*y == 55);
arena_free(a);
}
static void test_slices(void) {
Arena *a = arena_new();
// Create an array
typedef Array(int32_t) I32Array;
I32Array *arr = NULL;
for (int32_t i = 0; i < 5; i++) {
arr_push(a, arr, i * 10);
}
// Turn into slice
typedef Slice(int32_t) I32Slice;
I32Slice s = arr_slice(arr);
assert(s.len == 5);
for (int32_t i = 0; i < 5; i++) {
assert(s.e[i] == i * 10);
}
// Clone slice
Arena *a2 = arena_new();
I32Slice clone = arena_clone_slice(a2, s);
assert(clone.len == s.len);
for (int32_t i = 0; i < 5; i++) {
assert(clone.e[i] == s.e[i]);
}
// Mutate original
s.e[0] = 999;
// Cloned slice remains unaffected
assert(clone.e[0] == 0);
arena_free(a);
arena_free(a2);
}
static void test_rel_slices(void) {
Arena *a = arena_new();
// Create an array
typedef Array(int32_t) I32Array;
I32Array *arr = NULL;
for (int32_t i = 0; i < 5; i++) {
arr_push(a, arr, i * 10);
}
// Turn into slice
typedef Slice(int32_t) I32Slice;
I32Slice s = arr_slice(arr);
assert(s.len == 5);
for (int32_t i = 0; i < 5; i++) {
assert(s.e[i] == i * 10);
}
// Turn into relative slice
RelSlice rs = slice_rel(a, s);
assert(rs.len == 5);
// Turn back into slice
I32Slice s2 = slice_ptr(a, rs);
assert(s2.len == 5);
for (int32_t i = 0; i < 5; i++) {
assert(s2.e[i] == i * 10);
}
arena_free(a);
}