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/badlib.h | |
parent | 92129f31bf0e5c423f2a7e406ae3600d7128a443 (diff) | |
download | bdl-1b1ab143a96903b69ee5df781d06da5c34dd257a.tar.gz bdl-1b1ab143a96903b69ee5df781d06da5c34dd257a.zip |
Remove unnecessary casts and test arena free/realloc
Diffstat (limited to 'src/badlib.h')
-rw-r--r-- | src/badlib.h | 22 |
1 files changed, 10 insertions, 12 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 | } |