Refactor array to avoid having to include external types
This commit is contained in:
@@ -51,10 +51,6 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
|
||||
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 / "primitives" / "strings" / "str8" / "str8.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,
|
||||
@@ -66,6 +62,9 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
|
||||
datatypes: dict[CDataType, ArrayData] = {
|
||||
"Str8": ArrayData(
|
||||
array_typename="Str8Array",
|
||||
hdr_decl_types=[
|
||||
CStruct(name="str8", cargs=[], typedef_name="Str8"),
|
||||
],
|
||||
),
|
||||
}
|
||||
|
||||
@@ -111,6 +110,8 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
|
||||
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")
|
||||
@@ -123,8 +124,29 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
|
||||
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, str) and _type == "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)
|
||||
|
||||
stack_array_macro = CMacro(
|
||||
name=f"wapp_{type_string_lower}_array(...)",
|
||||
@@ -148,6 +170,28 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
|
||||
),
|
||||
)
|
||||
|
||||
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_macro = 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,
|
||||
),
|
||||
)
|
||||
|
||||
get_func = CFunc(
|
||||
name=f"wapp_{type_string_lower}_array_get",
|
||||
ret_type=type_string,
|
||||
@@ -171,7 +215,7 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
|
||||
args=[
|
||||
CArg(name="array", _type=array, pointer=CPointer(CPointerType.SINGLE)),
|
||||
CArg(name="index", _type=CType.U64),
|
||||
CArg(name="item", _type=type_string),
|
||||
CArg(name="item", _type=type_string, pointer=CPointer(CPointerType.SINGLE)),
|
||||
],
|
||||
body=__format_func_body(
|
||||
filename=snippets_dir / "array_set",
|
||||
@@ -187,7 +231,7 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
|
||||
ret_type=CType.VOID,
|
||||
args=[
|
||||
CArg(name="array", _type=array, pointer=CPointer(CPointerType.SINGLE)),
|
||||
CArg(name="item", _type=type_string),
|
||||
CArg(name="item", _type=type_string, pointer=CPointer(CPointerType.SINGLE)),
|
||||
],
|
||||
body=__format_func_body(
|
||||
filename=snippets_dir / "append_capped",
|
||||
@@ -229,21 +273,6 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
|
||||
),
|
||||
)
|
||||
|
||||
pop_func = CFunc(
|
||||
name=f"wapp_{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,
|
||||
),
|
||||
)
|
||||
|
||||
copy_capped_func = CFunc(
|
||||
name=f"wapp_{type_string_lower}_array_copy_capped",
|
||||
ret_type=CType.VOID,
|
||||
@@ -260,30 +289,13 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
|
||||
),
|
||||
)
|
||||
|
||||
alloc_capacity_func = CFunc(
|
||||
name=f"wapp_{type_string_lower}_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),
|
||||
],
|
||||
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),
|
||||
)
|
||||
|
||||
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),
|
||||
CArg(name="item", _type=type_string, pointer=CPointer(CPointerType.SINGLE)),
|
||||
],
|
||||
body=__format_func_body(
|
||||
filename=snippets_dir / "append_alloc",
|
||||
@@ -331,8 +343,29 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
|
||||
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.decl_types.extend(array_data.hdr_decl_types)
|
||||
header.macros.extend([stack_array_macro, stack_capacity_array_macro])
|
||||
header.macros.extend([
|
||||
stack_array_macro,
|
||||
stack_capacity_array_macro,
|
||||
alloc_capacity_array_macro,
|
||||
array_pop_macro,
|
||||
])
|
||||
header.types.extend([array])
|
||||
header.funcs.extend([
|
||||
get_func,
|
||||
@@ -340,16 +373,17 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
|
||||
append_capped_func,
|
||||
extend_capped_func,
|
||||
clear_func,
|
||||
pop_func,
|
||||
copy_capped_func,
|
||||
alloc_capacity_func,
|
||||
append_alloc_func,
|
||||
extend_alloc_func,
|
||||
copy_alloc_func,
|
||||
pop_func,
|
||||
])
|
||||
|
||||
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)
|
||||
|
@@ -1,18 +1,19 @@
|
||||
u64 allocation_size = sizeof({ArrayType}) + sizeof({T}) * capacity;
|
||||
{ArrayType} *array = NULL;
|
||||
u64 allocation_size = sizeof({ArrayType}) + item_size * capacity;
|
||||
{ArrayType} *array = NULL;
|
||||
|
||||
if (!allocator) {{
|
||||
goto RETURN_{Tupper}_ARRAY_ALLOC;
|
||||
goto RETURN_GENERIC_ARRAY_ALLOC;
|
||||
}}
|
||||
|
||||
array = wapp_mem_allocator_alloc(allocator, allocation_size);
|
||||
if (!array) {{
|
||||
goto RETURN_{Tupper}_ARRAY_ALLOC;
|
||||
goto RETURN_GENERIC_ARRAY_ALLOC;
|
||||
}}
|
||||
|
||||
array->items = ({T} *)((u8 *)array + sizeof({ArrayType}));
|
||||
array->count = 0;
|
||||
array->capacity = capacity;
|
||||
array->items = ({T} *)((u8 *)array + sizeof({ArrayType}));
|
||||
array->count = 0;
|
||||
array->capacity = capacity;
|
||||
array->item_size = item_size;
|
||||
|
||||
RETURN_{Tupper}_ARRAY_ALLOC:
|
||||
RETURN_GENERIC_ARRAY_ALLOC:
|
||||
return array;
|
||||
|
1
codegen/array/snippets/alloc_capacity_macro
Normal file
1
codegen/array/snippets/alloc_capacity_macro
Normal file
@@ -0,0 +1 @@
|
||||
(({ArrayType} *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof({T})))
|
@@ -6,7 +6,7 @@
|
||||
|
||||
if (array->count >= array->capacity) {{
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2);
|
||||
output = wapp_{Tlower}_array_alloc_capacity(allocator, new_capacity);
|
||||
output = ({ArrayType} *)_array_alloc_capacity(allocator, new_capacity, array->item_size);
|
||||
if (!output) {{
|
||||
output = array;
|
||||
goto RETURN_{Tupper}_ARRAY_APPEND_ALLOC;
|
||||
|
@@ -2,4 +2,5 @@
|
||||
return;
|
||||
}}
|
||||
|
||||
array->items[(array->count)++] = item;
|
||||
u64 index = (array->count)++;
|
||||
wapp_{Tlower}_array_set(array, index, item);
|
||||
|
@@ -2,4 +2,5 @@
|
||||
return NULL;
|
||||
}}
|
||||
|
||||
return &(array->items[index]);
|
||||
u8 *ptr = (u8 *)(array->items) + (array->item_size * index);
|
||||
return ({T} *)ptr;
|
||||
|
@@ -1,5 +1,4 @@
|
||||
if (!array || array->count == 0) {{
|
||||
return ({T}){{0}};
|
||||
}}
|
||||
|
||||
return array->items[--(array->count)];
|
||||
u64 index = array->count - 1;
|
||||
{T} *out = wapp_{Tlower}_array_get(array, index);
|
||||
--(array->count);
|
||||
return out;
|
||||
|
4
codegen/array/snippets/array_pop_macro
Normal file
4
codegen/array/snippets/array_pop_macro
Normal file
@@ -0,0 +1,4 @@
|
||||
(ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
|
||||
*_{Tlower}_array_pop(ARRAY_PTR) : \
|
||||
({T}){{0}} \
|
||||
)
|
@@ -1,5 +1,6 @@
|
||||
if (!array || index >= array->count) {{
|
||||
{T} *ptr = wapp_{Tlower}_array_get(array, index);
|
||||
if (!ptr) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
array->items[index] = item;
|
||||
memcpy((void *)ptr, (void *)item, array->item_size);
|
||||
|
@@ -6,7 +6,7 @@
|
||||
|
||||
if (src->count >= dst->capacity) {{
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2);
|
||||
output = wapp_{Tlower}_array_alloc_capacity(allocator, new_capacity);
|
||||
output = ({ArrayType} *)_array_alloc_capacity(allocator, new_capacity, src->item_size);
|
||||
if (!output) {{
|
||||
output = dst;
|
||||
goto RETURN_{Tupper}_ARRAY_COPY_ALLOC;
|
||||
|
@@ -20,5 +20,5 @@
|
||||
continue;
|
||||
}}
|
||||
|
||||
wapp_{Tlower}_array_append_capped(dst, *item);
|
||||
wapp_{Tlower}_array_append_capped(dst, item);
|
||||
}}
|
||||
|
@@ -7,7 +7,7 @@
|
||||
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 = wapp_{Tlower}_array_alloc_capacity(allocator, new_capacity);
|
||||
output = ({ArrayType} *)_array_alloc_capacity(allocator, new_capacity, array->item_size);
|
||||
if (!output) {{
|
||||
output = array;
|
||||
goto RETURN_{Tupper}_ARRAY_EXTEND_ALLOC;
|
||||
|
@@ -23,5 +23,5 @@
|
||||
continue;
|
||||
}}
|
||||
|
||||
wapp_{Tlower}_array_append_capped(array, *item);
|
||||
}}
|
||||
wapp_{Tlower}_array_append_capped(array, item);
|
||||
}}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
(({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) \
|
||||
.capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count({T}, __VA_ARGS__) * 2), \
|
||||
.item_size = sizeof({T}) \
|
||||
}})
|
||||
|
@@ -1 +1 @@
|
||||
(({ArrayType}){{.items = ({T}[CAPACITY]){{0}}, .count = 0, .capacity = CAPACITY}})
|
||||
(({ArrayType}){{.items = ({T}[CAPACITY]){{0}}, .count = 0, .capacity = CAPACITY, .item_size = sizeof({T})}})
|
||||
|
Reference in New Issue
Block a user