diff --git a/Makefile b/Makefile index 1082c7a..2b66b12 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ BUILD_TYPE = Debug BUILD_DIR = libwapp-build/$(PLATFORM)-$(BUILD_TYPE) INSTALL_PREFIX = dist CODEGEN_INPUT = "" +RUNTIME_ASSERT = true # Internal variables override CFLAGS = -Wall -Wextra -Werror -pedantic -Isrc @@ -43,33 +44,82 @@ else endif endif +# Disable runtime asserts +ifeq ($(RUNTIME_ASSERT), false) + override BUILD_FLAGS += WAPP_NO_RUNTIME_ASSERT +endif + ifeq ($(CC),gcc) # Used to disable the "ASan runtime does not come first in initial library list" error when compiling with gcc export ASAN_OPTIONS=verify_asan_link_order=0 endif +# Escape sequences +BOLD = \033[1m +BLACK = \033[30m +BG_BLACK = \033[40m +RED = \033[31m +BG_RED = \033[41m +GREEN = \033[32m +BG_GREEN = \033[42m +YELLOW = \033[33m +BG_YELLOW = \033[43m +BLUE = \033[34m +BG_BLUE = \033[44m +MAGENTA = \033[35m +BG_MAGENTA = \033[45m +CYAN = \033[36m +BG_CYAN = \033[46m +WHITE = \033[37m +BG_WHITE = \033[47m +GRAY = \033[90m +BG_GRAY = \033[100m +BRIGHT_RED = \033[91m +BG_BRIGHT_RED = \033[101m +BRIGHT_GREEN = \033[92m +BG_BRIGHT_GREEN = \033[102m +BRIGHT_YELLOW = \033[93m +BG_BRIGHT_YELLOW = \033[103m +BRIGHT_BLUE = \033[94m +BG_BRIGHT_BLUE = \033[104m +BRIGHT_MAGENTA = \033[95m +BG_BRIGHT_MAGENTA = \033[105m +BRIGHT_CYAN = \033[96m +BG_BRIGHT_CYAN = \033[106m +BRIGHT_WHITE = \033[97m +BG_BRIGHT_WHITE = \033[107m +RESET = \033[0m + +ECHO_E = echo -e +ifeq ($(KERNEL), Darwin) + ECHO_E = echo +endif + all: clean builddir codegen run-c-test full run-cc-test help: - @echo "Available build variables:" - @echo " CC C compiler to use (Default: clang)." - @echo " CXX C++ compiler to use (Default: clang++)." - @echo " AR Archiving utility to use for building static libraries (Default: ar)." - @echo " BUILD_TYPE Build type. Choose from \`Debug\`, \`RelWithDebInfo\` or \`Release\` (Default: Debug)." - @echo " BUILD_DIR Directory where build files will be written." - @echo " INSTALL_PREFIX Prefix where library and include files will be installed." - @echo " CODEGEN_INPUT Input file for code generation (See codegen_custom_data_example.json for an example)." - @echo - @echo "Available targets:" - @echo " make Build, install and test the full wapp library." - @echo " make full Build and install the full wapp library." - @echo " make core Build and install only the \`core\` component of the wapp library with all its dependencies." - @echo " make prng Build and install only the \`prng\` component of the wapp library with all its dependencies." - @echo " make uuid Build and install only the \`uuid\` component of the wapp library with all its dependencies." - @echo " make testing Build and install only the \`testing\` component of the wapp library with all its dependencies." - @echo " make primitives Build and install only the \`primitives\` component of the wapp library with all its dependencies." - @echo " make clean Clean build directory." - @echo " make help Print this help message and exit." + @$(ECHO_E) "$(BOLD)$(BLUE)Available build variables:$(RESET)" + @$(ECHO_E) " $(GREEN)CC$(RESET) C compiler to use $(YELLOW)(Default: clang)$(RESET)." + @$(ECHO_E) " $(GREEN)CXX$(RESET) C++ compiler to use $(YELLOW)(Default: clang++)$(RESET)." + @$(ECHO_E) " $(GREEN)AR$(RESET) Archiving utility to use for building static libraries $(YELLOW)(Default: ar)$(RESET)." + @$(ECHO_E) " $(GREEN)BUILD_TYPE$(RESET) Build type $(MAGENTA)[Debug | RelWithDebInfo | Release] $(YELLOW)(Default: Debug)$(RESET)." + @$(ECHO_E) " $(GREEN)BUILD_DIR$(RESET) Directory where build files will be written." + @$(ECHO_E) " $(GREEN)INSTALL_PREFIX$(RESET) Prefix where library and include files will be installed." + @$(ECHO_E) " $(GREEN)CODEGEN_INPUT$(RESET) Input file for code generation (See $(CYAN)codegen_custom_data_example.json$(RESET) for an example)." + @$(ECHO_E) " $(GREEN)RUNTIME_ASSERT$(RESET) Whether runtime asserts are enabled $(MAGENTA)[true | false] $(YELLOW)(Default: true)$(RESET)." + @$(ECHO_E) " $(GREEN)$(RESET) $(BOLD)$(BG_RED)DISCLAIMER:$(RESET) Using this flag is not recommended as it disables safety checks" + @$(ECHO_E) " $(GREEN)$(RESET) potentially leading to Undefined Behaviour." + @$(ECHO_E) + @$(ECHO_E) "$(BOLD)$(BLUE)Available targets:$(RESET)" + @$(ECHO_E) " $(GREEN)make$(RESET) Build, install and test the full wapp library." + @$(ECHO_E) " $(GREEN)make full$(RESET) Build and install the full wapp library." + @$(ECHO_E) " $(GREEN)make core$(RESET) Build and install only the $(CYAN)core$(RESET) component of the wapp library with all its dependencies." + @$(ECHO_E) " $(GREEN)make prng$(RESET) Build and install only the $(CYAN)prng$(RESET) component of the wapp library with all its dependencies." + @$(ECHO_E) " $(GREEN)make uuid$(RESET) Build and install only the $(CYAN)uuid$(RESET) component of the wapp library with all its dependencies." + @$(ECHO_E) " $(GREEN)make testing$(RESET) Build and install only the $(CYAN)testing$(RESET) component of the wapp library with all its dependencies." + @$(ECHO_E) " $(GREEN)make primitives$(RESET) Build and install only the $(CYAN)primitives$(RESET) component of the wapp library with all its dependencies." + @$(ECHO_E) " $(GREEN)make clean$(RESET) Clean the build directory." + @$(ECHO_E) " $(GREEN)make help$(RESET) Print this help message and exit." full: LIB_SRC = src/wapp.c full: INCLUDES = common core primitives prng testing uuid diff --git a/build b/build index e36c12d..8b3e53b 100755 --- a/build +++ b/build @@ -55,15 +55,4 @@ if ! contains ${BUILD_TYPE} "${ACCEPTED_BUILD_TYPES[@]}"; then exit 1 fi -if [[ $KERNEL == "Darwin" ]]; then - if [[ ! -d .venv ]]; then - python3 -m venv .venv - fi - - source .venv/bin/activate - pip install scan-build - intercept-build make CC=intercept-cc CXX=intercept-c++ BUILD_TYPE=$BUILD_TYPE $ARGS - deactivate -else - bear -- make BUILD_TYPE=$BUILD_TYPE $ARGS -fi +bear -- make BUILD_TYPE=$BUILD_TYPE $ARGS diff --git a/codegen/__main__.py b/codegen/__main__.py index 8a45e62..abf015d 100644 --- a/codegen/__main__.py +++ b/codegen/__main__.py @@ -2,29 +2,22 @@ import json from typing import Dict from pathlib import Path from codegen.datatypes import CDataType, CStruct -from codegen.constants import WAPP_REPO_ROOT, DBL_LIST_DATA, ARRAY_DATA +from codegen.constants import WAPP_REPO_ROOT, DBL_LIST_DATA from codegen.dbl_list.make_dbl_list import DblListData, make_dbl_list -from codegen.array.make_array import ArrayData, make_array def main(types_file: Path | None): dbl_list_datatypes: Dict[CDataType, DblListData] = {} - array_datatypes: Dict[CDataType, ArrayData] = {} if types_file is not None and types_file.is_file() and "json" in types_file.suffix.lower(): with types_file.open("r") as infile: datatypes = json.load(infile) dbl_list_data = datatypes.get(DBL_LIST_DATA) - array_data = datatypes.get(ARRAY_DATA) if dbl_list_data is not None and isinstance(dbl_list_data, dict): dbl_list_datatypes = {k: DblListData.from_dict(v) for k, v in dbl_list_data.items()} - if array_data is not None and isinstance(array_data, dict): - array_datatypes = {k: ArrayData.from_dict(v) for k, v in array_data.items()} - make_dbl_list(dbl_list_datatypes) - make_array(array_datatypes) # Save example types file custom_struct = CStruct(name="custom_type", cargs=[], typedef_name="CustomType") @@ -36,12 +29,6 @@ def main(types_file: Path | None): hdr_decl_types=[custom_struct], ).to_dict() }, - ARRAY_DATA: { - "CustomType": ArrayData( - array_typename="CustomTypeArray", - hdr_decl_types=[custom_struct], - ).to_dict() - }, } example_file = WAPP_REPO_ROOT / "codegen_custom_data_example.json" diff --git a/codegen/array/make_array.py b/codegen/array/make_array.py deleted file mode 100644 index d239ce3..0000000 --- a/codegen/array/make_array.py +++ /dev/null @@ -1,448 +0,0 @@ -from pathlib import Path -from dataclasses import dataclass, field -from typing import List, Dict, Any, Type -from codegen.constants import WAPP_SRC_ROOT -from codegen.utils import load_func_body_from_file, convert_to_relative -from codegen.datatypes import ( - CDataType, - CMacro, - CStruct, - CFunc, - CHeader, - CSource, - CArg, - CType, - CPointer, - CPointerType, - CQualifier, - CInclude, - SerialisableDataclass, - get_datatype_string, -) - - -@dataclass -class ArrayData(SerialisableDataclass): - array_typename: str - hdr_decl_types: List[CStruct] = field(default_factory=list) - src_decl_types: List[CStruct] = field(default_factory=list) - - @classmethod - def from_dict(cls: Type["ArrayData"], d: Dict[str, Any]) -> "ArrayData": - data = ArrayData(**d) - data.hdr_decl_types = [CStruct.from_dict(v) for v in data.hdr_decl_types if isinstance(v, dict)] - data.src_decl_types = [CStruct.from_dict(v) for v in data.src_decl_types if isinstance(v, dict)] - return data - - -def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): - def __format_func_body( - filename: Path, - type_string: str, - type_string_upper: str, - type_string_lower: str, - array_typename: str - ): - return load_func_body_from_file(filename).format( - T=type_string, - ArrayType=array_typename, - Tupper=type_string_upper, - Tlower=type_string_lower, - ) - - out_dir = WAPP_SRC_ROOT / "primitives" / "array" - out_dir.mkdir(parents=True, exist_ok=True) - - common_includes: List[CInclude] = [ - CInclude( - header=str(convert_to_relative(WAPP_SRC_ROOT / "primitives" / "mem_allocator" / "mem_allocator.h", out_dir)).replace("\\", "/"), - local=True, - ), - CInclude( - header=str(convert_to_relative(WAPP_SRC_ROOT / "common" / "misc" / "misc_utils.h", out_dir)).replace("\\", "/"), - local=True, - ), - ] - - common_decl_types: List[CStruct] = [] - - datatypes: dict[CDataType, ArrayData] = { - CType.VOID: ArrayData(array_typename="GenericArray"), - "void *": ArrayData(array_typename="VoidPArray"), - "Str8": ArrayData( - array_typename="Str8Array", - hdr_decl_types=[ - CStruct(name="str8", cargs=[], typedef_name="Str8"), - ], - ), - } - - for _type in CType: - if _type == CType.VOID: - continue - - type_title = _type.value.title() - datatypes[_type] = ArrayData( - array_typename=f"{type_title}Array", - ) - - datatypes.update(user_datatypes) - - snippets_dir = Path(__file__).parent / "snippets" - - header = CHeader( - name="array", - decl_types=[*common_decl_types], - includes=[], - types=[], - funcs=[] - ) - - source = CSource( - name=header.name, - decl_types=[*common_decl_types], - includes=[ - CInclude(header, local=True, same_dir=True), - CInclude( - header=str(convert_to_relative(WAPP_SRC_ROOT / "common" / "assert" / "assert.h", out_dir)).replace("\\", "/"), - local=True - ), - CInclude(header="stddef.h"), - ], - internal_funcs=[], - funcs=header.funcs - ) - - if len(common_includes) > 0: - header.includes.extend(common_includes) - source.includes.extend(common_includes) - - - generic_funcs = [] - - for _type, array_data in datatypes.items(): - type_string = get_datatype_string(_type) - clean_type_string = type_string.replace(" ", "").replace("*", "_ptr") - type_string_upper = clean_type_string.upper() - type_string_lower = clean_type_string.lower() - - array = CStruct( - name=array_data.array_typename, - cargs=[ - CArg(name="items", _type=type_string, pointer=CPointer(_type=CPointerType.SINGLE)), - CArg(name="count", _type=CType.U64), - CArg(name="capacity", _type=CType.U64), - CArg(name="item_size", _type=CType.U64), - ], - ) - - if isinstance(_type, CType) and _type == CType.VOID: - alloc_capacity_func = CFunc( - name=f"_array_alloc_capacity", - ret_type=array, - args=[ - CArg(name="allocator", _type="Allocator", pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST), - CArg(name="capacity", _type=CType.U64), - CArg(name="item_size", _type=CType.U64), - ], - body=__format_func_body( - filename=snippets_dir / "alloc_capacity", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - pointer=CPointer(CPointerType.SINGLE), - ) - generic_funcs.append(alloc_capacity_func) - else: - stack_array_cmacro = CMacro( - name=f"wapp_{type_string_lower}_array(...)", - value=__format_func_body( - filename=snippets_dir / "stack_array", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - 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_cmacro = CMacro( - name=f"wapp_{type_string_lower}_array_with_capacity(CAPACITY)", - value=__format_func_body( - filename=snippets_dir / "stack_capacity_array", - 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_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)", - value=__format_func_body( - filename=snippets_dir / "alloc_capacity_macro", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - ) - - array_pop_cmacro = CMacro( - name=f"wapp_{type_string_lower}_array_pop(ARRAY_PTR)", - value=__format_func_body( - filename=snippets_dir / "array_pop_macro", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - ) - - if "*" in type_string: - array_pop_cppmacro = CMacro( - name=f"wapp_{type_string_lower}_array_pop(ARRAY_PTR)", - value=__format_func_body( - filename=snippets_dir / "ptr_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, - ), - ) - else: - 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", - ret_type=type_string, - args=[ - CArg(name="array", _type=array, pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST), - CArg(name="index", _type=CType.U64), - ], - body=__format_func_body( - filename=snippets_dir / "array_get", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - pointer=CPointer(CPointerType.SINGLE), - ) - - set_func = CFunc( - name=f"wapp_{type_string_lower}_array_set", - ret_type=CType.VOID, - args=[ - CArg(name="array", _type=array, pointer=CPointer(CPointerType.SINGLE)), - CArg(name="index", _type=CType.U64), - CArg(name="item", _type=type_string, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body( - filename=snippets_dir / "array_set", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - ) - - append_capped_func = CFunc( - name=f"wapp_{type_string_lower}_array_append_capped", - ret_type=CType.VOID, - args=[ - CArg(name="array", _type=array, pointer=CPointer(CPointerType.SINGLE)), - CArg(name="item", _type=type_string, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body( - filename=snippets_dir / "append_capped", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - ) - - extend_capped_func = CFunc( - name=f"wapp_{type_string_lower}_array_extend_capped", - ret_type=CType.VOID, - args=[ - CArg(name="array", _type=array, pointer=CPointer(CPointerType.SINGLE)), - CArg(name="other", _type=array, pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST), - ], - body=__format_func_body( - filename=snippets_dir / "extend_capped", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - ) - - clear_func = CFunc( - name=f"wapp_{type_string_lower}_array_clear", - ret_type=CType.VOID, - args=[ - CArg(name="array", _type=array, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body( - filename=snippets_dir / "clear", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - ) - - copy_capped_func = CFunc( - name=f"wapp_{type_string_lower}_array_copy_capped", - ret_type=CType.VOID, - args=[ - CArg(name="src", _type=array, pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST), - CArg(name="dst", _type=array, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body( - filename=snippets_dir / "copy_capped", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - ) - - append_alloc_func = CFunc( - name=f"wapp_{type_string_lower}_array_append_alloc", - ret_type=array, - args=[ - CArg(name="allocator", _type="Allocator", pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST), - CArg(name="array", _type=array, pointer=CPointer(CPointerType.SINGLE)), - CArg(name="item", _type=type_string, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body( - filename=snippets_dir / "append_alloc", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - pointer=CPointer(CPointerType.SINGLE), - ) - - extend_alloc_func = CFunc( - name=f"wapp_{type_string_lower}_array_extend_alloc", - ret_type=array, - args=[ - CArg(name="allocator", _type="Allocator", pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST), - CArg(name="array", _type=array, pointer=CPointer(CPointerType.SINGLE)), - CArg(name="other", _type=array, pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST), - ], - body=__format_func_body( - filename=snippets_dir / "extend_alloc", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - pointer=CPointer(CPointerType.SINGLE), - ) - - copy_alloc_func = CFunc( - name=f"wapp_{type_string_lower}_array_copy_alloc", - ret_type=array, - args=[ - CArg(name="allocator", _type="Allocator", pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST), - CArg(name="src", _type=array, pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST), - CArg(name="dst", _type=array, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body( - filename=snippets_dir / "copy_alloc", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - pointer=CPointer(CPointerType.SINGLE), - ) - - pop_func = CFunc( - name=f"_{type_string_lower}_array_pop", - ret_type=type_string, - args=[ - CArg(name="array", _type=array, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body( - filename=snippets_dir / "array_pop", - type_string=type_string, - type_string_upper=type_string_upper, - type_string_lower=type_string_lower, - array_typename=array_data.array_typename, - ), - pointer=CPointer(CPointerType.SINGLE), - ) - - header.macros.extend([ - alloc_capacity_array_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.funcs.extend([ - get_func, - set_func, - append_capped_func, - extend_capped_func, - clear_func, - copy_capped_func, - append_alloc_func, - extend_alloc_func, - copy_alloc_func, - pop_func, - ]) - - header.decl_types.extend(array_data.hdr_decl_types) - header.types.extend([array]) - - source.decl_types.extend(array_data.src_decl_types) - source.funcs = header.funcs - - - header.funcs.extend(generic_funcs) - header.save(out_dir) - source.save(out_dir) diff --git a/codegen/array/snippets/alloc_capacity b/codegen/array/snippets/alloc_capacity deleted file mode 100644 index 1fe9e13..0000000 --- a/codegen/array/snippets/alloc_capacity +++ /dev/null @@ -1,15 +0,0 @@ - wapp_debug_assert(allocator != NULL, "`allocator` should not be NULL"); - - u64 allocation_size = sizeof({ArrayType}) + item_size * capacity; - {ArrayType} *array = wapp_mem_allocator_alloc(allocator, allocation_size); - if (!array) {{ - goto RETURN_GENERIC_ARRAY_ALLOC; - }} - - array->items = ({T} *)((u8 *)array + sizeof({ArrayType})); - array->count = 0; - array->capacity = capacity; - array->item_size = item_size; - -RETURN_GENERIC_ARRAY_ALLOC: - return array; diff --git a/codegen/array/snippets/alloc_capacity_macro b/codegen/array/snippets/alloc_capacity_macro deleted file mode 100644 index b3094c1..0000000 --- a/codegen/array/snippets/alloc_capacity_macro +++ /dev/null @@ -1 +0,0 @@ -(({ArrayType} *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof({T}))) diff --git a/codegen/array/snippets/append_alloc b/codegen/array/snippets/append_alloc deleted file mode 100644 index ff87081..0000000 --- a/codegen/array/snippets/append_alloc +++ /dev/null @@ -1,18 +0,0 @@ - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - {ArrayType} *output = array; - - if (array->count >= array->capacity) {{ - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = ({ArrayType} *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) {{ - output = array; - goto RETURN_{Tupper}_ARRAY_APPEND_ALLOC; - }} - wapp_{Tlower}_array_copy_capped(array, output); - }} - - wapp_{Tlower}_array_append_capped(output, item); - -RETURN_{Tupper}_ARRAY_APPEND_ALLOC: - return output; diff --git a/codegen/array/snippets/append_capped b/codegen/array/snippets/append_capped deleted file mode 100644 index 17de7ad..0000000 --- a/codegen/array/snippets/append_capped +++ /dev/null @@ -1,5 +0,0 @@ - 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_{Tlower}_array_set(array, index, item); diff --git a/codegen/array/snippets/array_get b/codegen/array/snippets/array_get deleted file mode 100644 index f23963a..0000000 --- a/codegen/array/snippets/array_get +++ /dev/null @@ -1,5 +0,0 @@ - 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 ({T} *)ptr; diff --git a/codegen/array/snippets/array_pop b/codegen/array/snippets/array_pop deleted file mode 100644 index f30681e..0000000 --- a/codegen/array/snippets/array_pop +++ /dev/null @@ -1,4 +0,0 @@ - u64 index = array->count - 1; - {T} *out = wapp_{Tlower}_array_get(array, index); - --(array->count); - return out; diff --git a/codegen/array/snippets/array_pop_macro b/codegen/array/snippets/array_pop_macro deleted file mode 100644 index 5fb1c49..0000000 --- a/codegen/array/snippets/array_pop_macro +++ /dev/null @@ -1,4 +0,0 @@ -(ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_{Tlower}_array_pop(ARRAY_PTR) : \ - ({T}){{0}} \ -) diff --git a/codegen/array/snippets/array_pop_macro_cpp b/codegen/array/snippets/array_pop_macro_cpp deleted file mode 100644 index 41784b6..0000000 --- a/codegen/array/snippets/array_pop_macro_cpp +++ /dev/null @@ -1,4 +0,0 @@ -(ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_{Tlower}_array_pop(ARRAY_PTR) : \ - {T}{{}} \ -) diff --git a/codegen/array/snippets/array_set b/codegen/array/snippets/array_set deleted file mode 100644 index f570d92..0000000 --- a/codegen/array/snippets/array_set +++ /dev/null @@ -1,3 +0,0 @@ - {T} *ptr = wapp_{Tlower}_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); diff --git a/codegen/array/snippets/clear b/codegen/array/snippets/clear deleted file mode 100644 index 0914817..0000000 --- a/codegen/array/snippets/clear +++ /dev/null @@ -1,2 +0,0 @@ - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; diff --git a/codegen/array/snippets/copy_alloc b/codegen/array/snippets/copy_alloc deleted file mode 100644 index f90b6e5..0000000 --- a/codegen/array/snippets/copy_alloc +++ /dev/null @@ -1,18 +0,0 @@ - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - {ArrayType} *output = dst; - - if (src->count >= dst->capacity) {{ - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = ({ArrayType} *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) {{ - output = dst; - goto RETURN_{Tupper}_ARRAY_COPY_ALLOC; - }} - }} - - wapp_{Tlower}_array_clear(output); - wapp_{Tlower}_array_copy_capped(src, output); - -RETURN_{Tupper}_ARRAY_COPY_ALLOC: - return output; diff --git a/codegen/array/snippets/copy_capped b/codegen/array/snippets/copy_capped deleted file mode 100644 index d4f6266..0000000 --- a/codegen/array/snippets/copy_capped +++ /dev/null @@ -1,22 +0,0 @@ - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_{Tlower}_array_clear(dst); - - {T} *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; - b8 running = true; - while (running) {{ - item = wapp_{Tlower}_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) {{ - continue; - }} - - wapp_{Tlower}_array_append_capped(dst, item); - }} diff --git a/codegen/array/snippets/extend_alloc b/codegen/array/snippets/extend_alloc deleted file mode 100644 index 2bce4ad..0000000 --- a/codegen/array/snippets/extend_alloc +++ /dev/null @@ -1,19 +0,0 @@ - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - {ArrayType} *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 = ({ArrayType} *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) {{ - output = array; - goto RETURN_{Tupper}_ARRAY_EXTEND_ALLOC; - }} - wapp_{Tlower}_array_copy_capped(array, output); - }} - - wapp_{Tlower}_array_extend_capped(output, other); - -RETURN_{Tupper}_ARRAY_EXTEND_ALLOC: - return output; diff --git a/codegen/array/snippets/extend_capped b/codegen/array/snippets/extend_capped deleted file mode 100644 index 06ae168..0000000 --- a/codegen/array/snippets/extend_capped +++ /dev/null @@ -1,23 +0,0 @@ - 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"); - - {T} *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; - b8 running = true; - while (running) {{ - item = wapp_{Tlower}_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) {{ - continue; - }} - - wapp_{Tlower}_array_append_capped(array, item); - }} diff --git a/codegen/array/snippets/ptr_array_pop_macro_cpp b/codegen/array/snippets/ptr_array_pop_macro_cpp deleted file mode 100644 index 1f53416..0000000 --- a/codegen/array/snippets/ptr_array_pop_macro_cpp +++ /dev/null @@ -1,4 +0,0 @@ -(ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_{Tlower}_array_pop(ARRAY_PTR) : \ - ({T})(0) \ -) diff --git a/codegen/array/snippets/stack_array b/codegen/array/snippets/stack_array deleted file mode 100644 index 1f986c9..0000000 --- a/codegen/array/snippets/stack_array +++ /dev/null @@ -1,6 +0,0 @@ -(({ArrayType}){{ \ - .items = ({T}[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count({T}, __VA_ARGS__) * 2)]){{__VA_ARGS__}}, \ - .count = wapp_misc_utils_va_args_count({T}, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count({T}, __VA_ARGS__) * 2), \ - .item_size = sizeof({T}) \ -}}) diff --git a/codegen/array/snippets/stack_array_cpp b/codegen/array/snippets/stack_array_cpp deleted file mode 100644 index 262c034..0000000 --- a/codegen/array/snippets/stack_array_cpp +++ /dev/null @@ -1,9 +0,0 @@ -([&]() {{ \ - wapp_persist {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 b/codegen/array/snippets/stack_capacity_array deleted file mode 100644 index 9d78e4b..0000000 --- a/codegen/array/snippets/stack_capacity_array +++ /dev/null @@ -1 +0,0 @@ -(({ArrayType}){{.items = ({T}[CAPACITY]){{0}}, .count = 0, .capacity = CAPACITY, .item_size = sizeof({T})}}) diff --git a/codegen/array/snippets/stack_capacity_array_cpp b/codegen/array/snippets/stack_capacity_array_cpp deleted file mode 100644 index 2b69acd..0000000 --- a/codegen/array/snippets/stack_capacity_array_cpp +++ /dev/null @@ -1,4 +0,0 @@ -([&]() {{ \ - wapp_persist {T} buf[CAPACITY] = {{}}; \ - return {ArrayType}{{buf, 0, CAPACITY, sizeof({T})}}; \ -}}()) diff --git a/codegen/constants.py b/codegen/constants.py index 29dc1c5..3647b1a 100644 --- a/codegen/constants.py +++ b/codegen/constants.py @@ -7,4 +7,3 @@ WAPP_SRC_ROOT = WAPP_REPO_ROOT / "src" # Dictionary Keys DBL_LIST_DATA = "dbl_list_data" -ARRAY_DATA = "array_data" diff --git a/codegen_custom_data_example.json b/codegen_custom_data_example.json index 536bfad..93a07e5 100644 --- a/codegen_custom_data_example.json +++ b/codegen_custom_data_example.json @@ -12,18 +12,5 @@ ], "src_decl_types": [] } - }, - "array_data": { - "CustomType": { - "array_typename": "CustomTypeArray", - "hdr_decl_types": [ - { - "name": "custom_type", - "cargs": [], - "typedef_name": "CustomType" - } - ], - "src_decl_types": [] - } } } \ No newline at end of file diff --git a/src/common/assert/assert.h b/src/common/assert/assert.h index ae98975..55733b3 100644 --- a/src/common/assert/assert.h +++ b/src/common/assert/assert.h @@ -14,7 +14,12 @@ BEGIN_C_LINKAGE #endif // !WAPP_PLATFORM_CPP #define wapp_static_assert(EXPR, MSG) extern char ASSERTION_FAILED[EXPR ? 1 : -1] -#define wapp_runtime_assert(EXPR, MSG) __wapp_runtime_assert(EXPR, MSG) + +#ifndef WAPP_NO_RUNTIME_ASSERT + #define wapp_runtime_assert(EXPR, MSG) __wapp_runtime_assert(EXPR, MSG) +#else + #define wapp_runtime_assert(EXPR, MSG) +#endif #ifdef WAPP_DEBUG_ASSERT #define wapp_debug_assert(EXPR, MSG) wapp_runtime_assert(EXPR, MSG) diff --git a/src/core/file/file.c b/src/core/file/file.c index c5fbfa2..1885211 100644 --- a/src/core/file/file.c +++ b/src/core/file/file.c @@ -61,13 +61,14 @@ u64 wapp_file_get_length(File *file) { return output; } -u64 wapp_file_read(GenericArray *dst, File *file, u64 item_count) { - wapp_debug_assert(dst != NULL && dst->items != NULL && file != NULL, - "`dst`, `dst->items` and `file` should not be NULL."); +u64 wapp_file_read(u8 *dst_byte_array, File *file, u64 item_count) { + wapp_debug_assert(dst_byte_array != NULL && file != NULL, + "`dst_byte_array` and `file` should not be NULL."); u64 file_length = wapp_file_get_length(file); - u64 dst_byte_capacity = dst->item_size * dst->capacity; - u64 req_byte_count = item_count * dst->item_size; + u64 item_size = wapp_array_item_size(dst_byte_array); + u64 dst_byte_capacity = wapp_array_capacity(dst_byte_array) * item_size; + u64 req_byte_count = item_count * item_size; u64 copy_byte_count = 0; if (req_byte_count <= file_length && req_byte_count <= dst_byte_capacity) { @@ -76,20 +77,24 @@ u64 wapp_file_read(GenericArray *dst, File *file, u64 item_count) { copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity; } - dst->count = fread(dst->items, sizeof(u8), copy_byte_count, file) / dst->item_size; + u64 count = fread(dst_byte_array, sizeof(u8), copy_byte_count, file); + if (ferror(file)) { return 0; } - return dst->count; + wapp_array_set_count(dst_byte_array, count / item_size); + + return wapp_array_count(dst_byte_array); } -u64 wapp_file_write(const GenericArray *src, File *file, u64 item_count) { - wapp_debug_assert(src != NULL && src->items != NULL && file != NULL, - "`src`, `src->items` and `file` should not be NULL."); +u64 wapp_file_write(const u8 *src_byte_array, File *file, u64 item_count) { + wapp_debug_assert(src_byte_array != NULL && file != NULL, + "`src_byte_array` and `file` should not be NULL."); - u64 src_byte_count = src->count * src->item_size; - u64 req_byte_count = item_count * src->item_size; + u64 item_size = wapp_array_item_size(src_byte_array); + u64 src_byte_count = wapp_array_count(src_byte_array) * 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; - return fwrite(src->items, sizeof(u8), to_copy, file); + return fwrite(src_byte_array, sizeof(u8), to_copy, file); } i32 wapp_file_flush(File *file) { diff --git a/src/core/file/file.h b/src/core/file/file.h index 7822e68..0a27bd6 100644 --- a/src/core/file/file.h +++ b/src/core/file/file.h @@ -4,7 +4,6 @@ #define FILE_H #include "../../common/aliases/aliases.h" -#include "../../primitives/array/array.h" #include "../../primitives/strings/str8/str8.h" #include @@ -12,21 +11,6 @@ BEGIN_C_LINKAGE #endif // !WAPP_PLATFORM_CPP -#ifdef WAPP_PLATFORM_CPP -#define wapp_file_item_to_array(ITEM) (GenericArray{&(ITEM), 1, 1, sizeof(ITEM)}) -#define wapp_file_array_to_item(TYPE, ARRAY) (sizeof(TYPE) == (ARRAY).item_size && (ARRAY).count == 1 ? \ - *((TYPE *)((ARRAY).items)) : \ - TYPE{}) -#else -#define wapp_file_item_to_array(ITEM) ((GenericArray){.items = &(ITEM), \ - .count = 1, \ - .capacity = 1, \ - .item_size = sizeof(ITEM)}) -#define wapp_file_array_to_item(TYPE, ARRAY) (sizeof(TYPE) == (ARRAY).item_size && (ARRAY).count == 1 ? \ - *((TYPE *)((ARRAY).items)) : \ - (TYPE){0}) -#endif // !WAPP_PLATFORM_CPP - typedef FILE File; typedef enum { @@ -60,8 +44,8 @@ File *wapp_file_open(Str8RO *filename, FileAccessMode mode); u64 wapp_file_get_current_position(File *file); i32 wapp_file_seek(File *file, u64 offset, FileSeekOrigin origin); u64 wapp_file_get_length(File *file); -u64 wapp_file_read(GenericArray *dst, File *file, u64 item_count); -u64 wapp_file_write(const GenericArray *src, File *file, u64 item_count); +u64 wapp_file_read(u8 *dst_byte_array, File *file, u64 item_count); +u64 wapp_file_write(const u8 *src_byte_array, File *file, u64 item_count); i32 wapp_file_flush(File *file); i32 wapp_file_close(File *file); diff --git a/src/primitives/array/array.c b/src/primitives/array/array.c index 3392e7a..86c5cc1 100644 --- a/src/primitives/array/array.c +++ b/src/primitives/array/array.c @@ -1,6 +1,4 @@ -/** - * THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN. - */ +// vim:fileencoding=utf-8:foldmethod=marker #include "./array.h" #include "../../common/assert/assert.h" @@ -10,3001 +8,227 @@ #include "../../common/platform/platform.h" #include -void * *wapp_void_ptr_array_get(const VoidPArray *array, u64 index) { +#define _array_header(DATA_PTR) (ArrayHeader *)((u8 *)DATA_PTR - sizeof(ArrayHeader)) + +u64 _array_count(u8 *array) { 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; + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + + return arr_header->count; } -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) { +u64 _array_capacity(u8 *array) { 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); + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + + return arr_header->capacity; } -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; - b8 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) { +u64 _array_item_size(u8 *array) { wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; + + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + + return arr_header->item_size; } -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"); +void _array_set_count(u8 *array, u64 count) { + wapp_debug_assert(array != NULL, "`array` should not be NULL"); - wapp_void_ptr_array_clear(dst); + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - 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; - b8 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); - } + arr_header->count = count; } -VoidPArray *wapp_void_ptr_array_append_alloc(const Allocator *allocator, VoidPArray *array, void * *item) { +u8 *_array_get(u8 *array, u64 index, u64 item_size) { + wapp_debug_assert(array != NULL, "`array` should not be NULL"); + + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); + wapp_runtime_assert(index < arr_header->count, "`index` is out of bounds"); + + return array + arr_header->item_size * index; +} + +void _array_set(u8 *array, u64 index, u8 *value, u64 item_size) { + u8 *item = _array_get(array, index, item_size); + ArrayHeader *arr_header = _array_header(array); + memcpy((void *)item, (void *)value, arr_header->item_size); +} + +void _array_append_capped(u8 *array, u8 *value, u64 item_size) { + wapp_debug_assert(array != NULL, "`array` should not be NULL"); + + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); + + if (arr_header->count >= arr_header->capacity) { return; } + + u64 index = (arr_header->count)++; + _array_set(array, index, value, item_size); +} + +void _array_extend_capped(u8 *dst_array, const u8 *src_array, u64 item_size) { + wapp_debug_assert(dst_array != NULL && src_array != NULL, "`dst_array` and `src_array` should not be NULL"); + + ArrayHeader *dst_header = _array_header(dst_array); + ArrayHeader *src_header = _array_header(src_array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`dst_array` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`src_array` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); + + u64 remaining_capacity = dst_header->capacity - dst_header->count; + + u64 copy_count = src_header->count < remaining_capacity ? src_header->count : remaining_capacity; + u8 *dst_ptr = dst_array + dst_header->count * dst_header->item_size; + memcpy((void *)dst_ptr, (const void *)src_array, copy_count * src_header->item_size); + dst_header->count += copy_count; +} + +void _array_copy_capped(u8 *dst_array, const u8 *src_array, u64 item_size) { + wapp_debug_assert(dst_array != NULL && src_array != NULL, "`dst_array` and `src_array` should not be NULL"); + + ArrayHeader *dst_header = _array_header(dst_array); + ArrayHeader *src_header = _array_header(src_array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`dst_array` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`src_array` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); + + _array_clear(dst_array, item_size); + u64 copy_count = src_header->count < dst_header->capacity ? src_header->count : dst_header->capacity; + memcpy((void *)dst_array, (const void *)src_array, copy_count * src_header->item_size); + dst_header->count = copy_count; +} + +u8 *_array_append_alloc(const Allocator *allocator, u8 *array, u8 *value, u64 item_size) { wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - VoidPArray *output = array; + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); - 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); + u8 *output = array; + + if (arr_header->count >= arr_header->capacity) { + u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(arr_header->capacity * 2); + output = (u8 *)_array_alloc_capacity(allocator, new_capacity, arr_header->item_size); if (!output) { output = array; - goto RETURN_VOID_PTR_ARRAY_APPEND_ALLOC; + goto RETURN_ARRAY_APPEND_ALLOC; } - wapp_void_ptr_array_copy_capped(array, output); + _array_copy_capped(output, array, item_size); } - wapp_void_ptr_array_append_capped(output, item); + _array_append_capped(output, value, item_size); -RETURN_VOID_PTR_ARRAY_APPEND_ALLOC: +RETURN_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"); +u8 *_array_extend_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size) { + wapp_debug_assert(allocator != NULL && dst_array != NULL && src_array != NULL, "`allocator`, `dst_array` and `src_array` should not be NULL"); - VoidPArray *output = array; + ArrayHeader *dst_header = _array_header(dst_array); + ArrayHeader *src_header = _array_header(src_array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`dst_array` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`src_array` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); - 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); + u8 *output = dst_array; + + u64 remaining_capacity = dst_header->capacity - dst_header->count; + if (src_header->count >= remaining_capacity) { + u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2); + output = (u8 *)_array_alloc_capacity(allocator, new_capacity, dst_header->item_size); if (!output) { - output = array; - goto RETURN_VOID_PTR_ARRAY_EXTEND_ALLOC; + output = dst_array; + goto RETURN_ARRAY_EXTEND_ALLOC; } - wapp_void_ptr_array_copy_capped(array, output); + _array_copy_capped(output, dst_array, item_size); } - wapp_void_ptr_array_extend_capped(output, other); + _array_extend_capped(output, src_array, item_size); -RETURN_VOID_PTR_ARRAY_EXTEND_ALLOC: +RETURN_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"); +u8 *_array_copy_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size) { + wapp_debug_assert(allocator != NULL && dst_array != NULL && src_array != NULL, "`allocator`, `dst_array` and `src_array` should not be NULL"); - VoidPArray *output = dst; + ArrayHeader *dst_header = _array_header(dst_array); + ArrayHeader *src_header = _array_header(src_array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`dst_array` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`src_array` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); - 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); + u8 *output = dst_array; + + if (src_header->count >= dst_header->capacity) { + u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2); + output = (u8 *)_array_alloc_capacity(allocator, new_capacity, src_header->item_size); if (!output) { - output = dst; - goto RETURN_VOID_PTR_ARRAY_COPY_ALLOC; + output = dst_array; + goto RETURN_ARRAY_COPY_ALLOC; } } - wapp_void_ptr_array_clear(output); - wapp_void_ptr_array_copy_capped(src, output); + _array_copy_capped(output, src_array, item_size); -RETURN_VOID_PTR_ARRAY_COPY_ALLOC: +RETURN_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); +u8 *_array_pop(u8 *array, u64 item_size) { + wapp_debug_assert(array != NULL, "`array` should not be NULL"); + + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); + + if (arr_header->count == 0) { return NULL; } + + u64 index = arr_header->count - 1; + u8 *out = _array_get(array, index, item_size); + --(arr_header->count); return out; } -Str8 *wapp_str8_array_get(const Str8Array *array, u64 index) { +void _array_clear(u8 *array, u64 item_size) { 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; - b8 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; - b8 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; -} - -b8 *wapp_b8_array_get(const B8Array *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 (b8 *)ptr; -} - -void wapp_b8_array_set(B8Array *array, u64 index, b8 *item) { - b8 *ptr = wapp_b8_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_b8_array_append_capped(B8Array *array, b8 *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_b8_array_set(array, index, item); -} - -void wapp_b8_array_extend_capped(B8Array *array, const B8Array *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"); - - b8 *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; - b8 running = true; - while (running) { - item = wapp_b8_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_b8_array_append_capped(array, item); - } -} - -void wapp_b8_array_clear(B8Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_b8_array_copy_capped(const B8Array *src, B8Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_b8_array_clear(dst); - - b8 *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; - b8 running = true; - while (running) { - item = wapp_b8_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_b8_array_append_capped(dst, item); - } -} - -B8Array *wapp_b8_array_append_alloc(const Allocator *allocator, B8Array *array, b8 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - B8Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (B8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_B8_ARRAY_APPEND_ALLOC; - } - wapp_b8_array_copy_capped(array, output); - } - - wapp_b8_array_append_capped(output, item); - -RETURN_B8_ARRAY_APPEND_ALLOC: - return output; -} - -B8Array *wapp_b8_array_extend_alloc(const Allocator *allocator, B8Array *array, const B8Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - B8Array *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 = (B8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_B8_ARRAY_EXTEND_ALLOC; - } - wapp_b8_array_copy_capped(array, output); - } - - wapp_b8_array_extend_capped(output, other); - -RETURN_B8_ARRAY_EXTEND_ALLOC: - return output; -} - -B8Array *wapp_b8_array_copy_alloc(const Allocator *allocator, const B8Array *src, B8Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - B8Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (B8Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_B8_ARRAY_COPY_ALLOC; - } - } - - wapp_b8_array_clear(output); - wapp_b8_array_copy_capped(src, output); - -RETURN_B8_ARRAY_COPY_ALLOC: - return output; -} - -b8 *_b8_array_pop(B8Array *array) { - u64 index = array->count - 1; - b8 *out = wapp_b8_array_get(array, index); - --(array->count); - return out; -} - -char *wapp_char_array_get(const CharArray *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 (char *)ptr; -} - -void wapp_char_array_set(CharArray *array, u64 index, char *item) { - char *ptr = wapp_char_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_char_array_append_capped(CharArray *array, char *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_char_array_set(array, index, item); -} - -void wapp_char_array_extend_capped(CharArray *array, const CharArray *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"); - - char *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; - b8 running = true; - while (running) { - item = wapp_char_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_char_array_append_capped(array, item); - } -} - -void wapp_char_array_clear(CharArray *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_char_array_copy_capped(const CharArray *src, CharArray *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_char_array_clear(dst); - - char *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; - b8 running = true; - while (running) { - item = wapp_char_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_char_array_append_capped(dst, item); - } -} - -CharArray *wapp_char_array_append_alloc(const Allocator *allocator, CharArray *array, char *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - CharArray *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (CharArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_CHAR_ARRAY_APPEND_ALLOC; - } - wapp_char_array_copy_capped(array, output); - } - - wapp_char_array_append_capped(output, item); - -RETURN_CHAR_ARRAY_APPEND_ALLOC: - return output; -} - -CharArray *wapp_char_array_extend_alloc(const Allocator *allocator, CharArray *array, const CharArray *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - CharArray *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 = (CharArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_CHAR_ARRAY_EXTEND_ALLOC; - } - wapp_char_array_copy_capped(array, output); - } - - wapp_char_array_extend_capped(output, other); - -RETURN_CHAR_ARRAY_EXTEND_ALLOC: - return output; -} - -CharArray *wapp_char_array_copy_alloc(const Allocator *allocator, const CharArray *src, CharArray *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - CharArray *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (CharArray *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_CHAR_ARRAY_COPY_ALLOC; - } - } - - wapp_char_array_clear(output); - wapp_char_array_copy_capped(src, output); - -RETURN_CHAR_ARRAY_COPY_ALLOC: - return output; -} - -char *_char_array_pop(CharArray *array) { - u64 index = array->count - 1; - char *out = wapp_char_array_get(array, index); - --(array->count); - return out; -} - -c8 *wapp_c8_array_get(const C8Array *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 (c8 *)ptr; -} - -void wapp_c8_array_set(C8Array *array, u64 index, c8 *item) { - c8 *ptr = wapp_c8_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_c8_array_append_capped(C8Array *array, c8 *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_c8_array_set(array, index, item); -} - -void wapp_c8_array_extend_capped(C8Array *array, const C8Array *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"); - - c8 *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; - b8 running = true; - while (running) { - item = wapp_c8_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_c8_array_append_capped(array, item); - } -} - -void wapp_c8_array_clear(C8Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_c8_array_copy_capped(const C8Array *src, C8Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_c8_array_clear(dst); - - c8 *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; - b8 running = true; - while (running) { - item = wapp_c8_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_c8_array_append_capped(dst, item); - } -} - -C8Array *wapp_c8_array_append_alloc(const Allocator *allocator, C8Array *array, c8 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - C8Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (C8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C8_ARRAY_APPEND_ALLOC; - } - wapp_c8_array_copy_capped(array, output); - } - - wapp_c8_array_append_capped(output, item); - -RETURN_C8_ARRAY_APPEND_ALLOC: - return output; -} - -C8Array *wapp_c8_array_extend_alloc(const Allocator *allocator, C8Array *array, const C8Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - C8Array *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 = (C8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C8_ARRAY_EXTEND_ALLOC; - } - wapp_c8_array_copy_capped(array, output); - } - - wapp_c8_array_extend_capped(output, other); - -RETURN_C8_ARRAY_EXTEND_ALLOC: - return output; -} - -C8Array *wapp_c8_array_copy_alloc(const Allocator *allocator, const C8Array *src, C8Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - C8Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (C8Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_C8_ARRAY_COPY_ALLOC; - } - } - - wapp_c8_array_clear(output); - wapp_c8_array_copy_capped(src, output); - -RETURN_C8_ARRAY_COPY_ALLOC: - return output; -} - -c8 *_c8_array_pop(C8Array *array) { - u64 index = array->count - 1; - c8 *out = wapp_c8_array_get(array, index); - --(array->count); - return out; -} - -c16 *wapp_c16_array_get(const C16Array *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 (c16 *)ptr; -} - -void wapp_c16_array_set(C16Array *array, u64 index, c16 *item) { - c16 *ptr = wapp_c16_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_c16_array_append_capped(C16Array *array, c16 *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_c16_array_set(array, index, item); -} - -void wapp_c16_array_extend_capped(C16Array *array, const C16Array *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"); - - c16 *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; - b8 running = true; - while (running) { - item = wapp_c16_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_c16_array_append_capped(array, item); - } -} - -void wapp_c16_array_clear(C16Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_c16_array_copy_capped(const C16Array *src, C16Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_c16_array_clear(dst); - - c16 *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; - b8 running = true; - while (running) { - item = wapp_c16_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_c16_array_append_capped(dst, item); - } -} - -C16Array *wapp_c16_array_append_alloc(const Allocator *allocator, C16Array *array, c16 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - C16Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (C16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C16_ARRAY_APPEND_ALLOC; - } - wapp_c16_array_copy_capped(array, output); - } - - wapp_c16_array_append_capped(output, item); - -RETURN_C16_ARRAY_APPEND_ALLOC: - return output; -} - -C16Array *wapp_c16_array_extend_alloc(const Allocator *allocator, C16Array *array, const C16Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - C16Array *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 = (C16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C16_ARRAY_EXTEND_ALLOC; - } - wapp_c16_array_copy_capped(array, output); - } - - wapp_c16_array_extend_capped(output, other); - -RETURN_C16_ARRAY_EXTEND_ALLOC: - return output; -} - -C16Array *wapp_c16_array_copy_alloc(const Allocator *allocator, const C16Array *src, C16Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - C16Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (C16Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_C16_ARRAY_COPY_ALLOC; - } - } - - wapp_c16_array_clear(output); - wapp_c16_array_copy_capped(src, output); - -RETURN_C16_ARRAY_COPY_ALLOC: - return output; -} - -c16 *_c16_array_pop(C16Array *array) { - u64 index = array->count - 1; - c16 *out = wapp_c16_array_get(array, index); - --(array->count); - return out; -} - -c32 *wapp_c32_array_get(const C32Array *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 (c32 *)ptr; -} - -void wapp_c32_array_set(C32Array *array, u64 index, c32 *item) { - c32 *ptr = wapp_c32_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_c32_array_append_capped(C32Array *array, c32 *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_c32_array_set(array, index, item); -} - -void wapp_c32_array_extend_capped(C32Array *array, const C32Array *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"); - - c32 *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; - b8 running = true; - while (running) { - item = wapp_c32_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_c32_array_append_capped(array, item); - } -} - -void wapp_c32_array_clear(C32Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_c32_array_copy_capped(const C32Array *src, C32Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_c32_array_clear(dst); - - c32 *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; - b8 running = true; - while (running) { - item = wapp_c32_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_c32_array_append_capped(dst, item); - } -} - -C32Array *wapp_c32_array_append_alloc(const Allocator *allocator, C32Array *array, c32 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - C32Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (C32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C32_ARRAY_APPEND_ALLOC; - } - wapp_c32_array_copy_capped(array, output); - } - - wapp_c32_array_append_capped(output, item); - -RETURN_C32_ARRAY_APPEND_ALLOC: - return output; -} - -C32Array *wapp_c32_array_extend_alloc(const Allocator *allocator, C32Array *array, const C32Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - C32Array *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 = (C32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_C32_ARRAY_EXTEND_ALLOC; - } - wapp_c32_array_copy_capped(array, output); - } - - wapp_c32_array_extend_capped(output, other); - -RETURN_C32_ARRAY_EXTEND_ALLOC: - return output; -} - -C32Array *wapp_c32_array_copy_alloc(const Allocator *allocator, const C32Array *src, C32Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - C32Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (C32Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_C32_ARRAY_COPY_ALLOC; - } - } - - wapp_c32_array_clear(output); - wapp_c32_array_copy_capped(src, output); - -RETURN_C32_ARRAY_COPY_ALLOC: - return output; -} - -c32 *_c32_array_pop(C32Array *array) { - u64 index = array->count - 1; - c32 *out = wapp_c32_array_get(array, index); - --(array->count); - return out; -} - -i8 *wapp_i8_array_get(const I8Array *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 (i8 *)ptr; -} - -void wapp_i8_array_set(I8Array *array, u64 index, i8 *item) { - i8 *ptr = wapp_i8_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_i8_array_append_capped(I8Array *array, i8 *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_i8_array_set(array, index, item); -} - -void wapp_i8_array_extend_capped(I8Array *array, const I8Array *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"); - - i8 *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; - b8 running = true; - while (running) { - item = wapp_i8_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_i8_array_append_capped(array, item); - } -} - -void wapp_i8_array_clear(I8Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_i8_array_copy_capped(const I8Array *src, I8Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_i8_array_clear(dst); - - i8 *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; - b8 running = true; - while (running) { - item = wapp_i8_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_i8_array_append_capped(dst, item); - } -} - -I8Array *wapp_i8_array_append_alloc(const Allocator *allocator, I8Array *array, i8 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - I8Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I8_ARRAY_APPEND_ALLOC; - } - wapp_i8_array_copy_capped(array, output); - } - - wapp_i8_array_append_capped(output, item); - -RETURN_I8_ARRAY_APPEND_ALLOC: - return output; -} - -I8Array *wapp_i8_array_extend_alloc(const Allocator *allocator, I8Array *array, const I8Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - I8Array *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 = (I8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I8_ARRAY_EXTEND_ALLOC; - } - wapp_i8_array_copy_capped(array, output); - } - - wapp_i8_array_extend_capped(output, other); - -RETURN_I8_ARRAY_EXTEND_ALLOC: - return output; -} - -I8Array *wapp_i8_array_copy_alloc(const Allocator *allocator, const I8Array *src, I8Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - I8Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (I8Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_I8_ARRAY_COPY_ALLOC; - } - } - - wapp_i8_array_clear(output); - wapp_i8_array_copy_capped(src, output); - -RETURN_I8_ARRAY_COPY_ALLOC: - return output; -} - -i8 *_i8_array_pop(I8Array *array) { - u64 index = array->count - 1; - i8 *out = wapp_i8_array_get(array, index); - --(array->count); - return out; -} - -i16 *wapp_i16_array_get(const I16Array *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 (i16 *)ptr; -} - -void wapp_i16_array_set(I16Array *array, u64 index, i16 *item) { - i16 *ptr = wapp_i16_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_i16_array_append_capped(I16Array *array, i16 *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_i16_array_set(array, index, item); -} - -void wapp_i16_array_extend_capped(I16Array *array, const I16Array *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"); - - i16 *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; - b8 running = true; - while (running) { - item = wapp_i16_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_i16_array_append_capped(array, item); - } -} - -void wapp_i16_array_clear(I16Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_i16_array_copy_capped(const I16Array *src, I16Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_i16_array_clear(dst); - - i16 *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; - b8 running = true; - while (running) { - item = wapp_i16_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_i16_array_append_capped(dst, item); - } -} - -I16Array *wapp_i16_array_append_alloc(const Allocator *allocator, I16Array *array, i16 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - I16Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I16_ARRAY_APPEND_ALLOC; - } - wapp_i16_array_copy_capped(array, output); - } - - wapp_i16_array_append_capped(output, item); - -RETURN_I16_ARRAY_APPEND_ALLOC: - return output; -} - -I16Array *wapp_i16_array_extend_alloc(const Allocator *allocator, I16Array *array, const I16Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - I16Array *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 = (I16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I16_ARRAY_EXTEND_ALLOC; - } - wapp_i16_array_copy_capped(array, output); - } - - wapp_i16_array_extend_capped(output, other); - -RETURN_I16_ARRAY_EXTEND_ALLOC: - return output; -} - -I16Array *wapp_i16_array_copy_alloc(const Allocator *allocator, const I16Array *src, I16Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - I16Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (I16Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_I16_ARRAY_COPY_ALLOC; - } - } - - wapp_i16_array_clear(output); - wapp_i16_array_copy_capped(src, output); - -RETURN_I16_ARRAY_COPY_ALLOC: - return output; -} - -i16 *_i16_array_pop(I16Array *array) { - u64 index = array->count - 1; - i16 *out = wapp_i16_array_get(array, index); - --(array->count); - return out; -} - -i32 *wapp_i32_array_get(const I32Array *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 (i32 *)ptr; -} - -void wapp_i32_array_set(I32Array *array, u64 index, i32 *item) { - i32 *ptr = wapp_i32_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_i32_array_append_capped(I32Array *array, i32 *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_i32_array_set(array, index, item); -} - -void wapp_i32_array_extend_capped(I32Array *array, const I32Array *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"); - - i32 *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; - b8 running = true; - while (running) { - item = wapp_i32_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_i32_array_append_capped(array, item); - } -} - -void wapp_i32_array_clear(I32Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_i32_array_copy_capped(const I32Array *src, I32Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_i32_array_clear(dst); - - i32 *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; - b8 running = true; - while (running) { - item = wapp_i32_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_i32_array_append_capped(dst, item); - } -} - -I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *array, i32 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - I32Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I32_ARRAY_APPEND_ALLOC; - } - wapp_i32_array_copy_capped(array, output); - } - - wapp_i32_array_append_capped(output, item); - -RETURN_I32_ARRAY_APPEND_ALLOC: - return output; -} - -I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *array, const I32Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - I32Array *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 = (I32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I32_ARRAY_EXTEND_ALLOC; - } - wapp_i32_array_copy_capped(array, output); - } - - wapp_i32_array_extend_capped(output, other); - -RETURN_I32_ARRAY_EXTEND_ALLOC: - return output; -} - -I32Array *wapp_i32_array_copy_alloc(const Allocator *allocator, const I32Array *src, I32Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - I32Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (I32Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_I32_ARRAY_COPY_ALLOC; - } - } - - wapp_i32_array_clear(output); - wapp_i32_array_copy_capped(src, output); - -RETURN_I32_ARRAY_COPY_ALLOC: - return output; -} - -i32 *_i32_array_pop(I32Array *array) { - u64 index = array->count - 1; - i32 *out = wapp_i32_array_get(array, index); - --(array->count); - return out; -} - -i64 *wapp_i64_array_get(const I64Array *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 (i64 *)ptr; -} - -void wapp_i64_array_set(I64Array *array, u64 index, i64 *item) { - i64 *ptr = wapp_i64_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_i64_array_append_capped(I64Array *array, i64 *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_i64_array_set(array, index, item); -} - -void wapp_i64_array_extend_capped(I64Array *array, const I64Array *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"); - - i64 *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; - b8 running = true; - while (running) { - item = wapp_i64_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_i64_array_append_capped(array, item); - } -} - -void wapp_i64_array_clear(I64Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_i64_array_copy_capped(const I64Array *src, I64Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_i64_array_clear(dst); - - i64 *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; - b8 running = true; - while (running) { - item = wapp_i64_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_i64_array_append_capped(dst, item); - } -} - -I64Array *wapp_i64_array_append_alloc(const Allocator *allocator, I64Array *array, i64 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - I64Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (I64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I64_ARRAY_APPEND_ALLOC; - } - wapp_i64_array_copy_capped(array, output); - } - - wapp_i64_array_append_capped(output, item); - -RETURN_I64_ARRAY_APPEND_ALLOC: - return output; -} - -I64Array *wapp_i64_array_extend_alloc(const Allocator *allocator, I64Array *array, const I64Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - I64Array *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 = (I64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_I64_ARRAY_EXTEND_ALLOC; - } - wapp_i64_array_copy_capped(array, output); - } - - wapp_i64_array_extend_capped(output, other); - -RETURN_I64_ARRAY_EXTEND_ALLOC: - return output; -} - -I64Array *wapp_i64_array_copy_alloc(const Allocator *allocator, const I64Array *src, I64Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - I64Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (I64Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_I64_ARRAY_COPY_ALLOC; - } - } - - wapp_i64_array_clear(output); - wapp_i64_array_copy_capped(src, output); - -RETURN_I64_ARRAY_COPY_ALLOC: - return output; -} - -i64 *_i64_array_pop(I64Array *array) { - u64 index = array->count - 1; - i64 *out = wapp_i64_array_get(array, index); - --(array->count); - return out; -} - -u8 *wapp_u8_array_get(const U8Array *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 (u8 *)ptr; -} - -void wapp_u8_array_set(U8Array *array, u64 index, u8 *item) { - u8 *ptr = wapp_u8_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_u8_array_append_capped(U8Array *array, u8 *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_u8_array_set(array, index, item); -} - -void wapp_u8_array_extend_capped(U8Array *array, const U8Array *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"); - - u8 *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; - b8 running = true; - while (running) { - item = wapp_u8_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_u8_array_append_capped(array, item); - } -} - -void wapp_u8_array_clear(U8Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_u8_array_copy_capped(const U8Array *src, U8Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_u8_array_clear(dst); - - u8 *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; - b8 running = true; - while (running) { - item = wapp_u8_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_u8_array_append_capped(dst, item); - } -} - -U8Array *wapp_u8_array_append_alloc(const Allocator *allocator, U8Array *array, u8 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - U8Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U8_ARRAY_APPEND_ALLOC; - } - wapp_u8_array_copy_capped(array, output); - } - - wapp_u8_array_append_capped(output, item); - -RETURN_U8_ARRAY_APPEND_ALLOC: - return output; -} - -U8Array *wapp_u8_array_extend_alloc(const Allocator *allocator, U8Array *array, const U8Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - U8Array *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 = (U8Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U8_ARRAY_EXTEND_ALLOC; - } - wapp_u8_array_copy_capped(array, output); - } - - wapp_u8_array_extend_capped(output, other); - -RETURN_U8_ARRAY_EXTEND_ALLOC: - return output; -} - -U8Array *wapp_u8_array_copy_alloc(const Allocator *allocator, const U8Array *src, U8Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - U8Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (U8Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_U8_ARRAY_COPY_ALLOC; - } - } - - wapp_u8_array_clear(output); - wapp_u8_array_copy_capped(src, output); - -RETURN_U8_ARRAY_COPY_ALLOC: - return output; -} - -u8 *_u8_array_pop(U8Array *array) { - u64 index = array->count - 1; - u8 *out = wapp_u8_array_get(array, index); - --(array->count); - return out; -} - -u16 *wapp_u16_array_get(const U16Array *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 (u16 *)ptr; -} - -void wapp_u16_array_set(U16Array *array, u64 index, u16 *item) { - u16 *ptr = wapp_u16_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_u16_array_append_capped(U16Array *array, u16 *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_u16_array_set(array, index, item); -} - -void wapp_u16_array_extend_capped(U16Array *array, const U16Array *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"); - - u16 *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; - b8 running = true; - while (running) { - item = wapp_u16_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_u16_array_append_capped(array, item); - } -} - -void wapp_u16_array_clear(U16Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_u16_array_copy_capped(const U16Array *src, U16Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_u16_array_clear(dst); - - u16 *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; - b8 running = true; - while (running) { - item = wapp_u16_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_u16_array_append_capped(dst, item); - } -} - -U16Array *wapp_u16_array_append_alloc(const Allocator *allocator, U16Array *array, u16 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - U16Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U16_ARRAY_APPEND_ALLOC; - } - wapp_u16_array_copy_capped(array, output); - } - - wapp_u16_array_append_capped(output, item); - -RETURN_U16_ARRAY_APPEND_ALLOC: - return output; -} - -U16Array *wapp_u16_array_extend_alloc(const Allocator *allocator, U16Array *array, const U16Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - U16Array *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 = (U16Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U16_ARRAY_EXTEND_ALLOC; - } - wapp_u16_array_copy_capped(array, output); - } - - wapp_u16_array_extend_capped(output, other); - -RETURN_U16_ARRAY_EXTEND_ALLOC: - return output; -} - -U16Array *wapp_u16_array_copy_alloc(const Allocator *allocator, const U16Array *src, U16Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - U16Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (U16Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_U16_ARRAY_COPY_ALLOC; - } - } - - wapp_u16_array_clear(output); - wapp_u16_array_copy_capped(src, output); - -RETURN_U16_ARRAY_COPY_ALLOC: - return output; -} - -u16 *_u16_array_pop(U16Array *array) { - u64 index = array->count - 1; - u16 *out = wapp_u16_array_get(array, index); - --(array->count); - return out; -} - -u32 *wapp_u32_array_get(const U32Array *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 (u32 *)ptr; -} - -void wapp_u32_array_set(U32Array *array, u64 index, u32 *item) { - u32 *ptr = wapp_u32_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_u32_array_append_capped(U32Array *array, u32 *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_u32_array_set(array, index, item); -} - -void wapp_u32_array_extend_capped(U32Array *array, const U32Array *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"); - - u32 *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; - b8 running = true; - while (running) { - item = wapp_u32_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_u32_array_append_capped(array, item); - } -} - -void wapp_u32_array_clear(U32Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_u32_array_copy_capped(const U32Array *src, U32Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_u32_array_clear(dst); - - u32 *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; - b8 running = true; - while (running) { - item = wapp_u32_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_u32_array_append_capped(dst, item); - } -} - -U32Array *wapp_u32_array_append_alloc(const Allocator *allocator, U32Array *array, u32 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - U32Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U32_ARRAY_APPEND_ALLOC; - } - wapp_u32_array_copy_capped(array, output); - } - - wapp_u32_array_append_capped(output, item); - -RETURN_U32_ARRAY_APPEND_ALLOC: - return output; -} - -U32Array *wapp_u32_array_extend_alloc(const Allocator *allocator, U32Array *array, const U32Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - U32Array *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 = (U32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U32_ARRAY_EXTEND_ALLOC; - } - wapp_u32_array_copy_capped(array, output); - } - - wapp_u32_array_extend_capped(output, other); - -RETURN_U32_ARRAY_EXTEND_ALLOC: - return output; -} - -U32Array *wapp_u32_array_copy_alloc(const Allocator *allocator, const U32Array *src, U32Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - U32Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (U32Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_U32_ARRAY_COPY_ALLOC; - } - } - - wapp_u32_array_clear(output); - wapp_u32_array_copy_capped(src, output); - -RETURN_U32_ARRAY_COPY_ALLOC: - return output; -} - -u32 *_u32_array_pop(U32Array *array) { - u64 index = array->count - 1; - u32 *out = wapp_u32_array_get(array, index); - --(array->count); - return out; -} - -u64 *wapp_u64_array_get(const U64Array *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 (u64 *)ptr; -} - -void wapp_u64_array_set(U64Array *array, u64 index, u64 *item) { - u64 *ptr = wapp_u64_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_u64_array_append_capped(U64Array *array, u64 *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_u64_array_set(array, index, item); -} - -void wapp_u64_array_extend_capped(U64Array *array, const U64Array *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"); - - u64 *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; - b8 running = true; - while (running) { - item = wapp_u64_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_u64_array_append_capped(array, item); - } -} - -void wapp_u64_array_clear(U64Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_u64_array_copy_capped(const U64Array *src, U64Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_u64_array_clear(dst); - - u64 *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; - b8 running = true; - while (running) { - item = wapp_u64_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_u64_array_append_capped(dst, item); - } -} - -U64Array *wapp_u64_array_append_alloc(const Allocator *allocator, U64Array *array, u64 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - U64Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (U64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U64_ARRAY_APPEND_ALLOC; - } - wapp_u64_array_copy_capped(array, output); - } - - wapp_u64_array_append_capped(output, item); - -RETURN_U64_ARRAY_APPEND_ALLOC: - return output; -} - -U64Array *wapp_u64_array_extend_alloc(const Allocator *allocator, U64Array *array, const U64Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - U64Array *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 = (U64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_U64_ARRAY_EXTEND_ALLOC; - } - wapp_u64_array_copy_capped(array, output); - } - - wapp_u64_array_extend_capped(output, other); - -RETURN_U64_ARRAY_EXTEND_ALLOC: - return output; -} - -U64Array *wapp_u64_array_copy_alloc(const Allocator *allocator, const U64Array *src, U64Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - U64Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (U64Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_U64_ARRAY_COPY_ALLOC; - } - } - - wapp_u64_array_clear(output); - wapp_u64_array_copy_capped(src, output); - -RETURN_U64_ARRAY_COPY_ALLOC: - return output; -} - -u64 *_u64_array_pop(U64Array *array) { - u64 index = array->count - 1; - u64 *out = wapp_u64_array_get(array, index); - --(array->count); - return out; -} - -f32 *wapp_f32_array_get(const F32Array *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 (f32 *)ptr; -} - -void wapp_f32_array_set(F32Array *array, u64 index, f32 *item) { - f32 *ptr = wapp_f32_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_f32_array_append_capped(F32Array *array, f32 *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_f32_array_set(array, index, item); -} - -void wapp_f32_array_extend_capped(F32Array *array, const F32Array *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"); - - f32 *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; - b8 running = true; - while (running) { - item = wapp_f32_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_f32_array_append_capped(array, item); - } -} - -void wapp_f32_array_clear(F32Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_f32_array_copy_capped(const F32Array *src, F32Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_f32_array_clear(dst); - - f32 *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; - b8 running = true; - while (running) { - item = wapp_f32_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_f32_array_append_capped(dst, item); - } -} - -F32Array *wapp_f32_array_append_alloc(const Allocator *allocator, F32Array *array, f32 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - F32Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (F32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F32_ARRAY_APPEND_ALLOC; - } - wapp_f32_array_copy_capped(array, output); - } - - wapp_f32_array_append_capped(output, item); - -RETURN_F32_ARRAY_APPEND_ALLOC: - return output; -} - -F32Array *wapp_f32_array_extend_alloc(const Allocator *allocator, F32Array *array, const F32Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - F32Array *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 = (F32Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F32_ARRAY_EXTEND_ALLOC; - } - wapp_f32_array_copy_capped(array, output); - } - - wapp_f32_array_extend_capped(output, other); - -RETURN_F32_ARRAY_EXTEND_ALLOC: - return output; -} - -F32Array *wapp_f32_array_copy_alloc(const Allocator *allocator, const F32Array *src, F32Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - F32Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (F32Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_F32_ARRAY_COPY_ALLOC; - } - } - - wapp_f32_array_clear(output); - wapp_f32_array_copy_capped(src, output); - -RETURN_F32_ARRAY_COPY_ALLOC: - return output; -} - -f32 *_f32_array_pop(F32Array *array) { - u64 index = array->count - 1; - f32 *out = wapp_f32_array_get(array, index); - --(array->count); - return out; -} - -f64 *wapp_f64_array_get(const F64Array *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 (f64 *)ptr; -} - -void wapp_f64_array_set(F64Array *array, u64 index, f64 *item) { - f64 *ptr = wapp_f64_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_f64_array_append_capped(F64Array *array, f64 *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_f64_array_set(array, index, item); -} - -void wapp_f64_array_extend_capped(F64Array *array, const F64Array *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"); - - f64 *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; - b8 running = true; - while (running) { - item = wapp_f64_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_f64_array_append_capped(array, item); - } -} - -void wapp_f64_array_clear(F64Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_f64_array_copy_capped(const F64Array *src, F64Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_f64_array_clear(dst); - - f64 *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; - b8 running = true; - while (running) { - item = wapp_f64_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_f64_array_append_capped(dst, item); - } -} - -F64Array *wapp_f64_array_append_alloc(const Allocator *allocator, F64Array *array, f64 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - F64Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (F64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F64_ARRAY_APPEND_ALLOC; - } - wapp_f64_array_copy_capped(array, output); - } - - wapp_f64_array_append_capped(output, item); - -RETURN_F64_ARRAY_APPEND_ALLOC: - return output; -} - -F64Array *wapp_f64_array_extend_alloc(const Allocator *allocator, F64Array *array, const F64Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - F64Array *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 = (F64Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F64_ARRAY_EXTEND_ALLOC; - } - wapp_f64_array_copy_capped(array, output); - } - - wapp_f64_array_extend_capped(output, other); - -RETURN_F64_ARRAY_EXTEND_ALLOC: - return output; -} - -F64Array *wapp_f64_array_copy_alloc(const Allocator *allocator, const F64Array *src, F64Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - F64Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (F64Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_F64_ARRAY_COPY_ALLOC; - } - } - - wapp_f64_array_clear(output); - wapp_f64_array_copy_capped(src, output); - -RETURN_F64_ARRAY_COPY_ALLOC: - return output; -} - -f64 *_f64_array_pop(F64Array *array) { - u64 index = array->count - 1; - f64 *out = wapp_f64_array_get(array, index); - --(array->count); - return out; -} - -f128 *wapp_f128_array_get(const F128Array *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 (f128 *)ptr; -} - -void wapp_f128_array_set(F128Array *array, u64 index, f128 *item) { - f128 *ptr = wapp_f128_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_f128_array_append_capped(F128Array *array, f128 *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_f128_array_set(array, index, item); -} - -void wapp_f128_array_extend_capped(F128Array *array, const F128Array *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"); - - f128 *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; - b8 running = true; - while (running) { - item = wapp_f128_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_f128_array_append_capped(array, item); - } -} - -void wapp_f128_array_clear(F128Array *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_f128_array_copy_capped(const F128Array *src, F128Array *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_f128_array_clear(dst); - - f128 *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; - b8 running = true; - while (running) { - item = wapp_f128_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_f128_array_append_capped(dst, item); - } -} - -F128Array *wapp_f128_array_append_alloc(const Allocator *allocator, F128Array *array, f128 *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - F128Array *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (F128Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F128_ARRAY_APPEND_ALLOC; - } - wapp_f128_array_copy_capped(array, output); - } - - wapp_f128_array_append_capped(output, item); - -RETURN_F128_ARRAY_APPEND_ALLOC: - return output; -} - -F128Array *wapp_f128_array_extend_alloc(const Allocator *allocator, F128Array *array, const F128Array *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - F128Array *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 = (F128Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_F128_ARRAY_EXTEND_ALLOC; - } - wapp_f128_array_copy_capped(array, output); - } - - wapp_f128_array_extend_capped(output, other); - -RETURN_F128_ARRAY_EXTEND_ALLOC: - return output; -} - -F128Array *wapp_f128_array_copy_alloc(const Allocator *allocator, const F128Array *src, F128Array *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - F128Array *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (F128Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_F128_ARRAY_COPY_ALLOC; - } - } - - wapp_f128_array_clear(output); - wapp_f128_array_copy_capped(src, output); - -RETURN_F128_ARRAY_COPY_ALLOC: - return output; -} - -f128 *_f128_array_pop(F128Array *array) { - u64 index = array->count - 1; - f128 *out = wapp_f128_array_get(array, index); - --(array->count); - return out; -} - -iptr *wapp_iptr_array_get(const IptrArray *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 (iptr *)ptr; -} - -void wapp_iptr_array_set(IptrArray *array, u64 index, iptr *item) { - iptr *ptr = wapp_iptr_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_iptr_array_append_capped(IptrArray *array, iptr *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_iptr_array_set(array, index, item); -} - -void wapp_iptr_array_extend_capped(IptrArray *array, const IptrArray *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"); - - iptr *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; - b8 running = true; - while (running) { - item = wapp_iptr_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_iptr_array_append_capped(array, item); - } -} - -void wapp_iptr_array_clear(IptrArray *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_iptr_array_copy_capped(const IptrArray *src, IptrArray *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_iptr_array_clear(dst); - - iptr *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; - b8 running = true; - while (running) { - item = wapp_iptr_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_iptr_array_append_capped(dst, item); - } -} - -IptrArray *wapp_iptr_array_append_alloc(const Allocator *allocator, IptrArray *array, iptr *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - IptrArray *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (IptrArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_IPTR_ARRAY_APPEND_ALLOC; - } - wapp_iptr_array_copy_capped(array, output); - } - - wapp_iptr_array_append_capped(output, item); - -RETURN_IPTR_ARRAY_APPEND_ALLOC: - return output; -} - -IptrArray *wapp_iptr_array_extend_alloc(const Allocator *allocator, IptrArray *array, const IptrArray *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - IptrArray *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 = (IptrArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_IPTR_ARRAY_EXTEND_ALLOC; - } - wapp_iptr_array_copy_capped(array, output); - } - - wapp_iptr_array_extend_capped(output, other); - -RETURN_IPTR_ARRAY_EXTEND_ALLOC: - return output; -} - -IptrArray *wapp_iptr_array_copy_alloc(const Allocator *allocator, const IptrArray *src, IptrArray *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - IptrArray *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (IptrArray *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_IPTR_ARRAY_COPY_ALLOC; - } - } - - wapp_iptr_array_clear(output); - wapp_iptr_array_copy_capped(src, output); - -RETURN_IPTR_ARRAY_COPY_ALLOC: - return output; -} - -iptr *_iptr_array_pop(IptrArray *array) { - u64 index = array->count - 1; - iptr *out = wapp_iptr_array_get(array, index); - --(array->count); - return out; -} - -uptr *wapp_uptr_array_get(const UptrArray *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 (uptr *)ptr; -} - -void wapp_uptr_array_set(UptrArray *array, u64 index, uptr *item) { - uptr *ptr = wapp_uptr_array_get(array, index); - - memcpy((void *)ptr, (void *)item, array->item_size); -} - -void wapp_uptr_array_append_capped(UptrArray *array, uptr *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_uptr_array_set(array, index, item); -} - -void wapp_uptr_array_extend_capped(UptrArray *array, const UptrArray *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"); - - uptr *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; - b8 running = true; - while (running) { - item = wapp_uptr_array_get(other, item_index); - ++item_index; - running = item_index < items_to_add; - - if (!item) { - continue; - } - - wapp_uptr_array_append_capped(array, item); - } -} - -void wapp_uptr_array_clear(UptrArray *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - array->count = 0; -} - -void wapp_uptr_array_copy_capped(const UptrArray *src, UptrArray *dst) { - wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL"); - - wapp_uptr_array_clear(dst); - - uptr *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; - b8 running = true; - while (running) { - item = wapp_uptr_array_get(src, item_index); - ++item_index; - running = item_index < to_copy; - - if (!item) { - continue; - } - - wapp_uptr_array_append_capped(dst, item); - } -} - -UptrArray *wapp_uptr_array_append_alloc(const Allocator *allocator, UptrArray *array, uptr *item) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); - - UptrArray *output = array; - - if (array->count >= array->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); - output = (UptrArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_UPTR_ARRAY_APPEND_ALLOC; - } - wapp_uptr_array_copy_capped(array, output); - } - - wapp_uptr_array_append_capped(output, item); - -RETURN_UPTR_ARRAY_APPEND_ALLOC: - return output; -} - -UptrArray *wapp_uptr_array_extend_alloc(const Allocator *allocator, UptrArray *array, const UptrArray *other) { - wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL"); - - UptrArray *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 = (UptrArray *)_array_alloc_capacity(allocator, new_capacity, array->item_size); - if (!output) { - output = array; - goto RETURN_UPTR_ARRAY_EXTEND_ALLOC; - } - wapp_uptr_array_copy_capped(array, output); - } - - wapp_uptr_array_extend_capped(output, other); - -RETURN_UPTR_ARRAY_EXTEND_ALLOC: - return output; -} - -UptrArray *wapp_uptr_array_copy_alloc(const Allocator *allocator, const UptrArray *src, UptrArray *dst) { - wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL"); - - UptrArray *output = dst; - - if (src->count >= dst->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); - output = (UptrArray *)_array_alloc_capacity(allocator, new_capacity, src->item_size); - if (!output) { - output = dst; - goto RETURN_UPTR_ARRAY_COPY_ALLOC; - } - } - - wapp_uptr_array_clear(output); - wapp_uptr_array_copy_capped(src, output); - -RETURN_UPTR_ARRAY_COPY_ALLOC: - return output; -} + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); -uptr *_uptr_array_pop(UptrArray *array) { - u64 index = array->count - 1; - uptr *out = wapp_uptr_array_get(array, index); - --(array->count); - return out; + arr_header->count = 0; } -GenericArray *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size) { +u8 *_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(GenericArray) + item_size * capacity; - GenericArray *array = wapp_mem_allocator_alloc(allocator, allocation_size); - if (!array) { - goto RETURN_GENERIC_ARRAY_ALLOC; + u8 *output = NULL; + + u64 allocation_size = sizeof(ArrayHeader) + item_size * capacity; + ArrayHeader *header = wapp_mem_allocator_alloc(allocator, allocation_size); + if (!header) { + goto RETURN_ARRAY_ALLOC; } - array->items = (void *)((u8 *)array + sizeof(GenericArray)); - array->count = 0; - array->capacity = capacity; - array->item_size = item_size; + output = (u8 *)(header + 1); + header->magic = WAPP_ARRAY_MAGIC; + header->count = 0; + header->capacity = capacity; + header->item_size = item_size; -RETURN_GENERIC_ARRAY_ALLOC: - return array; +RETURN_ARRAY_ALLOC: + return output; } - diff --git a/src/primitives/array/array.h b/src/primitives/array/array.h index 999cfbb..eac8e38 100644 --- a/src/primitives/array/array.h +++ b/src/primitives/array/array.h @@ -1,6 +1,4 @@ -/** - * THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN. - */ +// vim:fileencoding=utf-8:foldmethod=marker #ifndef ARRAY_H #define ARRAY_H @@ -14,962 +12,132 @@ BEGIN_C_LINKAGE #endif // !WAPP_PLATFORM_CPP -#define wapp_void_ptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((VoidPArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(void *))) -#define wapp_str8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((Str8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(Str8))) -#define wapp_b8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((B8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(b8))) -#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))) +#define WAPP_ARRAY_MAGIC (u64)0x57415F415252 + +#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 wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY) ((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(TYPE))) #ifdef WAPP_PLATFORM_CPP -#define wapp_void_ptr_array(...) ([&]() { \ - wapp_persist 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_array(TYPE, ...) ([&]() { \ + u64 capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \ + \ + TYPE items[_calc_array_capacity(TYPE, __VA_ARGS__)] = {__VA_ARGS__}; \ + \ + wapp_persist u8 array[ \ + sizeof(ArrayHeader) + _calc_array_capacity(TYPE, __VA_ARGS__) * sizeof(TYPE) \ + ] = {0}; \ + ArrayHeader *header = (ArrayHeader *)array; \ + header->magic = WAPP_ARRAY_MAGIC; \ + header->count = _calc_array_count(TYPE, __VA_ARGS__); \ + header->capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \ + header->item_size = sizeof(TYPE); \ + \ + u8 *buf = (u8 *)(header + 1); \ + memcpy(buf, items, capacity * sizeof(TYPE)); \ + return (TYPE *)buf; \ }()) -#define wapp_void_ptr_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist void * buf[CAPACITY] = {}; \ - return VoidPArray{buf, 0, CAPACITY, sizeof(void *)}; \ +#define wapp_array_with_capacity(TYPE, CAPACITY) ([&]() { \ + wapp_persist u8 array[ \ + sizeof(ArrayHeader) + CAPACITY * sizeof(TYPE) \ + ] = {0}; \ + ArrayHeader *header = (ArrayHeader *)array; \ + header->magic = WAPP_ARRAY_MAGIC; \ + header->count = 0; \ + header->capacity = CAPACITY; \ + header->item_size = sizeof(TYPE); \ + \ + return (TYPE *)(header + 1); \ }()) -#define wapp_void_ptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_void_ptr_array_pop(ARRAY_PTR) : \ - (void *)(0) \ -) -#define wapp_str8_array(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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_b8_array(...) ([&]() { \ - wapp_persist b8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return B8Array{ \ - buf, \ - wapp_misc_utils_va_args_count(b8, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b8, __VA_ARGS__) * 2), \ - sizeof(b8) \ - }; \ -}()) -#define wapp_b8_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist b8 buf[CAPACITY] = {}; \ - return B8Array{buf, 0, CAPACITY, sizeof(b8)}; \ -}()) -#define wapp_b8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_b8_array_pop(ARRAY_PTR) : \ - b8{} \ -) -#define wapp_char_array(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist i32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ - return I32Array{ \ - buf, \ - wapp_misc_utils_va_args_count(i32, __VA_ARGS__), \ - wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2), \ - sizeof(i32) \ - }; \ -}()) -#define wapp_i32_array_with_capacity(CAPACITY) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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(...) ([&]() { \ - wapp_persist 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) ([&]() { \ - wapp_persist 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{} \ -) +#define wapp_array_pop(TYPE, ARRAY_PTR) \ + (ARRAY_PTR != NULL && _array_count((u8 *)ARRAY_PTR) > 0 ? \ + *((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))) : \ + TYPE{} \ + ) #else -#define wapp_void_ptr_array(...) ((VoidPArray){ \ - .items = (void *[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(void *, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_void_ptr_array_pop(ARRAY_PTR) : \ - (void *){0} \ -) -#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__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_str8_array_pop(ARRAY_PTR) : \ - (Str8){0} \ -) -#define wapp_b8_array(...) ((B8Array){ \ - .items = (b8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(b8, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b8, __VA_ARGS__) * 2), \ - .item_size = sizeof(b8) \ -}) -#define wapp_b8_array_with_capacity(CAPACITY) ((B8Array){.items = (b8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(b8)}) -#define wapp_b8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_b8_array_pop(ARRAY_PTR) : \ - (b8){0} \ -) -#define wapp_char_array(...) ((CharArray){ \ - .items = (char[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(char, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_char_array_pop(ARRAY_PTR) : \ - (char){0} \ -) -#define wapp_c8_array(...) ((C8Array){ \ - .items = (c8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(c8, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_c8_array_pop(ARRAY_PTR) : \ - (c8){0} \ -) -#define wapp_c16_array(...) ((C16Array){ \ - .items = (c16[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(c16, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_c16_array_pop(ARRAY_PTR) : \ - (c16){0} \ -) -#define wapp_c32_array(...) ((C32Array){ \ - .items = (c32[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(c32, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_c32_array_pop(ARRAY_PTR) : \ - (c32){0} \ -) -#define wapp_i8_array(...) ((I8Array){ \ - .items = (i8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(i8, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i8_array_pop(ARRAY_PTR) : \ - (i8){0} \ -) -#define wapp_i16_array(...) ((I16Array){ \ - .items = (i16[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(i16, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i16_array_pop(ARRAY_PTR) : \ - (i16){0} \ -) -#define wapp_i32_array(...) ((I32Array){ \ - .items = (i32[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(i32, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i32_array_pop(ARRAY_PTR) : \ - (i32){0} \ -) -#define wapp_i64_array(...) ((I64Array){ \ - .items = (i64[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(i64, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_i64_array_pop(ARRAY_PTR) : \ - (i64){0} \ -) -#define wapp_u8_array(...) ((U8Array){ \ - .items = (u8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(u8, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u8_array_pop(ARRAY_PTR) : \ - (u8){0} \ -) -#define wapp_u16_array(...) ((U16Array){ \ - .items = (u16[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(u16, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u16_array_pop(ARRAY_PTR) : \ - (u16){0} \ -) -#define wapp_u32_array(...) ((U32Array){ \ - .items = (u32[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(u32, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u32_array_pop(ARRAY_PTR) : \ - (u32){0} \ -) -#define wapp_u64_array(...) ((U64Array){ \ - .items = (u64[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(u64, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_u64_array_pop(ARRAY_PTR) : \ - (u64){0} \ -) -#define wapp_f32_array(...) ((F32Array){ \ - .items = (f32[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(f32, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_f32_array_pop(ARRAY_PTR) : \ - (f32){0} \ -) -#define wapp_f64_array(...) ((F64Array){ \ - .items = (f64[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(f64, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_f64_array_pop(ARRAY_PTR) : \ - (f64){0} \ -) -#define wapp_f128_array(...) ((F128Array){ \ - .items = (f128[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(f128, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_f128_array_pop(ARRAY_PTR) : \ - (f128){0} \ -) -#define wapp_iptr_array(...) ((IptrArray){ \ - .items = (iptr[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(iptr, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_iptr_array_pop(ARRAY_PTR) : \ - (iptr){0} \ -) -#define wapp_uptr_array(...) ((UptrArray){ \ - .items = (uptr[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ - .count = wapp_misc_utils_va_args_count(uptr, __VA_ARGS__), \ - .capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2), \ - .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_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ - *_uptr_array_pop(ARRAY_PTR) : \ - (uptr){0} \ -) +#define _stack_array(TYPE, SIZE) struct { ArrayHeader header; TYPE items[SIZE]; } +#define wapp_array(TYPE, ...) \ + (TYPE *)( \ + (_stack_array(TYPE, _calc_array_capacity(TYPE, __VA_ARGS__))){ \ + .header = { \ + .magic = WAPP_ARRAY_MAGIC, \ + .count = _calc_array_count(TYPE, __VA_ARGS__), \ + .capacity = _calc_array_capacity(TYPE, __VA_ARGS__), \ + .item_size = sizeof(TYPE), \ + }, \ + .items = {__VA_ARGS__}, \ + }.items \ + ) +#define wapp_array_with_capacity(TYPE, CAPACITY) \ + (TYPE *)( \ + (_stack_array(TYPE, CAPACITY)){ \ + .header = { \ + .magic = WAPP_ARRAY_MAGIC, \ + .count = 0, \ + .capacity = CAPACITY, \ + .item_size = sizeof(TYPE), \ + }, \ + .items = {0}, \ + }.items \ + ) +#define wapp_array_pop(TYPE, ARRAY_PTR) \ + (ARRAY_PTR != NULL && _array_count((u8 *)ARRAY_PTR) > 0 ? \ + *((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))) : \ + (TYPE){0} \ + ) #endif // !WAPP_PLATFORM_CPP -typedef struct str8 Str8; +#define wapp_array_count(ARRAY_PTR) \ + _array_count((u8 *)ARRAY_PTR) +#define wapp_array_capacity(ARRAY_PTR) \ + _array_capacity((u8 *)ARRAY_PTR) +#define wapp_array_item_size(ARRAY_PTR) \ + _array_item_size((u8 *)ARRAY_PTR) +#define wapp_array_set_count(ARRAY_PTR, COUNT) \ + _array_set_count((u8 *)ARRAY_PTR, COUNT) +#define wapp_array_get(TYPE, ARRAY_PTR, INDEX) \ + ((TYPE *)_array_get((u8 *)ARRAY_PTR, INDEX, sizeof(TYPE))) +#define wapp_array_set(TYPE, ARRAY_PTR, INDEX, VALUE_PTR) \ + _array_set((u8 *)ARRAY_PTR, INDEX, (u8 *)VALUE_PTR, sizeof(TYPE)) +#define wapp_array_append_capped(TYPE, ARRAY_PTR, VALUE_PTR) \ + _array_append_capped((u8 *)ARRAY_PTR, (u8 *)VALUE_PTR, sizeof(TYPE)) +#define wapp_array_extend_capped(TYPE, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + _array_extend_capped((u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_copy_capped(TYPE, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + _array_copy_capped((u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY_PTR, VALUE_PTR) \ + (TYPE *)_array_append_alloc(ALLOCATOR_PTR, (u8 *)ARRAY_PTR, (u8 *)VALUE_PTR, sizeof(TYPE)) +#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + (TYPE *)_array_extend_alloc(ALLOCATOR_PTR, (u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + (TYPE *)_array_copy_alloc(ALLOCATOR_PTR, (u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_clear(TYPE, ARRAY_PTR) \ + _array_clear((u8 *)ARRAY_PTR, sizeof(TYPE)) -typedef struct GenericArray GenericArray; -struct GenericArray { - void *items; +typedef struct header ArrayHeader; +struct header { + u64 magic; u64 count; u64 capacity; u64 item_size; }; -typedef struct VoidPArray VoidPArray; -struct VoidPArray { - void * *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct Str8Array Str8Array; -struct Str8Array { - Str8 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct B8Array B8Array; -struct B8Array { - b8 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct CharArray CharArray; -struct CharArray { - char *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct C8Array C8Array; -struct C8Array { - c8 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct C16Array C16Array; -struct C16Array { - c16 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct C32Array C32Array; -struct C32Array { - c32 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct I8Array I8Array; -struct I8Array { - i8 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct I16Array I16Array; -struct I16Array { - i16 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct I32Array I32Array; -struct I32Array { - i32 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct I64Array I64Array; -struct I64Array { - i64 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct U8Array U8Array; -struct U8Array { - u8 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct U16Array U16Array; -struct U16Array { - u16 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct U32Array U32Array; -struct U32Array { - u32 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct U64Array U64Array; -struct U64Array { - u64 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct F32Array F32Array; -struct F32Array { - f32 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct F64Array F64Array; -struct F64Array { - f64 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct F128Array F128Array; -struct F128Array { - f128 *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct IptrArray IptrArray; -struct IptrArray { - iptr *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -typedef struct UptrArray UptrArray; -struct UptrArray { - uptr *items; - u64 count; - u64 capacity; - u64 item_size; -}; - -void * *wapp_void_ptr_array_get(const VoidPArray *array, u64 index); -void wapp_void_ptr_array_set(VoidPArray *array, u64 index, void * *item); -void wapp_void_ptr_array_append_capped(VoidPArray *array, void * *item); -void wapp_void_ptr_array_extend_capped(VoidPArray *array, const VoidPArray *other); -void wapp_void_ptr_array_clear(VoidPArray *array); -void wapp_void_ptr_array_copy_capped(const VoidPArray *src, VoidPArray *dst); -VoidPArray *wapp_void_ptr_array_append_alloc(const Allocator *allocator, VoidPArray *array, void * *item); -VoidPArray *wapp_void_ptr_array_extend_alloc(const Allocator *allocator, VoidPArray *array, const VoidPArray *other); -VoidPArray *wapp_void_ptr_array_copy_alloc(const Allocator *allocator, const VoidPArray *src, VoidPArray *dst); -void * *_void_ptr_array_pop(VoidPArray *array); -Str8 *wapp_str8_array_get(const Str8Array *array, u64 index); -void wapp_str8_array_set(Str8Array *array, u64 index, Str8 *item); -void wapp_str8_array_append_capped(Str8Array *array, Str8 *item); -void wapp_str8_array_extend_capped(Str8Array *array, const Str8Array *other); -void wapp_str8_array_clear(Str8Array *array); -void wapp_str8_array_copy_capped(const Str8Array *src, Str8Array *dst); -Str8Array *wapp_str8_array_append_alloc(const Allocator *allocator, Str8Array *array, Str8 *item); -Str8Array *wapp_str8_array_extend_alloc(const Allocator *allocator, Str8Array *array, const Str8Array *other); -Str8Array *wapp_str8_array_copy_alloc(const Allocator *allocator, const Str8Array *src, Str8Array *dst); -Str8 *_str8_array_pop(Str8Array *array); -b8 *wapp_b8_array_get(const B8Array *array, u64 index); -void wapp_b8_array_set(B8Array *array, u64 index, b8 *item); -void wapp_b8_array_append_capped(B8Array *array, b8 *item); -void wapp_b8_array_extend_capped(B8Array *array, const B8Array *other); -void wapp_b8_array_clear(B8Array *array); -void wapp_b8_array_copy_capped(const B8Array *src, B8Array *dst); -B8Array *wapp_b8_array_append_alloc(const Allocator *allocator, B8Array *array, b8 *item); -B8Array *wapp_b8_array_extend_alloc(const Allocator *allocator, B8Array *array, const B8Array *other); -B8Array *wapp_b8_array_copy_alloc(const Allocator *allocator, const B8Array *src, B8Array *dst); -b8 *_b8_array_pop(B8Array *array); -char *wapp_char_array_get(const CharArray *array, u64 index); -void wapp_char_array_set(CharArray *array, u64 index, char *item); -void wapp_char_array_append_capped(CharArray *array, char *item); -void wapp_char_array_extend_capped(CharArray *array, const CharArray *other); -void wapp_char_array_clear(CharArray *array); -void wapp_char_array_copy_capped(const CharArray *src, CharArray *dst); -CharArray *wapp_char_array_append_alloc(const Allocator *allocator, CharArray *array, char *item); -CharArray *wapp_char_array_extend_alloc(const Allocator *allocator, CharArray *array, const CharArray *other); -CharArray *wapp_char_array_copy_alloc(const Allocator *allocator, const CharArray *src, CharArray *dst); -char *_char_array_pop(CharArray *array); -c8 *wapp_c8_array_get(const C8Array *array, u64 index); -void wapp_c8_array_set(C8Array *array, u64 index, c8 *item); -void wapp_c8_array_append_capped(C8Array *array, c8 *item); -void wapp_c8_array_extend_capped(C8Array *array, const C8Array *other); -void wapp_c8_array_clear(C8Array *array); -void wapp_c8_array_copy_capped(const C8Array *src, C8Array *dst); -C8Array *wapp_c8_array_append_alloc(const Allocator *allocator, C8Array *array, c8 *item); -C8Array *wapp_c8_array_extend_alloc(const Allocator *allocator, C8Array *array, const C8Array *other); -C8Array *wapp_c8_array_copy_alloc(const Allocator *allocator, const C8Array *src, C8Array *dst); -c8 *_c8_array_pop(C8Array *array); -c16 *wapp_c16_array_get(const C16Array *array, u64 index); -void wapp_c16_array_set(C16Array *array, u64 index, c16 *item); -void wapp_c16_array_append_capped(C16Array *array, c16 *item); -void wapp_c16_array_extend_capped(C16Array *array, const C16Array *other); -void wapp_c16_array_clear(C16Array *array); -void wapp_c16_array_copy_capped(const C16Array *src, C16Array *dst); -C16Array *wapp_c16_array_append_alloc(const Allocator *allocator, C16Array *array, c16 *item); -C16Array *wapp_c16_array_extend_alloc(const Allocator *allocator, C16Array *array, const C16Array *other); -C16Array *wapp_c16_array_copy_alloc(const Allocator *allocator, const C16Array *src, C16Array *dst); -c16 *_c16_array_pop(C16Array *array); -c32 *wapp_c32_array_get(const C32Array *array, u64 index); -void wapp_c32_array_set(C32Array *array, u64 index, c32 *item); -void wapp_c32_array_append_capped(C32Array *array, c32 *item); -void wapp_c32_array_extend_capped(C32Array *array, const C32Array *other); -void wapp_c32_array_clear(C32Array *array); -void wapp_c32_array_copy_capped(const C32Array *src, C32Array *dst); -C32Array *wapp_c32_array_append_alloc(const Allocator *allocator, C32Array *array, c32 *item); -C32Array *wapp_c32_array_extend_alloc(const Allocator *allocator, C32Array *array, const C32Array *other); -C32Array *wapp_c32_array_copy_alloc(const Allocator *allocator, const C32Array *src, C32Array *dst); -c32 *_c32_array_pop(C32Array *array); -i8 *wapp_i8_array_get(const I8Array *array, u64 index); -void wapp_i8_array_set(I8Array *array, u64 index, i8 *item); -void wapp_i8_array_append_capped(I8Array *array, i8 *item); -void wapp_i8_array_extend_capped(I8Array *array, const I8Array *other); -void wapp_i8_array_clear(I8Array *array); -void wapp_i8_array_copy_capped(const I8Array *src, I8Array *dst); -I8Array *wapp_i8_array_append_alloc(const Allocator *allocator, I8Array *array, i8 *item); -I8Array *wapp_i8_array_extend_alloc(const Allocator *allocator, I8Array *array, const I8Array *other); -I8Array *wapp_i8_array_copy_alloc(const Allocator *allocator, const I8Array *src, I8Array *dst); -i8 *_i8_array_pop(I8Array *array); -i16 *wapp_i16_array_get(const I16Array *array, u64 index); -void wapp_i16_array_set(I16Array *array, u64 index, i16 *item); -void wapp_i16_array_append_capped(I16Array *array, i16 *item); -void wapp_i16_array_extend_capped(I16Array *array, const I16Array *other); -void wapp_i16_array_clear(I16Array *array); -void wapp_i16_array_copy_capped(const I16Array *src, I16Array *dst); -I16Array *wapp_i16_array_append_alloc(const Allocator *allocator, I16Array *array, i16 *item); -I16Array *wapp_i16_array_extend_alloc(const Allocator *allocator, I16Array *array, const I16Array *other); -I16Array *wapp_i16_array_copy_alloc(const Allocator *allocator, const I16Array *src, I16Array *dst); -i16 *_i16_array_pop(I16Array *array); -i32 *wapp_i32_array_get(const I32Array *array, u64 index); -void wapp_i32_array_set(I32Array *array, u64 index, i32 *item); -void wapp_i32_array_append_capped(I32Array *array, i32 *item); -void wapp_i32_array_extend_capped(I32Array *array, const I32Array *other); -void wapp_i32_array_clear(I32Array *array); -void wapp_i32_array_copy_capped(const I32Array *src, I32Array *dst); -I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *array, i32 *item); -I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *array, const I32Array *other); -I32Array *wapp_i32_array_copy_alloc(const Allocator *allocator, const I32Array *src, I32Array *dst); -i32 *_i32_array_pop(I32Array *array); -i64 *wapp_i64_array_get(const I64Array *array, u64 index); -void wapp_i64_array_set(I64Array *array, u64 index, i64 *item); -void wapp_i64_array_append_capped(I64Array *array, i64 *item); -void wapp_i64_array_extend_capped(I64Array *array, const I64Array *other); -void wapp_i64_array_clear(I64Array *array); -void wapp_i64_array_copy_capped(const I64Array *src, I64Array *dst); -I64Array *wapp_i64_array_append_alloc(const Allocator *allocator, I64Array *array, i64 *item); -I64Array *wapp_i64_array_extend_alloc(const Allocator *allocator, I64Array *array, const I64Array *other); -I64Array *wapp_i64_array_copy_alloc(const Allocator *allocator, const I64Array *src, I64Array *dst); -i64 *_i64_array_pop(I64Array *array); -u8 *wapp_u8_array_get(const U8Array *array, u64 index); -void wapp_u8_array_set(U8Array *array, u64 index, u8 *item); -void wapp_u8_array_append_capped(U8Array *array, u8 *item); -void wapp_u8_array_extend_capped(U8Array *array, const U8Array *other); -void wapp_u8_array_clear(U8Array *array); -void wapp_u8_array_copy_capped(const U8Array *src, U8Array *dst); -U8Array *wapp_u8_array_append_alloc(const Allocator *allocator, U8Array *array, u8 *item); -U8Array *wapp_u8_array_extend_alloc(const Allocator *allocator, U8Array *array, const U8Array *other); -U8Array *wapp_u8_array_copy_alloc(const Allocator *allocator, const U8Array *src, U8Array *dst); -u8 *_u8_array_pop(U8Array *array); -u16 *wapp_u16_array_get(const U16Array *array, u64 index); -void wapp_u16_array_set(U16Array *array, u64 index, u16 *item); -void wapp_u16_array_append_capped(U16Array *array, u16 *item); -void wapp_u16_array_extend_capped(U16Array *array, const U16Array *other); -void wapp_u16_array_clear(U16Array *array); -void wapp_u16_array_copy_capped(const U16Array *src, U16Array *dst); -U16Array *wapp_u16_array_append_alloc(const Allocator *allocator, U16Array *array, u16 *item); -U16Array *wapp_u16_array_extend_alloc(const Allocator *allocator, U16Array *array, const U16Array *other); -U16Array *wapp_u16_array_copy_alloc(const Allocator *allocator, const U16Array *src, U16Array *dst); -u16 *_u16_array_pop(U16Array *array); -u32 *wapp_u32_array_get(const U32Array *array, u64 index); -void wapp_u32_array_set(U32Array *array, u64 index, u32 *item); -void wapp_u32_array_append_capped(U32Array *array, u32 *item); -void wapp_u32_array_extend_capped(U32Array *array, const U32Array *other); -void wapp_u32_array_clear(U32Array *array); -void wapp_u32_array_copy_capped(const U32Array *src, U32Array *dst); -U32Array *wapp_u32_array_append_alloc(const Allocator *allocator, U32Array *array, u32 *item); -U32Array *wapp_u32_array_extend_alloc(const Allocator *allocator, U32Array *array, const U32Array *other); -U32Array *wapp_u32_array_copy_alloc(const Allocator *allocator, const U32Array *src, U32Array *dst); -u32 *_u32_array_pop(U32Array *array); -u64 *wapp_u64_array_get(const U64Array *array, u64 index); -void wapp_u64_array_set(U64Array *array, u64 index, u64 *item); -void wapp_u64_array_append_capped(U64Array *array, u64 *item); -void wapp_u64_array_extend_capped(U64Array *array, const U64Array *other); -void wapp_u64_array_clear(U64Array *array); -void wapp_u64_array_copy_capped(const U64Array *src, U64Array *dst); -U64Array *wapp_u64_array_append_alloc(const Allocator *allocator, U64Array *array, u64 *item); -U64Array *wapp_u64_array_extend_alloc(const Allocator *allocator, U64Array *array, const U64Array *other); -U64Array *wapp_u64_array_copy_alloc(const Allocator *allocator, const U64Array *src, U64Array *dst); -u64 *_u64_array_pop(U64Array *array); -f32 *wapp_f32_array_get(const F32Array *array, u64 index); -void wapp_f32_array_set(F32Array *array, u64 index, f32 *item); -void wapp_f32_array_append_capped(F32Array *array, f32 *item); -void wapp_f32_array_extend_capped(F32Array *array, const F32Array *other); -void wapp_f32_array_clear(F32Array *array); -void wapp_f32_array_copy_capped(const F32Array *src, F32Array *dst); -F32Array *wapp_f32_array_append_alloc(const Allocator *allocator, F32Array *array, f32 *item); -F32Array *wapp_f32_array_extend_alloc(const Allocator *allocator, F32Array *array, const F32Array *other); -F32Array *wapp_f32_array_copy_alloc(const Allocator *allocator, const F32Array *src, F32Array *dst); -f32 *_f32_array_pop(F32Array *array); -f64 *wapp_f64_array_get(const F64Array *array, u64 index); -void wapp_f64_array_set(F64Array *array, u64 index, f64 *item); -void wapp_f64_array_append_capped(F64Array *array, f64 *item); -void wapp_f64_array_extend_capped(F64Array *array, const F64Array *other); -void wapp_f64_array_clear(F64Array *array); -void wapp_f64_array_copy_capped(const F64Array *src, F64Array *dst); -F64Array *wapp_f64_array_append_alloc(const Allocator *allocator, F64Array *array, f64 *item); -F64Array *wapp_f64_array_extend_alloc(const Allocator *allocator, F64Array *array, const F64Array *other); -F64Array *wapp_f64_array_copy_alloc(const Allocator *allocator, const F64Array *src, F64Array *dst); -f64 *_f64_array_pop(F64Array *array); -f128 *wapp_f128_array_get(const F128Array *array, u64 index); -void wapp_f128_array_set(F128Array *array, u64 index, f128 *item); -void wapp_f128_array_append_capped(F128Array *array, f128 *item); -void wapp_f128_array_extend_capped(F128Array *array, const F128Array *other); -void wapp_f128_array_clear(F128Array *array); -void wapp_f128_array_copy_capped(const F128Array *src, F128Array *dst); -F128Array *wapp_f128_array_append_alloc(const Allocator *allocator, F128Array *array, f128 *item); -F128Array *wapp_f128_array_extend_alloc(const Allocator *allocator, F128Array *array, const F128Array *other); -F128Array *wapp_f128_array_copy_alloc(const Allocator *allocator, const F128Array *src, F128Array *dst); -f128 *_f128_array_pop(F128Array *array); -iptr *wapp_iptr_array_get(const IptrArray *array, u64 index); -void wapp_iptr_array_set(IptrArray *array, u64 index, iptr *item); -void wapp_iptr_array_append_capped(IptrArray *array, iptr *item); -void wapp_iptr_array_extend_capped(IptrArray *array, const IptrArray *other); -void wapp_iptr_array_clear(IptrArray *array); -void wapp_iptr_array_copy_capped(const IptrArray *src, IptrArray *dst); -IptrArray *wapp_iptr_array_append_alloc(const Allocator *allocator, IptrArray *array, iptr *item); -IptrArray *wapp_iptr_array_extend_alloc(const Allocator *allocator, IptrArray *array, const IptrArray *other); -IptrArray *wapp_iptr_array_copy_alloc(const Allocator *allocator, const IptrArray *src, IptrArray *dst); -iptr *_iptr_array_pop(IptrArray *array); -uptr *wapp_uptr_array_get(const UptrArray *array, u64 index); -void wapp_uptr_array_set(UptrArray *array, u64 index, uptr *item); -void wapp_uptr_array_append_capped(UptrArray *array, uptr *item); -void wapp_uptr_array_extend_capped(UptrArray *array, const UptrArray *other); -void wapp_uptr_array_clear(UptrArray *array); -void wapp_uptr_array_copy_capped(const UptrArray *src, UptrArray *dst); -UptrArray *wapp_uptr_array_append_alloc(const Allocator *allocator, UptrArray *array, uptr *item); -UptrArray *wapp_uptr_array_extend_alloc(const Allocator *allocator, UptrArray *array, const UptrArray *other); -UptrArray *wapp_uptr_array_copy_alloc(const Allocator *allocator, const UptrArray *src, UptrArray *dst); -uptr *_uptr_array_pop(UptrArray *array); -GenericArray *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size); +u64 _array_count(u8 *array); +u64 _array_capacity(u8 *array); +u64 _array_item_size(u8 *array); +void _array_set_count(u8 *array, u64 count); +u8 *_array_get(u8 *array, u64 index, u64 item_size); +void _array_set(u8 *array, u64 index, u8 *value, u64 item_size); +void _array_append_capped(u8 *array, u8 *value, u64 item_size); +void _array_extend_capped(u8 *dst_array, const u8 *src_array, u64 item_size); +void _array_copy_capped(u8 *dst_array, const u8 *src_array, u64 item_size); +u8 *_array_append_alloc(const Allocator *allocator, u8 *array, u8 *value, u64 item_size); +u8 *_array_extend_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size); +u8 *_array_copy_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size); +u8 *_array_pop(u8 *array, u64 item_size); +void _array_clear(u8 *array, u64 item_size); +u8 *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size); #ifdef WAPP_PLATFORM_CPP END_C_LINKAGE diff --git a/src/primitives/strings/str8/str8.c b/src/primitives/strings/str8/str8.c index 8f8dabb..6e14884 100644 --- a/src/primitives/strings/str8/str8.c +++ b/src/primitives/strings/str8/str8.c @@ -1,6 +1,7 @@ // vim:fileencoding=utf-8:foldmethod=marker #include "str8.h" +#include "../../array/array.h" #include "../../../common/aliases/aliases.h" #include "../../../common/assert/assert.h" #include "../../mem_allocator/mem_allocator.h" @@ -260,14 +261,15 @@ void wapp_str8_to_upper(Str8 *dst, Str8RO *src) { } } -void wapp_str8_from_bytes(Str8 *dst, const U8Array *src) { - u64 size = src->count * src->item_size; +void wapp_str8_from_bytes(Str8 *dst, const u8 *src_byte_array) { + wapp_debug_assert(src_byte_array != NULL && dst != NULL, "`dst` and `src` should not be NULL"); + + u64 size = wapp_array_count(src_byte_array) * wapp_array_item_size(src_byte_array); - wapp_debug_assert(src != NULL && dst != NULL, "`dst` and `src` should not be NULL"); wapp_debug_assert(dst->capacity >= size, "`dst` does not have enough capacity"); dst->size = size; - memcpy(dst->buf, src->items, size); + memcpy(dst->buf, src_byte_array, size); } i64 wapp_str8_find(Str8RO *str, Str8RO substr) { diff --git a/src/primitives/strings/str8/str8.h b/src/primitives/strings/str8/str8.h index cd69ef5..fef0288 100644 --- a/src/primitives/strings/str8/str8.h +++ b/src/primitives/strings/str8/str8.h @@ -37,14 +37,14 @@ typedef const Str8 Str8RO; #ifdef WAPP_PLATFORM_CPP // Uses a lambda to achieve the same behaviour achieved by the C macro #define wapp_str8_buf(CAPACITY) ([&](){ \ - wapp_persist c8 buf[CAPACITY] = {}; \ + wapp_persist c8 buf[CAPACITY] = {}; \ memset(buf, 0, CAPACITY); \ return Str8{CAPACITY, 0, buf}; \ }()) // Uses a lambda to achieve the same behaviour achieved by the C macro #define wapp_str8_lit(STRING) ([&]() { \ - wapp_persist c8 buf[sizeof(STRING) * 2] = {}; \ + wapp_persist c8 buf[sizeof(STRING) * 2] = {}; \ memcpy(buf, STRING, sizeof(STRING)); \ return Str8{(sizeof(STRING) - 1) * 2, sizeof(STRING) - 1, buf}; \ }()) @@ -99,7 +99,7 @@ void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity); void wapp_str8_format(Str8 *dst, const char *format, ...); void wapp_str8_to_lower(Str8 *dst, Str8RO *src); void wapp_str8_to_upper(Str8 *dst, Str8RO *src); -void wapp_str8_from_bytes(Str8 *dst, const U8Array *src); +void wapp_str8_from_bytes(Str8 *dst, const u8 *src_byte_array); /** * Str8 find functions diff --git a/tests/array/test_i32_array.c b/tests/array/test_i32_array.c index 0906fdd..f6a6698 100644 --- a/tests/array/test_i32_array.c +++ b/tests/array/test_i32_array.c @@ -4,16 +4,16 @@ TestFuncResult test_i32_array(void) { b8 result; - I32Array array = wapp_i32_array(1, 2, 3, 4, 5, 6, 7); - result = array.count == 7 && array.capacity == 16; + i32 *array = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7); + result = wapp_array_count(array) == 7 && wapp_array_capacity(array) == 16; i32 *item; - u64 count = array.count; - u64 index = 0; + u64 count = wapp_array_count(array); + u64 index = 0; b8 running = true; while (running) { - item = wapp_i32_array_get(&array, index); - result = result && item && (*item == (i32)(index + 1)); + item = wapp_array_get(i32, array, index); + result = result && item && *item == (i32)(index + 1); ++index; running = index < count; @@ -25,8 +25,8 @@ TestFuncResult test_i32_array(void) { TestFuncResult test_i32_array_with_capacity(void) { b8 result; - I32Array array = wapp_i32_array_with_capacity(64); - result = array.count == 0 && array.capacity == 64; + i32 *array = wapp_array_with_capacity(i32, 64); + result = wapp_array_count(array) == 0 && wapp_array_capacity(array) == 64; return wapp_tester_result(result); } @@ -34,15 +34,15 @@ TestFuncResult test_i32_array_with_capacity(void) { TestFuncResult test_i32_array_get(void) { b8 result = true; - I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = array.count; - u64 index = 0; + u64 count = wapp_array_count(array); + u64 index = 0; b8 running = true; while (running) { - item = wapp_i32_array_get(&array, index); - result = result && item && (*item == (i32)index); + item = wapp_array_get(i32, array, index); + result = result && item && *item == (i32)index; ++index; running = index < count; @@ -54,17 +54,17 @@ TestFuncResult test_i32_array_get(void) { TestFuncResult test_i32_array_set(void) { b8 result = true; - I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = array.count; - u64 index = 0; + u64 count = wapp_array_count(array); + u64 index = 0; b8 running = true; while (running) { i32 num = (i32)(index * 2); - wapp_i32_array_set(&array, index, &num); - item = wapp_i32_array_get(&array, index); - result = result && item && (*item == (i32)(index * 2)); + wapp_array_set(i32, array, index, &num); + item = wapp_array_get(i32, array, index); + result = result && item && *item == (i32)(index * 2); ++index; running = index < count; @@ -76,17 +76,17 @@ TestFuncResult test_i32_array_set(void) { TestFuncResult test_i32_array_append_capped(void) { b8 result; - I32Array array = wapp_i32_array_with_capacity(64); - wapp_i32_array_append_capped(&array, &((i32){10})); + i32 *array = wapp_array_with_capacity(i32, 64); + wapp_array_append_capped(i32, array, &((i32){10})); - result = array.count == 1; - i32 *item = wapp_i32_array_get(&array, 0); + result = wapp_array_count(array) == 1; + i32 *item = wapp_array_get(i32, array, 0); result = result && item && *item == 10; - array = wapp_i32_array(1); - wapp_i32_array_append_capped(&array, &((i32){10})); + array = wapp_array(i32, 1); + 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); } @@ -94,41 +94,14 @@ TestFuncResult test_i32_array_append_capped(void) { TestFuncResult test_i32_array_extend_capped(void) { b8 result; - I32Array array1 = wapp_i32_array(1, 2, 3, 4); - I32Array array2 = wapp_i32_array(10, 20); + i32 *array1 = wapp_array(i32, 1, 2, 3, 4); + i32 *array2 = wapp_array(i32, 10, 20); - result = array1.count == 4 && array2.count == 2; + result = wapp_array_count(array1) == 4 && wapp_array_count(array2) == 2; - wapp_i32_array_extend_capped(&array1, &array2); + wapp_array_extend_capped(i32, array1, array2); - result = result && array1.count == 6; - - return wapp_tester_result(result); -} - -TestFuncResult test_i32_array_clear(void) { - b8 result; - - I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); - result = array.count == 9; - - wapp_i32_array_clear(&array); - - result = result && array.count == 0; - - return wapp_tester_result(result); -} - -TestFuncResult test_i32_array_pop(void) { - b8 result; - - I32Array array1 = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); - I32Array array2 = wapp_i32_array_with_capacity(32); - - i32 item1 = wapp_i32_array_pop(&array1); - i32 item2 = wapp_i32_array_pop(&array2); - - result = item1 == 8 && item2 == 0; + result = result && wapp_array_count(array1) == 6; return wapp_tester_result(result); } @@ -136,31 +109,31 @@ TestFuncResult test_i32_array_pop(void) { TestFuncResult test_i32_array_copy_capped(void) { b8 result; - I32Array src = wapp_i32_array(1, 2, 3, 4, 5); - I32Array dst1 = wapp_i32_array(1, 2, 3, 4, 5, 6); - I32Array dst2 = wapp_i32_array(1, 2); + i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); + i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); + i32 *dst2 = wapp_array(i32, 1, 2); u64 expected_count = 5; - wapp_i32_array_copy_capped(&src, &dst1); - result = dst1.count == expected_count; + wapp_array_copy_capped(i32, dst1, src); + result = wapp_array_count(dst1) == expected_count; u64 index = 0; b8 running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(&dst1, index)); + result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst1, index); ++index; running = index < expected_count; } expected_count = 4; - wapp_i32_array_copy_capped(&src, &dst2); - result = result && dst2.count == expected_count; + wapp_array_copy_capped(i32, dst2, src); + result = result && wapp_array_count(dst2) == expected_count; index = 0; running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(&dst2, index)); + result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst2, index); ++index; running = index < expected_count; @@ -174,9 +147,9 @@ TestFuncResult test_i32_array_alloc_capacity(void) { Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); u64 capacity = 32; - I32Array *array = wapp_i32_array_alloc_capacity(&allocator, capacity); + i32 *array = wapp_array_alloc_capacity(i32, &allocator, capacity); - result = array && array->capacity == capacity; + result = array && wapp_array_capacity(array) == capacity; wapp_mem_arena_allocator_destroy(&allocator); @@ -187,23 +160,23 @@ TestFuncResult test_i32_array_append_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - I32Array array1 = wapp_i32_array(1, 2, 3, 4, 5, 6, 7, 8); - I32Array array2 = wapp_i32_array(1, 2); + i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array2 = wapp_array(i32, 1, 2); - I32Array *arr_ptr = wapp_i32_array_append_alloc(&allocator, &array1, &((i32){10})); - result = arr_ptr == &array1; + i32 *arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &((i32){10})); + result = arr_ptr == array1; u64 count = 4; u64 index = 0; b8 running = true; while (running) { i32 num = (i32)index; - arr_ptr = wapp_i32_array_append_alloc(&allocator, &array2, &num); + arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num); ++index; running = index < count; } - result = result && arr_ptr != &array2; + result = result && arr_ptr != array2; wapp_mem_arena_allocator_destroy(&allocator); @@ -214,15 +187,15 @@ TestFuncResult test_i32_array_extend_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - I32Array array1 = wapp_i32_array(1, 2, 3, 4, 5, 6, 7, 8); - I32Array array2 = wapp_i32_array(1, 2); - I32Array array3 = wapp_i32_array(1, 2, 3, 4); + i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array2 = wapp_array(i32, 1, 2); + i32 *array3 = wapp_array(i32, 1, 2, 3, 4); - I32Array *arr_ptr = wapp_i32_array_extend_alloc(&allocator, &array1, &array3); - result = arr_ptr == &array1; + i32 *arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3); + result = arr_ptr == array1; - arr_ptr = wapp_i32_array_extend_alloc(&allocator, &array2, &array3); - result = result && arr_ptr != &array2; + arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3); + result = result && arr_ptr != array2; wapp_mem_arena_allocator_destroy(&allocator); @@ -233,32 +206,32 @@ TestFuncResult test_i32_array_copy_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - I32Array src = wapp_i32_array(1, 2, 3, 4, 5); - I32Array dst1 = wapp_i32_array(1, 2, 3, 4, 5, 6); - I32Array dst2 = wapp_i32_array(1, 2); - I32Array *array_ptr = NULL; + i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); + i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); + i32 *dst2 = wapp_array(i32, 1, 2); + i32 *array_ptr = NULL; u64 expected_count = 5; - array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst1); - result = array_ptr->count == expected_count && array_ptr == &dst1; + array_ptr = wapp_array_copy_alloc(i32, &allocator, dst1, src); + result = wapp_array_count(array_ptr) == expected_count && array_ptr == dst1; u64 index = 0; b8 running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(array_ptr, index)); + result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, array_ptr, index); ++index; running = index < expected_count; } expected_count = 5; - array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst2); - result = result && array_ptr->count == expected_count && array_ptr != &dst2; + array_ptr = wapp_array_copy_alloc(i32, &allocator, dst2, src); + result = result && wapp_array_count(array_ptr) == expected_count && array_ptr != dst2; index = 0; running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(array_ptr, index)); + result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, array_ptr, index); ++index; running = index < expected_count; @@ -268,3 +241,30 @@ TestFuncResult test_i32_array_copy_alloc(void) { return wapp_tester_result(result); } + +TestFuncResult test_i32_array_pop(void) { + b8 result; + + i32 *array1 = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array2 = wapp_array_with_capacity(i32, 32); + + i32 item1 = wapp_array_pop(i32, array1); + i32 item2 = wapp_array_pop(i32, array2); + + result = item1 == 8 && item2 == 0; + + return wapp_tester_result(result); +} + +TestFuncResult test_i32_array_clear(void) { + b8 result; + + i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); + result = wapp_array_count(array) == 9; + + wapp_array_clear(i32, array); + + result = result && wapp_array_count(array) == 0; + + return wapp_tester_result(result); +} diff --git a/tests/array/test_i32_array.cc b/tests/array/test_i32_array.cc index 2247ed9..bb8d066 100644 --- a/tests/array/test_i32_array.cc +++ b/tests/array/test_i32_array.cc @@ -4,15 +4,15 @@ TestFuncResult test_i32_array(void) { b8 result; - I32Array array = wapp_i32_array(1, 2, 3, 4, 5, 6, 7); - result = array.count == 7 && array.capacity == 16; + i32 *array = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7); + result = wapp_array_count(array) == 7 && wapp_array_capacity(array) == 16; i32 *item; - u64 count = array.count; + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { - item = wapp_i32_array_get(&array, index); + item = wapp_array_get(i32, array, index); result = result && item && (*item == (i32)(index + 1)); ++index; @@ -25,8 +25,8 @@ TestFuncResult test_i32_array(void) { TestFuncResult test_i32_array_with_capacity(void) { b8 result; - I32Array array = wapp_i32_array_with_capacity(64); - result = array.count == 0 && array.capacity == 64; + i32 *array = wapp_array_with_capacity(i32, 64); + result = wapp_array_count(array) == 0 && wapp_array_capacity(array) == 64; return wapp_tester_result(result); } @@ -34,14 +34,14 @@ TestFuncResult test_i32_array_with_capacity(void) { TestFuncResult test_i32_array_get(void) { b8 result = true; - I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = array.count; + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { - item = wapp_i32_array_get(&array, index); + item = wapp_array_get(i32, array, index); result = result && item && (*item == (i32)index); ++index; @@ -54,16 +54,16 @@ TestFuncResult test_i32_array_get(void) { TestFuncResult test_i32_array_set(void) { b8 result = true; - I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = array.count; + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { i32 num = (i32)(index * 2); - wapp_i32_array_set(&array, index, &num); - item = wapp_i32_array_get(&array, index); + wapp_array_set(i32, array, index, &num); + item = wapp_array_get(i32, array, index); result = result && item && (*item == (i32)(index * 2)); ++index; @@ -76,19 +76,19 @@ TestFuncResult test_i32_array_set(void) { TestFuncResult test_i32_array_append_capped(void) { b8 result; - I32Array array = wapp_i32_array_with_capacity(64); + i32 *array = wapp_array_with_capacity(i32, 64); i32 item1 = 10; - wapp_i32_array_append_capped(&array, &item1); + wapp_array_append_capped(i32, array, &item1); - result = array.count == 1; - i32 *item = wapp_i32_array_get(&array, 0); + result = wapp_array_count(array) == 1; + i32 *item = wapp_array_get(i32, array, 0); result = result && item && *item == 10; - array = wapp_i32_array(1); + array = wapp_array(i32, 1); i32 item2 = 10; - wapp_i32_array_append_capped(&array, &item2); + wapp_array_append_capped(i32, array, &item2); - result = result && array.count == 2; + result = result && wapp_array_count(array) == 2; return wapp_tester_result(result); } @@ -96,41 +96,14 @@ TestFuncResult test_i32_array_append_capped(void) { TestFuncResult test_i32_array_extend_capped(void) { b8 result; - I32Array array1 = wapp_i32_array(1, 2, 3, 4); - I32Array array2 = wapp_i32_array(10, 20); + i32 *array1 = wapp_array(i32, 1, 2, 3, 4); + i32 *array2 = wapp_array(i32, 10, 20); - result = array1.count == 4 && array2.count == 2; + result = wapp_array_count(array1) == 4 && wapp_array_count(array2) == 2; - wapp_i32_array_extend_capped(&array1, &array2); + wapp_array_extend_capped(i32, array1, array2); - result = result && array1.count == 6; - - return wapp_tester_result(result); -} - -TestFuncResult test_i32_array_clear(void) { - b8 result; - - I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); - result = array.count == 9; - - wapp_i32_array_clear(&array); - - result = result && array.count == 0; - - return wapp_tester_result(result); -} - -TestFuncResult test_i32_array_pop(void) { - b8 result; - - I32Array array1 = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); - I32Array array2 = wapp_i32_array_with_capacity(32); - - i32 item1 = wapp_i32_array_pop(&array1); - i32 item2 = wapp_i32_array_pop(&array2); - - result = item1 == 8 && item2 == 0; + result = result && wapp_array_count(array1) == 6; return wapp_tester_result(result); } @@ -138,31 +111,31 @@ TestFuncResult test_i32_array_pop(void) { TestFuncResult test_i32_array_copy_capped(void) { b8 result; - I32Array src = wapp_i32_array(1, 2, 3, 4, 5); - I32Array dst1 = wapp_i32_array(1, 2, 3, 4, 5, 6); - I32Array dst2 = wapp_i32_array(1, 2); + i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); + i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); + i32 *dst2 = wapp_array(i32, 1, 2); u64 expected_count = 5; - wapp_i32_array_copy_capped(&src, &dst1); - result = dst1.count == expected_count; + wapp_array_copy_capped(i32, dst1, src); + result = wapp_array_count(dst1) == expected_count; u64 index = 0; b8 running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(&dst1, index)); + result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst1, index)); ++index; running = index < expected_count; } expected_count = 4; - wapp_i32_array_copy_capped(&src, &dst2); - result = result && dst2.count == expected_count; + wapp_array_copy_capped(i32, dst2, src); + result = result && wapp_array_count(dst2) == expected_count; index = 0; running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(&dst2, index)); + result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst2, index)); ++index; running = index < expected_count; @@ -176,9 +149,9 @@ TestFuncResult test_i32_array_alloc_capacity(void) { Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); u64 capacity = 32; - I32Array *array = wapp_i32_array_alloc_capacity(&allocator, capacity); + i32 *array = wapp_array_alloc_capacity(i32, &allocator, capacity); - result = array && array->capacity == capacity; + result = array && wapp_array_capacity(array) == capacity; wapp_mem_arena_allocator_destroy(&allocator); @@ -189,24 +162,24 @@ TestFuncResult test_i32_array_append_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - I32Array array1 = wapp_i32_array(1, 2, 3, 4, 5, 6, 7, 8); - I32Array array2 = wapp_i32_array(1, 2); + i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array2 = wapp_array(i32, 1, 2); i32 num = 10; - I32Array *arr_ptr = wapp_i32_array_append_alloc(&allocator, &array1, &num); - result = arr_ptr == &array1; + i32 *arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &num); + result = arr_ptr == array1; u64 count = 4; u64 index = 0; b8 running = true; while (running) { i32 num = (i32)index; - arr_ptr = wapp_i32_array_append_alloc(&allocator, &array2, &num); + arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num); ++index; running = index < count; } - result = result && arr_ptr != &array2; + result = result && arr_ptr != array2; wapp_mem_arena_allocator_destroy(&allocator); @@ -217,15 +190,15 @@ TestFuncResult test_i32_array_extend_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - I32Array array1 = wapp_i32_array(1, 2, 3, 4, 5, 6, 7, 8); - I32Array array2 = wapp_i32_array(1, 2); - I32Array array3 = wapp_i32_array(1, 2, 3, 4); + i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array2 = wapp_array(i32, 1, 2); + i32 *array3 = wapp_array(i32, 1, 2, 3, 4); - I32Array *arr_ptr = wapp_i32_array_extend_alloc(&allocator, &array1, &array3); - result = arr_ptr == &array1; + i32 *arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3); + result = arr_ptr == array1; - arr_ptr = wapp_i32_array_extend_alloc(&allocator, &array2, &array3); - result = result && arr_ptr != &array2; + arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3); + result = result && arr_ptr != array2; wapp_mem_arena_allocator_destroy(&allocator); @@ -236,32 +209,32 @@ TestFuncResult test_i32_array_copy_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - I32Array src = wapp_i32_array(1, 2, 3, 4, 5); - I32Array dst1 = wapp_i32_array(1, 2, 3, 4, 5, 6); - I32Array dst2 = wapp_i32_array(1, 2); - I32Array *array_ptr = nullptr; + i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); + i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); + i32 *dst2 = wapp_array(i32, 1, 2); + i32 *array_ptr = nullptr; u64 expected_count = 5; - array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst1); - result = array_ptr->count == expected_count && array_ptr == &dst1; + array_ptr = wapp_array_copy_alloc(i32, &allocator, dst1, src); + result = wapp_array_count(array_ptr) == expected_count && array_ptr == dst1; u64 index = 0; b8 running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(array_ptr, index)); + result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, array_ptr, index)); ++index; running = index < expected_count; } expected_count = 5; - array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst2); - result = result && array_ptr->count == expected_count && array_ptr != &dst2; + array_ptr = wapp_array_copy_alloc(i32, &allocator, dst2, src); + result = result && wapp_array_count(array_ptr) == expected_count && array_ptr != dst2; index = 0; running = true; while (running) { - result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(array_ptr, index)); + result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, array_ptr, index)); ++index; running = index < expected_count; @@ -271,3 +244,30 @@ TestFuncResult test_i32_array_copy_alloc(void) { return wapp_tester_result(result); } + +TestFuncResult test_i32_array_clear(void) { + b8 result; + + i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); + result = wapp_array_count(array) == 9; + + wapp_array_clear(i32, array); + + result = result && wapp_array_count(array) == 0; + + return wapp_tester_result(result); +} + +TestFuncResult test_i32_array_pop(void) { + b8 result; + + i32 *array1 = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); + i32 *array2 = wapp_array_with_capacity(i32, 32); + + i32 item1 = wapp_array_pop(i32, array1); + i32 item2 = wapp_array_pop(i32, array2); + + result = item1 == 8 && item2 == 0; + + return wapp_tester_result(result); +} diff --git a/tests/array/test_i32_array.h b/tests/array/test_i32_array.h index 6fdda5e..aa7b9f8 100644 --- a/tests/array/test_i32_array.h +++ b/tests/array/test_i32_array.h @@ -9,12 +9,12 @@ TestFuncResult test_i32_array_get(void); TestFuncResult test_i32_array_set(void); TestFuncResult test_i32_array_append_capped(void); TestFuncResult test_i32_array_extend_capped(void); -TestFuncResult test_i32_array_clear(void); -TestFuncResult test_i32_array_pop(void); TestFuncResult test_i32_array_copy_capped(void); TestFuncResult test_i32_array_alloc_capacity(void); TestFuncResult test_i32_array_append_alloc(void); TestFuncResult test_i32_array_extend_alloc(void); TestFuncResult test_i32_array_copy_alloc(void); +TestFuncResult test_i32_array_pop(void); +TestFuncResult test_i32_array_clear(void); #endif // !TEST_INT_ARRAY_H diff --git a/tests/array/test_str8_array.c b/tests/array/test_str8_array.c index 094ba74..484691d 100644 --- a/tests/array/test_str8_array.c +++ b/tests/array/test_str8_array.c @@ -6,15 +6,15 @@ TestFuncResult test_str8_array(void) { b8 result; Str8 expected[] = {wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")}; - Str8Array array = wapp_str8_array(wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")); - result = array.count == 3 && array.capacity == 8; + Str8 *array = wapp_array(Str8, wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")); + result = wapp_array_count(array) == 3 && wapp_array_capacity(array) == 8; Str8 *item; - u64 count = array.count; + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { - item = wapp_str8_array_get(&array, index); + item = wapp_array_get(Str8, array, index); result = result && item && (wapp_str8_equal(item, &expected[index])); ++index; diff --git a/tests/array/test_str8_array.cc b/tests/array/test_str8_array.cc index ac16b17..74fd544 100644 --- a/tests/array/test_str8_array.cc +++ b/tests/array/test_str8_array.cc @@ -10,16 +10,16 @@ TestFuncResult test_str8_array(void) { Str8 str1 = wapp_str8_lit("Hello"); Str8 str2 = wapp_str8_lit("Hi"); Str8 str3 = wapp_str8_lit("Bye"); - Str8Array array = wapp_str8_array(str1, str2, str3); + Str8 *array = wapp_array(Str8, str1, str2, str3); - result = array.count == 3 && array.capacity == 8; + result = wapp_array_count(array) == 3 && wapp_array_capacity(array) == 8; Str8 *item; - u64 count = array.count; + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { - item = wapp_str8_array_get(&array, index); + item = wapp_array_get(Str8, array, index); result = result && item && (wapp_str8_equal(item, &expected[index])); ++index; diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c index 5f7d5c4..9694842 100644 --- a/tests/str8/test_str8.c +++ b/tests/str8/test_str8.c @@ -615,11 +615,11 @@ TestFuncResult test_str8_join(void) { TestFuncResult test_str8_from_bytes(void) { b8 result; - Str8 str = wapp_str8_buf(1024); - U8Array bytes = wapp_u8_array('W', 'A', 'P', 'P'); - wapp_str8_from_bytes(&str, &bytes); + Str8 str = wapp_str8_buf(1024); + u8 *bytes = wapp_array(u8, 'W', 'A', 'P', 'P'); + wapp_str8_from_bytes(&str, bytes); - result = str.size == bytes.count * bytes.item_size; + result = str.size == wapp_array_count(bytes) * wapp_array_item_size(bytes); result = result && wapp_str8_equal(&str, &wapp_str8_lit_ro("WAPP")); return wapp_tester_result(result); diff --git a/tests/str8/test_str8.cc b/tests/str8/test_str8.cc index ba1309c..ffe27ea 100644 --- a/tests/str8/test_str8.cc +++ b/tests/str8/test_str8.cc @@ -617,10 +617,10 @@ TestFuncResult test_str8_from_bytes(void) { Str8 str = wapp_str8_buf(1024); Str8 expected = wapp_str8_lit_ro("WAPP"); - U8Array bytes = wapp_u8_array('W', 'A', 'P', 'P'); - wapp_str8_from_bytes(&str, &bytes); + u8 *bytes = wapp_array(u8, 'W', 'A', 'P', 'P'); + wapp_str8_from_bytes(&str, bytes); - result = str.size == bytes.count * bytes.item_size; + result = str.size == wapp_array_count(bytes) * wapp_array_item_size(bytes); result = result && wapp_str8_equal(&str, &expected); return wapp_tester_result(result); diff --git a/tests/wapptest.c b/tests/wapptest.c index 4ce3290..63d71a3 100644 --- a/tests/wapptest.c +++ b/tests/wapptest.c @@ -27,13 +27,13 @@ int main(void) { test_i32_array_set, test_i32_array_append_capped, test_i32_array_extend_capped, - test_i32_array_clear, - test_i32_array_pop, test_i32_array_copy_capped, test_i32_array_alloc_capacity, test_i32_array_append_alloc, test_i32_array_extend_alloc, test_i32_array_copy_alloc, + test_i32_array_pop, + test_i32_array_clear, test_str8_lit, test_str8_lit_ro, test_str8_buf,