Replace VoidPArray with GenericArray and remove function implementations

This commit is contained in:
Abdelrahman Said
2025-09-14 23:13:36 +01:00
parent b7eff6a3e4
commit 033fccb8a1
4 changed files with 267 additions and 454 deletions

View File

@@ -159,155 +159,6 @@ Str8 *_str8_array_pop(Str8Array *array) {
return out;
}
void * *wapp_void_ptr_array_get(const VoidPArray *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 (void * *)ptr;
}
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) {
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);
}
void wapp_void_ptr_array_extend_capped(VoidPArray *array, const VoidPArray *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");
void * *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;
b32 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);
}
}
void wapp_void_ptr_array_clear(VoidPArray *array) {
wapp_debug_assert(array != NULL, "`array` should not be NULL");
array->count = 0;
}
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;
b32 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) {
wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL");
VoidPArray *output = array;
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);
if (!output) {
output = array;
goto RETURN_VOID_PTR_ARRAY_APPEND_ALLOC;
}
wapp_void_ptr_array_copy_capped(array, output);
}
wapp_void_ptr_array_append_capped(output, item);
RETURN_VOID_PTR_ARRAY_APPEND_ALLOC:
return output;
}
VoidPArray *wapp_void_ptr_array_extend_alloc(const Allocator *allocator, VoidPArray *array, const VoidPArray *other) {
wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL");
VoidPArray *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 = (VoidPArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size);
if (!output) {
output = array;
goto RETURN_VOID_PTR_ARRAY_EXTEND_ALLOC;
}
wapp_void_ptr_array_copy_capped(array, output);
}
wapp_void_ptr_array_extend_capped(output, other);
RETURN_VOID_PTR_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");
VoidPArray *output = dst;
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);
if (!output) {
output = dst;
goto RETURN_VOID_PTR_ARRAY_COPY_ALLOC;
}
}
wapp_void_ptr_array_clear(output);
wapp_void_ptr_array_copy_capped(src, output);
RETURN_VOID_PTR_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);
return out;
}
b32 *wapp_b32_array_get(const B32Array *array, u64 index) {
wapp_debug_assert(array != NULL, "`array` should not be NULL");
wapp_runtime_assert(index < array->count, "`index` is out of bounds");
@@ -2990,16 +2841,16 @@ uptr *_uptr_array_pop(UptrArray *array) {
return out;
}
VoidPArray *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size) {
wapp_debug_assert(allocator != NULL, "`array` should not be NULL");
GenericArray *_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(VoidPArray) + item_size * capacity;
VoidPArray *array = wapp_mem_allocator_alloc(allocator, allocation_size);
u64 allocation_size = sizeof(GenericArray) + item_size * capacity;
GenericArray *array = wapp_mem_allocator_alloc(allocator, allocation_size);
if (!array) {
goto RETURN_GENERIC_ARRAY_ALLOC;
}
array->items = (void * *)((u8 *)array + sizeof(VoidPArray));
array->items = (void *)((u8 *)array + sizeof(GenericArray));
array->count = 0;
array->capacity = capacity;
array->item_size = item_size;