Remove array structs #10

Merged
abdelrahman merged 6 commits from no-array-structs into main 2026-01-02 16:50:53 +00:00
16 changed files with 441 additions and 357 deletions

View File

@@ -147,7 +147,7 @@ uuid: install
clean: clean:
@rm -rf "$(BUILD_DIR)" @rm -rf "$(BUILD_DIR)"
@rm -rf "$(INCLUDE_INSTALL)" @rm -rf "$(INCLUDE_INSTALL)"
@rm "$(LIB_INSTALL)/$(LIB_STATIC_NAME)" @rm -f "$(LIB_INSTALL)/$(LIB_STATIC_NAME)"
builddir: builddir:
@mkdir -p "$(BUILD_DIR)" @mkdir -p "$(BUILD_DIR)"

View File

@@ -52,9 +52,6 @@ mkdir -p $ObjDir > $null
mkdir -p $OutDir > $null mkdir -p $OutDir > $null
mkdir -p $TestsDir > $null mkdir -p $TestsDir > $null
# Run code generation
Invoke-Expression "python -m codegen"
# Build and run C tests # Build and run C tests
Invoke-Expression "$Compiler $GeneralFlags $CStd $IncludeDirs $TestIncludeDirs $SrcFiles $TestCSrcFiles $TestCOutputFlags" -ErrorAction Stop Invoke-Expression "$Compiler $GeneralFlags $CStd $IncludeDirs $TestIncludeDirs $SrcFiles $TestCSrcFiles $TestCOutputFlags" -ErrorAction Stop
Invoke-Expression "$TestsDir/$TestCOutputBasename.exe" Invoke-Expression "$TestsDir/$TestCOutputBasename.exe"

View File

@@ -8,65 +8,113 @@
#include <stddef.h> #include <stddef.h>
#define _offset_pointer(PTR, OFFSET) ((void *)((uptr)(PTR) + (OFFSET))) #define _offset_pointer(PTR, OFFSET) ((void *)((uptr)(PTR) + (OFFSET)))
#define _array_header(ARRAY) (ArrayHeader *)(_offset_pointer(ARRAY, (i64)sizeof(ArrayHeader) * -1))
wapp_persist inline void _array_validate(const GenericArray *array, u64 item_size); wapp_persist inline void _array_validate(const GenericArray array, u64 item_size);
void *_array_get(GenericArray *array, u64 index, u64 item_size) { u64 _array_count(GenericArray array) {
wapp_runtime_assert(array != NULL, "`array` should not be NULL"); wapp_debug_assert(array != NULL, "`array` should not be NULL");
_array_validate(array, item_size);
wapp_runtime_assert(index < array->count, "`index` is out of bounds");
return _offset_pointer(array->items, array->item_size * index); ArrayHeader *header = _array_header(array);
wapp_runtime_assert(WAPP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
return header->count;
} }
void _array_set(GenericArray *array, u64 index, void *value, u64 item_size) { u64 _array_capacity(GenericArray array) {
wapp_debug_assert(array != NULL, "`array` should not be NULL");
ArrayHeader *header = _array_header(array);
wapp_runtime_assert(WAPP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
return header->capacity;
}
u64 _array_item_size(GenericArray array) {
wapp_debug_assert(array != NULL, "`array` should not be NULL");
ArrayHeader *header = _array_header(array);
wapp_runtime_assert(WAPP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
return header->item_size;
}
void _array_set_count(GenericArray array, u64 count) {
wapp_debug_assert(array != NULL, "`array` should not be NULL");
ArrayHeader *header = _array_header(array);
wapp_runtime_assert(WAPP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
header->count = count;
}
void *_array_get(GenericArray array, u64 index, u64 item_size) {
wapp_runtime_assert(array != NULL, "`array` should not be NULL");
_array_validate(array, item_size);
ArrayHeader *header = _array_header(array);
wapp_runtime_assert(index < header->count, "`index` is out of bounds");
return _offset_pointer(array, header->item_size * index);
}
void _array_set(GenericArray array, u64 index, void *value, u64 item_size) {
void *item = _array_get(array, index, item_size); void *item = _array_get(array, index, item_size);
memcpy(item, value, array->item_size);
ArrayHeader *header = _array_header(array);
memcpy(item, value, header->item_size);
} }
void _array_append_capped(GenericArray *array, void *value, u64 item_size) { void _array_append_capped(GenericArray array, void *value, u64 item_size) {
wapp_runtime_assert(array != NULL, "`array` should not be NULL"); wapp_runtime_assert(array != NULL, "`array` should not be NULL");
_array_validate(array, item_size); _array_validate(array, item_size);
if (array->count >= array->capacity) { return; } ArrayHeader *header = _array_header(array);
if (header->count >= header->capacity) { return; }
u64 index = (array->count)++; u64 index = (header->count)++;
_array_set(array, index, value, item_size); _array_set(array, index, value, item_size);
} }
void _array_extend_capped(GenericArray *dst, const GenericArray *src, u64 item_size) { void _array_extend_capped(GenericArray dst, const GenericArray src, u64 item_size) {
wapp_runtime_assert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL"); wapp_runtime_assert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL");
_array_validate(dst, item_size); _array_validate(dst, item_size);
_array_validate(src, item_size); _array_validate(src, item_size);
u64 remaining_capacity = dst->capacity - dst->count; ArrayHeader *src_header = _array_header(src);
ArrayHeader *dst_header = _array_header(dst);
u64 remaining_capacity = dst_header->capacity - dst_header->count;
u64 copy_count = src->count < remaining_capacity ? src->count : remaining_capacity; u64 copy_count = src_header->count < remaining_capacity ? src_header->count : remaining_capacity;
void *dst_ptr = _offset_pointer(dst->items, dst->count * dst->item_size); void *dst_ptr = _offset_pointer(dst, dst_header->count * dst_header->item_size);
memcpy(dst_ptr, src->items, copy_count * src->item_size); memcpy(dst_ptr, src, copy_count * src_header->item_size);
dst->count += copy_count; dst_header->count += copy_count;
} }
void _array_copy_capped(GenericArray *dst, const GenericArray *src, u64 item_size) { void _array_copy_capped(GenericArray dst, const GenericArray src, u64 item_size) {
wapp_runtime_assert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL"); wapp_runtime_assert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL");
_array_validate(dst, item_size); _array_validate(dst, item_size);
_array_validate(src, item_size); _array_validate(src, item_size);
_array_clear(dst, item_size); _array_clear(dst, item_size);
u64 copy_count = src->count < dst->capacity ? src->count : dst->capacity;
memcpy(dst->items, src->items, copy_count * src->item_size); ArrayHeader *src_header = _array_header(src);
dst->count = copy_count; ArrayHeader *dst_header = _array_header(dst);
u64 copy_count = src_header->count < dst_header->capacity ? src_header->count : dst_header->capacity;
memcpy((void *)dst, (void *)src, copy_count * src_header->item_size);
dst_header->count = copy_count;
} }
GenericArray *_array_append_alloc(const Allocator *allocator, GenericArray *array, void *value, u64 item_size) { GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value, u64 item_size) {
wapp_runtime_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); wapp_runtime_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL");
_array_validate(array, item_size); _array_validate(array, item_size);
GenericArray *output = array; GenericArray output = array;
if (array->count >= array->capacity) { ArrayHeader *header = _array_header(array);
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); if (header->count >= header->capacity) {
output = (GenericArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size, false); u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(header->capacity * 2);
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, header->item_size, false);
if (!output) { if (!output) {
output = array; output = array;
goto RETURN_ARRAY_APPEND_ALLOC; goto RETURN_ARRAY_APPEND_ALLOC;
@@ -80,17 +128,19 @@ RETURN_ARRAY_APPEND_ALLOC:
return output; return output;
} }
GenericArray *_array_extend_alloc(const Allocator *allocator, GenericArray *dst, const GenericArray *src, u64 item_size) { GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size) {
wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL"); wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
_array_validate(dst, item_size); _array_validate(dst, item_size);
_array_validate(src, item_size); _array_validate(src, item_size);
GenericArray *output = dst; GenericArray output = dst;
u64 remaining_capacity = dst->capacity - dst->count; ArrayHeader *src_header = _array_header(src);
if (src->count >= remaining_capacity) { ArrayHeader *dst_header = _array_header(dst);
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); u64 remaining_capacity = dst_header->capacity - dst_header->count;
output = (GenericArray *)_array_alloc_capacity(allocator, new_capacity, dst->item_size, false); if (src_header->count >= remaining_capacity) {
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2);
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, dst_header->item_size, false);
if (!output) { if (!output) {
output = dst; output = dst;
goto RETURN_ARRAY_EXTEND_ALLOC; goto RETURN_ARRAY_EXTEND_ALLOC;
@@ -104,16 +154,19 @@ RETURN_ARRAY_EXTEND_ALLOC:
return output; return output;
} }
GenericArray *_array_copy_alloc(const Allocator *allocator, GenericArray *dst, const GenericArray *src, u64 item_size) { GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size) {
wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL"); wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
_array_validate(dst, item_size); _array_validate(dst, item_size);
_array_validate(src, item_size); _array_validate(src, item_size);
GenericArray *output = dst; GenericArray output = dst;
if (src->count >= dst->capacity) { ArrayHeader *src_header = _array_header(src);
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); ArrayHeader *dst_header = _array_header(dst);
output = (GenericArray *)_array_alloc_capacity(allocator, new_capacity, src->item_size, false); if (src_header->count >= dst_header->capacity) {
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2);
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity,
src_header->item_size, false);
if (!output) { if (!output) {
output = dst; output = dst;
goto RETURN_ARRAY_COPY_ALLOC; goto RETURN_ARRAY_COPY_ALLOC;
@@ -126,48 +179,50 @@ RETURN_ARRAY_COPY_ALLOC:
return output; return output;
} }
void *_array_pop(GenericArray *array, u64 item_size) { void *_array_pop(GenericArray array, u64 item_size) {
wapp_runtime_assert(array != NULL, "`array` should not be NULL"); wapp_runtime_assert(array != NULL, "`array` should not be NULL");
_array_validate(array, item_size); _array_validate(array, item_size);
if (array->count == 0) { return NULL; } ArrayHeader *header = _array_header(array);
if (header->count == 0) { return NULL; }
u64 index = array->count - 1; u64 index = header->count - 1;
void *out = _array_get(array, index, item_size); void *out = _array_get(array, index, item_size);
--(array->count); --(header->count);
return out; return out;
} }
void _array_clear(GenericArray *array, u64 item_size) { void _array_clear(GenericArray array, u64 item_size) {
wapp_runtime_assert(array != NULL, "`array` should not be NULL"); wapp_runtime_assert(array != NULL, "`array` should not be NULL");
_array_validate(array, item_size); _array_validate(array, item_size);
array->count = 0; ArrayHeader *header = _array_header(array);
header->count = 0;
} }
GenericArray *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size, b8 fill) { GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size, b8 fill) {
wapp_runtime_assert(allocator != NULL, "`allocator` should not be NULL"); wapp_runtime_assert(allocator != NULL, "`allocator` should not be NULL");
GenericArray *output = NULL; GenericArray output = NULL;
u64 allocation_size = sizeof(GenericArray) + item_size * capacity; u64 allocation_size = sizeof(ArrayHeader) + item_size * capacity;
ArrayHeader *header = wapp_mem_allocator_alloc(allocator, allocation_size);
output = wapp_mem_allocator_alloc(allocator, allocation_size); if (!header) {
if (!output) {
goto RETURN_ARRAY_ALLOC; goto RETURN_ARRAY_ALLOC;
} }
output->magic = WAPP_ARRAY_MAGIC; output = (u8 *)(header + 1);
output->count = fill ? capacity : 0; header->magic = WAPP_ARRAY_MAGIC;
output->capacity = capacity; header->count = fill ? capacity : 0;
output->item_size = item_size; header->capacity = capacity;
output->items = (void *)(output + 1); header->item_size = item_size;
RETURN_ARRAY_ALLOC: RETURN_ARRAY_ALLOC:
return output; return output;
} }
wapp_persist inline void _array_validate(const GenericArray *array, u64 item_size) { wapp_persist inline void _array_validate(const GenericArray array, u64 item_size) {
wapp_runtime_assert(WAPP_ARRAY_MAGIC == array->magic, "`array` is not a valid wapp array"); ArrayHeader *header = _array_header(array);
wapp_runtime_assert(item_size == array->item_size, "Invalid item type provided"); wapp_runtime_assert(WAPP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
wapp_runtime_assert(item_size == header->item_size, "Invalid item type provided");
} }

View File

@@ -17,144 +17,176 @@ BEGIN_C_LINKAGE
#define _calc_array_count(TYPE, ...) wapp_misc_utils_va_args_count(TYPE, __VA_ARGS__) #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 _calc_array_capacity(TYPE, ...) wapp_misc_utils_u64_round_up_pow2(_calc_array_count(TYPE, __VA_ARGS__) * 2)
#define wapp_array_alloc_capacity(ELEM_TYPE, ARRAY_TYPE, ALLOCATOR_PTR, CAPACITY, FILL) \ #define wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, FILL) \
((ARRAY_TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(ELEM_TYPE), FILL)) ((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(TYPE), FILL))
#define WAPP_DEF_ARRAY_TYPE(T, NAME) \ typedef struct Str8 Str8;
typedef struct { \
u64 magic; \ // NOTE (Abdelrahman): Typedefs to distinguish arrays from regular pointers
u64 count; \ typedef void *GenericArray;
u64 capacity; \ typedef void **VoidPtrArray;
u64 item_size; \ typedef c8 *C8Array;
T *items; \ typedef c16 *C16Array;
} NAME typedef c32 *C32Array;
typedef u8 *U8Array;
typedef u16 *U16Array;
typedef u32 *U32Array;
typedef u64 *U64Array;
typedef b8 *B8Array;
typedef i8 *I8Array;
typedef i16 *I16Array;
typedef i32 *I32Array;
typedef i64 *I64Array;
typedef f32 *F32Array;
typedef f64 *F64Array;
typedef f128 *F128Array;
typedef uptr *UptrArray;
typedef iptr *IptrArray;
typedef Str8 *Str8Array;
#ifdef WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP
#define wapp_array(ELEM_TYPE, ARRAY_TYPE, ...) ([&]() { \ #define wapp_array(TYPE, ...) ([&]() { \
wapp_persist ELEM_TYPE items[sizeof(ELEM_TYPE) == sizeof(*((ARRAY_TYPE{}).items)) ? \ u64 capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \
_calc_array_capacity(ELEM_TYPE, __VA_ARGS__) : -1 \ \
] = {__VA_ARGS__}; \ TYPE items[_calc_array_capacity(TYPE, __VA_ARGS__)] = {__VA_ARGS__}; \
\ \
return ARRAY_TYPE{ \ wapp_persist u8 array[ \
WAPP_ARRAY_MAGIC, \ sizeof(ArrayHeader) + _calc_array_capacity(TYPE, __VA_ARGS__) * sizeof(TYPE) \
_calc_array_count(ELEM_TYPE, __VA_ARGS__), \ ] = {0}; \
_calc_array_capacity(ELEM_TYPE, __VA_ARGS__), \ ArrayHeader *header = (ArrayHeader *)array; \
sizeof(ELEM_TYPE), \ header->magic = WAPP_ARRAY_MAGIC; \
items, \ 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_array_with_capacity(ELEM_TYPE, ARRAY_TYPE, CAPACITY, FILL) ([&]() { \ #define wapp_array_with_capacity(TYPE, CAPACITY, FILL) ([&]() { \
wapp_persist ELEM_TYPE items[sizeof(ELEM_TYPE) == sizeof(*((ARRAY_TYPE{}).items)) ? \ wapp_persist u8 array[ \
CAPACITY : -1] = {}; \ sizeof(ArrayHeader) + CAPACITY * sizeof(TYPE) \
\ ] = {0}; \
return ARRAY_TYPE{ \ ArrayHeader *header = (ArrayHeader *)array; \
WAPP_ARRAY_MAGIC, \ header->magic = WAPP_ARRAY_MAGIC; \
FILL ? CAPACITY : 0, \ header->count = FILL ? CAPACITY : 0; \
CAPACITY, \ header->capacity = CAPACITY; \
sizeof(ELEM_TYPE), \ header->item_size = sizeof(TYPE); \
items, \ \
}; \ return (TYPE *)(header + 1); \
}()) }())
#define wapp_array_pop(ELEM_TYPE, ARRAY_PTR) ([&]() { \ #define wapp_array_pop(TYPE, ARRAY) ([&]() { \
if (ARRAY_PTR == NULL || (ARRAY_PTR)->count == 0) { \ if (ARRAY == NULL || _array_count((GenericArray)ARRAY) == 0) { \
ELEM_TYPE result{}; \ TYPE result{}; \
return result; \ return result; \
} \ } \
\ \
return *((ELEM_TYPE *)_array_pop((GenericArray *)ARRAY_PTR, sizeof(ELEM_TYPE))); \ return *((TYPE *)_array_pop((GenericArray)ARRAY, sizeof(TYPE))); \
}()) }())
#else #else
#define wapp_array(ELEM_TYPE, ARRAY_TYPE, ...) \ #define _stack_array(TYPE, SIZE) struct { ArrayHeader header; TYPE items[SIZE]; }
((ARRAY_TYPE){ \ #define wapp_array(TYPE, ...) \
.magic = WAPP_ARRAY_MAGIC, \ (TYPE *)( \
.count = _calc_array_count(ELEM_TYPE, __VA_ARGS__), \ (_stack_array(TYPE, _calc_array_capacity(TYPE, __VA_ARGS__))){ \
.capacity = _calc_array_capacity(ELEM_TYPE, __VA_ARGS__), \ .header = { \
.item_size = sizeof(ELEM_TYPE), \ .magic = WAPP_ARRAY_MAGIC, \
.items = (ELEM_TYPE[sizeof(ELEM_TYPE) == sizeof(*((ARRAY_TYPE){0}.items)) ? \ .count = _calc_array_count(TYPE, __VA_ARGS__), \
_calc_array_capacity(ELEM_TYPE, __VA_ARGS__) : \ .capacity = _calc_array_capacity(TYPE, __VA_ARGS__), \
-1]){__VA_ARGS__} \ .item_size = sizeof(TYPE), \
}) }, \
#define wapp_array_with_capacity(ELEM_TYPE, ARRAY_TYPE, CAPACITY, FILL) \ .items = {__VA_ARGS__}, \
((ARRAY_TYPE){ \ }.items \
.magic = WAPP_ARRAY_MAGIC, \ )
.count = FILL ? CAPACITY : 0, \ #define wapp_array_with_capacity(TYPE, CAPACITY, FILL) \
.capacity = CAPACITY, \ (TYPE *)( \
.item_size = sizeof(ELEM_TYPE), \ (_stack_array(TYPE, CAPACITY)){ \
.items = (ELEM_TYPE[sizeof(ELEM_TYPE) == sizeof(*((ARRAY_TYPE){0}.items)) ? \ .header = { \
CAPACITY : -1]){0} \ .magic = WAPP_ARRAY_MAGIC, \
}) .count = FILL ? CAPACITY : 0, \
#define wapp_array_pop(ELEM_TYPE, ARRAY_PTR) \ .capacity = CAPACITY, \
(ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ .item_size = sizeof(TYPE), \
*((ELEM_TYPE *)_array_pop((GenericArray *)ARRAY_PTR, sizeof(ELEM_TYPE))) : \ }, \
(ELEM_TYPE){0} \ .items = {0}, \
}.items \
)
#define wapp_array_pop(TYPE, ARRAY) \
(ARRAY == NULL || _array_count((GenericArray)ARRAY) == 0 ? \
(TYPE){0} : \
*((TYPE *)_array_pop((GenericArray)ARRAY, sizeof(TYPE))) \
) )
#endif // !WAPP_PLATFORM_CPP #endif // !WAPP_PLATFORM_CPP
#define wapp_array_get(ELEM_TYPE, ARRAY_PTR, INDEX) \ #define wapp_array_count(ARRAY) \
((ELEM_TYPE *)_array_get((GenericArray *)ARRAY_PTR, INDEX, sizeof(ELEM_TYPE))) _array_count((GenericArray)ARRAY)
#define wapp_array_set(ELEM_TYPE, ARRAY_PTR, INDEX, VALUE_PTR) \ #define wapp_array_capacity(ARRAY) \
_array_set((GenericArray *)ARRAY_PTR, INDEX, (void *)VALUE_PTR, sizeof(ELEM_TYPE)) _array_capacity((GenericArray)ARRAY)
#define wapp_array_append_capped(ELEM_TYPE, ARRAY_PTR, VALUE_PTR) \ #define wapp_array_item_size(ARRAY) \
_array_append_capped((GenericArray *)ARRAY_PTR, (void *)VALUE_PTR, sizeof(ELEM_TYPE)) _array_item_size((GenericArray)ARRAY)
#define wapp_array_extend_capped(ELEM_TYPE, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ #define wapp_array_set_count(ARRAY, COUNT) \
_array_extend_capped( \ _array_set_count((GenericArray)ARRAY, COUNT)
(GenericArray *)DST_ARRAY_PTR, (GenericArray *)SRC_ARRAY_PTR, sizeof(ELEM_TYPE) \ #define wapp_array_get(TYPE, ARRAY, INDEX) \
) ((TYPE *)_array_get((GenericArray)ARRAY, \
#define wapp_array_copy_capped(ELEM_TYPE, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ INDEX, \
_array_copy_capped( \ sizeof(TYPE)))
(GenericArray *)DST_ARRAY_PTR, (GenericArray *)SRC_ARRAY_PTR, sizeof(ELEM_TYPE) \ #define wapp_array_set(TYPE, ARRAY, INDEX, VALUE_PTR) \
) _array_set((GenericArray)ARRAY, \
#define wapp_array_append_alloc(ELEM_TYPE, ARRAY_TYPE, ALLOCATOR_PTR, ARRAY_PTR, VALUE_PTR) \ INDEX, \
(ARRAY_TYPE *)_array_append_alloc( \ (u8 *)VALUE_PTR, \
ALLOCATOR_PTR, (GenericArray *)ARRAY_PTR, (void *)VALUE_PTR, sizeof(ELEM_TYPE) \ sizeof(TYPE))
) #define wapp_array_append_capped(TYPE, ARRAY, VALUE_PTR) \
#define wapp_array_extend_alloc(ELEM_TYPE, ARRAY_TYPE, ALLOCATOR_PTR, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ _array_append_capped((GenericArray)ARRAY, \
(ARRAY_TYPE *)_array_extend_alloc( \ (u8 *)VALUE_PTR, \
ALLOCATOR_PTR, (GenericArray *)DST_ARRAY_PTR, (GenericArray *)SRC_ARRAY_PTR, sizeof(ELEM_TYPE) \ sizeof(TYPE))
) #define wapp_array_extend_capped(TYPE, DST_ARRAY, SRC_ARRAY) \
#define wapp_array_copy_alloc(ELEM_TYPE, ARRAY_TYPE, ALLOCATOR_PTR, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ _array_extend_capped((GenericArray)DST_ARRAY, \
(ARRAY_TYPE *)_array_copy_alloc( \ (GenericArray)SRC_ARRAY, \
ALLOCATOR_PTR, (GenericArray *)DST_ARRAY_PTR, (GenericArray *)SRC_ARRAY_PTR, sizeof(ELEM_TYPE) \ sizeof(TYPE))
) #define wapp_array_copy_capped(TYPE, DST_ARRAY, SRC_ARRAY) \
#define wapp_array_clear(ELEM_TYPE, ARRAY_PTR) \ _array_copy_capped((GenericArray)DST_ARRAY, \
_array_clear((GenericArray *)ARRAY_PTR, sizeof(ELEM_TYPE)) (GenericArray)SRC_ARRAY, \
sizeof(TYPE))
#define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY, VALUE_PTR) \
(TYPE *)_array_append_alloc(ALLOCATOR_PTR, \
(GenericArray)ARRAY, \
(u8 *)VALUE_PTR, \
sizeof(TYPE))
#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \
(TYPE *)_array_extend_alloc(ALLOCATOR_PTR, \
(GenericArray)DST_ARRAY, \
(GenericArray)SRC_ARRAY, \
sizeof(TYPE))
#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \
(TYPE *)_array_copy_alloc(ALLOCATOR_PTR, \
(GenericArray)DST_ARRAY, \
(GenericArray)SRC_ARRAY, \
sizeof(TYPE))
#define wapp_array_clear(TYPE, ARRAY) \
_array_clear((GenericArray)ARRAY, \
sizeof(TYPE))
WAPP_DEF_ARRAY_TYPE(void, GenericArray); typedef struct header ArrayHeader;
struct header {
u64 magic;
u64 count;
u64 capacity;
u64 item_size;
};
void *_array_get(GenericArray *array, u64 index, u64 item_size); u64 _array_count(GenericArray array);
void _array_set(GenericArray *array, u64 index, void *value, u64 item_size); u64 _array_capacity(GenericArray array);
void _array_append_capped(GenericArray *array, void *value, u64 item_size); u64 _array_item_size(GenericArray array);
void _array_extend_capped(GenericArray *dst, const GenericArray *src, u64 item_size); void _array_set_count(GenericArray array, u64 count);
void _array_copy_capped(GenericArray *dst, const GenericArray *src, u64 item_size); void *_array_get(GenericArray array, u64 index, u64 item_size);
GenericArray *_array_append_alloc(const Allocator *allocator, GenericArray *array, void *value, u64 item_size); void _array_set(GenericArray array, u64 index, void *value, u64 item_size);
GenericArray *_array_extend_alloc(const Allocator *allocator, GenericArray *dst, const GenericArray *src, u64 item_size); void _array_append_capped(GenericArray array, void *value, u64 item_size);
GenericArray *_array_copy_alloc(const Allocator *allocator, GenericArray *dst, const GenericArray *src, u64 item_size); void _array_extend_capped(GenericArray dst, const GenericArray src, u64 item_size);
void *_array_pop(GenericArray *array, u64 item_size); void _array_copy_capped(GenericArray dst, const GenericArray src, u64 item_size);
void _array_clear(GenericArray *array, u64 item_size); GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value, u64 item_size);
GenericArray *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size, b8 fill); GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size);
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size);
// Base array types void *_array_pop(GenericArray array, u64 item_size);
typedef struct Str8 Str8; void _array_clear(GenericArray array, u64 item_size);
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size, b8 fill);
WAPP_DEF_ARRAY_TYPE(void *, VoidPtrArray);
WAPP_DEF_ARRAY_TYPE(c8 , C8Array);
WAPP_DEF_ARRAY_TYPE(c16 , C16Array);
WAPP_DEF_ARRAY_TYPE(c32 , C32Array);
WAPP_DEF_ARRAY_TYPE(u8 , U8Array);
WAPP_DEF_ARRAY_TYPE(u16 , U16Array);
WAPP_DEF_ARRAY_TYPE(u32 , U32Array);
WAPP_DEF_ARRAY_TYPE(u64 , U64Array);
WAPP_DEF_ARRAY_TYPE(b8 , B8Array);
WAPP_DEF_ARRAY_TYPE(i8 , I8Array);
WAPP_DEF_ARRAY_TYPE(i16 , I16Array);
WAPP_DEF_ARRAY_TYPE(i32 , I32Array);
WAPP_DEF_ARRAY_TYPE(i64 , I64Array);
WAPP_DEF_ARRAY_TYPE(f32 , F32Array);
WAPP_DEF_ARRAY_TYPE(f64 , F64Array);
WAPP_DEF_ARRAY_TYPE(f128 , F128Array);
WAPP_DEF_ARRAY_TYPE(uptr , UptrArray);
WAPP_DEF_ARRAY_TYPE(iptr , IptrArray);
WAPP_DEF_ARRAY_TYPE(Str8 , Str8Array);
#ifdef WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP
END_C_LINKAGE END_C_LINKAGE

View File

@@ -275,15 +275,15 @@ 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 U8Array src) {
wapp_debug_assert(src != NULL && dst != NULL, "`dst` and `src` should not be NULL"); wapp_debug_assert(src != NULL && dst != NULL, "`dst` and `src` should not be NULL");
u64 size = src->count * src->item_size; u64 size = wapp_array_count(src) * wapp_array_item_size(src);
wapp_debug_assert(dst->capacity >= size, "`dst` does not have enough capacity"); wapp_debug_assert(dst->capacity >= size, "`dst` does not have enough capacity");
dst->size = size; dst->size = size;
memcpy(dst->buf, src->items, size); memcpy(dst->buf, src, size);
} }
i64 wapp_str8_find(Str8RO *str, Str8RO substr) { i64 wapp_str8_find(Str8RO *str, Str8RO substr) {

View File

@@ -101,7 +101,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_format(Str8 *dst, const char *format, ...);
void wapp_str8_to_lower(Str8 *dst, Str8RO *src); void wapp_str8_to_lower(Str8 *dst, Str8RO *src);
void wapp_str8_to_upper(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 U8Array src);
/** /**
* Str8 find functions * Str8 find functions

View File

@@ -62,13 +62,13 @@ u64 wapp_file_get_length(File *file) {
return output; return output;
} }
u64 wapp_file_read(GenericArray *dst_buf, File *file, u64 item_count) { u64 wapp_file_read(GenericArray dst_buf, File *file, u64 item_count) {
wapp_debug_assert(dst_buf != NULL && file != NULL, wapp_debug_assert(dst_buf != NULL && file != NULL,
"`dst_buf` and `file` should not be NULL."); "`dst_buf` and `file` should not be NULL.");
u64 file_length = wapp_file_get_length(file); u64 file_length = wapp_file_get_length(file);
u64 item_size = dst_buf->item_size; u64 item_size = wapp_array_item_size(dst_buf);
u64 dst_byte_capacity = dst_buf->capacity * item_size; u64 dst_byte_capacity = wapp_array_capacity(dst_buf) * item_size;
u64 req_byte_count = item_count * item_size; u64 req_byte_count = item_count * item_size;
u64 copy_byte_count = 0; u64 copy_byte_count = 0;
@@ -78,24 +78,24 @@ u64 wapp_file_read(GenericArray *dst_buf, File *file, u64 item_count) {
copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity; copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity;
} }
u64 count = fread(dst_buf->items, sizeof(u8), copy_byte_count, file); u64 count = fread(dst_buf, sizeof(u8), copy_byte_count, file);
if (ferror(file)) { return 0; } if (ferror(file)) { return 0; }
dst_buf->count = count / item_size; wapp_array_set_count(dst_buf, count / item_size);
return dst_buf->count; return wapp_array_count(dst_buf);
} }
u64 wapp_file_write(const GenericArray *src_buf, File *file, u64 item_count) { u64 wapp_file_write(const GenericArray src_buf, File *file, u64 item_count) {
wapp_debug_assert(src_buf != NULL && file != NULL, wapp_debug_assert(src_buf != NULL && file != NULL,
"`src_buf` and `file` should not be NULL."); "`src_buf` and `file` should not be NULL.");
u64 item_size = src_buf->item_size; u64 item_size = wapp_array_item_size(src_buf);
u64 src_byte_count = src_buf->count * item_size; u64 src_byte_count = wapp_array_count(src_buf) * item_size;
u64 req_byte_count = item_count * item_size; u64 req_byte_count = item_count * item_size;
u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count; u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count;
return fwrite(src_buf->items, sizeof(u8), to_copy, file); return fwrite(src_buf, sizeof(u8), to_copy, file);
} }
i32 wapp_file_flush(File *file) { i32 wapp_file_flush(File *file) {

View File

@@ -44,8 +44,8 @@ File *wapp_file_open(Str8RO *filename, FileAccessMode mode);
u64 wapp_file_get_current_position(File *file); u64 wapp_file_get_current_position(File *file);
i32 wapp_file_seek(File *file, u64 offset, FileSeekOrigin origin); i32 wapp_file_seek(File *file, u64 offset, FileSeekOrigin origin);
u64 wapp_file_get_length(File *file); u64 wapp_file_get_length(File *file);
u64 wapp_file_read(GenericArray *dst_buf, File *file, u64 item_count); u64 wapp_file_read(GenericArray dst_buf, File *file, u64 item_count);
u64 wapp_file_write(const GenericArray *src_buf, File *file, u64 item_count); u64 wapp_file_write(const GenericArray src_buf, File *file, u64 item_count);
i32 wapp_file_flush(File *file); i32 wapp_file_flush(File *file);
i32 wapp_file_close(File *file); i32 wapp_file_close(File *file);

View File

@@ -32,7 +32,7 @@ struct CMDResult {
b8 exited; b8 exited;
#ifdef WAPP_PLATFORM_WINDOWS #ifdef WAPP_PLATFORM_WINDOWS
#include "../../../../common/misc/misc_utils.h" #include "../../../common/misc/misc_utils.h"
wapp_misc_utils_padding_size(sizeof(b8) + sizeof(i32) + sizeof(CMDError)); wapp_misc_utils_padding_size(sizeof(b8) + sizeof(i32) + sizeof(CMDError));
#endif // !WAPP_PLATFORM_WINDOWS #endif // !WAPP_PLATFORM_WINDOWS
}; };

View File

@@ -7,7 +7,7 @@
#ifdef WAPP_PLATFORM_WINDOWS #ifdef WAPP_PLATFORM_WINDOWS
#include "../terminal_colours.h" #include "../terminal_colours.h"
#include "../../../../../common/misc/misc_utils.h" #include "../../../../common/misc/misc_utils.h"
#include <stdio.h> #include <stdio.h>
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN

View File

@@ -4,15 +4,15 @@
TestFuncResult test_i32_array(void) { TestFuncResult test_i32_array(void) {
b8 result; b8 result;
I32Array array = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7); I32Array array = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7);
result = array.count == 7 && array.capacity == 16; result = wapp_array_count(array) == 7 && wapp_array_capacity(array) == 16;
i32 *item; i32 *item;
u64 count = array.count; u64 count = wapp_array_count(array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_array_get(i32, &array, index); item = wapp_array_get(i32, array, index);
result = result && item && *item == (i32)(index + 1); result = result && item && *item == (i32)(index + 1);
++index; ++index;
@@ -25,11 +25,11 @@ TestFuncResult test_i32_array(void) {
TestFuncResult test_i32_array_with_capacity(void) { TestFuncResult test_i32_array_with_capacity(void) {
b8 result; b8 result;
I32Array array1 = wapp_array_with_capacity(i32, I32Array, 64, false); I32Array array1 = wapp_array_with_capacity(i32, 64, false);
result = array1.count == 0 && array1.capacity == 64; result = wapp_array_count(array1) == 0 && wapp_array_capacity(array1) == 64;
I32Array array2 = wapp_array_with_capacity(i32, I32Array, 64, true); I32Array array2 = wapp_array_with_capacity(i32, 64, true);
result = array2.count == 64 && array2.capacity == 64; result = wapp_array_count(array2) == 64 && wapp_array_capacity(array2) == 64;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -37,14 +37,14 @@ TestFuncResult test_i32_array_with_capacity(void) {
TestFuncResult test_i32_array_get(void) { TestFuncResult test_i32_array_get(void) {
b8 result = true; b8 result = true;
I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *item; i32 *item;
u64 count = array.count; u64 count = wapp_array_count(array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_array_get(i32, &array, index); item = wapp_array_get(i32, array, index);
result = result && item && *item == (i32)index; result = result && item && *item == (i32)index;
++index; ++index;
@@ -57,16 +57,16 @@ TestFuncResult test_i32_array_get(void) {
TestFuncResult test_i32_array_set(void) { TestFuncResult test_i32_array_set(void) {
b8 result = true; b8 result = true;
I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *item; i32 *item;
u64 count = array.count; u64 count = wapp_array_count(array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
i32 num = (i32)(index * 2); i32 num = (i32)(index * 2);
wapp_array_set(i32, &array, index, &num); wapp_array_set(i32, array, index, &num);
item = wapp_array_get(i32, &array, index); item = wapp_array_get(i32, array, index);
result = result && item && *item == (i32)(index * 2); result = result && item && *item == (i32)(index * 2);
++index; ++index;
@@ -79,17 +79,17 @@ TestFuncResult test_i32_array_set(void) {
TestFuncResult test_i32_array_append_capped(void) { TestFuncResult test_i32_array_append_capped(void) {
b8 result; b8 result;
I32Array array = wapp_array_with_capacity(i32, I32Array, 64, false); I32Array array = wapp_array_with_capacity(i32, 64, false);
wapp_array_append_capped(i32, &array, &((i32){10})); wapp_array_append_capped(i32, array, &((i32){10}));
result = array.count == 1; result = wapp_array_count(array) == 1;
i32 *item = wapp_array_get(i32, &array, 0); i32 *item = wapp_array_get(i32, array, 0);
result = result && item && *item == 10; result = result && item && *item == 10;
array = wapp_array(i32, I32Array, 1); array = wapp_array(i32, 1);
wapp_array_append_capped(i32, &array, &((i32){10})); wapp_array_append_capped(i32, array, &((i32){10}));
result = result && array.count == 2; result = result && wapp_array_count(array) == 2;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -97,14 +97,14 @@ TestFuncResult test_i32_array_append_capped(void) {
TestFuncResult test_i32_array_extend_capped(void) { TestFuncResult test_i32_array_extend_capped(void) {
b8 result; b8 result;
I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4); I32Array array1 = wapp_array(i32, 1, 2, 3, 4);
I32Array array2 = wapp_array(i32, I32Array, 10, 20); I32Array array2 = wapp_array(i32, 10, 20);
result = array1.count == 4 && array2.count == 2; result = wapp_array_count(array1) == 4 && wapp_array_count(array2) == 2;
wapp_array_extend_capped(i32, &array1, &array2); wapp_array_extend_capped(i32, array1, array2);
result = result && array1.count == 6; result = result && wapp_array_count(array1) == 6;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -112,31 +112,31 @@ TestFuncResult test_i32_array_extend_capped(void) {
TestFuncResult test_i32_array_copy_capped(void) { TestFuncResult test_i32_array_copy_capped(void) {
b8 result; b8 result;
I32Array src = wapp_array(i32, I32Array, 1, 2, 3, 4, 5); I32Array src = wapp_array(i32, 1, 2, 3, 4, 5);
I32Array dst1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6); I32Array dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6);
I32Array dst2 = wapp_array(i32, I32Array, 1, 2); I32Array dst2 = wapp_array(i32, 1, 2);
u64 expected_count = 5; u64 expected_count = 5;
wapp_array_copy_capped(i32, &dst1, &src); wapp_array_copy_capped(i32, dst1, src);
result = dst1.count == expected_count; result = wapp_array_count(dst1) == expected_count;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst1, index); result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst1, index);
++index; ++index;
running = index < expected_count; running = index < expected_count;
} }
expected_count = 4; expected_count = 4;
wapp_array_copy_capped(i32, &dst2, &src); wapp_array_copy_capped(i32, dst2, src);
result = result && dst2.count == expected_count; result = result && wapp_array_count(dst2) == expected_count;
index = 0; index = 0;
running = true; running = true;
while (running) { while (running) {
result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst2, index); result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst2, index);
++index; ++index;
running = index < expected_count; running = index < expected_count;
@@ -150,9 +150,9 @@ TestFuncResult test_i32_array_alloc_capacity(void) {
Allocator allocator = wapp_mem_arena_allocator_init(MiB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MiB(4));
u64 capacity = 32; u64 capacity = 32;
I32Array *array = wapp_array_alloc_capacity(i32, I32Array, &allocator, capacity, false); I32Array array = wapp_array_alloc_capacity(i32, &allocator, capacity, false);
result = array && array->capacity == capacity; result = array && wapp_array_capacity(array) == capacity;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -163,23 +163,23 @@ TestFuncResult test_i32_array_append_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MiB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MiB(4));
I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_array(i32, I32Array, 1, 2); I32Array array2 = wapp_array(i32, 1, 2);
I32Array *arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array1, &((i32){10})); I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &((i32){10}));
result = arr_ptr == &array1; result = arr_ptr == array1;
u64 count = 4; u64 count = 4;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
i32 num = (i32)index; i32 num = (i32)index;
arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array2, &num); arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num);
++index; ++index;
running = index < count; running = index < count;
} }
result = result && arr_ptr != &array2; result = result && arr_ptr != array2;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -190,15 +190,15 @@ TestFuncResult test_i32_array_extend_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MiB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MiB(4));
I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_array(i32, I32Array, 1, 2); I32Array array2 = wapp_array(i32, 1, 2);
I32Array array3 = wapp_array(i32, I32Array, 1, 2, 3, 4); I32Array array3 = wapp_array(i32, 1, 2, 3, 4);
I32Array *arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array1, &array3); I32Array arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3);
result = arr_ptr == &array1; result = arr_ptr == array1;
arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array2, &array3); arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3);
result = result && arr_ptr != &array2; result = result && arr_ptr != array2;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -209,32 +209,32 @@ TestFuncResult test_i32_array_copy_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MiB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MiB(4));
I32Array src = wapp_array(i32, I32Array, 1, 2, 3, 4, 5); I32Array src = wapp_array(i32, 1, 2, 3, 4, 5);
I32Array dst1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6); I32Array dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6);
I32Array dst2 = wapp_array(i32, I32Array, 1, 2); I32Array dst2 = wapp_array(i32, 1, 2);
I32Array *array_ptr = NULL; I32Array array = NULL;
u64 expected_count = 5; u64 expected_count = 5;
array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst1, &src); array = wapp_array_copy_alloc(i32, &allocator, dst1, src);
result = array_ptr->count == expected_count && array_ptr == &dst1; result = wapp_array_count(array) == expected_count && array == dst1;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index); result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, array, index);
++index; ++index;
running = index < expected_count; running = index < expected_count;
} }
expected_count = 5; expected_count = 5;
array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst2, &src); array = wapp_array_copy_alloc(i32, &allocator, dst2, src);
result = result && array_ptr->count == expected_count && array_ptr != &dst2; result = result && wapp_array_count(array) == expected_count && array != dst2;
index = 0; index = 0;
running = true; running = true;
while (running) { while (running) {
result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index); result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, array, index);
++index; ++index;
running = index < expected_count; running = index < expected_count;
@@ -248,11 +248,11 @@ TestFuncResult test_i32_array_copy_alloc(void) {
TestFuncResult test_i32_array_pop(void) { TestFuncResult test_i32_array_pop(void) {
b8 result; b8 result;
I32Array array1 = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array1 = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_array_with_capacity(i32, I32Array, 32, false); I32Array array2 = wapp_array_with_capacity(i32, 32, false);
i32 item1 = wapp_array_pop(i32, &array1); i32 item1 = wapp_array_pop(i32, array1);
i32 item2 = wapp_array_pop(i32, &array2); i32 item2 = wapp_array_pop(i32, array2);
result = item1 == 8 && item2 == 0; result = item1 == 8 && item2 == 0;
@@ -262,12 +262,12 @@ TestFuncResult test_i32_array_pop(void) {
TestFuncResult test_i32_array_clear(void) { TestFuncResult test_i32_array_clear(void) {
b8 result; b8 result;
I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
result = array.count == 9; result = wapp_array_count(array) == 9;
wapp_array_clear(i32, &array); wapp_array_clear(i32, array);
result = result && array.count == 0; result = result && wapp_array_count(array) == 0;
return wapp_tester_result(result); return wapp_tester_result(result);
} }

View File

@@ -4,15 +4,15 @@
TestFuncResult test_i32_array(void) { TestFuncResult test_i32_array(void) {
b8 result; b8 result;
I32Array array = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7); I32Array array = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7);
result = array.count == 7 && array.capacity == 16; result = wapp_array_count(array) == 7 && wapp_array_capacity(array) == 16;
i32 *item; i32 *item;
u64 count = array.count; u64 count = wapp_array_count(array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_array_get(i32, &array, index); item = wapp_array_get(i32, array, index);
result = result && item && (*item == (i32)(index + 1)); result = result && item && (*item == (i32)(index + 1));
++index; ++index;
@@ -25,11 +25,11 @@ TestFuncResult test_i32_array(void) {
TestFuncResult test_i32_array_with_capacity(void) { TestFuncResult test_i32_array_with_capacity(void) {
b8 result; b8 result;
I32Array array1 = wapp_array_with_capacity(i32, I32Array, 64, false); I32Array array1 = wapp_array_with_capacity(i32, 64, false);
result = array1.count == 0 && array1.capacity == 64; result = wapp_array_count(array1) == 0 && wapp_array_capacity(array1) == 64;
I32Array array2 = wapp_array_with_capacity(i32, I32Array, 64, true); I32Array array2 = wapp_array_with_capacity(i32, 64, true);
result = array2.count == 64 && array2.capacity == 64; result = wapp_array_count(array2) == 64 && wapp_array_capacity(array2) == 64;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -37,14 +37,14 @@ TestFuncResult test_i32_array_with_capacity(void) {
TestFuncResult test_i32_array_get(void) { TestFuncResult test_i32_array_get(void) {
b8 result = true; b8 result = true;
I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *item; i32 *item;
u64 count = array.count; u64 count = wapp_array_count(array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_array_get(i32, &array, index); item = wapp_array_get(i32, array, index);
result = result && item && (*item == (i32)index); result = result && item && (*item == (i32)index);
++index; ++index;
@@ -57,16 +57,16 @@ TestFuncResult test_i32_array_get(void) {
TestFuncResult test_i32_array_set(void) { TestFuncResult test_i32_array_set(void) {
b8 result = true; b8 result = true;
I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *item; i32 *item;
u64 count = array.count; u64 count = wapp_array_count(array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
i32 num = (i32)(index * 2); i32 num = (i32)(index * 2);
wapp_array_set(i32, &array, index, &num); wapp_array_set(i32, array, index, &num);
item = wapp_array_get(i32, &array, index); item = wapp_array_get(i32, array, index);
result = result && item && (*item == (i32)(index * 2)); result = result && item && (*item == (i32)(index * 2));
++index; ++index;
@@ -79,19 +79,19 @@ TestFuncResult test_i32_array_set(void) {
TestFuncResult test_i32_array_append_capped(void) { TestFuncResult test_i32_array_append_capped(void) {
b8 result; b8 result;
I32Array array = wapp_array_with_capacity(i32, I32Array, 64, false); I32Array array = wapp_array_with_capacity(i32, 64, false);
i32 item1 = 10; i32 item1 = 10;
wapp_array_append_capped(i32, &array, &item1); wapp_array_append_capped(i32, array, &item1);
result = array.count == 1; result = wapp_array_count(array) == 1;
i32 *item = wapp_array_get(i32, &array, 0); i32 *item = wapp_array_get(i32, array, 0);
result = result && item && *item == 10; result = result && item && *item == 10;
array = wapp_array(i32, I32Array, 1); array = wapp_array(i32, 1);
i32 item2 = 10; i32 item2 = 10;
wapp_array_append_capped(i32, &array, &item2); wapp_array_append_capped(i32, array, &item2);
result = result && array.count == 2; result = result && wapp_array_count(array) == 2;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -99,14 +99,14 @@ TestFuncResult test_i32_array_append_capped(void) {
TestFuncResult test_i32_array_extend_capped(void) { TestFuncResult test_i32_array_extend_capped(void) {
b8 result; b8 result;
I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4); I32Array array1 = wapp_array(i32, 1, 2, 3, 4);
I32Array array2 = wapp_array(i32, I32Array, 10, 20); I32Array array2 = wapp_array(i32, 10, 20);
result = array1.count == 4 && array2.count == 2; result = wapp_array_count(array1) == 4 && wapp_array_count(array2) == 2;
wapp_array_extend_capped(i32, &array1, &array2); wapp_array_extend_capped(i32, array1, array2);
result = result && array1.count == 6; result = result && wapp_array_count(array1) == 6;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -114,31 +114,31 @@ TestFuncResult test_i32_array_extend_capped(void) {
TestFuncResult test_i32_array_copy_capped(void) { TestFuncResult test_i32_array_copy_capped(void) {
b8 result; b8 result;
I32Array src = wapp_array(i32, I32Array, 1, 2, 3, 4, 5); I32Array src = wapp_array(i32, 1, 2, 3, 4, 5);
I32Array dst1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6); I32Array dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6);
I32Array dst2 = wapp_array(i32, I32Array, 1, 2); I32Array dst2 = wapp_array(i32, 1, 2);
u64 expected_count = 5; u64 expected_count = 5;
wapp_array_copy_capped(i32, &dst1, &src); wapp_array_copy_capped(i32, dst1, src);
result = dst1.count == expected_count; result = wapp_array_count(dst1) == expected_count;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst1, index)); result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst1, index));
++index; ++index;
running = index < expected_count; running = index < expected_count;
} }
expected_count = 4; expected_count = 4;
wapp_array_copy_capped(i32, &dst2, &src); wapp_array_copy_capped(i32, dst2, src);
result = result && dst2.count == expected_count; result = result && wapp_array_count(dst2) == expected_count;
index = 0; index = 0;
running = true; running = true;
while (running) { while (running) {
result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst2, index)); result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst2, index));
++index; ++index;
running = index < expected_count; running = index < expected_count;
@@ -152,9 +152,9 @@ TestFuncResult test_i32_array_alloc_capacity(void) {
Allocator allocator = wapp_mem_arena_allocator_init(MiB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MiB(4));
u64 capacity = 32; u64 capacity = 32;
I32Array *array = wapp_array_alloc_capacity(i32, I32Array, &allocator, capacity, false); I32Array array = wapp_array_alloc_capacity(i32, &allocator, capacity, false);
result = array && array->capacity == capacity; result = array && wapp_array_capacity(array) == capacity;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -165,24 +165,24 @@ TestFuncResult test_i32_array_append_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MiB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MiB(4));
I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_array(i32, I32Array, 1, 2); I32Array array2 = wapp_array(i32, 1, 2);
i32 num = 10; i32 num = 10;
I32Array *arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array1, &num); I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &num);
result = arr_ptr == &array1; result = arr_ptr == array1;
u64 count = 4; u64 count = 4;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
num = (i32)index; num = (i32)index;
arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array2, &num); arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num);
++index; ++index;
running = index < count; running = index < count;
} }
result = result && arr_ptr != &array2; result = result && arr_ptr != array2;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -193,15 +193,15 @@ TestFuncResult test_i32_array_extend_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MiB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MiB(4));
I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_array(i32, I32Array, 1, 2); I32Array array2 = wapp_array(i32, 1, 2);
I32Array array3 = wapp_array(i32, I32Array, 1, 2, 3, 4); I32Array array3 = wapp_array(i32, 1, 2, 3, 4);
I32Array *arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array1, &array3); I32Array array = wapp_array_extend_alloc(i32, &allocator, array1, array3);
result = arr_ptr == &array1; result = array == array1;
arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array2, &array3); array = wapp_array_extend_alloc(i32, &allocator, array2, array3);
result = result && arr_ptr != &array2; result = result && array != array2;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -212,32 +212,32 @@ TestFuncResult test_i32_array_copy_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MiB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MiB(4));
I32Array src = wapp_array(i32, I32Array, 1, 2, 3, 4, 5); I32Array src = wapp_array(i32, 1, 2, 3, 4, 5);
I32Array dst1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6); I32Array dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6);
I32Array dst2 = wapp_array(i32, I32Array, 1, 2); I32Array dst2 = wapp_array(i32, 1, 2);
I32Array *array_ptr = nullptr; I32Array array = nullptr;
u64 expected_count = 5; u64 expected_count = 5;
array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst1, &src); array = wapp_array_copy_alloc(i32, &allocator, dst1, src);
result = array_ptr->count == expected_count && array_ptr == &dst1; result = wapp_array_count(array) == expected_count && array == dst1;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index)); result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, array, index));
++index; ++index;
running = index < expected_count; running = index < expected_count;
} }
expected_count = 5; expected_count = 5;
array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst2, &src); array = wapp_array_copy_alloc(i32, &allocator, dst2, src);
result = result && array_ptr->count == expected_count && array_ptr != &dst2; result = result && wapp_array_count(array) == expected_count && array != dst2;
index = 0; index = 0;
running = true; running = true;
while (running) { while (running) {
result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index)); result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, array, index));
++index; ++index;
running = index < expected_count; running = index < expected_count;
@@ -251,12 +251,12 @@ TestFuncResult test_i32_array_copy_alloc(void) {
TestFuncResult test_i32_array_clear(void) { TestFuncResult test_i32_array_clear(void) {
b8 result; b8 result;
I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
result = array.count == 9; result = wapp_array_count(array) == 9;
wapp_array_clear(i32, &array); wapp_array_clear(i32, array);
result = result && array.count == 0; result = result && wapp_array_count(array) == 0;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -264,11 +264,11 @@ TestFuncResult test_i32_array_clear(void) {
TestFuncResult test_i32_array_pop(void) { TestFuncResult test_i32_array_pop(void) {
b8 result; b8 result;
I32Array array1 = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); I32Array array1 = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_array_with_capacity(i32, I32Array, 32, false); I32Array array2 = wapp_array_with_capacity(i32, 32, false);
i32 item1 = wapp_array_pop(i32, &array1); i32 item1 = wapp_array_pop(i32, array1);
i32 item2 = wapp_array_pop(i32, &array2); i32 item2 = wapp_array_pop(i32, array2);
result = item1 == 8 && item2 == 0; result = item1 == 8 && item2 == 0;

View File

@@ -6,15 +6,15 @@ TestFuncResult test_str8_array(void) {
b8 result; b8 result;
Str8 expected[] = {wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")}; Str8 expected[] = {wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")};
Str8Array array = wapp_array(Str8, Str8Array, wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")); Str8Array array = wapp_array(Str8, wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye"));
result = array.count == 3 && array.capacity == 8; result = wapp_array_count(array) == 3 && wapp_array_capacity(array) == 8;
Str8 *item; Str8 *item;
u64 count = array.count; u64 count = wapp_array_count(array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_array_get(Str8, &array, index); item = wapp_array_get(Str8, array, index);
result = result && item && (wapp_str8_equal(item, &expected[index])); result = result && item && (wapp_str8_equal(item, &expected[index]));
++index; ++index;

View File

@@ -10,16 +10,16 @@ TestFuncResult test_str8_array(void) {
Str8 str1 = wapp_str8_lit("Hello"); Str8 str1 = wapp_str8_lit("Hello");
Str8 str2 = wapp_str8_lit("Hi"); Str8 str2 = wapp_str8_lit("Hi");
Str8 str3 = wapp_str8_lit("Bye"); Str8 str3 = wapp_str8_lit("Bye");
Str8Array array = wapp_array(Str8, Str8Array, str1, str2, str3); Str8Array array = wapp_array(Str8, str1, str2, str3);
result = array.count == 3 && array.capacity == 8; result = wapp_array_count(array) == 3 && wapp_array_capacity(array) == 8;
Str8 *item; Str8 *item;
u64 count = array.count; u64 count = wapp_array_count(array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_array_get(Str8, &array, index); item = wapp_array_get(Str8, array, index);
result = result && item && (wapp_str8_equal(item, &expected[index])); result = result && item && (wapp_str8_equal(item, &expected[index]));
++index; ++index;

View File

@@ -616,10 +616,10 @@ TestFuncResult test_str8_from_bytes(void) {
b8 result; b8 result;
Str8 str = wapp_str8_buf(1024); Str8 str = wapp_str8_buf(1024);
U8Array bytes = wapp_array(u8, U8Array, 'W', 'A', 'P', 'P'); U8Array bytes = wapp_array(u8, 'W', 'A', 'P', 'P');
wapp_str8_from_bytes(&str, &bytes); wapp_str8_from_bytes(&str, bytes);
result = str.size == bytes.count * bytes.item_size; result = str.size == wapp_array_count(bytes) * wapp_array_item_size(bytes);
result = result && wapp_str8_equal(&str, &wapp_str8_lit_ro("WAPP")); result = result && wapp_str8_equal(&str, &wapp_str8_lit_ro("WAPP"));
return wapp_tester_result(result); return wapp_tester_result(result);

View File

@@ -617,10 +617,10 @@ TestFuncResult test_str8_from_bytes(void) {
Str8 str = wapp_str8_buf(1024); Str8 str = wapp_str8_buf(1024);
Str8 expected = wapp_str8_lit_ro("WAPP"); Str8 expected = wapp_str8_lit_ro("WAPP");
U8Array bytes = wapp_array(u8, U8Array, 'W', 'A', 'P', 'P'); U8Array bytes = wapp_array(u8, 'W', 'A', 'P', 'P');
wapp_str8_from_bytes(&str, &bytes); wapp_str8_from_bytes(&str, bytes);
result = str.size == bytes.count * bytes.item_size; result = str.size == wapp_array_count(bytes) * wapp_array_item_size(bytes);
result = result && wapp_str8_equal(&str, &expected); result = result && wapp_str8_equal(&str, &expected);
return wapp_tester_result(result); return wapp_tester_result(result);