-
Notifications
You must be signed in to change notification settings - Fork 21
/
md.c
4450 lines (3998 loc) · 134 KB
/
md.c
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
// LICENSE AT END OF FILE (MIT).
/*
** Overrides & Options Macros
**
** Overridable
** "basic types" ** REQUIRED
** #define/typedef MD_i8, MD_i16, MD_i32, MD_i64
** #define/typedef MD_u8, MD_u16, MD_u32, MD_u64
** #define/typedef MD_f32, MD_f64
**
** "memset" ** REQUIRED
** #define MD_IMPL_Memset (void*, int, uint64) -> void*
** #define MD_IMPL_Memmove (void*, void*, uint64) -> void*
**
** "file iteration" ** OPTIONAL (required for the metadesk FileIter helpers to work)
** #define MD_IMPL_FileIterBegin (MD_FileIter*, MD_String8) -> Boolean
** #define MD_IMPL_FileIterNext (MD_Arena*, MD_FileIter*) -> MD_FileInfo
** #define MD_IMPL_FileIterEnd (MD_FileIter*) -> void
**
** "file load" ** OPTIONAL (required for MD_ParseWholeFile to work)
** #define MD_IMPL_LoadEntireFile (MD_Arena*, MD_String8 filename) -> MD_String8
**
** "low level memory" ** OPTIONAL (required when relying on the default arenas)
** #define MD_IMPL_Reserve (uint64) -> void*
** #define MD_IMPL_Commit (void*, uint64) -> MD_b32
** #define MD_IMPL_Decommit (void*, uint64) -> void
** #define MD_IMPL_Release (void*, uint64) -> void
**
**
** "arena" ** REQUIRED
** #define MD_IMPL_Arena <type> (must set before including md.h)
** #define MD_IMPL_ArenaMinPos uint64
** #define MD_IMPL_ArenaAlloc () -> MD_IMPL_Arena*
** #define MD_IMPL_ArenaRelease (MD_IMPL_Arena*) -> void
** #define MD_IMPL_ArenaGetPos (MD_IMPL_Arena*) -> uint64
** #define MD_IMPL_ArenaPush (MD_IMPL_Arena*, uint64) -> void*
** #define MD_IMPL_ArenaPopTo (MD_IMPL_Arena*, uint64) -> void
** #define MD_IMPL_ArenaSetAutoAlign (MD_IMPL_Arena*, uint64) -> void
**
** "scratch" ** REQUIRED
** #define MD_IMPL_GetScratch (MD_IMPL_Arena**, uint64) -> MD_IMPL_Arena*
** "scratch constants" ** OPTIONAL (required for default scratch)
** #define MD_IMPL_ScratchCount uint64 [default 2]
**
** "sprintf" ** REQUIRED
** #define MD_IMPL_Vsnprintf (char*, uint64, char const*, va_list) -> uint64
**
** Static Parameters to the Default Arena Implementation
** #define MD_DEFAULT_ARENA_RES_SIZE uint64 [default 64 megabytes]
** #define MD_DEFAULT_ARENA_CMT_SIZE uint64 [default 64 kilabytes]
**
** Default Implementation Controls
** These controls default to '1' i.e. 'enabled'
** #define MD_DEFAULT_BASIC_TYPES -> construct "basic types" from stdint.h header
** #define MD_DEFAULT_MEMSET -> construct "memset" from CRT
** #define MD_DEFAULT_FILE_ITER -> construct "file iteration" from OS headers
** #define MD_DEFAULT_MEMORY -> construct "low level memory" from OS headers
** #define MD_DEFAULT_ARENA -> construct "arena" from "low level memory"
** #define MD_DEFAULT_SCRATCH -> construct "scratch" from "arena"
** #define MD_DEFAULT_SPRINTF -> construct "vsnprintf" from internal implementaion
**
*/
#if !defined(MD_C)
#define MD_C
//~/////////////////////////////////////////////////////////////////////////////
/////////////////////////// CRT Implementation /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#if MD_DEFAULT_MEMSET
#include <stdlib.h>
#include <string.h>
#if !defined(MD_IMPL_Memset)
# define MD_IMPL_Memset MD_CRT_Memset
#endif
#if !defined(MD_IMPL_Memmove)
# define MD_IMPL_Memmove MD_CRT_Memmove
#endif
#define MD_CRT_Memset memset
#define MD_CRT_Memmove memmove
#endif
#if MD_DEFAULT_FILE_LOAD
#include <stdio.h>
#if !defined(MD_IMPL_LoadEntireFile)
# define MD_IMPL_LoadEntireFile MD_CRT_LoadEntireFile
#endif
MD_FUNCTION MD_String8
MD_CRT_LoadEntireFile(MD_Arena *arena, MD_String8 filename)
{
MD_ArenaTemp scratch = MD_GetScratch(&arena, 1);
MD_String8 file_contents = MD_ZERO_STRUCT;
MD_String8 filename_copy = MD_S8Copy(scratch.arena, filename);
FILE *file = fopen((char*)filename_copy.str, "rb");
if(file != 0)
{
fseek(file, 0, SEEK_END);
MD_u64 file_size = ftell(file);
fseek(file, 0, SEEK_SET);
file_contents.str = MD_PushArray(arena, MD_u8, file_size+1);
if(file_contents.str)
{
file_contents.size = file_size;
fread(file_contents.str, 1, file_size, file);
file_contents.str[file_contents.size] = 0;
}
fclose(file);
}
MD_ReleaseScratch(scratch);
return file_contents;
}
#endif
//~/////////////////////////////////////////////////////////////////////////////
/////////////////////////// Win32 Implementation ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//- win32 header
#if (MD_DEFAULT_FILE_ITER || MD_2DEFAULT_MEMORY) && MD_OS_WINDOWS
# include <Windows.h>
# pragma comment(lib, "User32.lib")
#endif
//- win32 "file iteration"
#if MD_DEFAULT_FILE_ITER && MD_OS_WINDOWS
#if !defined(MD_IMPL_FileIterBegin)
# define MD_IMPL_FileIterBegin MD_WIN32_FileIterBegin
#endif
#if !defined(MD_IMPL_FileIterNext)
# define MD_IMPL_FileIterNext MD_WIN32_FileIterNext
#endif
#if !defined(MD_IMPL_FileIterEnd)
# define MD_IMPL_FileIterEnd MD_WIN32_FileIterEnd
#endif
typedef struct MD_WIN32_FileIter{
HANDLE state;
MD_u64 first;
WIN32_FIND_DATAW find_data;
} MD_WIN32_FileIter;
MD_StaticAssert(sizeof(MD_FileIter) >= sizeof(MD_WIN32_FileIter), file_iter_size_check);
static MD_b32
MD_WIN32_FileIterBegin(MD_FileIter *it, MD_String8 path)
{
//- init search
MD_ArenaTemp scratch = MD_GetScratch(0, 0);
MD_u8 c = path.str[path.size - 1];
MD_b32 need_star = (c == '/' || c == '\\');
MD_String8 cpath = need_star ? MD_S8Fmt(scratch.arena, "%.*s*", MD_S8VArg(path)) : path;
MD_String16 cpath16 = MD_S16FromS8(scratch.arena, cpath);
WIN32_FIND_DATAW find_data = MD_ZERO_STRUCT;
HANDLE state = FindFirstFileW((WCHAR*)cpath16.str, &find_data);
MD_ReleaseScratch(scratch);
//- fill results
MD_b32 result = !!state;
if (result)
{
MD_WIN32_FileIter *win32_it = (MD_WIN32_FileIter*)it;
win32_it->state = state;
win32_it->first = 1;
MD_MemoryCopy(&win32_it->find_data, &find_data, sizeof(find_data));
}
return(result);
}
static MD_FileInfo
MD_WIN32_FileIterNext(MD_Arena *arena, MD_FileIter *it)
{
//- get low-level file info for this step
MD_b32 good = 0;
MD_WIN32_FileIter *win32_it = (MD_WIN32_FileIter*)it;
WIN32_FIND_DATAW *find_data = &win32_it->find_data;
if (win32_it->first)
{
win32_it->first = 0;
good = 1;
}
else
{
good = FindNextFileW(win32_it->state, find_data);
}
//- convert to MD_FileInfo
MD_FileInfo result = {0};
if (good)
{
if (find_data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
result.flags |= MD_FileFlag_Directory;
}
MD_u16 *filename_base = (MD_u16*)find_data->cFileName;
MD_u16 *ptr = filename_base;
for (;*ptr != 0; ptr += 1);
MD_String16 filename16 = {0};
filename16.str = filename_base;
filename16.size = (MD_u64)(ptr - filename_base);
result.filename = MD_S8FromS16(arena, filename16);
result.file_size = ((((MD_u64)find_data->nFileSizeHigh) << 32) |
((MD_u64)find_data->nFileSizeLow));
}
return(result);
}
static void
MD_WIN32_FileIterEnd(MD_FileIter *it)
{
MD_WIN32_FileIter *win32_it = (MD_WIN32_FileIter*)it;
FindClose(win32_it->state);
}
#endif
//- win32 "low level memory"
#if MD_DEFAULT_MEMORY && MD_OS_WINDOWS
#if !defined(MD_IMPL_Reserve)
# define MD_IMPL_Reserve MD_WIN32_Reserve
#endif
#if !defined(MD_IMPL_Commit)
# define MD_IMPL_Commit MD_WIN32_Commit
#endif
#if !defined(MD_IMPL_Decommit)
# define MD_IMPL_Decommit MD_WIN32_Decommit
#endif
#if !defined(MD_IMPL_Release)
# define MD_IMPL_Release MD_WIN32_Release
#endif
static void*
MD_WIN32_Reserve(MD_u64 size)
{
void *result = VirtualAlloc(0, size, MEM_RESERVE, PAGE_READWRITE);
return(result);
}
static MD_b32
MD_WIN32_Commit(void *ptr, MD_u64 size)
{
MD_b32 result = (VirtualAlloc(ptr, size, MEM_COMMIT, PAGE_READWRITE) != 0);
return(result);
}
static void
MD_WIN32_Decommit(void *ptr, MD_u64 size)
{
VirtualFree(ptr, size, MEM_DECOMMIT);
}
static void
MD_WIN32_Release(void *ptr, MD_u64 size)
{
(void)size;
VirtualFree(ptr, 0, MEM_RELEASE);
}
#endif
//~/////////////////////////////////////////////////////////////////////////////
////////////////////////// Linux Implementation ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//- linux headers
#if (MD_DEFAULT_FILE_ITER || MD_DEFAULT_MEMORY) && (MD_OS_LINUX || MD_OS_MAC)
# include <dirent.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include <sys/syscall.h>
# include <sys/mman.h>
// NOTE(mal): To get these constants I need to #define _GNU_SOURCE,
// which invites non-POSIX behavior I'd rather avoid
# ifndef O_PATH
# define O_PATH 010000000
# endif
# ifndef AT_NO_AUTOMOUNT
# define AT_NO_AUTOMOUNT 0x800
# endif
# ifndef AT_SYMLINK_NOFOLLOW
# define AT_SYMLINK_NOFOLLOW 0x100
# endif
#endif
//- linux "file iteration"
#if MD_DEFAULT_FILE_ITER && MD_OS_LINUX
#if !defined(MD_IMPL_FileIterIncrement)
# define MD_IMPL_FileIterIncrement MD_LINUX_FileIterIncrement
#endif
typedef struct MD_LINUX_FileIter MD_LINUX_FileIter;
struct MD_LINUX_FileIter
{
int dir_fd;
DIR *dir;
};
MD_StaticAssert(sizeof(MD_LINUX_FileIter) <= sizeof(MD_FileIter), file_iter_size_check);
static MD_b32
MD_LINUX_FileIterIncrement(MD_Arena *arena, MD_FileIter *opaque_it, MD_String8 path,
MD_FileInfo *out_info)
{
MD_b32 result = 0;
MD_LINUX_FileIter *it = (MD_LINUX_FileIter *)opaque_it;
if(it->dir == 0)
{
it->dir = opendir((char*)path.str);
it->dir_fd = open((char *)path.str, O_PATH|O_CLOEXEC);
}
if(it->dir != 0 && it->dir_fd != -1)
{
struct dirent *dir_entry = readdir(it->dir);
if(dir_entry)
{
out_info->filename = MD_S8Fmt(arena, "%s", dir_entry->d_name);
out_info->flags = 0;
struct stat st;
if(fstatat(it->dir_fd, dir_entry->d_name, &st, AT_NO_AUTOMOUNT|AT_SYMLINK_NOFOLLOW) == 0)
{
if((st.st_mode & S_IFMT) == S_IFDIR)
{
out_info->flags |= MD_FileFlag_Directory;
}
out_info->file_size = st.st_size;
}
result = 1;
}
}
if(result == 0)
{
if(it->dir != 0)
{
closedir(it->dir);
it->dir = 0;
}
if(it->dir_fd != -1)
{
close(it->dir_fd);
it->dir_fd = -1;
}
}
return result;
}
#endif
//- linux "low level memory"
#if MD_DEFAULT_MEMORY && (MD_OS_LINUX || MD_OS_MAC)
#if !defined(MD_IMPL_Reserve)
# define MD_IMPL_Reserve MD_LINUX_Reserve
#endif
#if !defined(MD_IMPL_Commit)
# define MD_IMPL_Commit MD_LINUX_Commit
#endif
#if !defined(MD_IMPL_Decommit)
# define MD_IMPL_Decommit MD_LINUX_Decommit
#endif
#if !defined(MD_IMPL_Release)
# define MD_IMPL_Release MD_LINUX_Release
#endif
static void*
MD_LINUX_Reserve(MD_u64 size)
{
void *result = mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, (off_t)0);
if(result == (void *)-1)
{
result = 0;
}
return(result);
}
static MD_b32
MD_LINUX_Commit(void *ptr, MD_u64 size)
{
MD_b32 result = (mprotect(ptr, size, PROT_READ|PROT_WRITE) == 0);
return(result);
}
static void
MD_LINUX_Decommit(void *ptr, MD_u64 size)
{
mprotect(ptr, size, PROT_NONE);
madvise(ptr, size, MADV_DONTNEED);
}
static void
MD_LINUX_Release(void *ptr, MD_u64 size)
{
munmap(ptr, size);
}
#endif
//~/////////////////////////////////////////////////////////////////////////////
///////////// MD Arena From Reserve/Commit/Decommit/Release ////////////////////
////////////////////////////////////////////////////////////////////////////////
#if MD_DEFAULT_ARENA
#if !defined(MD_DEFAULT_ARENA_RES_SIZE)
# define MD_DEFAULT_ARENA_RES_SIZE (64 << 20)
#endif
#if !defined(MD_DEFAULT_ARENA_CMT_SIZE)
# define MD_DEFAULT_ARENA_CMT_SIZE (64 << 10)
#endif
#define MD_DEFAULT_ARENA_VERY_BIG (MD_DEFAULT_ARENA_RES_SIZE - MD_IMPL_ArenaMinPos)/2
//- "low level memory" implementation check
#if !defined(MD_IMPL_Reserve)
# error Missing implementation for MD_IMPL_Reserve
#endif
#if !defined(MD_IMPL_Commit)
# error Missing implementation for MD_IMPL_Commit
#endif
#if !defined(MD_IMPL_Decommit)
# error Missing implementation for MD_IMPL_Decommit
#endif
#if !defined(MD_IMPL_Release)
# error Missing implementation for MD_IMPL_Release
#endif
#define MD_IMPL_ArenaMinPos 64
MD_StaticAssert(sizeof(MD_ArenaDefault) <= MD_IMPL_ArenaMinPos, arena_def_size_check);
#define MD_IMPL_ArenaAlloc MD_ArenaDefaultAlloc
#define MD_IMPL_ArenaRelease MD_ArenaDefaultRelease
#define MD_IMPL_ArenaGetPos MD_ArenaDefaultGetPos
#define MD_IMPL_ArenaPush MD_ArenaDefaultPush
#define MD_IMPL_ArenaPopTo MD_ArenaDefaultPopTo
#define MD_IMPL_ArenaSetAutoAlign MD_ArenaDefaultSetAutoAlign
static MD_ArenaDefault*
MD_ArenaDefaultAlloc__Size(MD_u64 cmt, MD_u64 res)
{
MD_Assert(MD_IMPL_ArenaMinPos < cmt && cmt <= res);
MD_u64 cmt_clamped = MD_ClampTop(cmt, res);
MD_ArenaDefault *result = 0;
void *mem = MD_IMPL_Reserve(res);
if (MD_IMPL_Commit(mem, cmt_clamped))
{
result = (MD_ArenaDefault*)mem;
result->prev = 0;
result->current = result;
result->base_pos = 0;
result->pos = MD_IMPL_ArenaMinPos;
result->cmt = cmt_clamped;
result->cap = res;
result->align = 8;
}
return(result);
}
static MD_ArenaDefault*
MD_ArenaDefaultAlloc(void)
{
MD_ArenaDefault *result = MD_ArenaDefaultAlloc__Size(MD_DEFAULT_ARENA_CMT_SIZE,
MD_DEFAULT_ARENA_RES_SIZE);
return(result);
}
static void
MD_ArenaDefaultRelease(MD_ArenaDefault *arena)
{
for (MD_ArenaDefault *node = arena->current, *prev = 0;
node != 0;
node = prev)
{
prev = node->prev;
MD_IMPL_Release(node, node->cap);
}
}
static MD_u64
MD_ArenaDefaultGetPos(MD_ArenaDefault *arena)
{
MD_ArenaDefault *current = arena->current;
MD_u64 result = current->base_pos + current->pos;
return(result);
}
static void*
MD_ArenaDefaultPush(MD_ArenaDefault *arena, MD_u64 size)
{
// try to be fast!
MD_ArenaDefault *current = arena->current;
MD_u64 align = arena->align;
MD_u64 pos = current->pos;
MD_u64 pos_aligned = MD_AlignPow2(pos, align);
MD_u64 new_pos = pos_aligned + size;
void *result = (MD_u8*)current + pos_aligned;
current->pos = new_pos;
// if it's not going to work do the slow path
if (new_pos > current->cmt)
{
result = 0;
current->pos = pos;
// new chunk if necessary
if (new_pos > current->cap)
{
MD_ArenaDefault *new_arena = 0;
if (size > MD_DEFAULT_ARENA_VERY_BIG)
{
MD_u64 big_size_unrounded = size + MD_IMPL_ArenaMinPos;
MD_u64 big_size = MD_AlignPow2(big_size_unrounded, (4 << 10));
new_arena = MD_ArenaDefaultAlloc__Size(big_size, big_size);
}
else
{
new_arena = MD_ArenaDefaultAlloc();
}
// link in new chunk & recompute new_pos
if (new_arena != 0)
{
new_arena->base_pos = current->base_pos + current->cap;
new_arena->prev = current;
current = new_arena;
pos_aligned = current->pos;
new_pos = pos_aligned + size;
}
}
// move ahead if the current chunk has enough reserve
if (new_pos <= current->cap)
{
// extend commit if necessary
if (new_pos > current->cmt)
{
MD_u64 new_cmt_unclamped = MD_AlignPow2(new_pos, MD_DEFAULT_ARENA_CMT_SIZE);
MD_u64 new_cmt = MD_ClampTop(new_cmt_unclamped, current->cap);
MD_u64 cmt_size = new_cmt - current->cmt;
if (MD_IMPL_Commit((MD_u8*)current + current->cmt, cmt_size))
{
current->cmt = new_cmt;
}
}
// move ahead if the current chunk has enough commit
if (new_pos <= current->cmt)
{
result = (MD_u8*)current + current->pos;
current->pos = new_pos;
}
}
}
return(result);
}
static void
MD_ArenaDefaultPopTo(MD_ArenaDefault *arena, MD_u64 pos)
{
// pop chunks in the chain
MD_u64 pos_clamped = MD_ClampBot(MD_IMPL_ArenaMinPos, pos);
(void)pos_clamped;
{
MD_ArenaDefault *node = arena->current;
for (MD_ArenaDefault *prev = 0;
node != 0 && node->base_pos >= pos;
node = prev)
{
prev = node->prev;
MD_IMPL_Release(node, node->cap);
}
arena->current = node;
}
// reset the pos of the current
{
MD_ArenaDefault *current = arena->current;
MD_u64 local_pos_unclamped = pos - current->base_pos;
MD_u64 local_pos = MD_ClampBot(local_pos_unclamped, MD_IMPL_ArenaMinPos);
current->pos = local_pos;
}
}
static void
MD_ArenaDefaultSetAutoAlign(MD_ArenaDefault *arena, MD_u64 align)
{
arena->align = align;
}
static void
MD_ArenaDefaultAbsorb(MD_ArenaDefault *arena, MD_ArenaDefault *sub_arena)
{
MD_ArenaDefault *current = arena->current;
MD_u64 base_pos_shift = current->base_pos + current->cap;
for (MD_ArenaDefault *node = sub_arena->current;
node != 0;
node = node->prev)
{
node->base_pos += base_pos_shift;
}
sub_arena->prev = arena->current;
arena->current = sub_arena->current;
}
#endif
//- "arena" implementation checks
#if !defined(MD_IMPL_ArenaAlloc)
# error Missing implementation for MD_IMPL_ArenaAlloc
#endif
#if !defined(MD_IMPL_ArenaRelease)
# error Missing implementation for MD_IMPL_ArenaRelease
#endif
#if !defined(MD_IMPL_ArenaGetPos)
# error Missing implementation for MD_IMPL_ArenaGetPos
#endif
#if !defined(MD_IMPL_ArenaPush)
# error Missing implementation for MD_IMPL_ArenaPush
#endif
#if !defined(MD_IMPL_ArenaPopTo)
# error Missing implementation for MD_IMPL_ArenaPopTo
#endif
#if !defined(MD_IMPL_ArenaSetAutoAlign)
# error Missing implementation for MD_IMPL_ArenaSetAutoAlign
#endif
#if !defined(MD_IMPL_ArenaMinPos)
# error Missing implementation for MD_IMPL_ArenaMinPos
#endif
//~/////////////////////////////////////////////////////////////////////////////
///////////////////////////// MD Scratch Pool //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#if MD_DEFAULT_SCRATCH
#if !defined(MD_IMPL_ScratchCount)
# define MD_IMPL_ScratchCount 2llu
#endif
#if !defined(MD_IMPL_GetScratch)
# define MD_IMPL_GetScratch MD_GetScratchDefault
#endif
MD_THREAD_LOCAL MD_Arena *md_thread_scratch_pool[MD_IMPL_ScratchCount] = {0, 0};
static MD_Arena*
MD_GetScratchDefault(MD_Arena **conflicts, MD_u64 count)
{
MD_Arena **scratch_pool = md_thread_scratch_pool;
if (scratch_pool[0] == 0)
{
MD_Arena **arena_ptr = scratch_pool;
for (MD_u64 i = 0; i < MD_IMPL_ScratchCount; i += 1, arena_ptr += 1)
{
*arena_ptr = MD_ArenaAlloc();
}
}
MD_Arena *result = 0;
MD_Arena **arena_ptr = scratch_pool;
for (MD_u64 i = 0; i < MD_IMPL_ScratchCount; i += 1, arena_ptr += 1)
{
MD_Arena *arena = *arena_ptr;
MD_Arena **conflict_ptr = conflicts;
for (MD_u32 j = 0; j < count; j += 1, conflict_ptr += 1)
{
if (arena == *conflict_ptr)
{
arena = 0;
break;
}
}
if (arena != 0)
{
result = arena;
break;
}
}
return(result);
}
#endif
//~/////////////////////////////////////////////////////////////////////////////
//////////////////////// MD Library Implementation /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#if MD_DEFAULT_SPRINTF
#define STB_SPRINTF_IMPLEMENTATION
#define STB_SPRINTF_DECORATE(name) md_stbsp_##name
#include "md_stb_sprintf.h"
#endif
//~ Nil Node Definition
static MD_Node _md_nil_node =
{
&_md_nil_node, // next
&_md_nil_node, // prev
&_md_nil_node, // parent
&_md_nil_node, // first_child
&_md_nil_node, // last_child
&_md_nil_node, // first_tag
&_md_nil_node, // last_tag
MD_NodeKind_Nil, // kind
0, // flags
MD_ZERO_STRUCT, // string
MD_ZERO_STRUCT, // raw_string
0, // at
&_md_nil_node, // ref_target
MD_ZERO_STRUCT, // prev_comment
MD_ZERO_STRUCT, // next_comment
};
//~ Arena Functions
MD_FUNCTION MD_Arena*
MD_ArenaAlloc(void)
{
return(MD_IMPL_ArenaAlloc());
}
MD_FUNCTION void
MD_ArenaRelease(MD_Arena *arena)
{
MD_IMPL_ArenaRelease(arena);
}
MD_FUNCTION void*
MD_ArenaPush(MD_Arena *arena, MD_u64 size)
{
void *result = MD_IMPL_ArenaPush(arena, size);
return(result);
}
MD_FUNCTION void
MD_ArenaPutBack(MD_Arena *arena, MD_u64 size)
{
MD_u64 pos = MD_IMPL_ArenaGetPos(arena);
MD_u64 new_pos = pos - size;
MD_u64 new_pos_clamped = MD_ClampBot(MD_IMPL_ArenaMinPos, new_pos);
MD_IMPL_ArenaPopTo(arena, new_pos_clamped);
}
MD_FUNCTION void
MD_ArenaSetAlign(MD_Arena *arena, MD_u64 boundary)
{
MD_IMPL_ArenaSetAutoAlign(arena, boundary);
}
MD_FUNCTION void
MD_ArenaPushAlign(MD_Arena *arena, MD_u64 boundary)
{
MD_u64 pos = MD_IMPL_ArenaGetPos(arena);
MD_u64 align_m1 = boundary - 1;
MD_u64 new_pos_aligned = (pos + align_m1)&(~align_m1);
if (new_pos_aligned > pos)
{
MD_u64 amt = new_pos_aligned - pos;
MD_MemoryZero(MD_IMPL_ArenaPush(arena, amt), amt);
}
}
MD_FUNCTION void
MD_ArenaClear(MD_Arena *arena)
{
MD_IMPL_ArenaPopTo(arena, MD_IMPL_ArenaMinPos);
}
MD_FUNCTION MD_ArenaTemp
MD_ArenaBeginTemp(MD_Arena *arena)
{
MD_ArenaTemp result;
result.arena = arena;
result.pos = MD_IMPL_ArenaGetPos(arena);
return(result);
}
MD_FUNCTION void
MD_ArenaEndTemp(MD_ArenaTemp temp)
{
MD_IMPL_ArenaPopTo(temp.arena, temp.pos);
}
//~ Arena Scratch Pool
MD_FUNCTION MD_ArenaTemp
MD_GetScratch(MD_Arena **conflicts, MD_u64 count)
{
MD_Arena *arena = MD_IMPL_GetScratch(conflicts, count);
MD_ArenaTemp result = MD_ZERO_STRUCT;
if (arena != 0)
{
result = MD_ArenaBeginTemp(arena);
}
return(result);
}
//~ Characters
MD_FUNCTION MD_b32
MD_CharIsAlpha(MD_u8 c)
{
return MD_CharIsAlphaUpper(c) || MD_CharIsAlphaLower(c);
}
MD_FUNCTION MD_b32
MD_CharIsAlphaUpper(MD_u8 c)
{
return c >= 'A' && c <= 'Z';
}
MD_FUNCTION MD_b32
MD_CharIsAlphaLower(MD_u8 c)
{
return c >= 'a' && c <= 'z';
}
MD_FUNCTION MD_b32
MD_CharIsDigit(MD_u8 c)
{
return (c >= '0' && c <= '9');
}
MD_FUNCTION MD_b32
MD_CharIsUnreservedSymbol(MD_u8 c)
{
return (c == '~' || c == '!' || c == '$' || c == '%' || c == '^' ||
c == '&' || c == '*' || c == '-' || c == '=' || c == '+' ||
c == '<' || c == '.' || c == '>' || c == '/' || c == '?' ||
c == '|');
}
MD_FUNCTION MD_b32
MD_CharIsReservedSymbol(MD_u8 c)
{
return (c == '{' || c == '}' || c == '(' || c == ')' || c == '\\' ||
c == '[' || c == ']' || c == '#' || c == ',' || c == ';' ||
c == ':' || c == '@');
}
MD_FUNCTION MD_b32
MD_CharIsSpace(MD_u8 c)
{
return c == ' ' || c == '\r' || c == '\t' || c == '\f' || c == '\v' || c == '\n';
}
MD_FUNCTION MD_u8
MD_CharToUpper(MD_u8 c)
{
return (c >= 'a' && c <= 'z') ? ('A' + (c - 'a')) : c;
}
MD_FUNCTION MD_u8
MD_CharToLower(MD_u8 c)
{
return (c >= 'A' && c <= 'Z') ? ('a' + (c - 'A')) : c;
}
MD_FUNCTION MD_u8
MD_CharToForwardSlash(MD_u8 c)
{
return (c == '\\' ? '/' : c);
}
//~ Strings
MD_FUNCTION MD_u64
MD_CalculateCStringLength(char *cstr)
{
MD_u64 i = 0;
for(; cstr[i]; i += 1);
return i;
}
MD_FUNCTION MD_String8
MD_S8(MD_u8 *str, MD_u64 size)
{
MD_String8 string;
string.str = str;
string.size = size;
return string;
}
MD_FUNCTION MD_String8
MD_S8Range(MD_u8 *first, MD_u8 *opl)
{
MD_String8 string;
string.str = first;
string.size = (MD_u64)(opl - first);
return string;
}
MD_FUNCTION MD_String8
MD_S8Substring(MD_String8 str, MD_u64 min, MD_u64 max)
{
if(max > str.size)
{
max = str.size;
}
if(min > str.size)
{
min = str.size;
}
if(min > max)
{
MD_u64 swap = min;
min = max;
max = swap;
}
str.size = max - min;
str.str += min;
return str;
}
MD_FUNCTION MD_String8
MD_S8Skip(MD_String8 str, MD_u64 min)
{
return MD_S8Substring(str, min, str.size);
}
MD_FUNCTION MD_String8
MD_S8Chop(MD_String8 str, MD_u64 nmax)
{
return MD_S8Substring(str, 0, str.size - nmax);
}
MD_FUNCTION MD_String8
MD_S8Prefix(MD_String8 str, MD_u64 size)
{
return MD_S8Substring(str, 0, size);
}
MD_FUNCTION MD_String8
MD_S8Suffix(MD_String8 str, MD_u64 size)
{
return MD_S8Substring(str, str.size - size, str.size);
}
MD_FUNCTION MD_b32
MD_S8Match(MD_String8 a, MD_String8 b, MD_MatchFlags flags)
{
int result = 0;
if(a.size == b.size || flags & MD_StringMatchFlag_RightSideSloppy)
{
result = 1;
for(MD_u64 i = 0; i < a.size && i < b.size; i += 1)
{
MD_b32 match = (a.str[i] == b.str[i]);
if(flags & MD_StringMatchFlag_CaseInsensitive)
{
match |= (MD_CharToLower(a.str[i]) == MD_CharToLower(b.str[i]));
}
if(flags & MD_StringMatchFlag_SlashInsensitive)
{
match |= (MD_CharToForwardSlash(a.str[i]) == MD_CharToForwardSlash(b.str[i]));
}
if(match == 0)
{
result = 0;
break;
}
}
}
return result;
}
MD_FUNCTION MD_u64
MD_S8FindSubstring(MD_String8 str, MD_String8 substring, MD_u64 start_pos, MD_MatchFlags flags)
{
MD_b32 found = 0;
MD_u64 found_idx = str.size;
for(MD_u64 i = start_pos; i < str.size; i += 1)
{
if(i + substring.size <= str.size)
{
MD_String8 substr_from_str = MD_S8Substring(str, i, i+substring.size);
if(MD_S8Match(substr_from_str, substring, flags))