From dc7033a3889aba09160362f061c75cd4ec36878a Mon Sep 17 00:00:00 2001 From: Jake Koroman Date: Wed, 8 Apr 2026 10:38:00 -0400 Subject: rename size to len, make pointer args const and some style changes. --- jrk.h | 321 +++++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 179 insertions(+), 142 deletions(-) diff --git a/jrk.h b/jrk.h index 9756a9d..26632db 100644 --- a/jrk.h +++ b/jrk.h @@ -1,7 +1,12 @@ +/* + * requires POSIX.1-2008 + * #define _POSIX_C_SOURCE 200809L + */ + /* * TODO: * - remove strnlen/strncmp from hm impl. should probably be using - * jrk_StringView's. + * jrk_String's. * * - add a log interface to give user control of logging. * disable completely, log to file, log format, etc. @@ -9,7 +14,6 @@ * - add an arg parsing api. * * - add real random api. and remove the rand() wrappers. - * */ #include @@ -28,6 +32,9 @@ typedef uint64_t u64; typedef float f32; typedef double f64; +#define _JRK_MAX_STRNLEN 1024 +#define _JRK_DEFAULT_ALIGNMENT (2*sizeof(uintptr_t)) + #define JRK_UNUSED(x) (void)x #define JRK_ARRLENGTH(arr) (sizeof(arr) / sizeof(arr[0])) #define JRK_MIN(a, b) ((a) < (b) ? (a) : (b)) @@ -52,10 +59,15 @@ typedef struct { } jrk_Arena; typedef struct { - char *data; - u64 size; + const char *data; + u64 len; } jrk_String; +#define JRK_STRFMT(s) (int)(s).len, (s).data + +bool jrk_is_power_of_two(uintptr_t); +uintptr_t jrk_align_forward(uintptr_t, u64); + void *jrk_ecalloc(u64, u64); void *jrk_erealloc(void*, u64); @@ -64,96 +76,93 @@ void *jrk_array_realloc_function_arena(void*, u64, u64, void*); jrk_Arena jrk_arena_create(u8*, u64); void *jrk_arena_push(jrk_Arena*, u64); -char *jrk_arena_push_strf(jrk_Arena*, char*, ...); +char *jrk_arena_push_strf(jrk_Arena*, const char*, ...); void jrk_arena_reset(jrk_Arena*); void *jrk_arena_resize(jrk_Arena*, void*, u64, u64); i32 jrk_rand_num(i32); i32 jrk_rand_num_range(i32, i32); -i32 jrk_fd_open_read(char*); -i32 jrk_fd_open_write(char*); -i32 jrk_fd_open_write_append(char*); +i32 jrk_fd_open_read(const char*); +i32 jrk_fd_open_write(const char*); +i32 jrk_fd_open_write_append(const char*); bool jrk_fd_size(i32, u64*); void jrk_fd_close(i32); -jrk_String jrk_string_from_parts(char*, u64); +jrk_String jrk_string_from_parts(const char*, u64); jrk_String jrk_string_trim_right(jrk_String); jrk_String jrk_string_trim_left(jrk_String); jrk_String jrk_string_trim(jrk_String); -jrk_String jrk_string_from_cstr(char*); +jrk_String jrk_string_from_cstr(const char*); +#define jrk_string_from_lit(str_lit) jrk_string_from_parts((str_lit), sizeof((str_lit)) - 1) jrk_String jrk_string_chop_delim(jrk_String*, char delim); -bool jrk_string_equals(jrk_String*, jrk_String*); // takes the lowest size and matches up to that size -bool jrk_string_equals_exact(jrk_String*, jrk_String*); // must be same size to be equals +bool jrk_string_equals(jrk_String, jrk_String); /* takes the lowest len and matches up to that size */ +bool jrk_string_equals_exact(jrk_String, jrk_String); /* must be same len to be equal */ -char *jrk_tmpstrings_push(char*); -char *jrk_tmpstrings_pushf(char*, ...); - -/* XXX: im not thrilled with this api, the memory can get messy real fast */ -#define jrk_string_chop_delim_loop(str, it, delim) \ - for (jrk_String it = jrk_string_chop_delim(str, delim); it.size != 0; it = jrk_string_chop_delim(str, delim)) +char *jrk_tmpstrings_push(const char*); +char *jrk_tmpstrings_pushf(const char*, ...); #ifndef jrk_die - #define jrk_die(x) \ - do { \ - fprintf(stderr, "%s:%d: error: "x" \n", __FILE__, __LINE__); \ - exit(69); \ - } while (0) + #define jrk_die(x) \ + do { \ + fprintf(stderr, "%s:%d: error: "x" \n", __FILE__, __LINE__); \ + exit(69); \ + } while (0) #endif #ifndef jrk_diev - #define jrk_diev(x, ...) \ - do { \ - fprintf(stderr, "%s:%d: error: "x"\n", __FILE__, __LINE__, __VA_ARGS__); \ - exit(69); \ - } while (0) + #define jrk_diev(x, ...) \ + do { \ + fprintf(stderr, "%s:%d: error: "x"\n", __FILE__, __LINE__, __VA_ARGS__); \ + exit(69); \ + } while (0) #endif #define jrk_edie(x) jrk_diev(x": %s", strerror(errno)) #define jrk_ediev(x, ...) jrk_diev(x": %s", __VA_ARGS__, strerror(errno)) #ifndef jrk_log - #define jrk_log(x) \ - do { \ - fprintf(stderr, "info: "x"\n"); \ - } while (0) + #define jrk_log(x) \ + do { \ + fprintf(stderr, "info: "x"\n"); \ + } while (0) #endif #ifndef jrk_logv - #define jrk_logv(x, ...) \ - do { \ - fprintf(stderr, "info: "x"\n", __VA_ARGS__); \ - } while (0) + #define jrk_logv(x, ...) \ + do { \ + fprintf(stderr, "info: "x"\n", __VA_ARGS__); \ + } while (0) #endif #ifndef jrk_elog - #define jrk_elog(x) jrk_log(x": %s", strerror(errno)) + #define jrk_elog(x) jrk_log(x": %s", strerror(errno)) #endif #ifndef jrk_elogv - #define jrk_elogv(x, ...) jrk_logv(x": %s", __VA_ARGS__, strerror(errno)) + #define jrk_elogv(x, ...) jrk_logv(x": %s", __VA_ARGS__, strerror(errno)) #endif #ifndef jrk_error - #define jrk_error(x) \ - do { \ - fprintf(stderr, "error: "x"\n"); \ - } while (0) + #define jrk_error(x) \ + do { \ + fprintf(stderr, "error: "x"\n"); \ + } while (0) #endif #ifndef jrk_errorv - #define jrk_errorv(x, ...) \ - do { \ - fprintf(stderr, "error: "x"\n", __VA_ARGS__); \ - } while (0) + #define jrk_errorv(x, ...) \ + do { \ + fprintf(stderr, "error: "x"\n", __VA_ARGS__); \ + } while (0) #endif #ifndef jrk_eerror - #define jrk_eerror(x) jrk_errorv(x": %s", strerror(errno)) + #define jrk_eerror(x) jrk_errorv(x": %s", strerror(errno)) #endif #ifndef jrk_eerrorv - #define jrk_eerrorv(x, ...) jrk_errorv(x": %s", __VA_ARGS__, strerror(errno)) + #define jrk_eerrorv(x, ...) jrk_errorv(x": %s", __VA_ARGS__, strerror(errno)) #endif #define jrk_assert(c, msg) do { if (!(c)) jrk_die("jrk_assert: "msg); } while(0) @@ -181,13 +190,13 @@ char *jrk_tmpstrings_pushf(char*, ...); typedef void *(*jrk_array_alloc_function_t) (u64, u64, void*); typedef void *(*jrk_array_realloc_function_t) (void*, u64, u64, void*); -#define __jrk_array_alloc(arr, count, size) (arr)->allocfn ? (arr)->allocfn((count), (size), (arr)->allocfn_user) : JRK_ARRAY_DEFAULT_ALLOC_FN((count), (size)) -#define __jrk_array_realloc(arr, _size) (arr)->reallocfn ? (arr)->reallocfn((arr)->items, (arr)->size * sizeof((arr)->items[0]), (_size), (arr)->allocfn_user) : JRK_ARRAY_DEFAULT_REALLOC_FN((arr)->items, (_size)) +#define __jrk_array_alloc(arr, count, len) (arr)->allocfn ? (arr)->allocfn((count), (len), (arr)->allocfn_user) : JRK_ARRAY_DEFAULT_ALLOC_FN((count), (len)) +#define __jrk_array_realloc(arr, _len) (arr)->reallocfn ? (arr)->reallocfn((arr)->items, (arr)->len * sizeof((arr)->items[0]), (_len), (arr)->allocfn_user) : JRK_ARRAY_DEFAULT_REALLOC_FN((arr)->items, (_len)) #define jrk_array_prototype(type) \ typedef struct { \ type *items; \ - u64 size; \ + u64 len; \ u64 capacity; \ jrk_array_alloc_function_t allocfn; \ jrk_array_realloc_function_t reallocfn; \ @@ -199,7 +208,7 @@ typedef void *(*jrk_array_realloc_function_t) (void*, u64, u64, void*); void jrk_array_##type##_deinit(jrk_Array_##type*); \ bool jrk_array_##type##_setcap(jrk_Array_##type*, u64); \ bool jrk_array_##type##_push(jrk_Array_##type*, type); \ - bool jrk_array_##type##_pushn(jrk_Array_##type*, type*, u64) + bool jrk_array_##type##_pushn(jrk_Array_##type*, const type*, u64) #define jrk_array_impl(type) \ bool jrk_array_##type##_init_ex(jrk_Array_##type *arr, u64 capacity, jrk_array_alloc_function_t allocfn, jrk_array_realloc_function_t reallocfn, void *allocfn_user) \ @@ -207,7 +216,7 @@ typedef void *(*jrk_array_realloc_function_t) (void*, u64, u64, void*); (arr)->allocfn = (allocfn) ? (allocfn) : NULL; \ (arr)->reallocfn = (reallocfn) ? (reallocfn) : NULL; \ (arr)->allocfn_user = (allocfn_user) ? (allocfn_user) : NULL; \ - (arr)->size = 0; \ + (arr)->len = 0; \ (arr)->capacity = (capacity); \ (arr)->items = __jrk_array_alloc((arr), (arr)->capacity, sizeof(type)); \ if (!(arr)->items) return false; \ @@ -231,81 +240,85 @@ typedef void *(*jrk_array_realloc_function_t) (void*, u64, u64, void*); if (new_capacity < (arr)->capacity) return false; \ (arr)->items = __jrk_array_realloc((arr), new_capacity * sizeof(type)); \ if (!(arr)->items) return false; \ - memset((arr)->items + (arr)->size, 0, (new_capacity * sizeof(type)) - ((arr)->capacity * sizeof(type))); \ + memset((arr)->items + (arr)->len, 0, (new_capacity * sizeof(type)) - ((arr)->capacity * sizeof(type))); \ (arr)->capacity = new_capacity; \ return true; \ } \ \ bool jrk_array_##type##_push(jrk_Array_##type *arr, type val) \ { \ - if ((arr)->size + 1 > (arr)->capacity) { \ - while ((arr)->size + 1 > ((arr)->capacity)) \ + if ((arr)->len+ 1 > (arr)->capacity) { \ + while ((arr)->len + 1 > ((arr)->capacity)) \ (arr)->capacity = (arr->capacity) ? (arr)->capacity * 2 : JRK_ARRAY_DEFAULT_INIT_CAPACITY; \ (arr)->items = __jrk_array_realloc((arr), (arr)->capacity * sizeof(type)); \ if (!(arr)->items) return false; \ } \ - (arr)->items[(arr)->size++] = (val); \ + (arr)->items[(arr)->len++] = (val); \ return true; \ } \ \ - bool jrk_array_##type##_pushn(jrk_Array_##type *arr, type *vals, u64 count) \ + bool jrk_array_##type##_pushn(jrk_Array_##type *arr, const type *vals, u64 count) \ { \ - if ((arr)->size + count > (arr)->capacity) { \ - while ((arr)->size + count > ((arr)->capacity)) \ + if ((arr)->len+ count > (arr)->capacity) { \ + while ((arr)->len+ count > ((arr)->capacity)) \ (arr)->capacity = (arr->capacity) ? (arr)->capacity * 2 : JRK_ARRAY_DEFAULT_INIT_CAPACITY; \ (arr)->items = __jrk_array_realloc((arr), (arr)->capacity * sizeof(type)); \ if (!(arr)->items) return false; \ - memset((arr)->items + (arr)->size, 0, count * sizeof(type)); \ + memset((arr)->items + (arr)->len, 0, count * sizeof(type)); \ } \ - memcpy(&(arr)->items[(arr)->size], (vals), sizeof(type) * (count)); \ - (arr)->size += count; \ + memcpy(&(arr)->items[(arr)->len], (vals), sizeof(type) * (count)); \ + (arr)->len+= count; \ return true; \ } -#define jrk_array_foreach(type, it, arr) for (type *it = (arr)->items; it < (arr)->items + (arr)->size; ++it) +#define jrk_array_foreach(type, it, arr) for (type *it = (arr)->items; it < (arr)->items + (arr)->len; ++it) jrk_array_prototype(char); -typedef jrk_Array_char jrk_StringBuilder; +typedef jrk_Array_char jrk_String_Builder; #define jrk_sb_append_null(sb) jrk_array_char_push((sb), 0) #define jrk_sb_append_cstr(sb, str) jrk_sb_appendf((sb), (str)) #define jrk_sb_append_buf(sb, buf, bufsz) jrk_array_char_pushn((sb), (buf), (bufsz)) +#define jrk_sb_append_string(sb, string) jrk_sb_append_buf((sb), (string).data, (string).len) +#define jrk_sb_append_lit(sb, str_lit) jrk_sb_append_buf((sb), (str_lit), sizeof((str_lit)) - 1) #define jrk_sb_init_ex(sb, size, allocfn, reallocfn, userarg) jrk_array_char_init_ex((sb), (size), (allocfn), (reallocfn), (userarg)) #define jrk_sb_init_arena(sb, size, arena) jrk_sb_init_ex((sb), (size), jrk_array_alloc_function_arena, jrk_array_realloc_function_arena, (void *)(arena)) #define jrk_sb_deinit(sb) jrk_array_char_deinit((sb)) -#define jrk_sb_to_string(sb) jrk_string_from_parts((sb).items, (sb).size) +#define jrk_sb_to_string(sb) jrk_string_from_parts((sb).items, (sb).len) -bool jrk_sb_appendf(jrk_StringBuilder*, char*, ...); -bool jrk_sb_append_buf_at(jrk_StringBuilder*, char*, u64, u64); -bool jrk_sb_fd_write(jrk_StringBuilder*, i32); -bool jrk_sb_write_entire_file(jrk_StringBuilder *, char *); -bool jrk_sb_fd_read_all(jrk_StringBuilder*, i32); -bool jrk_sb_read_entire_file(jrk_StringBuilder*, char*); +bool jrk_sb_appendf(jrk_String_Builder*, const char*, ...); +bool jrk_sb_append_buf_at(jrk_String_Builder*, const char*, u64, u64); +bool jrk_sb_fd_write(jrk_String_Builder*, i32); +bool jrk_sb_write_entire_file(jrk_String_Builder *, const char *); +bool jrk_sb_fd_read_all(jrk_String_Builder*, i32); +bool jrk_sb_read_entire_file(jrk_String_Builder*, const char*); -// http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param -// for 64 bit hash +/* + * http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param + * for 64 bit hash + */ #define JRK_FNV1A_64_OFFSET_BASIS 0xCBF29CE484222325 #define JRK_FNV1A_64_PRIME 0x100000001B3 -u64 jrk_fnv1a_64(u8*, u64); - -#define jrk_hm_prototype(type, max_key_size) \ - typedef struct { \ - char key[(max_key_size)]; \ - type val; \ - } jrk_HashMapItem_##type; \ - \ - typedef struct { \ - jrk_HashMapItem_##type *items; \ - u64 capacity; \ - } jrk_HashMap_##type; \ - \ - bool jrk_hm_##type##_init(jrk_HashMap_##type *map, jrk_HashMapItem_##type *items, u64 items_cap); \ - bool jrk_hm_##type##_push(jrk_HashMap_##type *map, char *key, type val); \ - bool jrk_hm_##type##_get(jrk_HashMap_##type *map, char *key, type *outval); +u64 jrk_fnv1a_64(const u8*, u64); + +#define jrk_hm_prototype(type, max_key_size) \ + typedef struct { \ + char key[(max_key_size)]; \ + type val; \ + } jrk_Hash_Map_Item_##type; \ + \ + typedef struct { \ + jrk_Hash_Map_Item_##type *items; \ + u64 capacity; \ + } jrk_Hash_Map_##type; \ + \ + bool jrk_hm_##type##_init(jrk_Hash_Map_##type *map, jrk_Hash_Map_Item_##type *items, u64 items_cap); \ + bool jrk_hm_##type##_push(jrk_Hash_Map_##type *map, char *key, type val); \ + bool jrk_hm_##type##_get(jrk_Hash_Map_##type *map, char *key, type *outval); #define jrk_hm_impl(type) \ bool \ - jrk_hm_##type##_init(jrk_HashMap_##type *map, jrk_HashMapItem_##type *items, u64 items_cap) \ + jrk_hm_##type##_init(jrk_Hash_Map_##type *map, jrk_Hash_Map_Item_##type *items, u64 items_cap) \ { \ if (!map) { \ fprintf(stderr, "error: failed to init hashmap: map pointer is NULL\n"); \ @@ -321,7 +334,7 @@ u64 jrk_fnv1a_64(u8*, u64); } \ \ bool \ - jrk_hm_##type##_push(jrk_HashMap_##type *map, char *key, type val) \ + jrk_hm_##type##_push(jrk_Hash_Map_##type *map, char *key, type val) \ { \ if (!key) { \ fprintf(stderr, "error: failed to push key into hashmap: key pointer is NULL\n"); \ @@ -333,7 +346,7 @@ u64 jrk_fnv1a_64(u8*, u64); } \ u64 keylen = sizeof(map->items->key); \ u64 idx = jrk_fnv1a_64((u8 *) key, strnlen(key, keylen)) % map->capacity; \ - jrk_HashMapItem_##type *item = &map->items[idx]; \ + jrk_Hash_Map_Item_##type *item = &map->items[idx]; \ if (item->key[0] != 0 && 0 != strncmp(item->key, key, sizeof(map->items->key))) \ printf("%s collision with %s\n", key, item->key); \ for (; item < &map->items[map->capacity] && item->key[0] != 0 && 0 != strncmp(item->key, key, keylen); ++item); \ @@ -344,13 +357,13 @@ u64 jrk_fnv1a_64(u8*, u64); return false; \ } \ } \ - memcpy(item->key, key, strlen(key)); \ + memcpy(item->key, key, strnlen(key, _JRK_MAX_STRNLEN)); \ item->val = val; \ return true; \ } \ \ bool \ - jrk_hm_##type##_get(jrk_HashMap_##type *map, char *key, type *outval) \ + jrk_hm_##type##_get(jrk_Hash_Map_##type *map, char *key, type *outval) \ { \ if (!key) { \ fprintf(stderr, "error: failed to get key from hashmap: key pointer is NULL\n"); \ @@ -366,7 +379,7 @@ u64 jrk_fnv1a_64(u8*, u64); } \ u64 keylen = sizeof(map->items->key); \ u64 idx = jrk_fnv1a_64((u8 *) key, strnlen(key, keylen)) % map->capacity; \ - jrk_HashMapItem_##type *item = &map->items[idx]; \ + jrk_Hash_Map_Item_##type *item = &map->items[idx]; \ if (0 == strncmp(item->key, key, keylen)) { \ *outval = item->val; \ return true; \ @@ -394,7 +407,28 @@ u64 jrk_fnv1a_64(u8*, u64); #include #include -// NOTE: for jrk_StringBuilder +bool +jrk_is_power_of_two(uintptr_t x) +{ + return (x & (x-1)) == 0; +} + +uintptr_t +jrk_align_forward(uintptr_t ptr, u64 alignment) +{ + uintptr_t result = ptr; + jrk_assertv(jrk_is_power_of_two(alignment), "jrk_align_forward(%p, %lu): alignment %ld is not a power of 2!", (void *)ptr, alignment, alignment); + + /* Same as (p % a) but faster as 'a' is a power of two */ + uintptr_t modulo = ptr & (alignment - 1); + if (modulo != 0) { + result += alignment - modulo; + } + + return result; +} + +/* NOTE: for jrk_String_Builder */ jrk_array_impl(char) void * @@ -416,7 +450,7 @@ jrk_array_realloc_function_arena(void *ptr, u64 old_size, u64 new_size, void *us } bool -jrk_sb_appendf(jrk_StringBuilder *sb, char *fmt, ...) +jrk_sb_appendf(jrk_String_Builder *sb, const char *fmt, ...) { char buf[JRK_KILOBYTES(1)]; va_list args; @@ -448,32 +482,32 @@ jrk_sb_appendf(jrk_StringBuilder *sb, char *fmt, ...) } bool -jrk_sb_append_buf_at(jrk_StringBuilder *sb, char *buf, u64 buf_sz, u64 idx) +jrk_sb_append_buf_at(jrk_String_Builder *sb, const char *buf, u64 buf_sz, u64 idx) { - if (idx > sb->size) { + if (idx > sb->len) { jrk_errorv("jrk_sb_append_buf_at(%p, %s, %ld, %ld): idx is greater than sb.size", (void *)sb, buf, buf_sz, idx); return false; } - if (sb->size + (buf_sz) > sb->capacity) { - while (sb->size + (buf_sz) > sb->capacity) { + if (sb->len + (buf_sz) > sb->capacity) { + while (sb->len + (buf_sz) > sb->capacity) { sb->capacity *= 2; } sb->items = __jrk_array_realloc(sb, sb->capacity); if (!sb->items) return false; - memset(sb->items + sb->size, 0, sb->capacity - sb->size); + memset(sb->items + sb->len, 0, sb->capacity - sb->len); } - memmove(sb->items + idx + buf_sz, sb->items + idx, sb->size - idx); + memmove(sb->items + idx + buf_sz, sb->items + idx, sb->len - idx); memcpy(sb->items + idx, buf, buf_sz); - sb->size += buf_sz; + sb->len += buf_sz; return true; } bool -jrk_sb_fd_read_all(jrk_StringBuilder *sb, i32 fd) +jrk_sb_fd_read_all(jrk_String_Builder *sb, i32 fd) { bool result = true; u64 filesize = 0; @@ -491,10 +525,10 @@ defer: } bool -jrk_sb_fd_write(jrk_StringBuilder *sb, i32 fd) +jrk_sb_fd_write(jrk_String_Builder *sb, i32 fd) { i64 result; - result = write(fd, (void *) sb->items, sb->size); + result = write(fd, (void *) sb->items, sb->len); if (result < 0) { jrk_eerrorv("jrk_sb_fd_write_all(%p, %d)", (void *)sb, fd); return -1; @@ -504,7 +538,7 @@ jrk_sb_fd_write(jrk_StringBuilder *sb, i32 fd) } bool -jrk_sb_read_entire_file(jrk_StringBuilder *sb, char *filename) +jrk_sb_read_entire_file(jrk_String_Builder *sb, const char *filename) { bool result = true; i32 fd = jrk_fd_open_read(filename); @@ -517,7 +551,7 @@ defer: } bool -jrk_sb_write_entire_file(jrk_StringBuilder *sb, char *filename) +jrk_sb_write_entire_file(jrk_String_Builder *sb, const char *filename) { bool result = true; i32 fd = jrk_fd_open_write(filename); @@ -549,12 +583,12 @@ jrk_erealloc(void *ptr, u64 size) return p; } -// NOTE: only allocate space for translation units with JRK_IMPLEMENTATION +/* NOTE: only allocate space for translation units with JRK_IMPLEMENTATION */ static char jrk__tmpstrings[JRK_TMPSTRINGS_ARR_CAPACITY][JRK_TMPSTRINGS_STR_CAPACITY]; static u32 jrk__tmpstrings_idx = 0; char * -jrk_tmpstrings_push(char *str) +jrk_tmpstrings_push(const char *str) { char *result; result = jrk_tmpstrings_pushf(str); @@ -562,7 +596,7 @@ jrk_tmpstrings_push(char *str) } char * -jrk_tmpstrings_pushf(char *fmt, ...) +jrk_tmpstrings_pushf(const char *fmt, ...) { char *result = jrk__tmpstrings[jrk__tmpstrings_idx]; @@ -591,22 +625,25 @@ jrk_arena_create(u8 *buffer, u64 buffer_count) void * jrk_arena_push(jrk_Arena *arena, u64 n) { - if (arena->offset + n > arena->capacity) { + u8 *aligned = (u8 *) jrk_align_forward((uintptr_t)(arena->data + arena->offset), _JRK_DEFAULT_ALIGNMENT); + u64 new_offset = aligned - arena->data; + + if (new_offset + n > arena->capacity) { jrk_errorv("jrk_arena_push(%p, %ld): arena push requires %ld bytes but has a capacity of %ld bytes", - (void *) arena, n, arena->offset + n, arena->capacity); + (void *) arena, n, new_offset + n, arena->capacity); return NULL; } - void *result = &arena->data[arena->offset]; + void *result = &arena->data[new_offset]; arena->prev_offset = arena->offset; - arena->offset += n; + arena->offset += new_offset + n; memset(result, 0, n); return result; } char * -jrk_arena_push_strf(jrk_Arena *arena, char *fmt, ...) +jrk_arena_push_strf(jrk_Arena *arena, const char *fmt, ...) { char *result = NULL; @@ -696,7 +733,7 @@ jrk_fd_close(i32 fd) } i32 -jrk_fd_open_read(char *path) +jrk_fd_open_read(const char *path) { i32 result = open(path, O_RDONLY); @@ -709,7 +746,7 @@ jrk_fd_open_read(char *path) } i32 -jrk_fd_open_write(char *path) +jrk_fd_open_write(const char *path) { i32 result = open(path, O_WRONLY | O_CREAT | O_TRUNC, @@ -724,7 +761,7 @@ jrk_fd_open_write(char *path) } i32 -jrk_fd_open_write_append(char *path) +jrk_fd_open_write_append(const char *path) { i32 result = open(path, O_WRONLY | O_CREAT | O_APPEND, @@ -739,20 +776,20 @@ jrk_fd_open_write_append(char *path) } jrk_String -jrk_string_from_parts(char *data, u64 size) +jrk_string_from_parts(const char *data, u64 len) { jrk_String result = {0}; result.data = data; - result.size = size; + result.len = len; return result; } jrk_String -jrk_string_from_cstr(char *data) +jrk_string_from_cstr(const char *data) { jrk_String result = {0}; result.data = data; - result.size = strlen(data); + result.len = strnlen(data, _JRK_MAX_STRNLEN); return result; } @@ -760,16 +797,16 @@ jrk_String jrk_string_trim_right(jrk_String str) { u64 i = 0; - for (; i < str.size && isspace(str.data[str.size - 1 - i]); ++i); - return jrk_string_from_parts(str.data, str.size - i); + for (; i < str.len && isspace(str.data[str.len - 1 - i]); ++i); + return jrk_string_from_parts(str.data, str.len - i); } jrk_String jrk_string_trim_left(jrk_String str) { u64 i = 0; - for (; i < str.size && isspace(str.data[i]); ++i); - return jrk_string_from_parts(str.data + i, str.size - i); + for (; i < str.len && isspace(str.data[i]); ++i); + return jrk_string_from_parts(str.data + i, str.len - i); } jrk_String @@ -782,43 +819,43 @@ jrk_String jrk_string_chop_delim(jrk_String *str, char delim) { u64 i = 0; - while (i < str->size && str->data[i] != delim) + while (i < str->len && str->data[i] != delim) ++i; jrk_String result = jrk_string_from_parts(str->data, i); - if (i < str->size) { + if (i < str->len) { str->data += i + 1; - str->size -= i + 1; + str->len -= i + 1; } else { str->data += i; - str->size -= i; + str->len -= i; } return result; } bool -jrk_string_equals(jrk_String *a, jrk_String *b) +jrk_string_equals(jrk_String a, jrk_String b) { - for (u64 i = 0; i < JRK_MIN(a->size, b->size); ++i) - if (a->data[i] != b->data[i]) return false; + for (u64 i = 0; i < JRK_MIN(a.len, b.len); ++i) + if (a.data[i] != b.data[i]) return false; return true; } bool -jrk_string_equals_exact(jrk_String *a, jrk_String *b) +jrk_string_equals_exact(jrk_String a, jrk_String b) { - if (a->size != b->size) return false; + if (a.len != b.len) return false; - for (u64 i = 0; i < a->size; ++i) - if (a->data[i] != b->data[i]) return false; + for (u64 i = 0; i < a.len; ++i) + if (a.data[i] != b.data[i]) return false; return true; } -u64 jrk_fnv1a_64(u8 *data, u64 size) +u64 jrk_fnv1a_64(const u8 *data, u64 size) { u64 result = JRK_FNV1A_64_OFFSET_BASIS; for (u64 i = 0; i < size; ++i) { @@ -840,4 +877,4 @@ jrk_rand_num_range(i32 min, i32 max) return rand() % (max - min + 1) + min; } -#endif // JRK_IMPLEMENTATION +#endif /* JRK_IMPLEMENTATION */ -- cgit v1.2.3