File utilities and datatype implementation for a C-based code generator (#5)

Co-authored-by: Abdelrahman Said <said.abdelrahman@flawlessai.com>
Reviewed-on: #5
Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com>
Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
This commit is contained in:
2025-09-20 13:48:08 +00:00
committed by Abdelrahman Said
parent 09e96f8112
commit 14bd6ce5fd
52 changed files with 3814 additions and 944 deletions

View File

@@ -1,165 +1,15 @@
/**
* THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN
* THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN.
*/
#include "./array.h"
#include "../../common/assert/assert.h"
#include "../../common/aliases/aliases.h"
#include "../mem_allocator/mem_allocator.h"
#include "../../common/misc/misc_utils.h"
#include "../../common/aliases/aliases.h"
#include "../../common/platform/platform.h"
#include <stddef.h>
Str8 *wapp_str8_array_get(const Str8Array *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 (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;
b32 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;
b32 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;
}
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");
@@ -309,6 +159,155 @@ void * *_void_ptr_array_pop(VoidPArray *array) {
return out;
}
Str8 *wapp_str8_array_get(const Str8Array *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 (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;
b32 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;
b32 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;
}
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");
@@ -2991,16 +2990,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;