From 9eaf710c465ac784062479a61aa84fc17dcf6794 Mon Sep 17 00:00:00 2001 From: Abdulmujeeb Raji Date: Thu, 23 Jan 2025 19:02:48 +0000 Subject: [PATCH] switch over to U64 for sizes, S8_difference --- bastd.c | 10 ++++---- bastd/buffer.c | 18 +++++++------- bastd/examples/memory.c | 2 +- bastd/memory.c | 48 ++++++++++++++++++------------------- bastd/os.c | 4 ++-- bastd/os_windows.c | 6 ++--- bastd/slice.c | 8 +++---- bastd/string.c | 53 ++++++++++++++++++++++++----------------- main.c | 3 +-- 9 files changed, 79 insertions(+), 73 deletions(-) diff --git a/bastd.c b/bastd.c index a0cb1cd..770ac2f 100644 --- a/bastd.c +++ b/bastd.c @@ -193,14 +193,12 @@ typedef U8 B8; #define NIL 0 -#define COUNT_OF(a) (ISize)(sizeof(a) / sizeof(*(a))) +#define COUNT_OF(a) (U64)(sizeof(a) / sizeof(*(a))) #define LENGTH_OF(s) (COUNT_OF(s) - 1) -#define KILO(n) (n << 10) -#define MEGA(n) (n << 20) -#define GIGA(n) (n << 30) -#define TERA(n) (n << 40) -#define PETA(n) (n << 50) +#define KILO(x) ((x) * 1024LL) +#define MEGA(x) (KILO(x) * 1024LL) +#define GIGA(x) (MEGA(x) * 1024LL) // User Config Flags #if !defined(BASTD_CLI) && !defined(BASTD_GUI) diff --git a/bastd/buffer.c b/bastd/buffer.c index 35fb0ca..07333b3 100644 --- a/bastd/buffer.c +++ b/bastd/buffer.c @@ -4,8 +4,8 @@ typedef struct Buffer Buffer; struct Buffer { U8 *raw; - ISize cap; - ISize len; + U64 cap; + U64 len; B8 error; }; @@ -76,11 +76,11 @@ Buffer_fileOutput(Buffer *b, S8 filename) } FUNCTION void -Buffer_append(Buffer *b, U8 *src, ISize len) +Buffer_append(Buffer *b, U8 *src, U64 len) { - ISize available = b->cap - b->len; - ISize amount = available < len ? available : len; - for (ISize i = 0; i < amount; i++) { + U64 available = b->cap - b->len; + U64 amount = available < len ? available : len; + for (U64 i = 0; i < amount; i++) { b->raw[b->len + i] = src[i]; } b->len += amount; @@ -120,13 +120,13 @@ Buffer_appendPtr(Buffer *b, void *p) { Buffer_appendS8(b, S8("0x")); UPtr u = (UPtr)p; - for (ISize i = 2*sizeof(u) - 1; i >= 0; i--) { + for (U64 i = 2*sizeof(u) - 1; i >= 0; i--) { Buffer_appendU8(b, "0123456789abcdef"[(u>>(4*i))&15]); } } FUNCTION void -Buffer_appendF64(Buffer *b, F64 x, ISize num_decimals) +Buffer_appendF64(Buffer *b, F64 x, U64 num_decimals) { F64 prec = 10 ^ num_decimals; @@ -167,7 +167,7 @@ Buffer_appendFile(Buffer *b, S8 filename, m_Allocator *perm) } FUNCTION void -Buffer_appendStandardInput(Buffer *b, ISize max_read_size, m_Allocator *perm) +Buffer_appendStandardInput(Buffer *b, U64 max_read_size, m_Allocator *perm) { int stdin_fd = 0; U32 len = 0; diff --git a/bastd/examples/memory.c b/bastd/examples/memory.c index 52a8be6..f858e80 100644 --- a/bastd/examples/memory.c +++ b/bastd/examples/memory.c @@ -4,7 +4,7 @@ CALLBACK_EXPORT os_ErrorCode os_entry(void) { - ISize buffer_size = KILO(2); + U64 buffer_size = KILO(2); /* Arena allocator allocates new values at an incrementing offset, and can only free by resetting the beginning pointer to a previous offset diff --git a/bastd/memory.c b/bastd/memory.c index a9fd3a7..eb03f2e 100644 --- a/bastd/memory.c +++ b/bastd/memory.c @@ -2,7 +2,7 @@ #define BASTD_MEM_C FUNCTION void * -m_memorySet(void *buffer, U8 value, ISize length) +m_memorySet(void *buffer, U8 value, U64 length) { U8* p = buffer; while (length-- > 0) { @@ -12,26 +12,26 @@ m_memorySet(void *buffer, U8 value, ISize length) } FUNCTION void * -m_memoryCopy(void *dst, void *src, ISize n) +m_memoryCopy(void *dst, void *src, U64 n) { U8 *s = (U8 *)src; U8 *d = (U8 *)dst; - for (ISize i = 0; i < n; i++) { + for (U64 i = 0; i < n; i++) { d[i] = s[i]; } return dst; } -FUNCTION ISize -m_memoryDifference(void *dst, void *src, ISize n) +FUNCTION U64 +m_memoryDifference(void *dst, void *src, U64 n) { U8 *s = (U8 *)src; U8 *d = (U8 *)dst; - ISize count = 0; - for (ISize i = 0; i < n; i++) { + U64 count = 0; + for (U64 i = 0; i < n; i++) { if (d[i] != s[i]) { count++; } @@ -46,7 +46,7 @@ m_memoryDifference(void *dst, void *src, ISize n) If new_size > 0, function like realloc and either resize or create If new_size <= 0, function like free and clear the memory */ -typedef void *(*m_AllocFunc)(void *ctx, void *ptr, ISize old_size, ISize new_size); +typedef void *(*m_AllocFunc)(void *ctx, void *ptr, U64 old_size, U64 new_size); typedef struct m_Allocator m_Allocator; struct m_Allocator { @@ -74,7 +74,7 @@ struct m_Arena { #define DEFAULT_ALIGNMENT (2 * sizeof(void *)) FUNCTION void * -m_Arena_alloc(void *ctx, void *ptr, ISize old_size, ISize new_size) +m_Arena_alloc(void *ctx, void *ptr, U64 old_size, U64 new_size) { m_Arena *a = (m_Arena *)ctx; @@ -82,15 +82,15 @@ m_Arena_alloc(void *ctx, void *ptr, ISize old_size, ISize new_size) /* Arena can only free the most recent block. This allows to follow stack pattern. All we do is move the beginning pointer back the size of the allocation. */ - ISize padding = -old_size & (DEFAULT_ALIGNMENT - 1); + U64 padding = -old_size & (DEFAULT_ALIGNMENT - 1); a->beg -= (old_size + padding); return NIL; } else { // Allocate a block ASSERT(new_size > old_size, "Can't reallocate to a smaller block"); - ISize padding = (UPtr)a->beg & (DEFAULT_ALIGNMENT - 1); - ISize available = a->end - a->beg - padding; + U64 padding = (UPtr)a->beg & (DEFAULT_ALIGNMENT - 1); + U64 available = a->end - a->beg - padding; if (available < 0 || new_size > available) { os_abort("Out of Memory!"); @@ -119,7 +119,7 @@ m_Arena_alloc(void *ctx, void *ptr, ISize old_size, ISize new_size) } FUNCTION m_Arena -m_Arena_create(void* buffer, ISize capacity) +m_Arena_create(void* buffer, U64 capacity) { m_Arena arena = {0}; arena.beg = (U8 *)buffer; @@ -154,7 +154,7 @@ m_Arena_setOffset(m_Arena *arena, m_ArenaOffset offset) */ typedef struct m_BuddyBlock m_BuddyBlock; struct m_BuddyBlock { - ISize size; + U64 size; B8 is_free; }; @@ -165,11 +165,11 @@ m_BuddyBlock_next(m_BuddyBlock *block) } FUNCTION m_BuddyBlock * -m_BuddyBlock_split(m_BuddyBlock *block, ISize size) +m_BuddyBlock_split(m_BuddyBlock *block, U64 size) { if (block != NIL && size != 0) { while (size < block->size) { - ISize sz = block->size >> 1; + U64 sz = block->size >> 1; block->size = sz; block = m_BuddyBlock_next(block); block->size = sz; @@ -185,7 +185,7 @@ m_BuddyBlock_split(m_BuddyBlock *block, ISize size) } FUNCTION m_BuddyBlock * -m_BuddyBlock_findBest(m_BuddyBlock *head, m_BuddyBlock *tail, ISize size) +m_BuddyBlock_findBest(m_BuddyBlock *head, m_BuddyBlock *tail, U64 size) { m_BuddyBlock *best_block = NIL; m_BuddyBlock *block = head; @@ -248,11 +248,11 @@ typedef struct m_Buddy m_Buddy; struct m_Buddy { m_BuddyBlock *head; m_BuddyBlock *tail; - ISize alignment; + U64 alignment; }; FUNCTION m_Buddy -m_Buddy_create(void *data, ISize size) +m_Buddy_create(void *data, U64 size) { m_Buddy res = {0}; res.alignment = DEFAULT_ALIGNMENT; @@ -271,12 +271,12 @@ m_Buddy_create(void *data, ISize size) return res; } -FUNCTION ISize -m_Buddy_requiredSize(m_Buddy *b, ISize size) { - ISize actual_size = b->alignment; +FUNCTION U64 +m_Buddy_requiredSize(m_Buddy *b, U64 size) { + U64 actual_size = b->alignment; size += sizeof(m_BuddyBlock); - ISize padding = -size & (b->alignment - 1); + U64 padding = -size & (b->alignment - 1); size += b->alignment + padding; while (size > actual_size) { @@ -324,7 +324,7 @@ m_Buddy_coalesce(m_BuddyBlock *head, m_BuddyBlock *tail) { } FUNCTION void * -m_Buddy_alloc(void *ctx, void *ptr, ISize old_size, ISize new_size) { +m_Buddy_alloc(void *ctx, void *ptr, U64 old_size, U64 new_size) { m_Buddy *b = (m_Buddy *)ctx; if (new_size <= 0 && ptr != NIL) { diff --git a/bastd/os.c b/bastd/os.c index dcb9b07..db40c51 100644 --- a/bastd/os.c +++ b/bastd/os.c @@ -2,10 +2,10 @@ #define BASTD_OS_C FUNCTION void os_abort(char *msg); -FUNCTION void *os_alloc(ISize cap); +FUNCTION void *os_alloc(U64 cap); FUNCTION B8 os_write(int fd, U8 *buf, int len); FUNCTION B8 os_read(int fd, U8 *buf, int len, U32 *bytes_read); -FUNCTION ISize os_getFileSize(int fd); +FUNCTION U64 os_getFileSize(int fd); FUNCTION int os_openFile(U8 *filename, B8 always_create); FUNCTION B8 os_closeFile(int fd); diff --git a/bastd/os_windows.c b/bastd/os_windows.c index d5ed192..3a51732 100644 --- a/bastd/os_windows.c +++ b/bastd/os_windows.c @@ -17,7 +17,7 @@ os_abort(char *msg) } FUNCTION void * -os_alloc(ISize cap) +os_alloc(U64 cap) { return VirtualAlloc(NIL, cap, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); } @@ -53,7 +53,7 @@ os_read(int fd, U8 *buf, int len, U32 *bytes_read) } } -FUNCTION ISize +FUNCTION U64 os_getFileSize(int fd) { HANDLE h = GetStdHandle(-10 - fd); @@ -64,7 +64,7 @@ os_getFileSize(int fd) LARGE_INTEGER file_size; GetFileSizeEx(h, &file_size); - return (ISize)file_size.QuadPart; + return (U64)file_size.QuadPart; } FUNCTION int diff --git a/bastd/slice.c b/bastd/slice.c index 0d43077..4e346d8 100644 --- a/bastd/slice.c +++ b/bastd/slice.c @@ -8,12 +8,12 @@ typedef struct sl_NAME sl_NAME; struct sl_NAME { sl_TYPE *elems; - ISize len; - ISize cap; + U64 len; + U64 cap; }; FUNCTION sl_NAME -CONCAT(sl_NAME, _create) (sl_TYPE *initial_data, ISize initial_len) +CONCAT(sl_NAME, _create) (sl_TYPE *initial_data, U64 initial_len) { sl_NAME res = {0}; res.elems = initial_data; @@ -30,7 +30,7 @@ CONCAT(sl_NAME, _push) (sl_NAME *slice, m_Allocator *perm) if (slice->len >= slice->cap) { // Slice is full, Grow slice to double its current size slice->cap = slice->cap ? slice->cap : 1; - ISize current_size = sizeof(*slice->elems) * slice->cap; + U64 current_size = sizeof(*slice->elems) * slice->cap; slice->elems = perm->alloc(perm->ctx, slice->elems, current_size, current_size*2); slice->cap *= 2; } diff --git a/bastd/string.c b/bastd/string.c index eee694d..12e2753 100644 --- a/bastd/string.c +++ b/bastd/string.c @@ -3,7 +3,7 @@ typedef struct S8 S8; struct S8 { - ISize len; + U64 len; U8 *raw; }; @@ -13,7 +13,7 @@ struct S8 { #define S8(s) (S8){.len = LENGTH_OF(s), .raw = (U8 *)s} FUNCTION S8 -S8_alloc(ISize len, m_Allocator *perm) +S8_alloc(U64 len, m_Allocator *perm) { S8 res = {0}; res.len = len; @@ -22,10 +22,19 @@ S8_alloc(ISize len, m_Allocator *perm) return res; } +FUNCTION ISize +S8_difference(S8 s1, S8 s2) +{ + if (s1.len != s2.len) { + return SIZE_MAX; + } + return m_memoryDifference(s1.raw, s2.raw, s1.len); +} + FUNCTION S8 S8_concat(S8 s1, S8 s2, m_Allocator *perm) { - ISize len = s1.len + s2.len; + U64 len = s1.len + s2.len; S8 s = S8_alloc(len, perm); m_memoryCopy(s.raw, s1.raw, s1.len); m_memoryCopy(&s.raw[s1.len], s2.raw, s2.len); @@ -33,7 +42,7 @@ S8_concat(S8 s1, S8 s2, m_Allocator *perm) } FUNCTION S8 -S8_sub(S8 s, ISize start, ISize end, m_Allocator *perm) +S8_sub(S8 s, U64 start, U64 end, m_Allocator *perm) { S8 res = {0}; if (end <= s.len && start < end) { @@ -47,7 +56,7 @@ FUNCTION B8 S8_contains(S8 haystack, S8 needle) { B8 found = FALSE; - for (ISize i = 0, j = 0; i < haystack.len && !found; i++) { + for (U64 i = 0, j = 0; i < haystack.len && !found; i++) { while (haystack.raw[i] == needle.raw[j]) { j += 1; i += 1; @@ -60,12 +69,12 @@ S8_contains(S8 haystack, S8 needle) return found; } -FUNCTION ISize +FUNCTION U64 S8_indexOf(S8 haystack, S8 needle) { - for (ISize i = 0; i < haystack.len; i += 1) { - ISize j = 0; - ISize start = i; + for (U64 i = 0; i < haystack.len; i += 1) { + U64 j = 0; + U64 start = i; while (haystack.raw[i] == needle.raw[j]) { j += 1; i += 1; @@ -74,14 +83,14 @@ S8_indexOf(S8 haystack, S8 needle) } } } - return (ISize)-1; + return (U64)-1; } FUNCTION S8 S8_subView(S8 haystack, S8 needle) { S8 r = {0}; - ISize start_index = S8_indexOf(haystack, needle); + U64 start_index = S8_indexOf(haystack, needle); if (start_index < haystack.len) { r.raw = &haystack.raw[start_index]; r.len = needle.len; @@ -99,7 +108,7 @@ S8_equal(S8 a, S8 b) } FUNCTION S8 -S8_view(S8 s, ISize start, ISize end) +S8_view(S8 s, U64 start, U64 end) { if (end < start || end - start > s.len) { return (S8){0}; @@ -123,15 +132,15 @@ FUNCTION sl_S8 S8_split(S8 s, S8 delimiter, m_Allocator *perm) { sl_S8 arr = sl_S8_create(NIL, 0); - ISize start = 0; - for (ISize i = 0; i < s.len; i += 1) { + U64 start = 0; + for (U64 i = 0; i < s.len; i += 1) { if (s.raw[i] != delimiter.raw[0]) { continue; } if (m_memoryDifference(&s.raw[i], delimiter.raw, delimiter.len) == 0) { // Clone the substring before the delimiter. - ISize end = i; + U64 end = i; S8 cloned = S8_sub(s, start, end, perm); *sl_S8_push(&arr, perm) = cloned; start = end + delimiter.len; @@ -148,14 +157,14 @@ S8_split(S8 s, S8 delimiter, m_Allocator *perm) FUNCTION sl_S8 S8_splitView(S8 s, S8 delimiter, m_Allocator *perm) { sl_S8 arr = sl_S8_create(NIL, 0); - ISize start = 0; - for (ISize i = 0; i < s.len; i += 1) { + U64 start = 0; + for (U64 i = 0; i < s.len; i += 1) { if (s.raw[i] != delimiter.raw[0]) { continue; } if (m_memoryDifference(&s.raw[i], delimiter.raw, delimiter.len) == 0) { - ISize end = i; + U64 end = i; S8 view = S8_view(s, start, end); *sl_S8_push(&arr, perm) = view; start = end + delimiter.len; @@ -170,14 +179,14 @@ S8_splitView(S8 s, S8 delimiter, m_Allocator *perm) { FUNCTION S8 S8_join(sl_S8 s, S8 join, m_Allocator *perm) { - ISize total_length = s.len * join.len; - for (ISize i = 0; i < s.len; i += 1) { + U64 total_length = s.len * join.len; + for (U64 i = 0; i < s.len; i += 1) { total_length += s.elems[i].len; } U8 *mem = m_MAKE(U8, total_length + 1, perm); - ISize offset = 0; - for (ISize i = 0; i < s.len; i += 1) { + U64 offset = 0; + for (U64 i = 0; i < s.len; i += 1) { m_memoryCopy(&mem[offset], s.elems[i].raw, s.elems[i].len); offset += s.elems[i].len; diff --git a/main.c b/main.c index 80dda01..47e7f77 100644 --- a/main.c +++ b/main.c @@ -37,8 +37,7 @@ os_entry(void) "m_TypeName_functionName". "m" is a lower-case shortened version of the module's name (usually 1-3 letters). */ - - m_Buddy buddy = m_Buddy_create(os_alloc(MEGA(2)), MEGA(2)); + m_Buddy buddy = m_Buddy_create(os_alloc(GIGA(2)), GIGA(2)); m_Allocator buddy_allocator = m_BUDDY_ALLOCATOR(buddy); sl_S8 args = getArgsSlice(&buddy_allocator);