segfault fixed

This commit is contained in:
2023-02-13 19:26:17 +06:00
parent 95fec8d166
commit 590790817b
21 changed files with 302 additions and 186 deletions

View File

@@ -8,9 +8,9 @@ extern "C" {
#define Autoarr_define(type) \
\
kt_define(Autoarr_##type, ____Autoarr_free_##type, NULL); \
kt_define(Autoarr_##type, ____Autoarr_##type##_free_g, NULL); \
\
void __Autoarr_add_##type(Autoarr_##type* ar, type element){ \
void __Autoarr_##type##_add(Autoarr_##type* ar, type element){ \
if(!ar->values){ \
ar->values=malloc(ar->max_blocks_count*sizeof(type*)); \
goto create_block; \
@@ -26,49 +26,49 @@ create_block: \
ar->block_length++; \
} \
\
type __Autoarr_get_##type(Autoarr_##type* ar, u32 index){ \
type __Autoarr_##type##_get(Autoarr_##type* ar, u32 index){ \
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
return ar->values[index/ar->max_block_length][index%ar->max_block_length]; \
} \
\
type* __Autoarr_getptr_##type(Autoarr_##type* ar, u32 index){ \
type* __Autoarr_##type##_getPtr(Autoarr_##type* ar, u32 index){ \
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
return ar->values[index/ar->max_block_length]+(index%ar->max_block_length); \
} \
\
void __Autoarr_set_##type(Autoarr_##type* ar, u32 index, type element){ \
void __Autoarr_##type##_set(Autoarr_##type* ar, u32 index, type element){ \
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
ar->values[index/ar->max_block_length][index%ar->max_block_length]=element; \
} \
\
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){ \
void __Autoarr_##type##_free_g(Autoarr_##type* ar, bool freePtr){ \
for(u16 i=0; i<ar->blocks_count;i++) \
free(ar->values[i]); \
free(ar->values); \
if(freePtr) free(ar); \
} \
void ____Autoarr_free_##type(void* ar){ \
__Autoarr_free_##type((Autoarr_##type*)ar, false); \
void ____Autoarr_##type##_free_g(void* ar){ \
__Autoarr_##type##_free_g((Autoarr_##type*)ar, false); \
} \
\
type* __Autoarr_toArray_##type(Autoarr_##type* ar){ \
type* __Autoarr_##type##_toArray(Autoarr_##type* ar){ \
u32 length=Autoarr_length(ar); \
type* array=malloc(length * sizeof(type)); \
for(u32 i=0; i<length; i++) \
array[i]=__Autoarr_get_##type(ar, i); \
array[i]=__Autoarr_##type##_get(ar, i); \
return array; \
} \
\
__functions_list_t_##type __functions_list_##type={ \
&__Autoarr_add_##type, \
&__Autoarr_get_##type, \
&__Autoarr_getptr_##type, \
&__Autoarr_set_##type, \
&__Autoarr_free_##type, \
&__Autoarr_toArray_##type \
__Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list={ \
&__Autoarr_##type##_add, \
&__Autoarr_##type##_get, \
&__Autoarr_##type##_getPtr, \
&__Autoarr_##type##_set, \
&__Autoarr_##type##_free_g, \
&__Autoarr_##type##_toArray \
}; \
\
Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length){ \
Autoarr_##type* __Autoarr_##type##_create(u16 max_blocks_count, u16 max_block_length){ \
Autoarr_##type* ar=malloc(sizeof(Autoarr_##type)); \
*ar=(Autoarr_##type){ \
.max_blocks_count=max_blocks_count, \
@@ -76,7 +76,7 @@ Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_leng
.max_block_length=max_block_length, \
.block_length=0, \
.values=NULL, \
.functions=&__functions_list_##type \
.functions=&__Autoarr_##type##_functions_list \
}; \
return ar; \
}