From df29d851c274c018a7cfa5000da47f35e33c1550 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 10 Aug 2025 16:18:21 +0100 Subject: [PATCH] Update array to support C++ --- codegen/array/make_array.py | 49 ++- codegen/array/snippets/array_pop_macro_cpp | 4 + codegen/array/snippets/stack_array_cpp | 9 + .../array/snippets/stack_capacity_array_cpp | 4 + src/common/misc/misc_utils.h | 9 + src/primitives/array/array.h | 384 +++++++++++++++++- 6 files changed, 433 insertions(+), 26 deletions(-) create mode 100644 codegen/array/snippets/array_pop_macro_cpp create mode 100644 codegen/array/snippets/stack_array_cpp create mode 100644 codegen/array/snippets/stack_capacity_array_cpp diff --git a/codegen/array/make_array.py b/codegen/array/make_array.py index af3922c..e395e34 100644 --- a/codegen/array/make_array.py +++ b/codegen/array/make_array.py @@ -153,7 +153,7 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): ) generic_funcs.append(alloc_capacity_func) - stack_array_macro = CMacro( + stack_array_cmacro = CMacro( name=f"wapp_{type_string_lower}_array(...)", value=__format_func_body( filename=snippets_dir / "stack_array", @@ -163,8 +163,18 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): array_typename=array_data.array_typename, ), ) + stack_array_cppmacro = CMacro( + name=f"wapp_{type_string_lower}_array(...)", + value=__format_func_body( + filename=snippets_dir / "stack_array_cpp", + type_string=type_string, + type_string_upper=type_string_upper, + type_string_lower=type_string_lower, + array_typename=array_data.array_typename, + ), + ) - stack_capacity_array_macro = CMacro( + stack_capacity_array_cmacro = CMacro( name=f"wapp_{type_string_lower}_array_with_capacity(CAPACITY)", value=__format_func_body( filename=snippets_dir / "stack_capacity_array", @@ -174,6 +184,16 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): array_typename=array_data.array_typename, ), ) + stack_capacity_array_cppmacro = CMacro( + name=f"wapp_{type_string_lower}_array_with_capacity(CAPACITY)", + value=__format_func_body( + filename=snippets_dir / "stack_capacity_array_cpp", + type_string=type_string, + type_string_upper=type_string_upper, + type_string_lower=type_string_lower, + array_typename=array_data.array_typename, + ), + ) alloc_capacity_array_macro = CMacro( name=f"wapp_{type_string_lower}_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY)", @@ -186,7 +206,7 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): ), ) - array_pop_macro = CMacro( + array_pop_cmacro = CMacro( name=f"wapp_{type_string_lower}_array_pop(ARRAY_PTR)", value=__format_func_body( filename=snippets_dir / "array_pop_macro", @@ -196,6 +216,16 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): array_typename=array_data.array_typename, ), ) + array_pop_cppmacro = CMacro( + name=f"wapp_{type_string_lower}_array_pop(ARRAY_PTR)", + value=__format_func_body( + filename=snippets_dir / "array_pop_macro_cpp", + type_string=type_string, + type_string_upper=type_string_upper, + type_string_lower=type_string_lower, + array_typename=array_data.array_typename, + ), + ) get_func = CFunc( name=f"wapp_{type_string_lower}_array_get", @@ -366,10 +396,17 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): header.decl_types.extend(array_data.hdr_decl_types) header.macros.extend([ - stack_array_macro, - stack_capacity_array_macro, alloc_capacity_array_macro, - array_pop_macro, + ]) + header.c_macros.extend([ + stack_array_cmacro, + stack_capacity_array_cmacro, + array_pop_cmacro, + ]) + header.cpp_macros.extend([ + stack_array_cppmacro, + stack_capacity_array_cppmacro, + array_pop_cppmacro, ]) header.types.extend([array]) header.funcs.extend([ diff --git a/codegen/array/snippets/array_pop_macro_cpp b/codegen/array/snippets/array_pop_macro_cpp new file mode 100644 index 0000000..41784b6 --- /dev/null +++ b/codegen/array/snippets/array_pop_macro_cpp @@ -0,0 +1,4 @@ +(ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_{Tlower}_array_pop(ARRAY_PTR) : \ + {T}{{}} \ +) diff --git a/codegen/array/snippets/stack_array_cpp b/codegen/array/snippets/stack_array_cpp new file mode 100644 index 0000000..938bccc --- /dev/null +++ b/codegen/array/snippets/stack_array_cpp @@ -0,0 +1,9 @@ +([]() {{ \ + persistent {T} buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count({T}, __VA_ARGS__) * 2)] = {{__VA_ARGS__}}; \ + return {ArrayType}{{ \ + buf, \ + wapp_misc_utils_va_args_count({T}, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count({T}, __VA_ARGS__) * 2), \ + sizeof({T}) \ + }}; \ +}}()) diff --git a/codegen/array/snippets/stack_capacity_array_cpp b/codegen/array/snippets/stack_capacity_array_cpp new file mode 100644 index 0000000..7ef0cb8 --- /dev/null +++ b/codegen/array/snippets/stack_capacity_array_cpp @@ -0,0 +1,4 @@ +([]() {{ \ + persistent {T} buf[CAPACITY] = {{}}; \ + return {ArrayType}{{buf, 0, CAPACITY, sizeof({T})}}; \ +}}()) diff --git a/src/common/misc/misc_utils.h b/src/common/misc/misc_utils.h index e416d2a..27c0074 100644 --- a/src/common/misc/misc_utils.h +++ b/src/common/misc/misc_utils.h @@ -38,10 +38,19 @@ BEGIN_C_LINKAGE ) + 1 \ ) +#ifdef WAPP_PLATFORM_CPP +#define wapp_misc_utils_va_args_count(T, ...) va_args_count(__VA_ARGS__) +#else #define wapp_misc_utils_va_args_count(T, ...) (sizeof((T[]){__VA_ARGS__})/sizeof(T)) +#endif // !WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP END_C_LINKAGE + +template +constexpr u64 va_args_count(Args&&...) { + return sizeof...(Args); +} #endif // !WAPP_PLATFORM_CPP #endif // !MISC_UTILS_H diff --git a/src/primitives/array/array.h b/src/primitives/array/array.h index 884e3a4..e4a1414 100644 --- a/src/primitives/array/array.h +++ b/src/primitives/array/array.h @@ -15,6 +15,369 @@ BEGIN_C_LINKAGE #endif // !WAPP_PLATFORM_CPP +#define wapp_str8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((Str8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(Str8))) +#define wapp_void_ptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((VoidPArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(void *))) +#define wapp_b32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((B32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(b32))) +#define wapp_char_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((CharArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(char))) +#define wapp_c8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c8))) +#define wapp_c16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c16))) +#define wapp_c32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c32))) +#define wapp_i8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i8))) +#define wapp_i16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i16))) +#define wapp_i32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i32))) +#define wapp_i64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i64))) +#define wapp_u8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u8))) +#define wapp_u16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u16))) +#define wapp_u32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u32))) +#define wapp_u64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u64))) +#define wapp_f32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f32))) +#define wapp_f64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f64))) +#define wapp_f128_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F128Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f128))) +#define wapp_iptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((IptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(iptr))) +#define wapp_uptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((UptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(uptr))) + +#ifdef WAPP_PLATFORM_CPP +#define wapp_str8_array(...) ([]() { \ + persistent Str8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return Str8Array{ \ + buf, \ + wapp_misc_utils_va_args_count(Str8, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2), \ + sizeof(Str8) \ + }; \ +}()) +#define wapp_str8_array_with_capacity(CAPACITY) ([]() { \ + persistent Str8 buf[CAPACITY] = {}; \ + return Str8Array{buf, 0, CAPACITY, sizeof(Str8)}; \ +}()) +#define wapp_str8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_str8_array_pop(ARRAY_PTR) : \ + Str8{} \ +) +#define wapp_void_ptr_array(...) ([]() { \ + persistent void * buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return VoidPArray{ \ + buf, \ + wapp_misc_utils_va_args_count(void *, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2), \ + sizeof(void *) \ + }; \ +}()) +#define wapp_void_ptr_array_with_capacity(CAPACITY) ([]() { \ + persistent void * buf[CAPACITY] = {}; \ + return VoidPArray{buf, 0, CAPACITY, sizeof(void *)}; \ +}()) +#define wapp_void_ptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_void_ptr_array_pop(ARRAY_PTR) : \ + void *{} \ +) +#define wapp_b32_array(...) ([]() { \ + persistent b32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return B32Array{ \ + buf, \ + wapp_misc_utils_va_args_count(b32, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b32, __VA_ARGS__) * 2), \ + sizeof(b32) \ + }; \ +}()) +#define wapp_b32_array_with_capacity(CAPACITY) ([]() { \ + persistent b32 buf[CAPACITY] = {}; \ + return B32Array{buf, 0, CAPACITY, sizeof(b32)}; \ +}()) +#define wapp_b32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_b32_array_pop(ARRAY_PTR) : \ + b32{} \ +) +#define wapp_char_array(...) ([]() { \ + persistent char buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return CharArray{ \ + buf, \ + wapp_misc_utils_va_args_count(char, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2), \ + sizeof(char) \ + }; \ +}()) +#define wapp_char_array_with_capacity(CAPACITY) ([]() { \ + persistent char buf[CAPACITY] = {}; \ + return CharArray{buf, 0, CAPACITY, sizeof(char)}; \ +}()) +#define wapp_char_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_char_array_pop(ARRAY_PTR) : \ + char{} \ +) +#define wapp_c8_array(...) ([]() { \ + persistent c8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return C8Array{ \ + buf, \ + wapp_misc_utils_va_args_count(c8, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2), \ + sizeof(c8) \ + }; \ +}()) +#define wapp_c8_array_with_capacity(CAPACITY) ([]() { \ + persistent c8 buf[CAPACITY] = {}; \ + return C8Array{buf, 0, CAPACITY, sizeof(c8)}; \ +}()) +#define wapp_c8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_c8_array_pop(ARRAY_PTR) : \ + c8{} \ +) +#define wapp_c16_array(...) ([]() { \ + persistent c16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return C16Array{ \ + buf, \ + wapp_misc_utils_va_args_count(c16, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2), \ + sizeof(c16) \ + }; \ +}()) +#define wapp_c16_array_with_capacity(CAPACITY) ([]() { \ + persistent c16 buf[CAPACITY] = {}; \ + return C16Array{buf, 0, CAPACITY, sizeof(c16)}; \ +}()) +#define wapp_c16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_c16_array_pop(ARRAY_PTR) : \ + c16{} \ +) +#define wapp_c32_array(...) ([]() { \ + persistent c32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return C32Array{ \ + buf, \ + wapp_misc_utils_va_args_count(c32, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2), \ + sizeof(c32) \ + }; \ +}()) +#define wapp_c32_array_with_capacity(CAPACITY) ([]() { \ + persistent c32 buf[CAPACITY] = {}; \ + return C32Array{buf, 0, CAPACITY, sizeof(c32)}; \ +}()) +#define wapp_c32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_c32_array_pop(ARRAY_PTR) : \ + c32{} \ +) +#define wapp_i8_array(...) ([]() { \ + persistent i8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return I8Array{ \ + buf, \ + wapp_misc_utils_va_args_count(i8, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2), \ + sizeof(i8) \ + }; \ +}()) +#define wapp_i8_array_with_capacity(CAPACITY) ([]() { \ + persistent i8 buf[CAPACITY] = {}; \ + return I8Array{buf, 0, CAPACITY, sizeof(i8)}; \ +}()) +#define wapp_i8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_i8_array_pop(ARRAY_PTR) : \ + i8{} \ +) +#define wapp_i16_array(...) ([]() { \ + persistent i16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return I16Array{ \ + buf, \ + wapp_misc_utils_va_args_count(i16, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2), \ + sizeof(i16) \ + }; \ +}()) +#define wapp_i16_array_with_capacity(CAPACITY) ([]() { \ + persistent i16 buf[CAPACITY] = {}; \ + return I16Array{buf, 0, CAPACITY, sizeof(i16)}; \ +}()) +#define wapp_i16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_i16_array_pop(ARRAY_PTR) : \ + i16{} \ +) +#define wapp_i32_array(...) ([]() { \ + persistent 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_i32_array_with_capacity(CAPACITY) ([]() { \ + persistent i32 buf[CAPACITY] = {}; \ + return I32Array{buf, 0, CAPACITY, sizeof(i32)}; \ +}()) +#define wapp_i32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_i32_array_pop(ARRAY_PTR) : \ + i32{} \ +) +#define wapp_i64_array(...) ([]() { \ + persistent i64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return I64Array{ \ + buf, \ + wapp_misc_utils_va_args_count(i64, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2), \ + sizeof(i64) \ + }; \ +}()) +#define wapp_i64_array_with_capacity(CAPACITY) ([]() { \ + persistent i64 buf[CAPACITY] = {}; \ + return I64Array{buf, 0, CAPACITY, sizeof(i64)}; \ +}()) +#define wapp_i64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_i64_array_pop(ARRAY_PTR) : \ + i64{} \ +) +#define wapp_u8_array(...) ([]() { \ + persistent u8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return U8Array{ \ + buf, \ + wapp_misc_utils_va_args_count(u8, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2), \ + sizeof(u8) \ + }; \ +}()) +#define wapp_u8_array_with_capacity(CAPACITY) ([]() { \ + persistent u8 buf[CAPACITY] = {}; \ + return U8Array{buf, 0, CAPACITY, sizeof(u8)}; \ +}()) +#define wapp_u8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_u8_array_pop(ARRAY_PTR) : \ + u8{} \ +) +#define wapp_u16_array(...) ([]() { \ + persistent u16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return U16Array{ \ + buf, \ + wapp_misc_utils_va_args_count(u16, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2), \ + sizeof(u16) \ + }; \ +}()) +#define wapp_u16_array_with_capacity(CAPACITY) ([]() { \ + persistent u16 buf[CAPACITY] = {}; \ + return U16Array{buf, 0, CAPACITY, sizeof(u16)}; \ +}()) +#define wapp_u16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_u16_array_pop(ARRAY_PTR) : \ + u16{} \ +) +#define wapp_u32_array(...) ([]() { \ + persistent u32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return U32Array{ \ + buf, \ + wapp_misc_utils_va_args_count(u32, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2), \ + sizeof(u32) \ + }; \ +}()) +#define wapp_u32_array_with_capacity(CAPACITY) ([]() { \ + persistent u32 buf[CAPACITY] = {}; \ + return U32Array{buf, 0, CAPACITY, sizeof(u32)}; \ +}()) +#define wapp_u32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_u32_array_pop(ARRAY_PTR) : \ + u32{} \ +) +#define wapp_u64_array(...) ([]() { \ + persistent u64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return U64Array{ \ + buf, \ + wapp_misc_utils_va_args_count(u64, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2), \ + sizeof(u64) \ + }; \ +}()) +#define wapp_u64_array_with_capacity(CAPACITY) ([]() { \ + persistent u64 buf[CAPACITY] = {}; \ + return U64Array{buf, 0, CAPACITY, sizeof(u64)}; \ +}()) +#define wapp_u64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_u64_array_pop(ARRAY_PTR) : \ + u64{} \ +) +#define wapp_f32_array(...) ([]() { \ + persistent f32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return F32Array{ \ + buf, \ + wapp_misc_utils_va_args_count(f32, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2), \ + sizeof(f32) \ + }; \ +}()) +#define wapp_f32_array_with_capacity(CAPACITY) ([]() { \ + persistent f32 buf[CAPACITY] = {}; \ + return F32Array{buf, 0, CAPACITY, sizeof(f32)}; \ +}()) +#define wapp_f32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_f32_array_pop(ARRAY_PTR) : \ + f32{} \ +) +#define wapp_f64_array(...) ([]() { \ + persistent f64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return F64Array{ \ + buf, \ + wapp_misc_utils_va_args_count(f64, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2), \ + sizeof(f64) \ + }; \ +}()) +#define wapp_f64_array_with_capacity(CAPACITY) ([]() { \ + persistent f64 buf[CAPACITY] = {}; \ + return F64Array{buf, 0, CAPACITY, sizeof(f64)}; \ +}()) +#define wapp_f64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_f64_array_pop(ARRAY_PTR) : \ + f64{} \ +) +#define wapp_f128_array(...) ([]() { \ + persistent f128 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return F128Array{ \ + buf, \ + wapp_misc_utils_va_args_count(f128, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2), \ + sizeof(f128) \ + }; \ +}()) +#define wapp_f128_array_with_capacity(CAPACITY) ([]() { \ + persistent f128 buf[CAPACITY] = {}; \ + return F128Array{buf, 0, CAPACITY, sizeof(f128)}; \ +}()) +#define wapp_f128_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_f128_array_pop(ARRAY_PTR) : \ + f128{} \ +) +#define wapp_iptr_array(...) ([]() { \ + persistent iptr buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return IptrArray{ \ + buf, \ + wapp_misc_utils_va_args_count(iptr, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2), \ + sizeof(iptr) \ + }; \ +}()) +#define wapp_iptr_array_with_capacity(CAPACITY) ([]() { \ + persistent iptr buf[CAPACITY] = {}; \ + return IptrArray{buf, 0, CAPACITY, sizeof(iptr)}; \ +}()) +#define wapp_iptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_iptr_array_pop(ARRAY_PTR) : \ + iptr{} \ +) +#define wapp_uptr_array(...) ([]() { \ + persistent uptr buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return UptrArray{ \ + buf, \ + wapp_misc_utils_va_args_count(uptr, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2), \ + sizeof(uptr) \ + }; \ +}()) +#define wapp_uptr_array_with_capacity(CAPACITY) ([]() { \ + persistent uptr buf[CAPACITY] = {}; \ + return UptrArray{buf, 0, CAPACITY, sizeof(uptr)}; \ +}()) +#define wapp_uptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_uptr_array_pop(ARRAY_PTR) : \ + uptr{} \ +) +#else #define wapp_str8_array(...) ((Str8Array){ \ .items = (Str8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ .count = wapp_misc_utils_va_args_count(Str8, __VA_ARGS__), \ @@ -22,7 +385,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(Str8) \ }) #define wapp_str8_array_with_capacity(CAPACITY) ((Str8Array){.items = (Str8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(Str8)}) -#define wapp_str8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((Str8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(Str8))) #define wapp_str8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_str8_array_pop(ARRAY_PTR) : \ (Str8){0} \ @@ -34,7 +396,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(void *) \ }) #define wapp_void_ptr_array_with_capacity(CAPACITY) ((VoidPArray){.items = (void *[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(void *)}) -#define wapp_void_ptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((VoidPArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(void *))) #define wapp_void_ptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_void_ptr_array_pop(ARRAY_PTR) : \ (void *){0} \ @@ -46,7 +407,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(b32) \ }) #define wapp_b32_array_with_capacity(CAPACITY) ((B32Array){.items = (b32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(b32)}) -#define wapp_b32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((B32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(b32))) #define wapp_b32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_b32_array_pop(ARRAY_PTR) : \ (b32){0} \ @@ -58,7 +418,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(char) \ }) #define wapp_char_array_with_capacity(CAPACITY) ((CharArray){.items = (char[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(char)}) -#define wapp_char_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((CharArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(char))) #define wapp_char_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_char_array_pop(ARRAY_PTR) : \ (char){0} \ @@ -70,7 +429,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(c8) \ }) #define wapp_c8_array_with_capacity(CAPACITY) ((C8Array){.items = (c8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c8)}) -#define wapp_c8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c8))) #define wapp_c8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_c8_array_pop(ARRAY_PTR) : \ (c8){0} \ @@ -82,7 +440,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(c16) \ }) #define wapp_c16_array_with_capacity(CAPACITY) ((C16Array){.items = (c16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c16)}) -#define wapp_c16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c16))) #define wapp_c16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_c16_array_pop(ARRAY_PTR) : \ (c16){0} \ @@ -94,7 +451,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(c32) \ }) #define wapp_c32_array_with_capacity(CAPACITY) ((C32Array){.items = (c32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c32)}) -#define wapp_c32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c32))) #define wapp_c32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_c32_array_pop(ARRAY_PTR) : \ (c32){0} \ @@ -106,7 +462,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(i8) \ }) #define wapp_i8_array_with_capacity(CAPACITY) ((I8Array){.items = (i8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i8)}) -#define wapp_i8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i8))) #define wapp_i8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_i8_array_pop(ARRAY_PTR) : \ (i8){0} \ @@ -118,7 +473,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(i16) \ }) #define wapp_i16_array_with_capacity(CAPACITY) ((I16Array){.items = (i16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i16)}) -#define wapp_i16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i16))) #define wapp_i16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_i16_array_pop(ARRAY_PTR) : \ (i16){0} \ @@ -130,7 +484,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(i32) \ }) #define wapp_i32_array_with_capacity(CAPACITY) ((I32Array){.items = (i32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i32)}) -#define wapp_i32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i32))) #define wapp_i32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_i32_array_pop(ARRAY_PTR) : \ (i32){0} \ @@ -142,7 +495,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(i64) \ }) #define wapp_i64_array_with_capacity(CAPACITY) ((I64Array){.items = (i64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i64)}) -#define wapp_i64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i64))) #define wapp_i64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_i64_array_pop(ARRAY_PTR) : \ (i64){0} \ @@ -154,7 +506,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(u8) \ }) #define wapp_u8_array_with_capacity(CAPACITY) ((U8Array){.items = (u8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u8)}) -#define wapp_u8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u8))) #define wapp_u8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_u8_array_pop(ARRAY_PTR) : \ (u8){0} \ @@ -166,7 +517,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(u16) \ }) #define wapp_u16_array_with_capacity(CAPACITY) ((U16Array){.items = (u16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u16)}) -#define wapp_u16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u16))) #define wapp_u16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_u16_array_pop(ARRAY_PTR) : \ (u16){0} \ @@ -178,7 +528,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(u32) \ }) #define wapp_u32_array_with_capacity(CAPACITY) ((U32Array){.items = (u32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u32)}) -#define wapp_u32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u32))) #define wapp_u32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_u32_array_pop(ARRAY_PTR) : \ (u32){0} \ @@ -190,7 +539,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(u64) \ }) #define wapp_u64_array_with_capacity(CAPACITY) ((U64Array){.items = (u64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u64)}) -#define wapp_u64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u64))) #define wapp_u64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_u64_array_pop(ARRAY_PTR) : \ (u64){0} \ @@ -202,7 +550,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(f32) \ }) #define wapp_f32_array_with_capacity(CAPACITY) ((F32Array){.items = (f32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f32)}) -#define wapp_f32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f32))) #define wapp_f32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_f32_array_pop(ARRAY_PTR) : \ (f32){0} \ @@ -214,7 +561,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(f64) \ }) #define wapp_f64_array_with_capacity(CAPACITY) ((F64Array){.items = (f64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f64)}) -#define wapp_f64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f64))) #define wapp_f64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_f64_array_pop(ARRAY_PTR) : \ (f64){0} \ @@ -226,7 +572,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(f128) \ }) #define wapp_f128_array_with_capacity(CAPACITY) ((F128Array){.items = (f128[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f128)}) -#define wapp_f128_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F128Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f128))) #define wapp_f128_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_f128_array_pop(ARRAY_PTR) : \ (f128){0} \ @@ -238,7 +583,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(iptr) \ }) #define wapp_iptr_array_with_capacity(CAPACITY) ((IptrArray){.items = (iptr[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(iptr)}) -#define wapp_iptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((IptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(iptr))) #define wapp_iptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_iptr_array_pop(ARRAY_PTR) : \ (iptr){0} \ @@ -250,11 +594,11 @@ BEGIN_C_LINKAGE .item_size = sizeof(uptr) \ }) #define wapp_uptr_array_with_capacity(CAPACITY) ((UptrArray){.items = (uptr[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(uptr)}) -#define wapp_uptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((UptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(uptr))) #define wapp_uptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_uptr_array_pop(ARRAY_PTR) : \ (uptr){0} \ ) +#endif // !WAPP_PLATFORM_CPP typedef struct str8 Str8;