-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
550 lines (456 loc) · 13.2 KB
/
utils.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
/*
`utils.h` - My own simple utility library (you can say it's my own standard library for working with C)
*/
#ifndef UTILS_H_
#define UTILS_H_
#define UTILS_VERSION UT_MAKE_VERSION(0, 1, 0)
#define ARENA_REGION_BACKEND_LINUX_MMAP 1
#define ARENA_REGION_BACKEND_VIRTUAL_ALLOC 2
#if defined(_WIN32)
#define UT_TARGET_WIN32
#elif defined(__linux__)
#ifdef __ANDROID__
#define UT_TARGET_ANDROID
#error "Currently not supporting the Android platform"
#else
#define UT_TARGET_LINUX
#endif
#else
#error "Unsupported platform"
#endif
#ifndef UTDEF
#define UTDEF
#endif
#ifndef UT_UNUSED
#define UT_UNUSED(x) (void)(x)
#endif
#ifndef UT_NULLABLE
#define UT_NULLABLE
#endif
#ifndef UT_ARRAY_LEN
#define UT_ARRAY_LEN(xs) (sizeof(xs)/sizeof(*xs))
#endif
#ifndef UT_SWAP
#define UT_SWAP(T, a, b) \
do { \
T tmp = (a); \
(a) = (b); \
(b) = tmp; \
} while(0)
#endif
#ifndef UT_MAKE_VERSION
#define UT_MAKE_VERSION(major, minor, rev) ((ut_uint32)(((major) << 22) | ((minor) << 12) | (rev)))
#endif
#ifndef UT_VERSION_MAJOR
#define UT_VERSION_MAJOR(version) ((ut_uint32)((version) >> 22) & 0x3FF)
#endif
#ifndef UT_VERSION_MINOR
#define UT_VERSION_MINOR(version) ((ut_uint32)((version) >> 12) & 0x3FF)
#endif
#ifndef UT_VERSION_REVISION
#define UT_VERSION_REVISION(version) ((ut_uint32)((version) & 0xFFF))
#endif
#ifndef UT_STATIC_ASSERT
#define UT_STATIC_ASSERT(cond) _Static_assert(cond, #cond)
#endif
#ifndef UT_LITERAL
#ifdef __cplusplus
#define UT_LITERAL(T) T
#else
#define UT_LITERAL(T) (T)
#endif
#endif
#ifndef UT_ASSERT
#include <assert.h>
#define UT_ASSERT(cond) assert(cond)
#endif
#ifndef UT_NULL
#define UT_NULL ((void *)0)
#endif
#ifndef ut_false
#define ut_false 0
#endif
#ifndef ut_true
#define ut_true 1
#endif
typedef unsigned char ut_uint8;
UT_STATIC_ASSERT(sizeof(ut_uint8) == 1);
typedef unsigned int ut_uint32;
UT_STATIC_ASSERT(sizeof(ut_uint32) == 4);
typedef ut_uint8 ut_bool;
typedef unsigned long long ut_size;
typedef void *ut_uintptr;
#ifndef ARENA_REGION_BACKEND
#if defined(UT_TARGET_LINUX)
#define ARENA_REGION_BACKEND ARENA_REGION_BACKEND_LINUX_MMAP
#elif defined(UT_TARGET_WIN32)
#define ARENA_REGION_BACKEND ARENA_REGION_BACKEND_VIRTUAL_ALLOC
#error "Not implemented"
#else
#error "Not supported platform"
#endif
#endif
#ifndef DEFAULT_ARENA_REGION_SIZE
#define DEFAULT_ARENA_REGION_SIZE (32*1024)
#endif
typedef struct Buffer {
void *data;
ut_size count;
ut_size capacity;
} Buffer;
typedef struct BufferView {
const void *data;
ut_size count;
} BufferView;
typedef struct ArenaRegion ArenaRegion;
struct ArenaRegion {
ArenaRegion *next;
ut_size count;
ut_size capacity;
ut_uintptr data[];
};
typedef struct Arena {
ArenaRegion *begin;
ArenaRegion *end;
} Arena;
typedef struct StringView {
const char *data;
ut_size count;
} StringView;
#define arena_da_append(a, da, item) \
do { \
if((da)->count + 1 > (da)->capacity) { \
ut_size per_item_size = sizeof(*(da)->items); \
ut_size new_capacity = (da)->capacity * 2; \
if(new_capacity == 0) new_capacity = 8; \
void *new_items = arena_malloc((a), new_capacity * per_item_size); \
UT_ASSERT(new_items && "Buy more RAM LOL!"); \
if((da)->items) ut_memcpy(new_items, (da)->items, \
(da)->count * per_item_size); \
(da)->items = new_items; \
(da)->capacity = new_capacity; \
} \
(da)->items[(da)->count++] = (item); \
} while(0)
#define arena_da_append_many(a, da, new_items, new_items_count) \
do { \
ut_size per_item_size = sizeof(*(da)->items); \
if((da)->count + (new_items_count) > (da)->capacity) { \
ut_size new_capacity = (da)->capacity * 2 + (new_items_count); \
if(new_capacity == 0) new_capacity = (new_items_count) + 8; \
void *new_items2 = arena_malloc((a), new_capacity * per_item_size); \
UT_ASSERT(new_items2 && "Buy more RAM LOL!"); \
if((da)->items) ut_memcpy(new_items2, (da)->items, \
(da)->count * per_item_size); \
(da)->items = new_items2; \
(da)->capacity = new_capacity; \
} \
ut_memcpy((da)->items + (da)->count, (new_items), (new_items_count)*per_item_size); \
(da)->count += (new_items_count); \
} while(0)
#define SV_STATIC(s) { .data = (s), .count = ((sizeof(s) - 1)/sizeof(char)) }
#define SV(s) (StringView){ .data = (s), .count = ((sizeof(s) - 1)/sizeof(char)) }
#define SV_FMT "%.*s"
#define SV_ARGV(sv) (int)(sv).count, (sv).data
UTDEF StringView sv_from(const void *data, ut_size count);
UTDEF StringView sv_from_cstr(const char *cstr);
UTDEF StringView sv_slice(StringView view, ut_size start, ut_size end);
UTDEF ut_bool sv_eq(StringView a, StringView b);
UTDEF ut_bool sv_has_prefix(StringView sv, StringView prefix);
UTDEF ut_bool sv_has_suffix(StringView sv, StringView suffix);
UTDEF int sv_find(StringView sv, StringView needle, ut_size index);
UTDEF int sv_to_int(StringView view);
UTDEF ArenaRegion *create_arena_region(ut_size capacity);
UTDEF void destroy_arena_region(ArenaRegion *region);
UTDEF void *arena_malloc(Arena *a, ut_size size);
UTDEF void arena_free(Arena *a);
UTDEF void arena_reset(Arena *a);
UTDEF ut_bool ut_isalpha(char ch);
UTDEF ut_bool ut_isspace(char ch);
UTDEF ut_bool ut_isalnum(char ch);
UTDEF ut_bool ut_isdigit(char ch);
UTDEF ut_size ut_strlen(const char *cstr);
UTDEF void *ut_memcpy(void *dst, const void *src, ut_size size);
UTDEF void *ut_memset(void *dst, const int val, ut_size size);
UTDEF void buffer_init(Buffer *buf);
UTDEF void buffer_append_with_arena(Buffer *buf, const void *data, ut_size datasz, Arena *a);
UTDEF BufferView buffer_slice(Buffer buf, ut_size start, ut_size size);
#ifndef UTILS_WITHOUT_STDIO
UTDEF ut_bool buffer_save_to_file(const Buffer buf, const char *file_path);
UTDEF ut_bool buffer_load_from_file_with_arena(Buffer *buf, const char *file_path, Arena *a);
UTDEF char *load_file_text_with_arena(const char *file_path, Arena *a);
#endif
#endif // UTILS_H_
#ifdef UTILS_IMPLEMENTATION
#if ARENA_REGION_BACKEND == ARENA_REGION_BACKEND_LINUX_MMAP
#include <sys/mman.h>
ArenaRegion *create_arena_region(ut_size capacity)
{
ut_size byte_size = sizeof(ArenaRegion) + (capacity * sizeof(ut_uintptr));
ArenaRegion *r = mmap(UT_NULL, byte_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if(!r) {
return UT_NULL;
}
r->capacity = capacity;
r->count = 0;
r->next = UT_NULL;
return r;
}
void destroy_arena_region(ArenaRegion *region)
{
ut_size byte_size = sizeof(ArenaRegion) + (region->capacity * sizeof(ut_uintptr));
munmap(region, byte_size);
}
#endif
void *arena_malloc(Arena *a, ut_size size_bytes)
{
ut_size size = (size_bytes + sizeof(ut_uintptr) - 1)/sizeof(ut_uintptr);
if(a->end == UT_NULL) {
UT_ASSERT(a->begin == UT_NULL);
ut_size capacity = DEFAULT_ARENA_REGION_SIZE;
if(capacity < size) capacity = size;
a->end = create_arena_region(capacity);
a->begin = a->end;
}
while((a->end->count + size > a->end->capacity) && (a->end->next != UT_NULL)) {
a->end = a->end->next;
}
if(a->end->count + size > a->end->capacity) {
UT_ASSERT(a->end->next == UT_NULL);
size_t capacity = DEFAULT_ARENA_REGION_SIZE;
if(capacity < size) capacity = size;
a->end->next = create_arena_region(capacity);
a->end = a->end->next;
}
void *result = &a->end->data[a->end->count];
a->end->count += size;
return result;
}
void arena_free(Arena *a)
{
ArenaRegion *r = a->begin;
while (r) {
ArenaRegion *r0 = r;
r = r->next;
destroy_arena_region(r0);
}
a->begin = UT_NULL;
a->end = UT_NULL;
}
void arena_reset(Arena *a)
{
for (ArenaRegion *r = a->begin; r != UT_NULL; r = r->next) {
r->count = 0;
}
a->end = a->begin;
}
void *ut_memcpy(void *dst, const void *src, ut_size size)
{
for(ut_size i = 0; i < size; ++i) {
((ut_uint8 *)dst)[i] = ((const ut_uint8 *)src)[i];
}
return dst;
}
void *ut_memset(void *dst, const int val, ut_size size)
{
for(ut_size i = 0; i < size; ++i) {
((ut_uint8 *)dst)[i] = ((const ut_uint8)val);
}
return dst;
}
ut_bool ut_isspace(char ch)
{
return (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
}
ut_bool ut_isalpha(char ch)
{
return ('A' <= ch && ch <= 'Z')
|| ('a' <= ch && ch <= 'z');
}
ut_bool ut_isalnum(char ch)
{
return ('A' <= ch && ch <= 'Z')
|| ('a' <= ch && ch <= 'z')
|| ('0' <= ch && ch <= '9')
;
}
ut_bool ut_isdigit(char ch)
{
return ('0' <= ch && ch <= '9');
}
ut_size ut_strlen(const char *cstr)
{
ut_size i;
for(i = 0; cstr[i] != '\0'; ++i);
return i;
}
StringView sv_from(const void *data, ut_size count)
{
StringView result;
result.data = data;
result.count = count;
return result;
}
StringView sv_from_cstr(const char *cstr)
{
StringView result;
result.data = cstr;
result.count = ut_strlen(cstr);
return result;
}
StringView sv_slice(StringView view, ut_size start, ut_size end)
{
if(end < start) UT_SWAP(size_t, start, end);
StringView res;
res.data = 0;
res.count = 0;
if(view.count < start)
return res;
res.data = view.data + start;
res.count = end - start;
return res;
}
int sv_to_int(StringView view)
{
ut_bool is_negative = ut_false;
if(view.data[0] == '-') {
is_negative = ut_true;
view.count -= 1;
view.data += 1;
}
int result = 0;
for (size_t i = 0; i < view.count && ut_isdigit(view.data[i]); ++i) {
result = result * 10 + (int) view.data[i] - '0';
}
if(is_negative) result *= -1;
return result;
}
ut_bool sv_eq(StringView a, StringView b)
{
if(a.count != b.count) return ut_false;
for(ut_size i = 0; i < a.count; ++i) {
if(a.data[i] != b.data[i]) return ut_false;
}
return ut_true;
}
ut_bool sv_has_prefix(StringView sv, StringView prefix)
{
if(sv.count < prefix.count) return ut_false;
for(ut_size i = 0; i < prefix.count; ++i) {
if(sv.data[i] != prefix.data[i])
return ut_false;
}
return ut_true;
}
ut_bool sv_has_suffix(StringView sv, StringView suffix)
{
if(sv.count < suffix.count) return ut_false;
for(ut_size i = 0; i < suffix.count; ++i) {
if(sv.data[sv.count - suffix.count + i] != suffix.data[i])
return ut_false;
}
return ut_true;
}
int sv_find(StringView sv, StringView needle, ut_size index)
{
if(sv.count < needle.count) return -1;
ut_size j = 0;
ut_size found = 0;
for(ut_size i = 0; i < sv.count; ++i) {
if(sv.data[i] == needle.data[j]) {
j += 1;
if(j >= needle.count) {
if(found == index) return i - j + 1;
found += 1;
}
} else if(j > 0) {
j = 0;
}
}
return -1;
}
void buffer_init(Buffer *buf)
{
buf->data = 0;
buf->count = 0;
buf->capacity = 0;
}
void buffer_append_with_arena(Buffer *buf, const void *data, ut_size datasz, Arena *a)
{
UT_ASSERT(buf);
UT_ASSERT(data);
UT_ASSERT(a);
if(buf->count + datasz > buf->capacity) {
ut_size new_capacity = buf->capacity * 2 + datasz;
if(new_capacity == 0) new_capacity = 32 + datasz;
void *new_data = arena_malloc(a, new_capacity);
UT_ASSERT(new_data);
ut_memcpy(new_data, buf->data, buf->capacity);
buf->data = new_data;
buf->capacity = new_capacity;
}
ut_memcpy((ut_uint8 *)buf->data + buf->count, data, datasz);
buf->count += datasz;
}
BufferView buffer_slice(Buffer buf, ut_size start, ut_size size)
{
BufferView result;
result.data = (ut_uint8 *)buf.data + start;
result.count = size;
return result;
}
#ifndef UTILS_WITHOUT_STDIO
#include <stdio.h>
ut_bool buffer_save_to_file(const Buffer buf, const char *file_path)
{
FILE *f = fopen(file_path, "wb");
if(!f) return ut_false;
fwrite(buf.data, buf.count, 1, f);
fclose(f);
return ut_true;
}
ut_bool buffer_load_from_file_with_arena(Buffer *buf, const char *file_path, Arena *a)
{
FILE *f = fopen(file_path, "rb");
if(!f) return ut_false;
ut_size fsz;
fseek(f, 0, SEEK_END);
fsz = ftell(f);
fseek(f, 0, SEEK_SET);
void *data = arena_malloc(a, fsz);
if(!data) {
fclose(f);
return ut_false;
}
ut_size rsz = fread(data, 1, fsz, f);
if(rsz != fsz) {
fclose(f);
return ut_false;
}
buf->data = data;
buf->count = fsz;
buf->capacity = fsz;
return ut_true;
}
char *load_file_text_with_arena(const char *file_path, Arena *a)
{
FILE *f = fopen(file_path, "r");
if(!f) return UT_NULL;
ut_size fsz;
fseek(f, 0, SEEK_END);
fsz = ftell(f);
fseek(f, 0, SEEK_SET);
void *data = arena_malloc(a, fsz);
if(!data) {
fclose(f);
return UT_NULL;
}
ut_size rsz = fread(data, 1, fsz, f);
if(rsz != fsz) {
fclose(f);
return UT_NULL;
}
return data;
}
#endif
#endif // UTILS_IMPLEMENTATION