diff --git a/src/core/file/file.c b/src/core/file/file.c index c5fbfa2..a0fe059 100644 --- a/src/core/file/file.c +++ b/src/core/file/file.c @@ -66,8 +66,8 @@ u64 wapp_file_read(GenericArray *dst, File *file, u64 item_count) { "`dst`, `dst->items` and `file` should not be NULL."); u64 file_length = wapp_file_get_length(file); - u64 dst_byte_capacity = dst->item_size * dst->capacity; - u64 req_byte_count = item_count * dst->item_size; + u64 dst_byte_capacity = dst->header.item_size * dst->header.capacity; + u64 req_byte_count = item_count * dst->header.item_size; u64 copy_byte_count = 0; if (req_byte_count <= file_length && req_byte_count <= dst_byte_capacity) { @@ -76,17 +76,17 @@ u64 wapp_file_read(GenericArray *dst, File *file, u64 item_count) { copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity; } - dst->count = fread(dst->items, sizeof(u8), copy_byte_count, file) / dst->item_size; + dst->header.count = fread(dst->items, sizeof(u8), copy_byte_count, file) / dst->header.item_size; - return dst->count; + return dst->header.count; } u64 wapp_file_write(const GenericArray *src, File *file, u64 item_count) { wapp_debug_assert(src != NULL && src->items != NULL && file != NULL, "`src`, `src->items` and `file` should not be NULL."); - u64 src_byte_count = src->count * src->item_size; - u64 req_byte_count = item_count * src->item_size; + u64 src_byte_count = src->header.count * src->header.item_size; + u64 req_byte_count = item_count * src->header.item_size; u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count; return fwrite(src->items, sizeof(u8), to_copy, file); diff --git a/src/primitives/array/array.c b/src/primitives/array/array.c index 3392e7a..bfbd69e 100644 --- a/src/primitives/array/array.c +++ b/src/primitives/array/array.c @@ -1,7 +1,3 @@ -/** - * THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN. - */ - #include "./array.h" #include "../../common/assert/assert.h" #include "../mem_allocator/mem_allocator.h" @@ -10,3001 +6,221 @@ #include "../../common/platform/platform.h" #include -void * *wapp_void_ptr_array_get(const VoidPArray *array, u64 index) { +#define _array_header(DATA_PTR) (ArrayHeader *)((u8 *)DATA_PTR - sizeof(ArrayHeader)) + +u64 _array_count(u8 *array, u64 item_size) { wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (void * *)ptr; + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); + + return arr_header->count; } -void wapp_void_ptr_array_set(VoidPArray *array, u64 index, void * *item) { - void * *ptr = wapp_void_ptr_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_void_ptr_array_append_capped(VoidPArray *array, void * *item) { +u64 _array_capacity(u8 *array, u64 item_size) { wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - u64 index = (array->count)++; - wapp_void_ptr_array_set(array, index, item); + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); + + return arr_header->capacity; } -void wapp_void_ptr_array_extend_capped(VoidPArray *array, const VoidPArray *other) { +u64 _array_item_size(u8 *array, u64 item_size) { + wapp_debug_assert(array != NULL, "`array` should not be NULL"); + + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); + + return arr_header->item_size; +} + +u8 *_array_get(u8 *array, u64 index, u64 item_size) { + wapp_debug_assert(array != NULL, "`array` should not be NULL"); + + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); + wapp_runtime_assert(index < arr_header->count, "`index` is out of bounds"); + + return array + arr_header->item_size * index; +} + +void _array_set(u8 *array, u64 index, u8 *value, u64 item_size) { + u8 *item = _array_get(array, index, item_size); + ArrayHeader *arr_header = _array_header(array); + memcpy((void *)item, (void *)value, arr_header->item_size); +} + +void _array_append_capped(u8 *array, u8 *value, u64 item_size) { + wapp_debug_assert(array != NULL, "`array` should not be NULL"); + + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); + + if (arr_header->count >= arr_header->capacity) { return; } + + u64 index = (arr_header->count)++; + _array_set(array, index, value, item_size); +} + +void _array_extend_capped(u8 *array, const u8 *other, u64 item_size) { wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); + ArrayHeader *dst_header = _array_header(array); + ArrayHeader *src_header = _array_header(other); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`other` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); - void * *item; + u64 remaining_capacity = dst_header->capacity - dst_header->count; - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_void_ptr_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_void_ptr_array_append_capped(array, item); - } + u64 copy_count = src_header->count < remaining_capacity ? src_header->count : remaining_capacity; + u8 *dst_ptr = array + dst_header->count * dst_header->item_size; + memcpy((void *)dst_ptr, (const void *)other, copy_count * src_header->item_size); + dst_header->count += copy_count; } -void wapp_void_ptr_array_clear(VoidPArray *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; +void _array_copy_capped(u8 *array, const u8 *other, u64 item_size) { + wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); + + ArrayHeader *dst_header = _array_header(array); + ArrayHeader *src_header = _array_header(other); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`other` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); + + _array_clear(array, item_size); + u64 copy_count = src_header->count < dst_header->capacity ? src_header->count : dst_header->capacity; + memcpy((void *)array, (const void *)other, copy_count * src_header->item_size); + dst_header->count = copy_count; } -void wapp_void_ptr_array_copy_capped(const VoidPArray *src, VoidPArray *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_void_ptr_array_clear(dst); - - void * *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_void_ptr_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_void_ptr_array_append_capped(dst, item); - } -} - -VoidPArray *wapp_void_ptr_array_append_alloc(const Allocator *allocator, VoidPArray *array, void * *item) { +u8 *_array_append_alloc(const Allocator *allocator, u8 *array, u8 *value, u64 item_size) { wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - VoidPArray *output = array; + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (VoidPArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); + u8 *output = array; + + if (arr_header->count >= arr_header->capacity) { + u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(arr_header->capacity * 2); + output = (u8 *)_array_alloc_capacity(allocator, new_capacity, arr_header->item_size); if (!output) { output = array; - goto RETURN_VOID_PTR_ARRAY_APPEND_ALLOC; + goto RETURN_ARRAY_APPEND_ALLOC; } - wapp_void_ptr_array_copy_capped(array, output); + _array_copy_capped(output, array, item_size); } - wapp_void_ptr_array_append_capped(output, item); + _array_append_capped(output, value, item_size); -RETURN_VOID_PTR_ARRAY_APPEND_ALLOC: +RETURN_ARRAY_APPEND_ALLOC: return output; } -VoidPArray *wapp_void_ptr_array_extend_alloc(const Allocator *allocator, VoidPArray *array, const VoidPArray *other) { +u8 *_array_extend_alloc(const Allocator *allocator, u8 *array, const u8 *other, u64 item_size) { wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - VoidPArray *output = array; + ArrayHeader *dst_header = _array_header(array); + ArrayHeader *src_header = _array_header(other); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`other` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (VoidPArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); + u8 *output = array; + + u64 remaining_capacity = dst_header->capacity - dst_header->count; + if (src_header->count >= remaining_capacity) { + u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2); + output = (u8 *)_array_alloc_capacity(allocator, new_capacity, dst_header->item_size); if (!output) { output = array; - goto RETURN_VOID_PTR_ARRAY_EXTEND_ALLOC; + goto RETURN_ARRAY_EXTEND_ALLOC; } - wapp_void_ptr_array_copy_capped(array, output); + _array_copy_capped(output, array, item_size); } - wapp_void_ptr_array_extend_capped(output, other); + _array_extend_capped(output, other, item_size); -RETURN_VOID_PTR_ARRAY_EXTEND_ALLOC: +RETURN_ARRAY_EXTEND_ALLOC: return output; } -VoidPArray *wapp_void_ptr_array_copy_alloc(const Allocator *allocator, const VoidPArray *src, VoidPArray *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); +u8 *_array_copy_alloc(const Allocator *allocator, u8 *array, const u8 *other, u64 item_size) { + wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - VoidPArray *output = dst; + ArrayHeader *dst_header = _array_header(array); + ArrayHeader *src_header = _array_header(other); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`other` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (VoidPArray *)_array_alloc_capacity(allocator, new_capacity, src->item_size); + u8 *output = array; + + if (src_header->count >= dst_header->capacity) { + u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2); + output = (u8 *)_array_alloc_capacity(allocator, new_capacity, src_header->item_size); if (!output) { - output = dst; - goto RETURN_VOID_PTR_ARRAY_COPY_ALLOC; + output = array; + goto RETURN_ARRAY_COPY_ALLOC; } } - wapp_void_ptr_array_clear(output); - wapp_void_ptr_array_copy_capped(src, output); + _array_copy_capped(output, other, item_size); -RETURN_VOID_PTR_ARRAY_COPY_ALLOC: +RETURN_ARRAY_COPY_ALLOC: return output; } -void * *_void_ptr_array_pop(VoidPArray *array) { - u64 index = array->count - 1; - void * *out = wapp_void_ptr_array_get(array, index); - --(array->count); +u8 *_array_pop(u8 *array, u64 item_size) { + wapp_debug_assert(array != NULL, "`array` should not be NULL"); + + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); + + if (arr_header->count == 0) { return NULL; } + + u64 index = arr_header->count - 1; + u8 *out = _array_get(array, index, item_size); + --(arr_header->count); return out; } -Str8 *wapp_str8_array_get(const Str8Array *array, u64 index) { +void _array_clear(u8 *array, u64 item_size) { wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (Str8 *)ptr; -} - -void wapp_str8_array_set(Str8Array *array, u64 index, Str8 *item) { - Str8 *ptr = wapp_str8_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_str8_array_append_capped(Str8Array *array, Str8 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_str8_array_set(array, index, item); -} - -void wapp_str8_array_extend_capped(Str8Array *array, const Str8Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - Str8 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_str8_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_str8_array_append_capped(array, item); - } -} - -void wapp_str8_array_clear(Str8Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_str8_array_copy_capped(const Str8Array *src, Str8Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_str8_array_clear(dst); - - Str8 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_str8_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_str8_array_append_capped(dst, item); - } -} - -Str8Array *wapp_str8_array_append_alloc(const Allocator *allocator, Str8Array *array, Str8 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - Str8Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (Str8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_STR8_ARRAY_APPEND_ALLOC; - } - wapp_str8_array_copy_capped(array, output); - } - - wapp_str8_array_append_capped(output, item); - -RETURN_STR8_ARRAY_APPEND_ALLOC: - return output; -} - -Str8Array *wapp_str8_array_extend_alloc(const Allocator *allocator, Str8Array *array, const Str8Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - Str8Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (Str8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_STR8_ARRAY_EXTEND_ALLOC; - } - wapp_str8_array_copy_capped(array, output); - } - - wapp_str8_array_extend_capped(output, other); - -RETURN_STR8_ARRAY_EXTEND_ALLOC: - return output; -} - -Str8Array *wapp_str8_array_copy_alloc(const Allocator *allocator, const Str8Array *src, Str8Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - Str8Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (Str8Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_STR8_ARRAY_COPY_ALLOC; - } - } - - wapp_str8_array_clear(output); - wapp_str8_array_copy_capped(src, output); - -RETURN_STR8_ARRAY_COPY_ALLOC: - return output; -} - -Str8 *_str8_array_pop(Str8Array *array) { - u64 index = array->count - 1; - Str8 *out = wapp_str8_array_get(array, index); - --(array->count); - return out; -} - -b8 *wapp_b8_array_get(const B8Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (b8 *)ptr; -} - -void wapp_b8_array_set(B8Array *array, u64 index, b8 *item) { - b8 *ptr = wapp_b8_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_b8_array_append_capped(B8Array *array, b8 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_b8_array_set(array, index, item); -} - -void wapp_b8_array_extend_capped(B8Array *array, const B8Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - b8 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_b8_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_b8_array_append_capped(array, item); - } -} - -void wapp_b8_array_clear(B8Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_b8_array_copy_capped(const B8Array *src, B8Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_b8_array_clear(dst); - - b8 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_b8_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_b8_array_append_capped(dst, item); - } -} - -B8Array *wapp_b8_array_append_alloc(const Allocator *allocator, B8Array *array, b8 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - B8Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (B8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_B8_ARRAY_APPEND_ALLOC; - } - wapp_b8_array_copy_capped(array, output); - } - - wapp_b8_array_append_capped(output, item); - -RETURN_B8_ARRAY_APPEND_ALLOC: - return output; -} - -B8Array *wapp_b8_array_extend_alloc(const Allocator *allocator, B8Array *array, const B8Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - B8Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (B8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_B8_ARRAY_EXTEND_ALLOC; - } - wapp_b8_array_copy_capped(array, output); - } - - wapp_b8_array_extend_capped(output, other); - -RETURN_B8_ARRAY_EXTEND_ALLOC: - return output; -} - -B8Array *wapp_b8_array_copy_alloc(const Allocator *allocator, const B8Array *src, B8Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - B8Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (B8Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_B8_ARRAY_COPY_ALLOC; - } - } - - wapp_b8_array_clear(output); - wapp_b8_array_copy_capped(src, output); - -RETURN_B8_ARRAY_COPY_ALLOC: - return output; -} - -b8 *_b8_array_pop(B8Array *array) { - u64 index = array->count - 1; - b8 *out = wapp_b8_array_get(array, index); - --(array->count); - return out; -} - -char *wapp_char_array_get(const CharArray *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (char *)ptr; -} - -void wapp_char_array_set(CharArray *array, u64 index, char *item) { - char *ptr = wapp_char_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_char_array_append_capped(CharArray *array, char *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_char_array_set(array, index, item); -} - -void wapp_char_array_extend_capped(CharArray *array, const CharArray *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - char *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_char_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_char_array_append_capped(array, item); - } -} - -void wapp_char_array_clear(CharArray *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_char_array_copy_capped(const CharArray *src, CharArray *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_char_array_clear(dst); - - char *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_char_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_char_array_append_capped(dst, item); - } -} - -CharArray *wapp_char_array_append_alloc(const Allocator *allocator, CharArray *array, char *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - CharArray *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (CharArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_CHAR_ARRAY_APPEND_ALLOC; - } - wapp_char_array_copy_capped(array, output); - } - - wapp_char_array_append_capped(output, item); - -RETURN_CHAR_ARRAY_APPEND_ALLOC: - return output; -} - -CharArray *wapp_char_array_extend_alloc(const Allocator *allocator, CharArray *array, const CharArray *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - CharArray *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (CharArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_CHAR_ARRAY_EXTEND_ALLOC; - } - wapp_char_array_copy_capped(array, output); - } - - wapp_char_array_extend_capped(output, other); - -RETURN_CHAR_ARRAY_EXTEND_ALLOC: - return output; -} - -CharArray *wapp_char_array_copy_alloc(const Allocator *allocator, const CharArray *src, CharArray *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - CharArray *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (CharArray *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_CHAR_ARRAY_COPY_ALLOC; - } - } - - wapp_char_array_clear(output); - wapp_char_array_copy_capped(src, output); - -RETURN_CHAR_ARRAY_COPY_ALLOC: - return output; -} - -char *_char_array_pop(CharArray *array) { - u64 index = array->count - 1; - char *out = wapp_char_array_get(array, index); - --(array->count); - return out; -} - -c8 *wapp_c8_array_get(const C8Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (c8 *)ptr; -} - -void wapp_c8_array_set(C8Array *array, u64 index, c8 *item) { - c8 *ptr = wapp_c8_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_c8_array_append_capped(C8Array *array, c8 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_c8_array_set(array, index, item); -} - -void wapp_c8_array_extend_capped(C8Array *array, const C8Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - c8 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_c8_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_c8_array_append_capped(array, item); - } -} - -void wapp_c8_array_clear(C8Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_c8_array_copy_capped(const C8Array *src, C8Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_c8_array_clear(dst); - - c8 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_c8_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_c8_array_append_capped(dst, item); - } -} - -C8Array *wapp_c8_array_append_alloc(const Allocator *allocator, C8Array *array, c8 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - C8Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (C8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C8_ARRAY_APPEND_ALLOC; - } - wapp_c8_array_copy_capped(array, output); - } - - wapp_c8_array_append_capped(output, item); - -RETURN_C8_ARRAY_APPEND_ALLOC: - return output; -} - -C8Array *wapp_c8_array_extend_alloc(const Allocator *allocator, C8Array *array, const C8Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - C8Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (C8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C8_ARRAY_EXTEND_ALLOC; - } - wapp_c8_array_copy_capped(array, output); - } - - wapp_c8_array_extend_capped(output, other); - -RETURN_C8_ARRAY_EXTEND_ALLOC: - return output; -} - -C8Array *wapp_c8_array_copy_alloc(const Allocator *allocator, const C8Array *src, C8Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - C8Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (C8Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_C8_ARRAY_COPY_ALLOC; - } - } - - wapp_c8_array_clear(output); - wapp_c8_array_copy_capped(src, output); - -RETURN_C8_ARRAY_COPY_ALLOC: - return output; -} - -c8 *_c8_array_pop(C8Array *array) { - u64 index = array->count - 1; - c8 *out = wapp_c8_array_get(array, index); - --(array->count); - return out; -} - -c16 *wapp_c16_array_get(const C16Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (c16 *)ptr; -} - -void wapp_c16_array_set(C16Array *array, u64 index, c16 *item) { - c16 *ptr = wapp_c16_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_c16_array_append_capped(C16Array *array, c16 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_c16_array_set(array, index, item); -} - -void wapp_c16_array_extend_capped(C16Array *array, const C16Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - c16 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_c16_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_c16_array_append_capped(array, item); - } -} - -void wapp_c16_array_clear(C16Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_c16_array_copy_capped(const C16Array *src, C16Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_c16_array_clear(dst); - - c16 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_c16_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_c16_array_append_capped(dst, item); - } -} - -C16Array *wapp_c16_array_append_alloc(const Allocator *allocator, C16Array *array, c16 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - C16Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (C16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C16_ARRAY_APPEND_ALLOC; - } - wapp_c16_array_copy_capped(array, output); - } - - wapp_c16_array_append_capped(output, item); - -RETURN_C16_ARRAY_APPEND_ALLOC: - return output; -} - -C16Array *wapp_c16_array_extend_alloc(const Allocator *allocator, C16Array *array, const C16Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - C16Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (C16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C16_ARRAY_EXTEND_ALLOC; - } - wapp_c16_array_copy_capped(array, output); - } - - wapp_c16_array_extend_capped(output, other); - -RETURN_C16_ARRAY_EXTEND_ALLOC: - return output; -} - -C16Array *wapp_c16_array_copy_alloc(const Allocator *allocator, const C16Array *src, C16Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - C16Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (C16Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_C16_ARRAY_COPY_ALLOC; - } - } - - wapp_c16_array_clear(output); - wapp_c16_array_copy_capped(src, output); - -RETURN_C16_ARRAY_COPY_ALLOC: - return output; -} - -c16 *_c16_array_pop(C16Array *array) { - u64 index = array->count - 1; - c16 *out = wapp_c16_array_get(array, index); - --(array->count); - return out; -} - -c32 *wapp_c32_array_get(const C32Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (c32 *)ptr; -} - -void wapp_c32_array_set(C32Array *array, u64 index, c32 *item) { - c32 *ptr = wapp_c32_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_c32_array_append_capped(C32Array *array, c32 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_c32_array_set(array, index, item); -} - -void wapp_c32_array_extend_capped(C32Array *array, const C32Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - c32 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_c32_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_c32_array_append_capped(array, item); - } -} - -void wapp_c32_array_clear(C32Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_c32_array_copy_capped(const C32Array *src, C32Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_c32_array_clear(dst); - - c32 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_c32_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_c32_array_append_capped(dst, item); - } -} - -C32Array *wapp_c32_array_append_alloc(const Allocator *allocator, C32Array *array, c32 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - C32Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (C32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C32_ARRAY_APPEND_ALLOC; - } - wapp_c32_array_copy_capped(array, output); - } - - wapp_c32_array_append_capped(output, item); - -RETURN_C32_ARRAY_APPEND_ALLOC: - return output; -} - -C32Array *wapp_c32_array_extend_alloc(const Allocator *allocator, C32Array *array, const C32Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - C32Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (C32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C32_ARRAY_EXTEND_ALLOC; - } - wapp_c32_array_copy_capped(array, output); - } - - wapp_c32_array_extend_capped(output, other); - -RETURN_C32_ARRAY_EXTEND_ALLOC: - return output; -} - -C32Array *wapp_c32_array_copy_alloc(const Allocator *allocator, const C32Array *src, C32Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - C32Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (C32Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_C32_ARRAY_COPY_ALLOC; - } - } - - wapp_c32_array_clear(output); - wapp_c32_array_copy_capped(src, output); - -RETURN_C32_ARRAY_COPY_ALLOC: - return output; -} - -c32 *_c32_array_pop(C32Array *array) { - u64 index = array->count - 1; - c32 *out = wapp_c32_array_get(array, index); - --(array->count); - return out; -} - -i8 *wapp_i8_array_get(const I8Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (i8 *)ptr; -} - -void wapp_i8_array_set(I8Array *array, u64 index, i8 *item) { - i8 *ptr = wapp_i8_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_i8_array_append_capped(I8Array *array, i8 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_i8_array_set(array, index, item); -} - -void wapp_i8_array_extend_capped(I8Array *array, const I8Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - i8 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_i8_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_i8_array_append_capped(array, item); - } -} - -void wapp_i8_array_clear(I8Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_i8_array_copy_capped(const I8Array *src, I8Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_i8_array_clear(dst); - - i8 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_i8_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_i8_array_append_capped(dst, item); - } -} - -I8Array *wapp_i8_array_append_alloc(const Allocator *allocator, I8Array *array, i8 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - I8Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I8_ARRAY_APPEND_ALLOC; - } - wapp_i8_array_copy_capped(array, output); - } - - wapp_i8_array_append_capped(output, item); - -RETURN_I8_ARRAY_APPEND_ALLOC: - return output; -} - -I8Array *wapp_i8_array_extend_alloc(const Allocator *allocator, I8Array *array, const I8Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - I8Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I8_ARRAY_EXTEND_ALLOC; - } - wapp_i8_array_copy_capped(array, output); - } - - wapp_i8_array_extend_capped(output, other); - -RETURN_I8_ARRAY_EXTEND_ALLOC: - return output; -} - -I8Array *wapp_i8_array_copy_alloc(const Allocator *allocator, const I8Array *src, I8Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - I8Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (I8Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_I8_ARRAY_COPY_ALLOC; - } - } - - wapp_i8_array_clear(output); - wapp_i8_array_copy_capped(src, output); - -RETURN_I8_ARRAY_COPY_ALLOC: - return output; -} - -i8 *_i8_array_pop(I8Array *array) { - u64 index = array->count - 1; - i8 *out = wapp_i8_array_get(array, index); - --(array->count); - return out; -} - -i16 *wapp_i16_array_get(const I16Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (i16 *)ptr; -} - -void wapp_i16_array_set(I16Array *array, u64 index, i16 *item) { - i16 *ptr = wapp_i16_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_i16_array_append_capped(I16Array *array, i16 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_i16_array_set(array, index, item); -} - -void wapp_i16_array_extend_capped(I16Array *array, const I16Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - i16 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_i16_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_i16_array_append_capped(array, item); - } -} - -void wapp_i16_array_clear(I16Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_i16_array_copy_capped(const I16Array *src, I16Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_i16_array_clear(dst); - - i16 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_i16_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_i16_array_append_capped(dst, item); - } -} - -I16Array *wapp_i16_array_append_alloc(const Allocator *allocator, I16Array *array, i16 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - I16Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I16_ARRAY_APPEND_ALLOC; - } - wapp_i16_array_copy_capped(array, output); - } - - wapp_i16_array_append_capped(output, item); - -RETURN_I16_ARRAY_APPEND_ALLOC: - return output; -} - -I16Array *wapp_i16_array_extend_alloc(const Allocator *allocator, I16Array *array, const I16Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - I16Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I16_ARRAY_EXTEND_ALLOC; - } - wapp_i16_array_copy_capped(array, output); - } - - wapp_i16_array_extend_capped(output, other); - -RETURN_I16_ARRAY_EXTEND_ALLOC: - return output; -} - -I16Array *wapp_i16_array_copy_alloc(const Allocator *allocator, const I16Array *src, I16Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - I16Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (I16Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_I16_ARRAY_COPY_ALLOC; - } - } - - wapp_i16_array_clear(output); - wapp_i16_array_copy_capped(src, output); - -RETURN_I16_ARRAY_COPY_ALLOC: - return output; -} - -i16 *_i16_array_pop(I16Array *array) { - u64 index = array->count - 1; - i16 *out = wapp_i16_array_get(array, index); - --(array->count); - return out; -} - -i32 *wapp_i32_array_get(const I32Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (i32 *)ptr; -} - -void wapp_i32_array_set(I32Array *array, u64 index, i32 *item) { - i32 *ptr = wapp_i32_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_i32_array_append_capped(I32Array *array, i32 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_i32_array_set(array, index, item); -} - -void wapp_i32_array_extend_capped(I32Array *array, const I32Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - i32 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_i32_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_i32_array_append_capped(array, item); - } -} - -void wapp_i32_array_clear(I32Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_i32_array_copy_capped(const I32Array *src, I32Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_i32_array_clear(dst); - - i32 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_i32_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_i32_array_append_capped(dst, item); - } -} - -I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *array, i32 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - I32Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I32_ARRAY_APPEND_ALLOC; - } - wapp_i32_array_copy_capped(array, output); - } - - wapp_i32_array_append_capped(output, item); - -RETURN_I32_ARRAY_APPEND_ALLOC: - return output; -} - -I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *array, const I32Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - I32Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I32_ARRAY_EXTEND_ALLOC; - } - wapp_i32_array_copy_capped(array, output); - } - - wapp_i32_array_extend_capped(output, other); - -RETURN_I32_ARRAY_EXTEND_ALLOC: - return output; -} - -I32Array *wapp_i32_array_copy_alloc(const Allocator *allocator, const I32Array *src, I32Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - I32Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (I32Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_I32_ARRAY_COPY_ALLOC; - } - } - - wapp_i32_array_clear(output); - wapp_i32_array_copy_capped(src, output); - -RETURN_I32_ARRAY_COPY_ALLOC: - return output; -} - -i32 *_i32_array_pop(I32Array *array) { - u64 index = array->count - 1; - i32 *out = wapp_i32_array_get(array, index); - --(array->count); - return out; -} - -i64 *wapp_i64_array_get(const I64Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (i64 *)ptr; -} - -void wapp_i64_array_set(I64Array *array, u64 index, i64 *item) { - i64 *ptr = wapp_i64_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_i64_array_append_capped(I64Array *array, i64 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_i64_array_set(array, index, item); -} - -void wapp_i64_array_extend_capped(I64Array *array, const I64Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - i64 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_i64_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_i64_array_append_capped(array, item); - } -} - -void wapp_i64_array_clear(I64Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_i64_array_copy_capped(const I64Array *src, I64Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_i64_array_clear(dst); - - i64 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_i64_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_i64_array_append_capped(dst, item); - } -} - -I64Array *wapp_i64_array_append_alloc(const Allocator *allocator, I64Array *array, i64 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - I64Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I64_ARRAY_APPEND_ALLOC; - } - wapp_i64_array_copy_capped(array, output); - } - - wapp_i64_array_append_capped(output, item); - -RETURN_I64_ARRAY_APPEND_ALLOC: - return output; -} - -I64Array *wapp_i64_array_extend_alloc(const Allocator *allocator, I64Array *array, const I64Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - I64Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I64_ARRAY_EXTEND_ALLOC; - } - wapp_i64_array_copy_capped(array, output); - } - - wapp_i64_array_extend_capped(output, other); - -RETURN_I64_ARRAY_EXTEND_ALLOC: - return output; -} - -I64Array *wapp_i64_array_copy_alloc(const Allocator *allocator, const I64Array *src, I64Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - I64Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (I64Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_I64_ARRAY_COPY_ALLOC; - } - } - - wapp_i64_array_clear(output); - wapp_i64_array_copy_capped(src, output); - -RETURN_I64_ARRAY_COPY_ALLOC: - return output; -} - -i64 *_i64_array_pop(I64Array *array) { - u64 index = array->count - 1; - i64 *out = wapp_i64_array_get(array, index); - --(array->count); - return out; -} - -u8 *wapp_u8_array_get(const U8Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (u8 *)ptr; -} - -void wapp_u8_array_set(U8Array *array, u64 index, u8 *item) { - u8 *ptr = wapp_u8_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_u8_array_append_capped(U8Array *array, u8 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_u8_array_set(array, index, item); -} - -void wapp_u8_array_extend_capped(U8Array *array, const U8Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - u8 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_u8_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_u8_array_append_capped(array, item); - } -} - -void wapp_u8_array_clear(U8Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_u8_array_copy_capped(const U8Array *src, U8Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_u8_array_clear(dst); - - u8 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_u8_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_u8_array_append_capped(dst, item); - } -} - -U8Array *wapp_u8_array_append_alloc(const Allocator *allocator, U8Array *array, u8 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - U8Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U8_ARRAY_APPEND_ALLOC; - } - wapp_u8_array_copy_capped(array, output); - } - - wapp_u8_array_append_capped(output, item); - -RETURN_U8_ARRAY_APPEND_ALLOC: - return output; -} - -U8Array *wapp_u8_array_extend_alloc(const Allocator *allocator, U8Array *array, const U8Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - U8Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U8_ARRAY_EXTEND_ALLOC; - } - wapp_u8_array_copy_capped(array, output); - } - - wapp_u8_array_extend_capped(output, other); - -RETURN_U8_ARRAY_EXTEND_ALLOC: - return output; -} - -U8Array *wapp_u8_array_copy_alloc(const Allocator *allocator, const U8Array *src, U8Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - U8Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (U8Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_U8_ARRAY_COPY_ALLOC; - } - } - - wapp_u8_array_clear(output); - wapp_u8_array_copy_capped(src, output); - -RETURN_U8_ARRAY_COPY_ALLOC: - return output; -} - -u8 *_u8_array_pop(U8Array *array) { - u64 index = array->count - 1; - u8 *out = wapp_u8_array_get(array, index); - --(array->count); - return out; -} - -u16 *wapp_u16_array_get(const U16Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (u16 *)ptr; -} - -void wapp_u16_array_set(U16Array *array, u64 index, u16 *item) { - u16 *ptr = wapp_u16_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_u16_array_append_capped(U16Array *array, u16 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_u16_array_set(array, index, item); -} - -void wapp_u16_array_extend_capped(U16Array *array, const U16Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - u16 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_u16_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_u16_array_append_capped(array, item); - } -} - -void wapp_u16_array_clear(U16Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_u16_array_copy_capped(const U16Array *src, U16Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_u16_array_clear(dst); - - u16 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_u16_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_u16_array_append_capped(dst, item); - } -} - -U16Array *wapp_u16_array_append_alloc(const Allocator *allocator, U16Array *array, u16 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - U16Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U16_ARRAY_APPEND_ALLOC; - } - wapp_u16_array_copy_capped(array, output); - } - - wapp_u16_array_append_capped(output, item); - -RETURN_U16_ARRAY_APPEND_ALLOC: - return output; -} - -U16Array *wapp_u16_array_extend_alloc(const Allocator *allocator, U16Array *array, const U16Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - U16Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U16_ARRAY_EXTEND_ALLOC; - } - wapp_u16_array_copy_capped(array, output); - } - - wapp_u16_array_extend_capped(output, other); - -RETURN_U16_ARRAY_EXTEND_ALLOC: - return output; -} - -U16Array *wapp_u16_array_copy_alloc(const Allocator *allocator, const U16Array *src, U16Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - U16Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (U16Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_U16_ARRAY_COPY_ALLOC; - } - } - - wapp_u16_array_clear(output); - wapp_u16_array_copy_capped(src, output); - -RETURN_U16_ARRAY_COPY_ALLOC: - return output; -} - -u16 *_u16_array_pop(U16Array *array) { - u64 index = array->count - 1; - u16 *out = wapp_u16_array_get(array, index); - --(array->count); - return out; -} - -u32 *wapp_u32_array_get(const U32Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (u32 *)ptr; -} - -void wapp_u32_array_set(U32Array *array, u64 index, u32 *item) { - u32 *ptr = wapp_u32_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_u32_array_append_capped(U32Array *array, u32 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_u32_array_set(array, index, item); -} - -void wapp_u32_array_extend_capped(U32Array *array, const U32Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - u32 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_u32_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_u32_array_append_capped(array, item); - } -} - -void wapp_u32_array_clear(U32Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_u32_array_copy_capped(const U32Array *src, U32Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_u32_array_clear(dst); - - u32 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_u32_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_u32_array_append_capped(dst, item); - } -} - -U32Array *wapp_u32_array_append_alloc(const Allocator *allocator, U32Array *array, u32 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - U32Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U32_ARRAY_APPEND_ALLOC; - } - wapp_u32_array_copy_capped(array, output); - } - - wapp_u32_array_append_capped(output, item); - -RETURN_U32_ARRAY_APPEND_ALLOC: - return output; -} - -U32Array *wapp_u32_array_extend_alloc(const Allocator *allocator, U32Array *array, const U32Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - U32Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U32_ARRAY_EXTEND_ALLOC; - } - wapp_u32_array_copy_capped(array, output); - } - - wapp_u32_array_extend_capped(output, other); - -RETURN_U32_ARRAY_EXTEND_ALLOC: - return output; -} - -U32Array *wapp_u32_array_copy_alloc(const Allocator *allocator, const U32Array *src, U32Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - U32Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (U32Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_U32_ARRAY_COPY_ALLOC; - } - } - - wapp_u32_array_clear(output); - wapp_u32_array_copy_capped(src, output); - -RETURN_U32_ARRAY_COPY_ALLOC: - return output; -} - -u32 *_u32_array_pop(U32Array *array) { - u64 index = array->count - 1; - u32 *out = wapp_u32_array_get(array, index); - --(array->count); - return out; -} - -u64 *wapp_u64_array_get(const U64Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (u64 *)ptr; -} - -void wapp_u64_array_set(U64Array *array, u64 index, u64 *item) { - u64 *ptr = wapp_u64_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_u64_array_append_capped(U64Array *array, u64 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_u64_array_set(array, index, item); -} - -void wapp_u64_array_extend_capped(U64Array *array, const U64Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - u64 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_u64_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_u64_array_append_capped(array, item); - } -} - -void wapp_u64_array_clear(U64Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_u64_array_copy_capped(const U64Array *src, U64Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_u64_array_clear(dst); - - u64 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_u64_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_u64_array_append_capped(dst, item); - } -} - -U64Array *wapp_u64_array_append_alloc(const Allocator *allocator, U64Array *array, u64 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - U64Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U64_ARRAY_APPEND_ALLOC; - } - wapp_u64_array_copy_capped(array, output); - } - - wapp_u64_array_append_capped(output, item); - -RETURN_U64_ARRAY_APPEND_ALLOC: - return output; -} - -U64Array *wapp_u64_array_extend_alloc(const Allocator *allocator, U64Array *array, const U64Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - U64Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U64_ARRAY_EXTEND_ALLOC; - } - wapp_u64_array_copy_capped(array, output); - } - - wapp_u64_array_extend_capped(output, other); - -RETURN_U64_ARRAY_EXTEND_ALLOC: - return output; -} - -U64Array *wapp_u64_array_copy_alloc(const Allocator *allocator, const U64Array *src, U64Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - U64Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (U64Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_U64_ARRAY_COPY_ALLOC; - } - } - - wapp_u64_array_clear(output); - wapp_u64_array_copy_capped(src, output); - -RETURN_U64_ARRAY_COPY_ALLOC: - return output; -} - -u64 *_u64_array_pop(U64Array *array) { - u64 index = array->count - 1; - u64 *out = wapp_u64_array_get(array, index); - --(array->count); - return out; -} - -f32 *wapp_f32_array_get(const F32Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (f32 *)ptr; -} - -void wapp_f32_array_set(F32Array *array, u64 index, f32 *item) { - f32 *ptr = wapp_f32_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_f32_array_append_capped(F32Array *array, f32 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_f32_array_set(array, index, item); -} - -void wapp_f32_array_extend_capped(F32Array *array, const F32Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - f32 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_f32_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_f32_array_append_capped(array, item); - } -} - -void wapp_f32_array_clear(F32Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_f32_array_copy_capped(const F32Array *src, F32Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_f32_array_clear(dst); - - f32 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_f32_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_f32_array_append_capped(dst, item); - } -} - -F32Array *wapp_f32_array_append_alloc(const Allocator *allocator, F32Array *array, f32 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - F32Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (F32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F32_ARRAY_APPEND_ALLOC; - } - wapp_f32_array_copy_capped(array, output); - } - - wapp_f32_array_append_capped(output, item); - -RETURN_F32_ARRAY_APPEND_ALLOC: - return output; -} - -F32Array *wapp_f32_array_extend_alloc(const Allocator *allocator, F32Array *array, const F32Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - F32Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (F32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F32_ARRAY_EXTEND_ALLOC; - } - wapp_f32_array_copy_capped(array, output); - } - - wapp_f32_array_extend_capped(output, other); - -RETURN_F32_ARRAY_EXTEND_ALLOC: - return output; -} - -F32Array *wapp_f32_array_copy_alloc(const Allocator *allocator, const F32Array *src, F32Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - F32Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (F32Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_F32_ARRAY_COPY_ALLOC; - } - } - - wapp_f32_array_clear(output); - wapp_f32_array_copy_capped(src, output); - -RETURN_F32_ARRAY_COPY_ALLOC: - return output; -} - -f32 *_f32_array_pop(F32Array *array) { - u64 index = array->count - 1; - f32 *out = wapp_f32_array_get(array, index); - --(array->count); - return out; -} - -f64 *wapp_f64_array_get(const F64Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (f64 *)ptr; -} - -void wapp_f64_array_set(F64Array *array, u64 index, f64 *item) { - f64 *ptr = wapp_f64_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_f64_array_append_capped(F64Array *array, f64 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_f64_array_set(array, index, item); -} - -void wapp_f64_array_extend_capped(F64Array *array, const F64Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - f64 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_f64_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_f64_array_append_capped(array, item); - } -} - -void wapp_f64_array_clear(F64Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_f64_array_copy_capped(const F64Array *src, F64Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_f64_array_clear(dst); - - f64 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_f64_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_f64_array_append_capped(dst, item); - } -} - -F64Array *wapp_f64_array_append_alloc(const Allocator *allocator, F64Array *array, f64 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - F64Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (F64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F64_ARRAY_APPEND_ALLOC; - } - wapp_f64_array_copy_capped(array, output); - } - - wapp_f64_array_append_capped(output, item); - -RETURN_F64_ARRAY_APPEND_ALLOC: - return output; -} - -F64Array *wapp_f64_array_extend_alloc(const Allocator *allocator, F64Array *array, const F64Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - F64Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (F64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F64_ARRAY_EXTEND_ALLOC; - } - wapp_f64_array_copy_capped(array, output); - } - - wapp_f64_array_extend_capped(output, other); - -RETURN_F64_ARRAY_EXTEND_ALLOC: - return output; -} - -F64Array *wapp_f64_array_copy_alloc(const Allocator *allocator, const F64Array *src, F64Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - F64Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (F64Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_F64_ARRAY_COPY_ALLOC; - } - } - - wapp_f64_array_clear(output); - wapp_f64_array_copy_capped(src, output); - -RETURN_F64_ARRAY_COPY_ALLOC: - return output; -} - -f64 *_f64_array_pop(F64Array *array) { - u64 index = array->count - 1; - f64 *out = wapp_f64_array_get(array, index); - --(array->count); - return out; -} - -f128 *wapp_f128_array_get(const F128Array *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (f128 *)ptr; -} - -void wapp_f128_array_set(F128Array *array, u64 index, f128 *item) { - f128 *ptr = wapp_f128_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_f128_array_append_capped(F128Array *array, f128 *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_f128_array_set(array, index, item); -} - -void wapp_f128_array_extend_capped(F128Array *array, const F128Array *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - f128 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_f128_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_f128_array_append_capped(array, item); - } -} - -void wapp_f128_array_clear(F128Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_f128_array_copy_capped(const F128Array *src, F128Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_f128_array_clear(dst); - - f128 *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_f128_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_f128_array_append_capped(dst, item); - } -} - -F128Array *wapp_f128_array_append_alloc(const Allocator *allocator, F128Array *array, f128 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - F128Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (F128Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F128_ARRAY_APPEND_ALLOC; - } - wapp_f128_array_copy_capped(array, output); - } - - wapp_f128_array_append_capped(output, item); - -RETURN_F128_ARRAY_APPEND_ALLOC: - return output; -} - -F128Array *wapp_f128_array_extend_alloc(const Allocator *allocator, F128Array *array, const F128Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - F128Array *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (F128Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F128_ARRAY_EXTEND_ALLOC; - } - wapp_f128_array_copy_capped(array, output); - } - - wapp_f128_array_extend_capped(output, other); - -RETURN_F128_ARRAY_EXTEND_ALLOC: - return output; -} - -F128Array *wapp_f128_array_copy_alloc(const Allocator *allocator, const F128Array *src, F128Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - F128Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (F128Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_F128_ARRAY_COPY_ALLOC; - } - } - - wapp_f128_array_clear(output); - wapp_f128_array_copy_capped(src, output); - -RETURN_F128_ARRAY_COPY_ALLOC: - return output; -} - -f128 *_f128_array_pop(F128Array *array) { - u64 index = array->count - 1; - f128 *out = wapp_f128_array_get(array, index); - --(array->count); - return out; -} - -iptr *wapp_iptr_array_get(const IptrArray *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (iptr *)ptr; -} - -void wapp_iptr_array_set(IptrArray *array, u64 index, iptr *item) { - iptr *ptr = wapp_iptr_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_iptr_array_append_capped(IptrArray *array, iptr *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_iptr_array_set(array, index, item); -} - -void wapp_iptr_array_extend_capped(IptrArray *array, const IptrArray *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - iptr *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_iptr_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_iptr_array_append_capped(array, item); - } -} - -void wapp_iptr_array_clear(IptrArray *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_iptr_array_copy_capped(const IptrArray *src, IptrArray *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_iptr_array_clear(dst); - - iptr *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_iptr_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_iptr_array_append_capped(dst, item); - } -} - -IptrArray *wapp_iptr_array_append_alloc(const Allocator *allocator, IptrArray *array, iptr *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - IptrArray *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (IptrArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_IPTR_ARRAY_APPEND_ALLOC; - } - wapp_iptr_array_copy_capped(array, output); - } - - wapp_iptr_array_append_capped(output, item); - -RETURN_IPTR_ARRAY_APPEND_ALLOC: - return output; -} - -IptrArray *wapp_iptr_array_extend_alloc(const Allocator *allocator, IptrArray *array, const IptrArray *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - IptrArray *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (IptrArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_IPTR_ARRAY_EXTEND_ALLOC; - } - wapp_iptr_array_copy_capped(array, output); - } - - wapp_iptr_array_extend_capped(output, other); - -RETURN_IPTR_ARRAY_EXTEND_ALLOC: - return output; -} - -IptrArray *wapp_iptr_array_copy_alloc(const Allocator *allocator, const IptrArray *src, IptrArray *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - IptrArray *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (IptrArray *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_IPTR_ARRAY_COPY_ALLOC; - } - } - - wapp_iptr_array_clear(output); - wapp_iptr_array_copy_capped(src, output); - -RETURN_IPTR_ARRAY_COPY_ALLOC: - return output; -} - -iptr *_iptr_array_pop(IptrArray *array) { - u64 index = array->count - 1; - iptr *out = wapp_iptr_array_get(array, index); - --(array->count); - return out; -} - -uptr *wapp_uptr_array_get(const UptrArray *array, u64 index) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - - u8 *ptr = (u8 *)(array->items) + (array->item_size * index); - return (uptr *)ptr; -} - -void wapp_uptr_array_set(UptrArray *array, u64 index, uptr *item) { - uptr *ptr = wapp_uptr_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_uptr_array_append_capped(UptrArray *array, uptr *item) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_runtime_assert(array->count < array->capacity, "`array` is full"); - - u64 index = (array->count)++; - wapp_uptr_array_set(array, index, item); -} - -void wapp_uptr_array_extend_capped(UptrArray *array, const UptrArray *other) { - wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL"); - - u64 remaining_capacity = array->capacity - array->count; - wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity"); - - uptr *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 items_to_add = other->count; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_uptr_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_uptr_array_append_capped(array, item); - } -} - -void wapp_uptr_array_clear(UptrArray *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_uptr_array_copy_capped(const UptrArray *src, UptrArray *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_uptr_array_clear(dst); - - uptr *item; - - // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of - // MSVC Spectre mitigation warnings - u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity; - u64 item_index = 0; - b8 running = true; - while (running) { - item = wapp_uptr_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_uptr_array_append_capped(dst, item); - } -} - -UptrArray *wapp_uptr_array_append_alloc(const Allocator *allocator, UptrArray *array, uptr *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - UptrArray *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (UptrArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_UPTR_ARRAY_APPEND_ALLOC; - } - wapp_uptr_array_copy_capped(array, output); - } - - wapp_uptr_array_append_capped(output, item); - -RETURN_UPTR_ARRAY_APPEND_ALLOC: - return output; -} - -UptrArray *wapp_uptr_array_extend_alloc(const Allocator *allocator, UptrArray *array, const UptrArray *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - UptrArray *output = array; - - u64 remaining_capacity = array->capacity - array->count; - if (other->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (UptrArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_UPTR_ARRAY_EXTEND_ALLOC; - } - wapp_uptr_array_copy_capped(array, output); - } - - wapp_uptr_array_extend_capped(output, other); - -RETURN_UPTR_ARRAY_EXTEND_ALLOC: - return output; -} - -UptrArray *wapp_uptr_array_copy_alloc(const Allocator *allocator, const UptrArray *src, UptrArray *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - UptrArray *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (UptrArray *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_UPTR_ARRAY_COPY_ALLOC; - } - } - - wapp_uptr_array_clear(output); - wapp_uptr_array_copy_capped(src, output); - -RETURN_UPTR_ARRAY_COPY_ALLOC: - return output; -} + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); -uptr *_uptr_array_pop(UptrArray *array) { - u64 index = array->count - 1; - uptr *out = wapp_uptr_array_get(array, index); - --(array->count); - return out; + arr_header->count = 0; } -GenericArray *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size) { +u8 *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size) { wapp_debug_assert(allocator != NULL, "`allocator` should not be NULL"); - u64 allocation_size = sizeof(GenericArray) + item_size * capacity; - GenericArray *array = wapp_mem_allocator_alloc(allocator, allocation_size); - if (!array) { - goto RETURN_GENERIC_ARRAY_ALLOC; + u8 *output = NULL; + + u64 allocation_size = sizeof(ArrayHeader) + item_size * capacity; + ArrayHeader *header = wapp_mem_allocator_alloc(allocator, allocation_size); + if (!header) { + goto RETURN_ARRAY_ALLOC; } - array->items = (void *)((u8 *)array + sizeof(GenericArray)); - array->count = 0; - array->capacity = capacity; - array->item_size = item_size; + output = ((u8 *)header) + sizeof(ArrayHeader); + header->magic = WAPP_ARRAY_MAGIC; + header->count = 0; + header->capacity = capacity; + header->item_size = item_size; -RETURN_GENERIC_ARRAY_ALLOC: - return array; +RETURN_ARRAY_ALLOC: + return output; } - diff --git a/src/primitives/array/array.h b/src/primitives/array/array.h index 999cfbb..13f4980 100644 --- a/src/primitives/array/array.h +++ b/src/primitives/array/array.h @@ -1,7 +1,3 @@ -/** - * THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN. - */ - #ifndef ARRAY_H #define ARRAY_H @@ -14,181 +10,14 @@ BEGIN_C_LINKAGE #endif // !WAPP_PLATFORM_CPP -#define wapp_void_ptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((VoidPArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(void *))) -#define wapp_str8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((Str8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(Str8))) -#define wapp_b8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((B8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(b8))) -#define wapp_char_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((CharArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(char))) -#define wapp_c8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c8))) -#define wapp_c16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c16))) -#define wapp_c32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c32))) -#define wapp_i8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i8))) -#define wapp_i16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i16))) -#define wapp_i32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i32))) -#define wapp_i64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i64))) -#define wapp_u8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u8))) -#define wapp_u16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u16))) -#define wapp_u32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u32))) -#define wapp_u64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u64))) -#define wapp_f32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f32))) -#define wapp_f64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f64))) -#define wapp_f128_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F128Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f128))) -#define wapp_iptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((IptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(iptr))) -#define wapp_uptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((UptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(uptr))) +#define WAPP_ARRAY_MAGIC (u64)0x57415F415252 + +#define _calc_array_count(TYPE, ...) wapp_misc_utils_va_args_count(TYPE, __VA_ARGS__) +#define _calc_array_capacity(TYPE, ...) wapp_misc_utils_u64_round_up_pow2(_calc_array_count(TYPE, __VA_ARGS__) * 2) + +#define wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY) ((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(TYPE))) #ifdef WAPP_PLATFORM_CPP -#define wapp_void_ptr_array(...) ([&]() { \ - wapp_persist void * buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return VoidPArray{ \ - buf, \ - wapp_misc_utils_va_args_count(void *, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2), \ - sizeof(void *) \ - }; \ -}()) -#define wapp_void_ptr_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist void * buf[CAPACITY] = {}; \ - return VoidPArray{buf, 0, CAPACITY, sizeof(void *)}; \ -}()) -#define wapp_void_ptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_void_ptr_array_pop(ARRAY_PTR) : \ - (void *)(0) \ -) -#define wapp_str8_array(...) ([&]() { \ - wapp_persist Str8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return Str8Array{ \ - buf, \ - wapp_misc_utils_va_args_count(Str8, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2), \ - sizeof(Str8) \ - }; \ -}()) -#define wapp_str8_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist Str8 buf[CAPACITY] = {}; \ - return Str8Array{buf, 0, CAPACITY, sizeof(Str8)}; \ -}()) -#define wapp_str8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_str8_array_pop(ARRAY_PTR) : \ - Str8{} \ -) -#define wapp_b8_array(...) ([&]() { \ - wapp_persist b8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return B8Array{ \ - buf, \ - wapp_misc_utils_va_args_count(b8, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b8, __VA_ARGS__) * 2), \ - sizeof(b8) \ - }; \ -}()) -#define wapp_b8_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist b8 buf[CAPACITY] = {}; \ - return B8Array{buf, 0, CAPACITY, sizeof(b8)}; \ -}()) -#define wapp_b8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_b8_array_pop(ARRAY_PTR) : \ - b8{} \ -) -#define wapp_char_array(...) ([&]() { \ - wapp_persist char buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return CharArray{ \ - buf, \ - wapp_misc_utils_va_args_count(char, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2), \ - sizeof(char) \ - }; \ -}()) -#define wapp_char_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist char buf[CAPACITY] = {}; \ - return CharArray{buf, 0, CAPACITY, sizeof(char)}; \ -}()) -#define wapp_char_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_char_array_pop(ARRAY_PTR) : \ - char{} \ -) -#define wapp_c8_array(...) ([&]() { \ - wapp_persist c8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return C8Array{ \ - buf, \ - wapp_misc_utils_va_args_count(c8, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2), \ - sizeof(c8) \ - }; \ -}()) -#define wapp_c8_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist c8 buf[CAPACITY] = {}; \ - return C8Array{buf, 0, CAPACITY, sizeof(c8)}; \ -}()) -#define wapp_c8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_c8_array_pop(ARRAY_PTR) : \ - c8{} \ -) -#define wapp_c16_array(...) ([&]() { \ - wapp_persist c16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return C16Array{ \ - buf, \ - wapp_misc_utils_va_args_count(c16, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2), \ - sizeof(c16) \ - }; \ -}()) -#define wapp_c16_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist c16 buf[CAPACITY] = {}; \ - return C16Array{buf, 0, CAPACITY, sizeof(c16)}; \ -}()) -#define wapp_c16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_c16_array_pop(ARRAY_PTR) : \ - c16{} \ -) -#define wapp_c32_array(...) ([&]() { \ - wapp_persist c32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return C32Array{ \ - buf, \ - wapp_misc_utils_va_args_count(c32, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2), \ - sizeof(c32) \ - }; \ -}()) -#define wapp_c32_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist c32 buf[CAPACITY] = {}; \ - return C32Array{buf, 0, CAPACITY, sizeof(c32)}; \ -}()) -#define wapp_c32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_c32_array_pop(ARRAY_PTR) : \ - c32{} \ -) -#define wapp_i8_array(...) ([&]() { \ - wapp_persist i8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return I8Array{ \ - buf, \ - wapp_misc_utils_va_args_count(i8, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2), \ - sizeof(i8) \ - }; \ -}()) -#define wapp_i8_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist i8 buf[CAPACITY] = {}; \ - return I8Array{buf, 0, CAPACITY, sizeof(i8)}; \ -}()) -#define wapp_i8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i8_array_pop(ARRAY_PTR) : \ - i8{} \ -) -#define wapp_i16_array(...) ([&]() { \ - wapp_persist i16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return I16Array{ \ - buf, \ - wapp_misc_utils_va_args_count(i16, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2), \ - sizeof(i16) \ - }; \ -}()) -#define wapp_i16_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist i16 buf[CAPACITY] = {}; \ - return I16Array{buf, 0, CAPACITY, sizeof(i16)}; \ -}()) -#define wapp_i16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i16_array_pop(ARRAY_PTR) : \ - i16{} \ -) #define wapp_i32_array(...) ([&]() { \ wapp_persist i32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return I32Array{ \ @@ -206,770 +35,97 @@ BEGIN_C_LINKAGE *_i32_array_pop(ARRAY_PTR) : \ i32{} \ ) -#define wapp_i64_array(...) ([&]() { \ - wapp_persist i64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return I64Array{ \ - buf, \ - wapp_misc_utils_va_args_count(i64, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2), \ - sizeof(i64) \ - }; \ -}()) -#define wapp_i64_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist i64 buf[CAPACITY] = {}; \ - return I64Array{buf, 0, CAPACITY, sizeof(i64)}; \ -}()) -#define wapp_i64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i64_array_pop(ARRAY_PTR) : \ - i64{} \ -) -#define wapp_u8_array(...) ([&]() { \ - wapp_persist u8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return U8Array{ \ - buf, \ - wapp_misc_utils_va_args_count(u8, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2), \ - sizeof(u8) \ - }; \ -}()) -#define wapp_u8_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist u8 buf[CAPACITY] = {}; \ - return U8Array{buf, 0, CAPACITY, sizeof(u8)}; \ -}()) -#define wapp_u8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u8_array_pop(ARRAY_PTR) : \ - u8{} \ -) -#define wapp_u16_array(...) ([&]() { \ - wapp_persist u16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return U16Array{ \ - buf, \ - wapp_misc_utils_va_args_count(u16, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2), \ - sizeof(u16) \ - }; \ -}()) -#define wapp_u16_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist u16 buf[CAPACITY] = {}; \ - return U16Array{buf, 0, CAPACITY, sizeof(u16)}; \ -}()) -#define wapp_u16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u16_array_pop(ARRAY_PTR) : \ - u16{} \ -) -#define wapp_u32_array(...) ([&]() { \ - wapp_persist u32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return U32Array{ \ - buf, \ - wapp_misc_utils_va_args_count(u32, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2), \ - sizeof(u32) \ - }; \ -}()) -#define wapp_u32_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist u32 buf[CAPACITY] = {}; \ - return U32Array{buf, 0, CAPACITY, sizeof(u32)}; \ -}()) -#define wapp_u32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u32_array_pop(ARRAY_PTR) : \ - u32{} \ -) -#define wapp_u64_array(...) ([&]() { \ - wapp_persist u64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return U64Array{ \ - buf, \ - wapp_misc_utils_va_args_count(u64, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2), \ - sizeof(u64) \ - }; \ -}()) -#define wapp_u64_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist u64 buf[CAPACITY] = {}; \ - return U64Array{buf, 0, CAPACITY, sizeof(u64)}; \ -}()) -#define wapp_u64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u64_array_pop(ARRAY_PTR) : \ - u64{} \ -) -#define wapp_f32_array(...) ([&]() { \ - wapp_persist f32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return F32Array{ \ - buf, \ - wapp_misc_utils_va_args_count(f32, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2), \ - sizeof(f32) \ - }; \ -}()) -#define wapp_f32_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist f32 buf[CAPACITY] = {}; \ - return F32Array{buf, 0, CAPACITY, sizeof(f32)}; \ -}()) -#define wapp_f32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_f32_array_pop(ARRAY_PTR) : \ - f32{} \ -) -#define wapp_f64_array(...) ([&]() { \ - wapp_persist f64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return F64Array{ \ - buf, \ - wapp_misc_utils_va_args_count(f64, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2), \ - sizeof(f64) \ - }; \ -}()) -#define wapp_f64_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist f64 buf[CAPACITY] = {}; \ - return F64Array{buf, 0, CAPACITY, sizeof(f64)}; \ -}()) -#define wapp_f64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_f64_array_pop(ARRAY_PTR) : \ - f64{} \ -) -#define wapp_f128_array(...) ([&]() { \ - wapp_persist f128 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return F128Array{ \ - buf, \ - wapp_misc_utils_va_args_count(f128, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2), \ - sizeof(f128) \ - }; \ -}()) -#define wapp_f128_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist f128 buf[CAPACITY] = {}; \ - return F128Array{buf, 0, CAPACITY, sizeof(f128)}; \ -}()) -#define wapp_f128_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_f128_array_pop(ARRAY_PTR) : \ - f128{} \ -) -#define wapp_iptr_array(...) ([&]() { \ - wapp_persist iptr buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return IptrArray{ \ - buf, \ - wapp_misc_utils_va_args_count(iptr, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2), \ - sizeof(iptr) \ - }; \ -}()) -#define wapp_iptr_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist iptr buf[CAPACITY] = {}; \ - return IptrArray{buf, 0, CAPACITY, sizeof(iptr)}; \ -}()) -#define wapp_iptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_iptr_array_pop(ARRAY_PTR) : \ - iptr{} \ -) -#define wapp_uptr_array(...) ([&]() { \ - wapp_persist uptr buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return UptrArray{ \ - buf, \ - wapp_misc_utils_va_args_count(uptr, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2), \ - sizeof(uptr) \ - }; \ -}()) -#define wapp_uptr_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist uptr buf[CAPACITY] = {}; \ - return UptrArray{buf, 0, CAPACITY, sizeof(uptr)}; \ -}()) -#define wapp_uptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_uptr_array_pop(ARRAY_PTR) : \ - uptr{} \ -) #else -#define wapp_void_ptr_array(...) ((VoidPArray){ \ - .items = (void *[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(void *, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2), \ - .item_size = sizeof(void *) \ -}) -#define wapp_void_ptr_array_with_capacity(CAPACITY) ((VoidPArray){.items = (void *[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(void *)}) -#define wapp_void_ptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_void_ptr_array_pop(ARRAY_PTR) : \ - (void *){0} \ -) -#define wapp_str8_array(...) ((Str8Array){ \ - .items = (Str8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(Str8, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2), \ - .item_size = sizeof(Str8) \ -}) -#define wapp_str8_array_with_capacity(CAPACITY) ((Str8Array){.items = (Str8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(Str8)}) -#define wapp_str8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_str8_array_pop(ARRAY_PTR) : \ - (Str8){0} \ -) -#define wapp_b8_array(...) ((B8Array){ \ - .items = (b8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(b8, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b8, __VA_ARGS__) * 2), \ - .item_size = sizeof(b8) \ -}) -#define wapp_b8_array_with_capacity(CAPACITY) ((B8Array){.items = (b8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(b8)}) -#define wapp_b8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_b8_array_pop(ARRAY_PTR) : \ - (b8){0} \ -) -#define wapp_char_array(...) ((CharArray){ \ - .items = (char[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(char, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2), \ - .item_size = sizeof(char) \ -}) -#define wapp_char_array_with_capacity(CAPACITY) ((CharArray){.items = (char[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(char)}) -#define wapp_char_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_char_array_pop(ARRAY_PTR) : \ - (char){0} \ -) -#define wapp_c8_array(...) ((C8Array){ \ - .items = (c8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(c8, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2), \ - .item_size = sizeof(c8) \ -}) -#define wapp_c8_array_with_capacity(CAPACITY) ((C8Array){.items = (c8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c8)}) -#define wapp_c8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_c8_array_pop(ARRAY_PTR) : \ - (c8){0} \ -) -#define wapp_c16_array(...) ((C16Array){ \ - .items = (c16[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(c16, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2), \ - .item_size = sizeof(c16) \ -}) -#define wapp_c16_array_with_capacity(CAPACITY) ((C16Array){.items = (c16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c16)}) -#define wapp_c16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_c16_array_pop(ARRAY_PTR) : \ - (c16){0} \ -) -#define wapp_c32_array(...) ((C32Array){ \ - .items = (c32[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(c32, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2), \ - .item_size = sizeof(c32) \ -}) -#define wapp_c32_array_with_capacity(CAPACITY) ((C32Array){.items = (c32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c32)}) -#define wapp_c32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_c32_array_pop(ARRAY_PTR) : \ - (c32){0} \ -) -#define wapp_i8_array(...) ((I8Array){ \ - .items = (i8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(i8, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2), \ - .item_size = sizeof(i8) \ -}) -#define wapp_i8_array_with_capacity(CAPACITY) ((I8Array){.items = (i8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i8)}) -#define wapp_i8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i8_array_pop(ARRAY_PTR) : \ - (i8){0} \ -) -#define wapp_i16_array(...) ((I16Array){ \ - .items = (i16[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(i16, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2), \ - .item_size = sizeof(i16) \ -}) -#define wapp_i16_array_with_capacity(CAPACITY) ((I16Array){.items = (i16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i16)}) -#define wapp_i16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i16_array_pop(ARRAY_PTR) : \ - (i16){0} \ -) -#define wapp_i32_array(...) ((I32Array){ \ - .items = (i32[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(i32, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2), \ - .item_size = sizeof(i32) \ -}) -#define wapp_i32_array_with_capacity(CAPACITY) ((I32Array){.items = (i32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i32)}) -#define wapp_i32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i32_array_pop(ARRAY_PTR) : \ - (i32){0} \ -) -#define wapp_i64_array(...) ((I64Array){ \ - .items = (i64[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(i64, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2), \ - .item_size = sizeof(i64) \ -}) -#define wapp_i64_array_with_capacity(CAPACITY) ((I64Array){.items = (i64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i64)}) -#define wapp_i64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i64_array_pop(ARRAY_PTR) : \ - (i64){0} \ -) -#define wapp_u8_array(...) ((U8Array){ \ - .items = (u8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(u8, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2), \ - .item_size = sizeof(u8) \ -}) -#define wapp_u8_array_with_capacity(CAPACITY) ((U8Array){.items = (u8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u8)}) -#define wapp_u8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u8_array_pop(ARRAY_PTR) : \ - (u8){0} \ -) -#define wapp_u16_array(...) ((U16Array){ \ - .items = (u16[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(u16, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2), \ - .item_size = sizeof(u16) \ -}) -#define wapp_u16_array_with_capacity(CAPACITY) ((U16Array){.items = (u16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u16)}) -#define wapp_u16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u16_array_pop(ARRAY_PTR) : \ - (u16){0} \ -) -#define wapp_u32_array(...) ((U32Array){ \ - .items = (u32[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(u32, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2), \ - .item_size = sizeof(u32) \ -}) -#define wapp_u32_array_with_capacity(CAPACITY) ((U32Array){.items = (u32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u32)}) -#define wapp_u32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u32_array_pop(ARRAY_PTR) : \ - (u32){0} \ -) -#define wapp_u64_array(...) ((U64Array){ \ - .items = (u64[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(u64, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2), \ - .item_size = sizeof(u64) \ -}) -#define wapp_u64_array_with_capacity(CAPACITY) ((U64Array){.items = (u64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u64)}) -#define wapp_u64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u64_array_pop(ARRAY_PTR) : \ - (u64){0} \ -) -#define wapp_f32_array(...) ((F32Array){ \ - .items = (f32[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(f32, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2), \ - .item_size = sizeof(f32) \ -}) -#define wapp_f32_array_with_capacity(CAPACITY) ((F32Array){.items = (f32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f32)}) -#define wapp_f32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_f32_array_pop(ARRAY_PTR) : \ - (f32){0} \ -) -#define wapp_f64_array(...) ((F64Array){ \ - .items = (f64[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(f64, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2), \ - .item_size = sizeof(f64) \ -}) -#define wapp_f64_array_with_capacity(CAPACITY) ((F64Array){.items = (f64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f64)}) -#define wapp_f64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_f64_array_pop(ARRAY_PTR) : \ - (f64){0} \ -) -#define wapp_f128_array(...) ((F128Array){ \ - .items = (f128[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(f128, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2), \ - .item_size = sizeof(f128) \ -}) -#define wapp_f128_array_with_capacity(CAPACITY) ((F128Array){.items = (f128[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f128)}) -#define wapp_f128_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_f128_array_pop(ARRAY_PTR) : \ - (f128){0} \ -) -#define wapp_iptr_array(...) ((IptrArray){ \ - .items = (iptr[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(iptr, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2), \ - .item_size = sizeof(iptr) \ -}) -#define wapp_iptr_array_with_capacity(CAPACITY) ((IptrArray){.items = (iptr[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(iptr)}) -#define wapp_iptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_iptr_array_pop(ARRAY_PTR) : \ - (iptr){0} \ -) -#define wapp_uptr_array(...) ((UptrArray){ \ - .items = (uptr[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(uptr, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2), \ - .item_size = sizeof(uptr) \ -}) -#define wapp_uptr_array_with_capacity(CAPACITY) ((UptrArray){.items = (uptr[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(uptr)}) -#define wapp_uptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_uptr_array_pop(ARRAY_PTR) : \ - (uptr){0} \ -) +#define _stack_array(TYPE, SIZE) struct { ArrayHeader header; TYPE items[SIZE]; } +#define wapp_array(TYPE, ...) \ + (TYPE *)( \ + (_stack_array(TYPE, _calc_array_capacity(TYPE, __VA_ARGS__))){ \ + .header = { \ + .magic = WAPP_ARRAY_MAGIC, \ + .count = _calc_array_count(TYPE, __VA_ARGS__), \ + .capacity = _calc_array_capacity(TYPE, __VA_ARGS__), \ + .item_size = sizeof(TYPE), \ + }, \ + .items = {__VA_ARGS__}, \ + }.items \ + ) +#define wapp_array_with_capacity(TYPE, CAPACITY) \ + (TYPE *)( \ + (_stack_array(TYPE, CAPACITY)){ \ + .header = { \ + .magic = WAPP_ARRAY_MAGIC, \ + .count = 0, \ + .capacity = CAPACITY, \ + .item_size = sizeof(TYPE), \ + }, \ + .items = {0}, \ + }.items \ + ) +#define wapp_array_count(TYPE, ARRAY_PTR) \ + _array_count((u8 *)ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_capacity(TYPE, ARRAY_PTR) \ + _array_capacity((u8 *)ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_item_size(TYPE, ARRAY_PTR) \ + _array_item_size((u8 *)ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_get(TYPE, ARRAY_PTR, INDEX) \ + (*((TYPE *)_array_get((u8 *)ARRAY_PTR, INDEX, sizeof(TYPE)))) +#define wapp_array_set(TYPE, ARRAY_PTR, INDEX, VALUE_PTR) \ + _array_set((u8 *)ARRAY_PTR, INDEX, (u8 *)VALUE_PTR, sizeof(TYPE)) +#define wapp_array_append_capped(TYPE, ARRAY_PTR, VALUE_PTR) \ + _array_append_capped((u8 *)ARRAY_PTR, (u8 *)VALUE_PTR, sizeof(TYPE)) +#define wapp_array_extend_capped(TYPE, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + _array_extend_capped((u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_copy_capped(TYPE, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + _array_copy_capped((u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY_PTR, VALUE_PTR) \ + (TYPE *)_array_append_alloc(ALLOCATOR_PTR, (u8 *)ARRAY_PTR, (u8 *)VALUE_PTR, sizeof(TYPE)) +#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + (TYPE *)_array_extend_alloc(ALLOCATOR_PTR, (u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + (TYPE *)_array_copy_alloc(ALLOCATOR_PTR, (u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_pop(TYPE, ARRAY_PTR) \ + (ARRAY_PTR != NULL && _array_count((u8 *)ARRAY_PTR, sizeof(TYPE)) > 0 ? \ + *((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))) : \ + (TYPE){0} \ + ) +#define wapp_array_clear(TYPE, ARRAY_PTR) \ + _array_clear((u8 *)ARRAY_PTR, sizeof(TYPE)) #endif // !WAPP_PLATFORM_CPP -typedef struct str8 Str8; +typedef struct header ArrayHeader; +struct header { + u64 magic; + u64 count; + u64 capacity; + u64 item_size; +}; + +typedef struct array Array; +struct array { + ArrayHeader header; + u8 *items; +}; typedef struct GenericArray GenericArray; struct GenericArray { + ArrayHeader header; void *items; - u64 count; - u64 capacity; - u64 item_size; }; -typedef struct VoidPArray VoidPArray; -struct VoidPArray { - void * *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct Str8Array Str8Array; -struct Str8Array { - Str8 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct B8Array B8Array; -struct B8Array { - b8 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct CharArray CharArray; -struct CharArray { - char *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct C8Array C8Array; -struct C8Array { - c8 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct C16Array C16Array; -struct C16Array { - c16 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct C32Array C32Array; -struct C32Array { - c32 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct I8Array I8Array; -struct I8Array { - i8 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct I16Array I16Array; -struct I16Array { - i16 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct I32Array I32Array; -struct I32Array { - i32 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct I64Array I64Array; -struct I64Array { - i64 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct U8Array U8Array; -struct U8Array { - u8 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct U16Array U16Array; -struct U16Array { - u16 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct U32Array U32Array; -struct U32Array { - u32 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct U64Array U64Array; -struct U64Array { - u64 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct F32Array F32Array; -struct F32Array { - f32 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct F64Array F64Array; -struct F64Array { - f64 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct F128Array F128Array; -struct F128Array { - f128 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct IptrArray IptrArray; -struct IptrArray { - iptr *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct UptrArray UptrArray; -struct UptrArray { - uptr *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -void * *wapp_void_ptr_array_get(const VoidPArray *array, u64 index); -void wapp_void_ptr_array_set(VoidPArray *array, u64 index, void * *item); -void wapp_void_ptr_array_append_capped(VoidPArray *array, void * *item); -void wapp_void_ptr_array_extend_capped(VoidPArray *array, const VoidPArray *other); -void wapp_void_ptr_array_clear(VoidPArray *array); -void wapp_void_ptr_array_copy_capped(const VoidPArray *src, VoidPArray *dst); -VoidPArray *wapp_void_ptr_array_append_alloc(const Allocator *allocator, VoidPArray *array, void * *item); -VoidPArray *wapp_void_ptr_array_extend_alloc(const Allocator *allocator, VoidPArray *array, const VoidPArray *other); -VoidPArray *wapp_void_ptr_array_copy_alloc(const Allocator *allocator, const VoidPArray *src, VoidPArray *dst); -void * *_void_ptr_array_pop(VoidPArray *array); -Str8 *wapp_str8_array_get(const Str8Array *array, u64 index); -void wapp_str8_array_set(Str8Array *array, u64 index, Str8 *item); -void wapp_str8_array_append_capped(Str8Array *array, Str8 *item); -void wapp_str8_array_extend_capped(Str8Array *array, const Str8Array *other); -void wapp_str8_array_clear(Str8Array *array); -void wapp_str8_array_copy_capped(const Str8Array *src, Str8Array *dst); -Str8Array *wapp_str8_array_append_alloc(const Allocator *allocator, Str8Array *array, Str8 *item); -Str8Array *wapp_str8_array_extend_alloc(const Allocator *allocator, Str8Array *array, const Str8Array *other); -Str8Array *wapp_str8_array_copy_alloc(const Allocator *allocator, const Str8Array *src, Str8Array *dst); -Str8 *_str8_array_pop(Str8Array *array); -b8 *wapp_b8_array_get(const B8Array *array, u64 index); -void wapp_b8_array_set(B8Array *array, u64 index, b8 *item); -void wapp_b8_array_append_capped(B8Array *array, b8 *item); -void wapp_b8_array_extend_capped(B8Array *array, const B8Array *other); -void wapp_b8_array_clear(B8Array *array); -void wapp_b8_array_copy_capped(const B8Array *src, B8Array *dst); -B8Array *wapp_b8_array_append_alloc(const Allocator *allocator, B8Array *array, b8 *item); -B8Array *wapp_b8_array_extend_alloc(const Allocator *allocator, B8Array *array, const B8Array *other); -B8Array *wapp_b8_array_copy_alloc(const Allocator *allocator, const B8Array *src, B8Array *dst); -b8 *_b8_array_pop(B8Array *array); -char *wapp_char_array_get(const CharArray *array, u64 index); -void wapp_char_array_set(CharArray *array, u64 index, char *item); -void wapp_char_array_append_capped(CharArray *array, char *item); -void wapp_char_array_extend_capped(CharArray *array, const CharArray *other); -void wapp_char_array_clear(CharArray *array); -void wapp_char_array_copy_capped(const CharArray *src, CharArray *dst); -CharArray *wapp_char_array_append_alloc(const Allocator *allocator, CharArray *array, char *item); -CharArray *wapp_char_array_extend_alloc(const Allocator *allocator, CharArray *array, const CharArray *other); -CharArray *wapp_char_array_copy_alloc(const Allocator *allocator, const CharArray *src, CharArray *dst); -char *_char_array_pop(CharArray *array); -c8 *wapp_c8_array_get(const C8Array *array, u64 index); -void wapp_c8_array_set(C8Array *array, u64 index, c8 *item); -void wapp_c8_array_append_capped(C8Array *array, c8 *item); -void wapp_c8_array_extend_capped(C8Array *array, const C8Array *other); -void wapp_c8_array_clear(C8Array *array); -void wapp_c8_array_copy_capped(const C8Array *src, C8Array *dst); -C8Array *wapp_c8_array_append_alloc(const Allocator *allocator, C8Array *array, c8 *item); -C8Array *wapp_c8_array_extend_alloc(const Allocator *allocator, C8Array *array, const C8Array *other); -C8Array *wapp_c8_array_copy_alloc(const Allocator *allocator, const C8Array *src, C8Array *dst); -c8 *_c8_array_pop(C8Array *array); -c16 *wapp_c16_array_get(const C16Array *array, u64 index); -void wapp_c16_array_set(C16Array *array, u64 index, c16 *item); -void wapp_c16_array_append_capped(C16Array *array, c16 *item); -void wapp_c16_array_extend_capped(C16Array *array, const C16Array *other); -void wapp_c16_array_clear(C16Array *array); -void wapp_c16_array_copy_capped(const C16Array *src, C16Array *dst); -C16Array *wapp_c16_array_append_alloc(const Allocator *allocator, C16Array *array, c16 *item); -C16Array *wapp_c16_array_extend_alloc(const Allocator *allocator, C16Array *array, const C16Array *other); -C16Array *wapp_c16_array_copy_alloc(const Allocator *allocator, const C16Array *src, C16Array *dst); -c16 *_c16_array_pop(C16Array *array); -c32 *wapp_c32_array_get(const C32Array *array, u64 index); -void wapp_c32_array_set(C32Array *array, u64 index, c32 *item); -void wapp_c32_array_append_capped(C32Array *array, c32 *item); -void wapp_c32_array_extend_capped(C32Array *array, const C32Array *other); -void wapp_c32_array_clear(C32Array *array); -void wapp_c32_array_copy_capped(const C32Array *src, C32Array *dst); -C32Array *wapp_c32_array_append_alloc(const Allocator *allocator, C32Array *array, c32 *item); -C32Array *wapp_c32_array_extend_alloc(const Allocator *allocator, C32Array *array, const C32Array *other); -C32Array *wapp_c32_array_copy_alloc(const Allocator *allocator, const C32Array *src, C32Array *dst); -c32 *_c32_array_pop(C32Array *array); -i8 *wapp_i8_array_get(const I8Array *array, u64 index); -void wapp_i8_array_set(I8Array *array, u64 index, i8 *item); -void wapp_i8_array_append_capped(I8Array *array, i8 *item); -void wapp_i8_array_extend_capped(I8Array *array, const I8Array *other); -void wapp_i8_array_clear(I8Array *array); -void wapp_i8_array_copy_capped(const I8Array *src, I8Array *dst); -I8Array *wapp_i8_array_append_alloc(const Allocator *allocator, I8Array *array, i8 *item); -I8Array *wapp_i8_array_extend_alloc(const Allocator *allocator, I8Array *array, const I8Array *other); -I8Array *wapp_i8_array_copy_alloc(const Allocator *allocator, const I8Array *src, I8Array *dst); -i8 *_i8_array_pop(I8Array *array); -i16 *wapp_i16_array_get(const I16Array *array, u64 index); -void wapp_i16_array_set(I16Array *array, u64 index, i16 *item); -void wapp_i16_array_append_capped(I16Array *array, i16 *item); -void wapp_i16_array_extend_capped(I16Array *array, const I16Array *other); -void wapp_i16_array_clear(I16Array *array); -void wapp_i16_array_copy_capped(const I16Array *src, I16Array *dst); -I16Array *wapp_i16_array_append_alloc(const Allocator *allocator, I16Array *array, i16 *item); -I16Array *wapp_i16_array_extend_alloc(const Allocator *allocator, I16Array *array, const I16Array *other); -I16Array *wapp_i16_array_copy_alloc(const Allocator *allocator, const I16Array *src, I16Array *dst); -i16 *_i16_array_pop(I16Array *array); -i32 *wapp_i32_array_get(const I32Array *array, u64 index); -void wapp_i32_array_set(I32Array *array, u64 index, i32 *item); -void wapp_i32_array_append_capped(I32Array *array, i32 *item); -void wapp_i32_array_extend_capped(I32Array *array, const I32Array *other); -void wapp_i32_array_clear(I32Array *array); -void wapp_i32_array_copy_capped(const I32Array *src, I32Array *dst); -I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *array, i32 *item); -I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *array, const I32Array *other); -I32Array *wapp_i32_array_copy_alloc(const Allocator *allocator, const I32Array *src, I32Array *dst); -i32 *_i32_array_pop(I32Array *array); -i64 *wapp_i64_array_get(const I64Array *array, u64 index); -void wapp_i64_array_set(I64Array *array, u64 index, i64 *item); -void wapp_i64_array_append_capped(I64Array *array, i64 *item); -void wapp_i64_array_extend_capped(I64Array *array, const I64Array *other); -void wapp_i64_array_clear(I64Array *array); -void wapp_i64_array_copy_capped(const I64Array *src, I64Array *dst); -I64Array *wapp_i64_array_append_alloc(const Allocator *allocator, I64Array *array, i64 *item); -I64Array *wapp_i64_array_extend_alloc(const Allocator *allocator, I64Array *array, const I64Array *other); -I64Array *wapp_i64_array_copy_alloc(const Allocator *allocator, const I64Array *src, I64Array *dst); -i64 *_i64_array_pop(I64Array *array); -u8 *wapp_u8_array_get(const U8Array *array, u64 index); -void wapp_u8_array_set(U8Array *array, u64 index, u8 *item); -void wapp_u8_array_append_capped(U8Array *array, u8 *item); -void wapp_u8_array_extend_capped(U8Array *array, const U8Array *other); -void wapp_u8_array_clear(U8Array *array); -void wapp_u8_array_copy_capped(const U8Array *src, U8Array *dst); -U8Array *wapp_u8_array_append_alloc(const Allocator *allocator, U8Array *array, u8 *item); -U8Array *wapp_u8_array_extend_alloc(const Allocator *allocator, U8Array *array, const U8Array *other); -U8Array *wapp_u8_array_copy_alloc(const Allocator *allocator, const U8Array *src, U8Array *dst); -u8 *_u8_array_pop(U8Array *array); -u16 *wapp_u16_array_get(const U16Array *array, u64 index); -void wapp_u16_array_set(U16Array *array, u64 index, u16 *item); -void wapp_u16_array_append_capped(U16Array *array, u16 *item); -void wapp_u16_array_extend_capped(U16Array *array, const U16Array *other); -void wapp_u16_array_clear(U16Array *array); -void wapp_u16_array_copy_capped(const U16Array *src, U16Array *dst); -U16Array *wapp_u16_array_append_alloc(const Allocator *allocator, U16Array *array, u16 *item); -U16Array *wapp_u16_array_extend_alloc(const Allocator *allocator, U16Array *array, const U16Array *other); -U16Array *wapp_u16_array_copy_alloc(const Allocator *allocator, const U16Array *src, U16Array *dst); -u16 *_u16_array_pop(U16Array *array); -u32 *wapp_u32_array_get(const U32Array *array, u64 index); -void wapp_u32_array_set(U32Array *array, u64 index, u32 *item); -void wapp_u32_array_append_capped(U32Array *array, u32 *item); -void wapp_u32_array_extend_capped(U32Array *array, const U32Array *other); -void wapp_u32_array_clear(U32Array *array); -void wapp_u32_array_copy_capped(const U32Array *src, U32Array *dst); -U32Array *wapp_u32_array_append_alloc(const Allocator *allocator, U32Array *array, u32 *item); -U32Array *wapp_u32_array_extend_alloc(const Allocator *allocator, U32Array *array, const U32Array *other); -U32Array *wapp_u32_array_copy_alloc(const Allocator *allocator, const U32Array *src, U32Array *dst); -u32 *_u32_array_pop(U32Array *array); -u64 *wapp_u64_array_get(const U64Array *array, u64 index); -void wapp_u64_array_set(U64Array *array, u64 index, u64 *item); -void wapp_u64_array_append_capped(U64Array *array, u64 *item); -void wapp_u64_array_extend_capped(U64Array *array, const U64Array *other); -void wapp_u64_array_clear(U64Array *array); -void wapp_u64_array_copy_capped(const U64Array *src, U64Array *dst); -U64Array *wapp_u64_array_append_alloc(const Allocator *allocator, U64Array *array, u64 *item); -U64Array *wapp_u64_array_extend_alloc(const Allocator *allocator, U64Array *array, const U64Array *other); -U64Array *wapp_u64_array_copy_alloc(const Allocator *allocator, const U64Array *src, U64Array *dst); -u64 *_u64_array_pop(U64Array *array); -f32 *wapp_f32_array_get(const F32Array *array, u64 index); -void wapp_f32_array_set(F32Array *array, u64 index, f32 *item); -void wapp_f32_array_append_capped(F32Array *array, f32 *item); -void wapp_f32_array_extend_capped(F32Array *array, const F32Array *other); -void wapp_f32_array_clear(F32Array *array); -void wapp_f32_array_copy_capped(const F32Array *src, F32Array *dst); -F32Array *wapp_f32_array_append_alloc(const Allocator *allocator, F32Array *array, f32 *item); -F32Array *wapp_f32_array_extend_alloc(const Allocator *allocator, F32Array *array, const F32Array *other); -F32Array *wapp_f32_array_copy_alloc(const Allocator *allocator, const F32Array *src, F32Array *dst); -f32 *_f32_array_pop(F32Array *array); -f64 *wapp_f64_array_get(const F64Array *array, u64 index); -void wapp_f64_array_set(F64Array *array, u64 index, f64 *item); -void wapp_f64_array_append_capped(F64Array *array, f64 *item); -void wapp_f64_array_extend_capped(F64Array *array, const F64Array *other); -void wapp_f64_array_clear(F64Array *array); -void wapp_f64_array_copy_capped(const F64Array *src, F64Array *dst); -F64Array *wapp_f64_array_append_alloc(const Allocator *allocator, F64Array *array, f64 *item); -F64Array *wapp_f64_array_extend_alloc(const Allocator *allocator, F64Array *array, const F64Array *other); -F64Array *wapp_f64_array_copy_alloc(const Allocator *allocator, const F64Array *src, F64Array *dst); -f64 *_f64_array_pop(F64Array *array); -f128 *wapp_f128_array_get(const F128Array *array, u64 index); -void wapp_f128_array_set(F128Array *array, u64 index, f128 *item); -void wapp_f128_array_append_capped(F128Array *array, f128 *item); -void wapp_f128_array_extend_capped(F128Array *array, const F128Array *other); -void wapp_f128_array_clear(F128Array *array); -void wapp_f128_array_copy_capped(const F128Array *src, F128Array *dst); -F128Array *wapp_f128_array_append_alloc(const Allocator *allocator, F128Array *array, f128 *item); -F128Array *wapp_f128_array_extend_alloc(const Allocator *allocator, F128Array *array, const F128Array *other); -F128Array *wapp_f128_array_copy_alloc(const Allocator *allocator, const F128Array *src, F128Array *dst); -f128 *_f128_array_pop(F128Array *array); -iptr *wapp_iptr_array_get(const IptrArray *array, u64 index); -void wapp_iptr_array_set(IptrArray *array, u64 index, iptr *item); -void wapp_iptr_array_append_capped(IptrArray *array, iptr *item); -void wapp_iptr_array_extend_capped(IptrArray *array, const IptrArray *other); -void wapp_iptr_array_clear(IptrArray *array); -void wapp_iptr_array_copy_capped(const IptrArray *src, IptrArray *dst); -IptrArray *wapp_iptr_array_append_alloc(const Allocator *allocator, IptrArray *array, iptr *item); -IptrArray *wapp_iptr_array_extend_alloc(const Allocator *allocator, IptrArray *array, const IptrArray *other); -IptrArray *wapp_iptr_array_copy_alloc(const Allocator *allocator, const IptrArray *src, IptrArray *dst); -iptr *_iptr_array_pop(IptrArray *array); -uptr *wapp_uptr_array_get(const UptrArray *array, u64 index); -void wapp_uptr_array_set(UptrArray *array, u64 index, uptr *item); -void wapp_uptr_array_append_capped(UptrArray *array, uptr *item); -void wapp_uptr_array_extend_capped(UptrArray *array, const UptrArray *other); -void wapp_uptr_array_clear(UptrArray *array); -void wapp_uptr_array_copy_capped(const UptrArray *src, UptrArray *dst); -UptrArray *wapp_uptr_array_append_alloc(const Allocator *allocator, UptrArray *array, uptr *item); -UptrArray *wapp_uptr_array_extend_alloc(const Allocator *allocator, UptrArray *array, const UptrArray *other); -UptrArray *wapp_uptr_array_copy_alloc(const Allocator *allocator, const UptrArray *src, UptrArray *dst); -uptr *_uptr_array_pop(UptrArray *array); -GenericArray *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size); +u64 _array_count(u8 *array, u64 item_size); +u64 _array_capacity(u8 *array, u64 item_size); +u64 _array_item_size(u8 *array, u64 item_size); +u8 *_array_get(u8 *array, u64 index, u64 item_size); +void _array_set(u8 *array, u64 index, u8 *value, u64 item_size); +void _array_append_capped(u8 *array, u8 *value, u64 item_size); +void _array_extend_capped(u8 *array, const u8 *other, u64 item_size); +void _array_copy_capped(u8 *array, const u8 *other, u64 item_size); +u8 *_array_append_alloc(const Allocator *allocator, u8 *array, u8 *value, u64 item_size); +u8 *_array_extend_alloc(const Allocator *allocator, u8 *array, const u8 *other, u64 item_size); +u8 *_array_copy_alloc(const Allocator *allocator, u8 *array, const u8 *other, u64 item_size); +u8 *_array_pop(u8 *array, u64 item_size); +void _array_clear(u8 *array, u64 item_size); +u8 *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size); #ifdef WAPP_PLATFORM_CPP END_C_LINKAGE diff --git a/src/primitives/strings/str8/str8.c b/src/primitives/strings/str8/str8.c index 8f8dabb..c03b748 100644 --- a/src/primitives/strings/str8/str8.c +++ b/src/primitives/strings/str8/str8.c @@ -1,6 +1,7 @@ // vim:fileencoding=utf-8:foldmethod=marker #include "str8.h" +#include "../../array/array.h" #include "../../../common/aliases/aliases.h" #include "../../../common/assert/assert.h" #include "../../mem_allocator/mem_allocator.h" @@ -260,14 +261,15 @@ void wapp_str8_to_upper(Str8 *dst, Str8RO *src) { } } -void wapp_str8_from_bytes(Str8 *dst, const U8Array *src) { - u64 size = src->count * src->item_size; +void wapp_str8_from_bytes(Str8 *dst, const u8 *src_byte_array) { + wapp_debug_assert(src_byte_array != NULL && dst != NULL, "`dst` and `src` should not be NULL"); + + u64 size = wapp_array_count(u8, src_byte_array) * wapp_array_item_size(u8, src_byte_array); - wapp_debug_assert(src != NULL && dst != NULL, "`dst` and `src` should not be NULL"); wapp_debug_assert(dst->capacity >= size, "`dst` does not have enough capacity"); dst->size = size; - memcpy(dst->buf, src->items, size); + memcpy(dst->buf, src_byte_array, size); } i64 wapp_str8_find(Str8RO *str, Str8RO substr) { diff --git a/src/primitives/strings/str8/str8.h b/src/primitives/strings/str8/str8.h index cd69ef5..5a958b4 100644 --- a/src/primitives/strings/str8/str8.h +++ b/src/primitives/strings/str8/str8.h @@ -99,7 +99,7 @@ void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity); void wapp_str8_format(Str8 *dst, const char *format, ...); void wapp_str8_to_lower(Str8 *dst, Str8RO *src); void wapp_str8_to_upper(Str8 *dst, Str8RO *src); -void wapp_str8_from_bytes(Str8 *dst, const U8Array *src); +void wapp_str8_from_bytes(Str8 *dst, const u8 *src); /** * Str8 find functions diff --git a/tests/array/test_i32_array.c b/tests/array/test_i32_array.c index 0906fdd..793017d 100644 --- a/tests/array/test_i32_array.c +++ b/tests/array/test_i32_array.c @@ -4,16 +4,16 @@ TestFuncResult test_i32_array(void) { b8 result; - I32Array array = wapp_i32_array(1, 2, 3, 4, 5, 6, 7); - result = array.count == 7 && array.capacity == 16; + i32 *array = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7); + result = wapp_array_count(i32, array) == 7 && wapp_array_capacity(i32, array) == 16; - i32 *item; - u64 count = array.count; - u64 index = 0; + i32 item; + u64 count = wapp_array_count(i32, array); + u64 index = 0; b8 running = true; while (running) { - item = wapp_i32_array_get(&array, index); - result = result && item && (*item == (i32)(index + 1)); + item = wapp_array_get(i32, array, index); + result = result && item && item == (i32)(index + 1); ++index; running = index < count; @@ -25,8 +25,8 @@ TestFuncResult test_i32_array(void) { TestFuncResult test_i32_array_with_capacity(void) { b8 result; - I32Array array = wapp_i32_array_with_capacity(64); - result = array.count == 0 && array.capacity == 64; + i32 *array = wapp_array_with_capacity(i32, 64); + result = wapp_array_count(i32, array) == 0 && wapp_array_capacity(i32, array) == 64; return wapp_tester_result(result); } @@ -34,15 +34,15 @@ TestFuncResult test_i32_array_with_capacity(void) { TestFuncResult test_i32_array_get(void) { b8 result = true; - I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); - i32 *item; - u64 count = array.count; - u64 index = 0; + i32 item; + u64 count = wapp_array_count(i32, array); + u64 index = 0; b8 running = true; while (running) { - item = wapp_i32_array_get(&array, index); - result = result && item && (*item == (i32)index); + item = wapp_array_get(i32, array, index); + result = result && item == (i32)index; ++index; running = index < count; @@ -54,17 +54,17 @@ TestFuncResult test_i32_array_get(void) { TestFuncResult test_i32_array_set(void) { b8 result = true; - I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); - i32 *item; - u64 count = array.count; - u64 index = 0; + i32 item; + u64 count = wapp_array_count(i32, array); + u64 index = 0; b8 running = true; while (running) { i32 num = (i32)(index * 2); - wapp_i32_array_set(&array, index, &num); - item = wapp_i32_array_get(&array, index); - result = result && item && (*item == (i32)(index * 2)); + wapp_array_set(i32, array, index, &num); + item = wapp_array_get(i32, array, index); + result = result && item == (i32)(index * 2); ++index; running = index < count; @@ -76,17 +76,17 @@ TestFuncResult test_i32_array_set(void) { TestFuncResult test_i32_array_append_capped(void) { b8 result; - I32Array array = wapp_i32_array_with_capacity(64); - wapp_i32_array_append_capped(&array, &((i32){10})); + i32 *array = wapp_array_with_capacity(i32, 64); + wapp_array_append_capped(i32, array, &((i32){10})); - result = array.count == 1; - i32 *item = wapp_i32_array_get(&array, 0); - result = result && item && *item == 10; + result = wapp_array_count(i32, array) == 1; + i32 item = wapp_array_get(i32, array, 0); + result = result && item == 10; - array = wapp_i32_array(1); - wapp_i32_array_append_capped(&array, &((i32){10})); + array = wapp_array(i32, 1); + wapp_array_append_capped(i32, array, &((i32){10})); - result = result && array.count == 2; + result = result && wapp_array_count(i32, array) == 2; return wapp_tester_result(result); } @@ -94,41 +94,14 @@ TestFuncResult test_i32_array_append_capped(void) { TestFuncResult test_i32_array_extend_capped(void) { b8 result; - I32Array array1 = wapp_i32_array(1, 2, 3, 4); - I32Array array2 = wapp_i32_array(10, 20); + i32 *array1 = wapp_array(i32, 1, 2, 3, 4); + i32 *array2 = wapp_array(i32, 10, 20); - result = array1.count == 4 && array2.count == 2; + result = wapp_array_count(i32, array1) == 4 && wapp_array_count(i32, array2) == 2; - wapp_i32_array_extend_capped(&array1, &array2); + wapp_array_extend_capped(i32, array1, array2); - result = result && array1.count == 6; - - return wapp_tester_result(result); -} - -TestFuncResult test_i32_array_clear(void) { - b8 result; - - I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); - result = array.count == 9; - - wapp_i32_array_clear(&array); - - result = result && array.count == 0; - - return wapp_tester_result(result); -} - -TestFuncResult test_i32_array_pop(void) { - b8 result; - - I32Array array1 = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); - I32Array array2 = wapp_i32_array_with_capacity(32); - - i32 item1 = wapp_i32_array_pop(&array1); - i32 item2 = wapp_i32_array_pop(&array2); - - result = item1 == 8 && item2 == 0; + result = result && wapp_array_count(i32, array1) == 6; return wapp_tester_result(result); } @@ -136,31 +109,31 @@ TestFuncResult test_i32_array_pop(void) { TestFuncResult test_i32_array_copy_capped(void) { b8 result; - I32Array src = wapp_i32_array(1, 2, 3, 4, 5); - I32Array dst1 = wapp_i32_array(1, 2, 3, 4, 5, 6); - I32Array dst2 = wapp_i32_array(1, 2); + i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); + i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); + i32 *dst2 = wapp_array(i32, 1, 2); u64 expected_count = 5; - wapp_i32_array_copy_capped(&src, &dst1); - result = dst1.count == expected_count; + wapp_array_copy_capped(i32, dst1, src); + result = wapp_array_count(i32, dst1) == expected_count; u64 index = 0; b8 running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(&dst1, index)); + result = result && wapp_array_get(i32, src, index) == wapp_array_get(i32, dst1, index); ++index; running = index < expected_count; } expected_count = 4; - wapp_i32_array_copy_capped(&src, &dst2); - result = result && dst2.count == expected_count; + wapp_array_copy_capped(i32, dst2, src); + result = result && wapp_array_count(i32, dst2) == expected_count; index = 0; running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(&dst2, index)); + result = result && wapp_array_get(i32, src, index) == wapp_array_get(i32, dst2, index); ++index; running = index < expected_count; @@ -174,9 +147,9 @@ TestFuncResult test_i32_array_alloc_capacity(void) { Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); u64 capacity = 32; - I32Array *array = wapp_i32_array_alloc_capacity(&allocator, capacity); + i32 *array = wapp_array_alloc_capacity(i32, &allocator, capacity); - result = array && array->capacity == capacity; + result = array && wapp_array_capacity(i32, array) == capacity; wapp_mem_arena_allocator_destroy(&allocator); @@ -187,23 +160,23 @@ TestFuncResult test_i32_array_append_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - I32Array array1 = wapp_i32_array(1, 2, 3, 4, 5, 6, 7, 8); - I32Array array2 = wapp_i32_array(1, 2); + i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array2 = wapp_array(i32, 1, 2); - I32Array *arr_ptr = wapp_i32_array_append_alloc(&allocator, &array1, &((i32){10})); - result = arr_ptr == &array1; + i32 *arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &((i32){10})); + result = arr_ptr == array1; u64 count = 4; u64 index = 0; b8 running = true; while (running) { i32 num = (i32)index; - arr_ptr = wapp_i32_array_append_alloc(&allocator, &array2, &num); + arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num); ++index; running = index < count; } - result = result && arr_ptr != &array2; + result = result && arr_ptr != array2; wapp_mem_arena_allocator_destroy(&allocator); @@ -214,15 +187,15 @@ TestFuncResult test_i32_array_extend_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - I32Array array1 = wapp_i32_array(1, 2, 3, 4, 5, 6, 7, 8); - I32Array array2 = wapp_i32_array(1, 2); - I32Array array3 = wapp_i32_array(1, 2, 3, 4); + i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array2 = wapp_array(i32, 1, 2); + i32 *array3 = wapp_array(i32, 1, 2, 3, 4); - I32Array *arr_ptr = wapp_i32_array_extend_alloc(&allocator, &array1, &array3); - result = arr_ptr == &array1; + i32 *arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3); + result = arr_ptr == array1; - arr_ptr = wapp_i32_array_extend_alloc(&allocator, &array2, &array3); - result = result && arr_ptr != &array2; + arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3); + result = result && arr_ptr != array2; wapp_mem_arena_allocator_destroy(&allocator); @@ -233,32 +206,32 @@ TestFuncResult test_i32_array_copy_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - I32Array src = wapp_i32_array(1, 2, 3, 4, 5); - I32Array dst1 = wapp_i32_array(1, 2, 3, 4, 5, 6); - I32Array dst2 = wapp_i32_array(1, 2); - I32Array *array_ptr = NULL; + i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); + i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); + i32 *dst2 = wapp_array(i32, 1, 2); + i32 *array_ptr = NULL; u64 expected_count = 5; - array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst1); - result = array_ptr->count == expected_count && array_ptr == &dst1; + array_ptr = wapp_array_copy_alloc(i32, &allocator, dst1, src); + result = wapp_array_count(i32, array_ptr) == expected_count && array_ptr == dst1; u64 index = 0; b8 running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(array_ptr, index)); + result = result && wapp_array_get(i32, src, index) == wapp_array_get(i32, array_ptr, index); ++index; running = index < expected_count; } expected_count = 5; - array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst2); - result = result && array_ptr->count == expected_count && array_ptr != &dst2; + array_ptr = wapp_array_copy_alloc(i32, &allocator, dst2, src); + result = result && wapp_array_count(i32, array_ptr) == expected_count && array_ptr != dst2; index = 0; running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(array_ptr, index)); + result = result && wapp_array_get(i32, src, index) == wapp_array_get(i32, array_ptr, index); ++index; running = index < expected_count; @@ -268,3 +241,30 @@ TestFuncResult test_i32_array_copy_alloc(void) { return wapp_tester_result(result); } + +TestFuncResult test_i32_array_pop(void) { + b8 result; + + i32 *array1 = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array2 = wapp_array_with_capacity(i32, 32); + + i32 item1 = wapp_array_pop(i32, array1); + i32 item2 = wapp_array_pop(i32, array2); + + result = item1 == 8 && item2 == 0; + + return wapp_tester_result(result); +} + +TestFuncResult test_i32_array_clear(void) { + b8 result; + + i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); + result = wapp_array_count(i32, array) == 9; + + wapp_array_clear(i32, array); + + result = result && wapp_array_count(i32, array) == 0; + + return wapp_tester_result(result); +} diff --git a/tests/array/test_i32_array.h b/tests/array/test_i32_array.h index 6fdda5e..aa7b9f8 100644 --- a/tests/array/test_i32_array.h +++ b/tests/array/test_i32_array.h @@ -9,12 +9,12 @@ TestFuncResult test_i32_array_get(void); TestFuncResult test_i32_array_set(void); TestFuncResult test_i32_array_append_capped(void); TestFuncResult test_i32_array_extend_capped(void); -TestFuncResult test_i32_array_clear(void); -TestFuncResult test_i32_array_pop(void); TestFuncResult test_i32_array_copy_capped(void); TestFuncResult test_i32_array_alloc_capacity(void); TestFuncResult test_i32_array_append_alloc(void); TestFuncResult test_i32_array_extend_alloc(void); TestFuncResult test_i32_array_copy_alloc(void); +TestFuncResult test_i32_array_pop(void); +TestFuncResult test_i32_array_clear(void); #endif // !TEST_INT_ARRAY_H diff --git a/tests/array/test_str8_array.c b/tests/array/test_str8_array.c index 094ba74..fa2eadc 100644 --- a/tests/array/test_str8_array.c +++ b/tests/array/test_str8_array.c @@ -6,16 +6,16 @@ TestFuncResult test_str8_array(void) { b8 result; Str8 expected[] = {wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")}; - Str8Array array = wapp_str8_array(wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")); - result = array.count == 3 && array.capacity == 8; + Str8 *array = wapp_array(Str8, wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")); + result = wapp_array_count(Str8, array) == 3 && wapp_array_capacity(Str8, array) == 8; - Str8 *item; - u64 count = array.count; + Str8 item; + u64 count = wapp_array_count(Str8, array); u64 index = 0; b8 running = true; while (running) { - item = wapp_str8_array_get(&array, index); - result = result && item && (wapp_str8_equal(item, &expected[index])); + item = wapp_array_get(Str8, array, index); + result = result && (wapp_str8_equal(&item, &expected[index])); ++index; running = index < count; diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c index 5f7d5c4..834d415 100644 --- a/tests/str8/test_str8.c +++ b/tests/str8/test_str8.c @@ -615,11 +615,11 @@ TestFuncResult test_str8_join(void) { TestFuncResult test_str8_from_bytes(void) { b8 result; - Str8 str = wapp_str8_buf(1024); - U8Array bytes = wapp_u8_array('W', 'A', 'P', 'P'); - wapp_str8_from_bytes(&str, &bytes); + Str8 str = wapp_str8_buf(1024); + u8 *bytes = wapp_array(u8, 'W', 'A', 'P', 'P'); + wapp_str8_from_bytes(&str, bytes); - result = str.size == bytes.count * bytes.item_size; + result = str.size == wapp_array_count(u8, bytes) * wapp_array_item_size(u8, bytes); result = result && wapp_str8_equal(&str, &wapp_str8_lit_ro("WAPP")); return wapp_tester_result(result); diff --git a/tests/wapptest.c b/tests/wapptest.c index 4ce3290..63d71a3 100644 --- a/tests/wapptest.c +++ b/tests/wapptest.c @@ -27,13 +27,13 @@ int main(void) { test_i32_array_set, test_i32_array_append_capped, test_i32_array_extend_capped, - test_i32_array_clear, - test_i32_array_pop, test_i32_array_copy_capped, test_i32_array_alloc_capacity, test_i32_array_append_alloc, test_i32_array_extend_alloc, test_i32_array_copy_alloc, + test_i32_array_pop, + test_i32_array_clear, test_str8_lit, test_str8_lit_ro, test_str8_buf,