From 217382b3cb37c0966c934b625431738f99da1d99 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 14 Dec 2025 00:36:59 +0000 Subject: [PATCH] Migrate C++ array API --- src/primitives/array/array.h | 85 ++++++++------ src/primitives/strings/str8/str8.h | 2 +- tests/array/test_i32_array.cc | 172 ++++++++++++++--------------- tests/array/test_str8_array.cc | 8 +- tests/str8/test_str8.cc | 6 +- 5 files changed, 143 insertions(+), 130 deletions(-) diff --git a/src/primitives/array/array.h b/src/primitives/array/array.h index 6f6bb7e..aa379f4 100644 --- a/src/primitives/array/array.h +++ b/src/primitives/array/array.h @@ -20,23 +20,41 @@ BEGIN_C_LINKAGE #define wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY) ((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(TYPE))) #ifdef WAPP_PLATFORM_CPP -#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{ \ - buf, \ - wapp_misc_utils_va_args_count(i32, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2), \ - sizeof(i32) \ - }; \ +#define wapp_array(TYPE, ...) ([&]() { \ + u64 capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \ + \ + TYPE items[_calc_array_capacity(TYPE, __VA_ARGS__)] = {__VA_ARGS__}; \ + \ + wapp_persist u8 array[ \ + sizeof(ArrayHeader) + _calc_array_capacity(TYPE, __VA_ARGS__) * sizeof(TYPE) \ + ] = {0}; \ + ArrayHeader *header = (ArrayHeader *)array; \ + header->magic = WAPP_ARRAY_MAGIC; \ + header->count = _calc_array_count(TYPE, __VA_ARGS__); \ + header->capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \ + header->item_size = sizeof(TYPE); \ + \ + u8 *buf = (u8 *)(header + 1); \ + memcpy(buf, items, capacity * sizeof(TYPE)); \ + return (TYPE *)buf; \ }()) -#define wapp_i32_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist i32 buf[CAPACITY] = {}; \ - return I32Array{buf, 0, CAPACITY, sizeof(i32)}; \ +#define wapp_array_with_capacity(TYPE, CAPACITY) ([&]() { \ + wapp_persist u8 array[ \ + sizeof(ArrayHeader) + CAPACITY * sizeof(TYPE) \ + ] = {0}; \ + ArrayHeader *header = (ArrayHeader *)array; \ + header->magic = WAPP_ARRAY_MAGIC; \ + header->count = 0; \ + header->capacity = CAPACITY; \ + header->item_size = sizeof(TYPE); \ + \ + return (TYPE *)(header + 1); \ }()) -#define wapp_i32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i32_array_pop(ARRAY_PTR) : \ - i32{} \ -) +#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{} \ + ) #else #define _stack_array(TYPE, SIZE) struct { ArrayHeader header; TYPE items[SIZE]; } #define wapp_array(TYPE, ...) \ @@ -63,6 +81,13 @@ BEGIN_C_LINKAGE .items = {0}, \ }.items \ ) +#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} \ + ) +#endif // !WAPP_PLATFORM_CPP + #define wapp_array_count(TYPE, ARRAY_PTR) \ _array_count((u8 *)ARRAY_PTR, sizeof(TYPE)) #define wapp_array_capacity(TYPE, ARRAY_PTR) \ @@ -85,14 +110,8 @@ BEGIN_C_LINKAGE (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 header ArrayHeader; struct header { @@ -102,32 +121,26 @@ struct header { u64 item_size; }; -typedef struct array Array; -struct array { - ArrayHeader header; - u8 *items; -}; - typedef struct GenericArray GenericArray; struct GenericArray { ArrayHeader header; void *items; }; -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); +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 *dst_array, const u8 *src_array, u64 item_size); void _array_copy_capped(u8 *dst_array, const u8 *src_array, 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 *dst_array, const u8 *src_array, u64 item_size); -u8 *_array_copy_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size); -u8 *_array_pop(u8 *array, 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 *dst_array, const u8 *src_array, u64 item_size); +u8 *_array_copy_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, 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); +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.h b/src/primitives/strings/str8/str8.h index e9abc58..fef0288 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 u8 *src); +void wapp_str8_from_bytes(Str8 *dst, const u8 *src_byte_array); /** * Str8 find functions diff --git a/tests/array/test_i32_array.cc b/tests/array/test_i32_array.cc index 2247ed9..19ff9dc 100644 --- a/tests/array/test_i32_array.cc +++ b/tests/array/test_i32_array.cc @@ -4,15 +4,15 @@ 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 count = wapp_array_count(i32, array); u64 index = 0; b8 running = true; while (running) { - item = wapp_i32_array_get(&array, index); + item = wapp_array_get(i32, array, index); result = result && item && (*item == (i32)(index + 1)); ++index; @@ -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,14 +34,14 @@ 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 count = wapp_array_count(i32, array); u64 index = 0; b8 running = true; while (running) { - item = wapp_i32_array_get(&array, index); + item = wapp_array_get(i32, array, index); result = result && item && (*item == (i32)index); ++index; @@ -54,16 +54,16 @@ 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 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); + wapp_array_set(i32, array, index, &num); + item = wapp_array_get(i32, array, index); result = result && item && (*item == (i32)(index * 2)); ++index; @@ -76,19 +76,19 @@ TestFuncResult test_i32_array_set(void) { TestFuncResult test_i32_array_append_capped(void) { b8 result; - I32Array array = wapp_i32_array_with_capacity(64); + i32 *array = wapp_array_with_capacity(i32, 64); i32 item1 = 10; - wapp_i32_array_append_capped(&array, &item1); + wapp_array_append_capped(i32, array, &item1); - result = array.count == 1; - i32 *item = wapp_i32_array_get(&array, 0); + result = wapp_array_count(i32, array) == 1; + i32 *item = wapp_array_get(i32, array, 0); result = result && item && *item == 10; - array = wapp_i32_array(1); + array = wapp_array(i32, 1); i32 item2 = 10; - wapp_i32_array_append_capped(&array, &item2); + wapp_array_append_capped(i32, array, &item2); - result = result && array.count == 2; + result = result && wapp_array_count(i32, array) == 2; return wapp_tester_result(result); } @@ -96,41 +96,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); } @@ -138,31 +111,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; @@ -176,9 +149,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); @@ -189,24 +162,24 @@ 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); i32 num = 10; - I32Array *arr_ptr = wapp_i32_array_append_alloc(&allocator, &array1, &num); - result = arr_ptr == &array1; + i32 *arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &num); + 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); @@ -217,15 +190,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); @@ -236,32 +209,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 = nullptr; + 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 = nullptr; 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; @@ -271,3 +244,30 @@ TestFuncResult test_i32_array_copy_alloc(void) { 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); +} + +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); +} diff --git a/tests/array/test_str8_array.cc b/tests/array/test_str8_array.cc index ac16b17..2cf02e1 100644 --- a/tests/array/test_str8_array.cc +++ b/tests/array/test_str8_array.cc @@ -10,16 +10,16 @@ TestFuncResult test_str8_array(void) { Str8 str1 = wapp_str8_lit("Hello"); Str8 str2 = wapp_str8_lit("Hi"); Str8 str3 = wapp_str8_lit("Bye"); - Str8Array array = wapp_str8_array(str1, str2, str3); + Str8 *array = wapp_array(Str8, str1, str2, str3); - result = array.count == 3 && array.capacity == 8; + result = wapp_array_count(Str8, array) == 3 && wapp_array_capacity(Str8, array) == 8; Str8 *item; - u64 count = array.count; + u64 count = wapp_array_count(Str8, array); u64 index = 0; b8 running = true; while (running) { - item = wapp_str8_array_get(&array, index); + item = wapp_array_get(Str8, array, index); result = result && item && (wapp_str8_equal(item, &expected[index])); ++index; diff --git a/tests/str8/test_str8.cc b/tests/str8/test_str8.cc index ba1309c..b6b63fc 100644 --- a/tests/str8/test_str8.cc +++ b/tests/str8/test_str8.cc @@ -617,10 +617,10 @@ TestFuncResult test_str8_from_bytes(void) { Str8 str = wapp_str8_buf(1024); Str8 expected = wapp_str8_lit_ro("WAPP"); - U8Array bytes = wapp_u8_array('W', 'A', 'P', 'P'); - wapp_str8_from_bytes(&str, &bytes); + 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, &expected); return wapp_tester_result(result);