diff options
author | Bad Diode <bd@badd10de.dev> | 2024-06-15 09:08:24 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2024-06-15 09:08:24 +0200 |
commit | 1b1ab143a96903b69ee5df781d06da5c34dd257a (patch) | |
tree | de25e73416a6db37857136d9d7d30b4b65a53699 /src | |
parent | 92129f31bf0e5c423f2a7e406ae3600d7128a443 (diff) | |
download | bdl-1b1ab143a96903b69ee5df781d06da5c34dd257a.tar.gz bdl-1b1ab143a96903b69ee5df781d06da5c34dd257a.zip |
Remove unnecessary casts and test arena free/realloc
Diffstat (limited to 'src')
-rw-r--r-- | src/badlib.h | 22 | ||||
-rw-r--r-- | src/main.c | 43 |
2 files changed, 40 insertions, 25 deletions
diff --git a/src/badlib.h b/src/badlib.h index 7e1a715..da76577 100644 --- a/src/badlib.h +++ b/src/badlib.h | |||
@@ -6,14 +6,11 @@ | |||
6 | // - sub | 6 | // - sub |
7 | // - delete | 7 | // - delete |
8 | // - Add math operations for vectors and matrices. | 8 | // - Add math operations for vectors and matrices. |
9 | // - Can we remove the PlatformAPI function pointers and just use extern | ||
10 | // references? | ||
11 | // - Breakdown this file in the different library parts. | 9 | // - Breakdown this file in the different library parts. |
12 | // - Sort arrays / linked lists with custom functions? | 10 | // - Sort arrays / linked lists with custom functions? |
13 | // - Implement binary search for searching into an array: | 11 | // - Implement binary search for searching into an array: |
14 | // SearchResult find_array(Array haystack, Array needle). | 12 | // SearchResult find_array(Array haystack, Array needle). |
15 | // - Logger functions for hash map, arrays and growable arrays (print the entire | 13 | // - Logger functions for hash map and queues. |
16 | // thing or at least a small range). | ||
17 | // | 14 | // |
18 | 15 | ||
19 | #include <stdarg.h> | 16 | #include <stdarg.h> |
@@ -143,7 +140,7 @@ arena_realloc(void *ptr, sz old_size, sz new_size, void *ctx) { | |||
143 | a->size += new_size - old_size; | 140 | a->size += new_size - old_size; |
144 | return ptr; | 141 | return ptr; |
145 | } | 142 | } |
146 | u8 *p = (u8 *)arena_malloc(new_size, ctx); | 143 | u8 *p = arena_malloc(new_size, ctx); |
147 | memcpy(p, ptr, old_size); | 144 | memcpy(p, ptr, old_size); |
148 | return p; | 145 | return p; |
149 | } | 146 | } |
@@ -151,7 +148,7 @@ arena_realloc(void *ptr, sz old_size, sz new_size, void *ctx) { | |||
151 | Arena | 148 | Arena |
152 | arena_create(sz cap, Allocator allocator) { | 149 | arena_create(sz cap, Allocator allocator) { |
153 | Arena arena = {0}; | 150 | Arena arena = {0}; |
154 | arena.beg = (u8 *)allocator.malloc(cap, allocator.ctx); | 151 | arena.beg = allocator.malloc(cap, allocator.ctx); |
155 | arena.cap = arena.beg ? cap : 0; | 152 | arena.cap = arena.beg ? cap : 0; |
156 | return arena; | 153 | return arena; |
157 | } | 154 | } |
@@ -593,7 +590,8 @@ str_to_int(Str s) { | |||
593 | s = str_remove_prefix(s, cstr("0x")); | 590 | s = str_remove_prefix(s, cstr("0x")); |
594 | while (s.size) { | 591 | while (s.size) { |
595 | char c = str_next(&s); | 592 | char c = str_next(&s); |
596 | assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')); | 593 | assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || |
594 | (c >= 'A' && c <= 'F')); | ||
597 | if (c >= '0' && c <= '9') { | 595 | if (c >= '0' && c <= '9') { |
598 | num = num * 16 + (c - '0'); | 596 | num = num * 16 + (c - '0'); |
599 | } else if (c >= 'a' && c <= 'f') { | 597 | } else if (c >= 'a' && c <= 'f') { |
@@ -631,7 +629,7 @@ typedef struct Queue { | |||
631 | void | 629 | void |
632 | queue_push(Queue *l, void *val, Arena *a) { | 630 | queue_push(Queue *l, void *val, Arena *a) { |
633 | assert(l); | 631 | assert(l); |
634 | QueueVal *next = (QueueVal *)arena_calloc(sizeof(QueueVal), a); | 632 | QueueVal *next = arena_calloc(sizeof(QueueVal), a); |
635 | next->val = val; | 633 | next->val = val; |
636 | if (l->size == 0) { | 634 | if (l->size == 0) { |
637 | l->head = next; | 635 | l->head = next; |
@@ -698,7 +696,7 @@ map_upsert(Map *m, void *key, Arena *a) { | |||
698 | h = (h << 2) | (h >> 62); | 696 | h = (h << 2) | (h >> 62); |
699 | item = &(*item)->child[h & 0x3]; | 697 | item = &(*item)->child[h & 0x3]; |
700 | } | 698 | } |
701 | *item = (MapNode *)arena_calloc(sizeof(MapNode), a); | 699 | *item = arena_calloc(sizeof(MapNode), a); |
702 | (*item)->key = key; | 700 | (*item)->key = key; |
703 | return *item; | 701 | return *item; |
704 | } | 702 | } |
@@ -715,7 +713,7 @@ map_insert(Map *m, void *key, void *val, Arena *a) { | |||
715 | h = (h << 2) | (h >> 62); | 713 | h = (h << 2) | (h >> 62); |
716 | item = &(*item)->child[h & 0x3]; | 714 | item = &(*item)->child[h & 0x3]; |
717 | } | 715 | } |
718 | *item = (MapNode *)arena_calloc(sizeof(MapNode), a); | 716 | *item = arena_calloc(sizeof(MapNode), a); |
719 | (*item)->key = key; | 717 | (*item)->key = key; |
720 | (*item)->val = val; | 718 | (*item)->val = val; |
721 | } | 719 | } |
@@ -801,7 +799,7 @@ typedef struct ArrayHeader { | |||
801 | 799 | ||
802 | static inline void * | 800 | static inline void * |
803 | _array_reserve(sz num_elem, sz type_size, Arena *a) { | 801 | _array_reserve(sz num_elem, sz type_size, Arena *a) { |
804 | u8 *p = (u8 *)arena_malloc(num_elem * type_size + sizeof(ArrayHeader), a); | 802 | u8 *p = arena_malloc(num_elem * type_size + sizeof(ArrayHeader), a); |
805 | p += sizeof(ArrayHeader); | 803 | p += sizeof(ArrayHeader); |
806 | array_head(p)->size = 0; | 804 | array_head(p)->size = 0; |
807 | array_head(p)->cap = num_elem; | 805 | array_head(p)->cap = num_elem; |
@@ -994,7 +992,7 @@ platform_read_file(Str path, Arena *a) { | |||
994 | 992 | ||
995 | return (FileContents){ | 993 | return (FileContents){ |
996 | .path = path, | 994 | .path = path, |
997 | .data = (Array){.mem = (u8 *)memory, .size = file_size}, | 995 | .data = (Array){.mem = memory, .size = file_size}, |
998 | .err = FILE_ERR_OK, | 996 | .err = FILE_ERR_OK, |
999 | }; | 997 | }; |
1000 | } | 998 | } |
@@ -23,19 +23,36 @@ init(void) { | |||
23 | 23 | ||
24 | void | 24 | void |
25 | process_file(Str path) { | 25 | process_file(Str path) { |
26 | println("processing: "); | 26 | (void)path; |
27 | println("%s", path); | 27 | // println("processing: "); |
28 | println("initialing memory..."); | 28 | // println("%s", path); |
29 | // println("initialing memory..."); | ||
29 | Arena lexer_arena = arena_create(LEXER_MEM, os_allocator); | 30 | Arena lexer_arena = arena_create(LEXER_MEM, os_allocator); |
30 | println("reading file..."); | 31 | u8 *mem; |
31 | FileContents file = platform_read_file(path, &lexer_arena); | 32 | mem = arena_malloc(16, &lexer_arena); |
32 | 33 | println("%{Arena}", &lexer_arena); | |
33 | Str scanner = file.data; | 34 | mem[0] = 255; |
34 | // NOTE: Testing file read line by line. | 35 | mem[1] = 255; |
35 | for (sz i = 0; scanner.size != 0; i++) { | 36 | mem[2] = 0xba; |
36 | Str line = str_split(&scanner, cstr("\n")); | 37 | mem[3] = 0xdd; |
37 | println("%x{4} %s", i + 1, line); | 38 | mem[4] = 0x10; |
38 | } | 39 | mem[5] = 0xde; |
40 | mem[0] = 255; | ||
41 | mem[1] = 255; | ||
42 | println("%{Arena}", &lexer_arena); | ||
43 | arena_malloc(8, &lexer_arena); | ||
44 | mem = arena_realloc(mem, 16, 32, &lexer_arena); | ||
45 | // arena_free(mem, 32, &lexer_arena); | ||
46 | println("%{Arena}", &lexer_arena); | ||
47 | // println("reading file..."); | ||
48 | // FileContents file = platform_read_file(path, &lexer_arena); | ||
49 | |||
50 | // Str scanner = file.data; | ||
51 | // // NOTE: Testing file read line by line. | ||
52 | // for (sz i = 0; scanner.size != 0; i++) { | ||
53 | // Str line = str_split(&scanner, cstr("\n")); | ||
54 | // println("%x{4} %s", i + 1, line); | ||
55 | // } | ||
39 | 56 | ||
40 | // println("<<< %x{4} %b{4} %f{2} %s %{Arena} >>>", 123, 3, 1.345, | 57 | // println("<<< %x{4} %b{4} %f{2} %s %{Arena} >>>", 123, 3, 1.345, |
41 | // cstr("BOOM!"), &logger_inf.storage); | 58 | // cstr("BOOM!"), &logger_inf.storage); |
@@ -43,7 +60,7 @@ process_file(Str path) { | |||
43 | // cstr("error: testing string logger")); | 60 | // cstr("error: testing string logger")); |
44 | 61 | ||
45 | println("arena_memory:\n%{Mem}", | 62 | println("arena_memory:\n%{Mem}", |
46 | &(Array){logger_inf.storage.beg, logger_inf.storage.size}); | 63 | &(Array){lexer_arena.beg, lexer_arena.size}); |
47 | // TODO: run lexer. | 64 | // TODO: run lexer. |
48 | } | 65 | } |
49 | 66 | ||