diff --git a/TODO.md b/TODO.md index a37154d..df46828 100644 --- a/TODO.md +++ b/TODO.md @@ -6,9 +6,11 @@ - check allocator_free call order - deal with StackingAllocator_free not freing memory sometimes - replace LinearAllocator with StackingAllocator when possible (in DtsodV24_deserialize) +- use LinkedList instead of complicated LinearAllocator logic +- configurable LinearAllocator chunk size (static/growing) ## Autoarr -- store lenght and max_lenght inside the struct instead of calculating them by macro +- store length and max_length inside the struct instead of calculating them by macro - keep Autoarr_length() and Autoarr_maxLength() to old code compatibility - toString() diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index f2159c8..2cbb0a0 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -1,17 +1,17 @@ #include "Autoarr.h" -Autoarr_define(Pointer, true) +// Autoarr_define(Pointer, true) Autoarr_define(char, false) -Autoarr_define(bool, false) -Autoarr_define(f32, false) -Autoarr_define(f64, false) -Autoarr_define(u8, false) -Autoarr_define(i8, false) -Autoarr_define(u16, false) -Autoarr_define(i16, false) -Autoarr_define(u32, false) -Autoarr_define(i32, false) -Autoarr_define(u64, false) -Autoarr_define(i64, false) +// Autoarr_define(bool, false) +// Autoarr_define(f32, false) +// Autoarr_define(f64, false) +// Autoarr_define(u8, false) +// Autoarr_define(i8, false) +// Autoarr_define(u16, false) +// Autoarr_define(i16, false) +// Autoarr_define(u32, false) +// Autoarr_define(i32, false) +// Autoarr_define(u64, false) +// Autoarr_define(i64, false) -Autoarr_define(Unitype, false) +// Autoarr_define(Unitype, false) diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index 09249d3..7b97477 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -23,21 +23,6 @@ Autoarr_declare(u64) Autoarr_declare(Unitype) -#define Autoarr_foreach(ar, elem, codeblock...) { \ - if(ar->chunks_count>0) { \ - typeof(**ar->chunks) elem; \ - for(u16 chunkI=0;chunkIchunks_count-1;chunkI++) \ - for(u32 elemI=0;elemImax_chunk_length;elemI++){ \ - elem=ar->chunks[chunkI][elemI]; \ - { codeblock; } \ - } \ - for(u16 elemI=0;elemIchunk_length;elemI++){ \ - elem=ar->chunks[ar->chunks_count-1][elemI]; \ - { codeblock; } \ - } \ - } \ -} - #if __cplusplus } #endif diff --git a/src/Autoarr/Autoarr_KVPair_exported.c b/src/Autoarr/Autoarr_KVPair_exported.c deleted file mode 100644 index e9ccc2e..0000000 --- a/src/Autoarr/Autoarr_KVPair_exported.c +++ /dev/null @@ -1,38 +0,0 @@ -#if __cplusplus -extern "C" { -#endif - -#include "Autoarr.h" -#include "../Hashtable/KeyValuePair.h" - -EXPORT void CALL kerep_Autoarr_KVPair_create(u16 max_chunks_count, u16 max_chunk_length, Autoarr_KVPair** output){ - *output=Autoarr_create(KVPair, max_chunks_count, max_chunk_length); -} - -EXPORT void CALL kerep_Autoarr_KVPair_destruct(Autoarr_KVPair* ar){ - Autoarr_destruct(ar, true); -} - -EXPORT void CALL kerep_Autoarr_KVPair_get(Autoarr_KVPair* ar, u32 index, KVPair* output){ - *output=Autoarr_get(ar, index); -} - -EXPORT void CALL kerep_Autoarr_KVPair_add(Autoarr_KVPair* ar, KVPair element){ - Autoarr_add(ar, element); -} - -EXPORT void CALL kerep_Autoarr_KVPair_set(Autoarr_KVPair* ar, u32 index, KVPair element){ - Autoarr_set(ar, index, element); -} - -EXPORT void CALL kerep_Autoarr_KVPair_length(Autoarr_KVPair* ar, u32* output){ - *output=Autoarr_length(ar); -} - -EXPORT void CALL kerep_Autoarr_KVPair_max_length(Autoarr_KVPair* ar, u32* output){ - *output=Autoarr_max_length(ar); -} - -#if __cplusplus -} -#endif diff --git a/src/Autoarr/Autoarr_Unitype_exported.c b/src/Autoarr/Autoarr_Unitype_exported.c_old similarity index 92% rename from src/Autoarr/Autoarr_Unitype_exported.c rename to src/Autoarr/Autoarr_Unitype_exported.c_old index 5ea7c12..4ef89ee 100644 --- a/src/Autoarr/Autoarr_Unitype_exported.c +++ b/src/Autoarr/Autoarr_Unitype_exported.c_old @@ -5,7 +5,7 @@ extern "C" { #include "Autoarr.h" EXPORT void CALL kerep_Autoarr_Unitype_create(u16 max_chunks_count, u16 max_chunk_length, Autoarr_Unitype** output){ - *output=Autoarr_create(Unitype, max_chunks_count, max_chunk_length); + *output=Autoarr_construct(Unitype, max_chunks_count, max_chunk_length); } EXPORT void CALL kerep_Autoarr_Unitype_destruct(Autoarr_Unitype* ar){ diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h index 21725df..352d106 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -6,67 +6,60 @@ extern "C" { #include "../base/base.h" -#define Autoarr_declare(type) \ +#define Autoarr_declare(TYPE) \ \ -struct Autoarr_##type; \ +typedef struct Autoarr_##TYPE Autoarr_##TYPE; \ \ -typedef struct __Autoarr_##type##_functions_list_t { \ - void (*add)(struct Autoarr_##type* ar, type element); \ - type (*get)(struct Autoarr_##type* ar, u32 index); \ - type* (*getPtr)(struct Autoarr_##type* ar, u32 index); \ - void (*set)(struct Autoarr_##type* ar, u32 index, type element); \ - void (*freeWithMembers)(struct Autoarr_##type* ar, bool freePtr); \ - void (*freeWithoutMembers)(struct Autoarr_##type* ar, bool freePtr); \ - type* (*toArray)(struct Autoarr_##type* ar); \ -} __Autoarr_##type##_functions_list_t; \ +typedef struct __Autoarr_##TYPE##_functions_list_t { \ + void (*add)(Autoarr_##TYPE* ar, TYPE element); \ + TYPE (*get)(Autoarr_##TYPE* ar, u32 index); \ + TYPE* (*getPtr)(Autoarr_##TYPE* ar, u32 index); \ + void (*set)(Autoarr_##TYPE* ar, u32 index, TYPE element); \ + TYPE* (*toArray)(Autoarr_##TYPE* ar, allocator_ptr array_holder); \ +} __Autoarr_##TYPE##_functions_list_t; \ \ -extern __Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list; \ -\ -STRUCT(Autoarr_##type, \ - u16 chunks_count; \ - u16 max_chunks_count; \ - u16 chunk_length; \ - u16 max_chunk_length; \ - type** chunks; \ - __Autoarr_##type##_functions_list_t* functions; \ +STRUCT(Autoarr_##TYPE, \ + InternalAllocator_declare(LinearAllocator); \ + __Autoarr_##TYPE##_functions_list_t* functions; \ + ktDescriptor* type; \ + u32 length; \ + TYPE* _typeof_target; \ ) \ \ -Autoarr_##type* __Autoarr_##type##_create(u16 max_chunks_count, u16 max_chunk_length); \ -void __Autoarr_##type##_destructWithMembers(Autoarr_##type* ar, bool freePtr); \ -void ____Autoarr_##type##_destructWithMembers(void* ar); +void __Autoarr_##TYPE##_construct(Autoarr_##TYPE* ar, alloc_size_t starting_size, allocator_ptr external_al); \ +void __Autoarr_##TYPE##_destruct(Autoarr_##TYPE* ar); \ -#define Autoarr(type) Autoarr_##type +#define Autoarr(TYPE) Autoarr_##TYPE -#define Autoarr_create(type, max_chunks_count, max_chunk_length) \ - __Autoarr_##type##_create(max_chunks_count, max_chunk_length) -#define Autoarr_add(autoarr, element) \ - autoarr->functions->add(autoarr, element) -#define Autoarr_get(autoarr, index) \ - autoarr->functions->get(autoarr,index) -#define Autoarr_getPtr(autoarr, index) \ - autoarr->functions->getPtr(autoarr,index) -#define Autoarr_set(autoarr, index, element) \ - autoarr->functions->set(autoarr, index, element) -#define Autoarr_destruct(autoarr, freePtr) \ - autoarr->functions->freeWithMembers(autoarr, freePtr) -#define Autoarr_destructWithoutMembers(autoarr, freePtr) \ - autoarr->functions->freeWithoutMembers(autoarr, freePtr) -#define Autoarr_toArray(autoarr) \ - autoarr->functions->toArray(autoarr) +#define Autoarr_construct(ptr, TYPE, starting_size, data_allocator) \ + __Autoarr_##TYPE##_construct(ptr, starting_size, data_allocator) +#define Autoarr_destruct(autoarr) (autoarr)->type->destruct(autoarr) +#define Autoarr_add(autoarr, element) (autoarr)->functions->add(autoarr, element) +#define Autoarr_get(autoarr, index) (autoarr)->functions->get(autoarr,index) +#define Autoarr_getPtr(autoarr, index) (autoarr)->functions->getPtr(autoarr,index) +#define Autoarr_set(autoarr, index, element) (autoarr)->functions->set(autoarr, index, element) +#define Autoarr_toArray(autoarr, array_alloctr) (autoarr)->functions->toArray(autoarr, array_alloctr) +#define Autoarr_length(autoarr) (autoarr)->length +#define Autoarr_pop(autoarr) { \ + u32 new_len=(autoarr)->length-1; \ + allocator_free(InternalAllocator_getPtr((autoarr)), Autoarr_getPtr((autoarr), new_len)); \ + (autoarr)->length=new_len; \ +} -#define Autoarr_length(autoarr) \ - (u32)(!autoarr->chunks_count ? 0 : \ - autoarr->max_chunk_length*(autoarr->chunks_count-1)+autoarr->chunk_length) -#define Autoarr_max_length(autoarr) \ - (u32)(autoarr->max_chunk_length*autoarr->max_chunks_count) - -#define Autoarr_pop(AR){ \ - if(AR->chunk_length==1){ \ - AR->chunks_count--; \ - AR->chunk_length=AR->max_chunk_length; \ - free(AR->chunks[AR->chunks_count]); \ +#define Autoarr_foreach(ar, elem, codeblock...) { \ + if((ar)->length > 0) { \ + typeof(*((ar)->_typeof_target)) elem; \ + LinearAllocator* al=(LinearAllocator*)InternalAllocator_getPtr(ar); \ + for(u16 chunk_i=0; chunk_i <= al->curr_chunk_i; chunk_i++) { \ + MemoryChunk chunk = al->chunks[chunk_i]; \ + alloc_size_t chunk_elem_count = chunk.occupied_size/sizeof(elem); \ + typeof((ar)->_typeof_target) chunk_data = (void*)chunk.data; \ + for(u32 elem##_i=0; elem##_i < chunk_elem_count; elem##_i++) { \ + elem = chunk_data[elem##_i]; \ + { codeblock; } \ + } \ + } \ } \ - else AR->chunk_length--; \ } #if __cplusplus diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h index 91cbf3d..cf5a1b3 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -6,94 +6,79 @@ extern "C" { #include "../base/base.h" -#define Autoarr_define(type, TYPE_IS_PTR) \ +#define Autoarr_define(TYPE, TYPE_IS_PTR) \ \ -kt_define(Autoarr_##type, ____Autoarr_##type##_destructWithMembers, NULL); \ +void __Autoarr_##TYPE##_add(Autoarr_##TYPE* ar, TYPE element){ \ + TYPE* ptr = allocator_alloc(InternalAllocator_getPtr(ar), sizeof(element)); \ + *ptr=element; \ + ar->length++; \ +} \ \ -void __Autoarr_##type##_add(Autoarr_##type* ar, type element){ \ - if(!ar->chunks){ \ - ar->chunks=malloc(ar->max_chunks_count*sizeof(type*)); \ - goto create_chunk; \ +TYPE* __Autoarr_##TYPE##_getPtr(Autoarr_##TYPE* ar, u32 index){ \ + if(index >= Autoarr_length(ar)) \ + throw(ERR_WRONGINDEX); \ + u32 elem_count_sum=0; \ + LinearAllocator* al=(LinearAllocator*)InternalAllocator_getPtr(ar); \ + for(u16 chunk_i=0; chunk_i <= al->curr_chunk_i; chunk_i++) { \ + MemoryChunk chunk = al->chunks[chunk_i]; \ + alloc_size_t chunk_elem_count = chunk.occupied_size/sizeof(TYPE); \ + alloc_size_t chunk_elem_i = index - elem_count_sum; \ + if(chunk_elem_i < chunk_elem_count){ \ + return chunk.data + chunk_elem_i*sizeof(TYPE); \ + } \ + elem_count_sum += chunk_elem_count; \ } \ - if(ar->chunk_length==ar->max_chunk_length){ \ - if (ar->chunks_count>=ar->max_chunks_count) throw(ERR_MAXLENGTH); \ - ar->chunk_length=0; \ -create_chunk: \ - ar->chunks[ar->chunks_count]=malloc(ar->max_chunk_length*sizeof(type)); \ - ar->chunks_count++; \ - } \ - ar->chunks[ar->chunks_count-1][ar->chunk_length]=element; \ - ar->chunk_length++; \ + return NULL; \ } \ \ -type __Autoarr_##type##_get(Autoarr_##type* ar, u32 index){ \ - if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ - return ar->chunks[index/ar->max_chunk_length][index%ar->max_chunk_length]; \ +TYPE __Autoarr_##TYPE##_get(Autoarr_##TYPE* ar, u32 index){ \ + TYPE* ptr=__Autoarr_##TYPE##_getPtr(ar, index); \ + return *ptr; \ } \ \ -type* __Autoarr_##type##_getPtr(Autoarr_##type* ar, u32 index){ \ - if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ - return ar->chunks[index/ar->max_chunk_length]+(index%ar->max_chunk_length); \ +void __Autoarr_##TYPE##_set(Autoarr_##TYPE* ar, u32 index, TYPE value){ \ + TYPE* ptr=__Autoarr_##TYPE##_getPtr(ar, index); \ + *ptr=value; \ } \ \ -void __Autoarr_##type##_set(Autoarr_##type* ar, u32 index, type element){ \ - if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ - ar->chunks[index/ar->max_chunk_length][index%ar->max_chunk_length]=element; \ -} \ -\ -void __Autoarr_##type##_destructWithoutMembers(Autoarr_##type* ar, bool freePtr){ \ - for(u16 i=0; ichunks_count;i++) \ - free(ar->chunks[i]); \ - free(ar->chunks); \ - if(freePtr) free(ar); \ -} \ -\ -void __Autoarr_##type##_destructWithMembers(Autoarr_##type* ar, bool freePtr){ \ - if(ktDescriptor_##type.freeMembers!=NULL) { \ - Autoarr_foreach(ar, el, \ - void* members_ptr=⪙ \ - if(TYPE_IS_PTR) members_ptr=*(type**)members_ptr; \ - ktDescriptor_##type.freeMembers(members_ptr); \ +void __Autoarr_##TYPE##_destruct(Autoarr_##TYPE* ar){ \ + destruct_t value_destructor=ar->type->destruct; \ + if(value_destructor!=NULL) { \ + Autoarr_foreach(ar, el, \ + TYPE* value_ptr = TYPE_IS_PTR ? *(TYPE**)(&el) : ⪙ \ + value_destructor(value_ptr); \ ); \ } \ - __Autoarr_##type##_destructWithoutMembers(ar, freePtr); \ -} \ -void ____Autoarr_##type##_destructWithMembers(void* ar){ \ - __Autoarr_##type##_destructWithMembers((Autoarr_##type*)ar, false); \ + InternalAllocator_destructIfInternal(LinearAllocator, ar); \ } \ \ -type* __Autoarr_##type##_toArray(Autoarr_##type* ar){ \ +TYPE* __Autoarr_##TYPE##_toArray(Autoarr_##TYPE* ar, allocator_ptr array_alloctr){ \ u32 length=Autoarr_length(ar); \ if(length==0) \ return NULL; \ - type* array=malloc(length * sizeof(type)); \ - for(u32 i=0; ifunctions=&__Autoarr_##TYPE##_functions_list; \ + ar->type = TYPE_IS_PTR ? &ktDescriptor_namePtr(TYPE) : &ktDescriptor_name(TYPE); \ + ar->length=0; \ +} \ +\ +kt_define(Autoarr_##TYPE, (destruct_t)__Autoarr_##TYPE##_destruct, NULL); #if __cplusplus } diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index da3d911..87a9faa 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -1,8 +1,7 @@ #include "DtsodV24.h" #include "../String/StringBuilder.h" -#define ARR_BC 64 -#define ARR_BL 1024 +#define ARR_SZ_START 64 typedef struct DeserializeSharedData{ @@ -149,19 +148,20 @@ Maybe __ReadString(DeserializeSharedData* shared){ #define ReadString() __ReadString(shared) Maybe __ReadList(DeserializeSharedData* shared){ - Autoarr(Unitype)* list=Autoarr_create(Unitype,ARR_BC,ARR_BL); + Autoarr(Unitype) list; + Autoarr_construct(&list, Unitype, ARR_SZ_START, NULL); bool readingList=true; while (true){ - try(ReadValue((&readingList)), m_val, Autoarr_destruct(list, true)) - Autoarr_add(list,m_val.value); + try(ReadValue((&readingList)), m_val, Autoarr_destruct(&list)) + Autoarr_add(&list, m_val.value); if (!readingList){ if(Unitype_isUniNull(m_val.value)) - Autoarr_pop(list); + Autoarr_pop(&list); break; } } - return SUCCESS(UniHeapPtr(Autoarr_Unitype,list)); + return SUCCESS(UniHeapPtr(Autoarr_Unitype, &list)); }; #define ReadList() __ReadList(shared) @@ -320,7 +320,8 @@ Maybe __deserialize(char** _text, bool _calledRecursively, allocator_ptr _tmp_al // allocator_free(tmp_al, nameCPtr); } else{ - list=Autoarr_create(Unitype,ARR_BC,ARR_BL); + list=allocator_alloc(tmp_al, sizeof(*list)); + Autoarr_construct(list, Unitype, ARR_SZ_START, NULL); Hashtable_add(dict,nameCPtr,UniHeapPtr(Autoarr_Unitype,list)); } Autoarr_add(list,val.value); diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index 5b7b513..1e9fe5e 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -15,7 +15,7 @@ Hashtable* Hashtable_create(){ ht->hein=HT_HEIN_MIN; ht->rows=malloc(HT_HEIGHTS[HT_HEIN_MIN]*sizeof(Autoarr(KVPair)*)); for(u16 i=0;irows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL); + ht->rows[i]=Autoarr_construct(KVPair,ARR_BC,ARR_BL); return ht; } @@ -38,7 +38,7 @@ void Hashtable_expand(Hashtable* ht){ Autoarr(KVPair)** newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KVPair)*)); for(u16 i=0;ihein];i++) - newrows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL); + newrows[i]=Autoarr_construct(KVPair,ARR_BC,ARR_BL); for(u16 i=0;ihein-1];i++){ Autoarr(KVPair)* ar=ht->rows[i]; diff --git a/src/LinkedList/LinkedList.h b/src/LinkedList/LinkedList.h index 0ffd2b7..d592f18 100644 --- a/src/LinkedList/LinkedList.h +++ b/src/LinkedList/LinkedList.h @@ -16,7 +16,7 @@ extern "C" { #define LLNode_create(TYPE, VALUE) LLNode_##TYPE##_create(VALUE) #define LinkedList_create(TYPE) LinkedList_##TYPE##_create() -#define LinkedList_destruct(LLIST) ({ LLIST->_functions->freeMembers(LLIST); free(LLIST); }) +#define LinkedList_destruct(LLIST) ({ LLIST->_functions->destruct(LLIST); free(LLIST); }) void LinkedList_addToBeginning(void* _llist, void* _new_node); diff --git a/src/LinkedList/LinkedList_declare.h b/src/LinkedList/LinkedList_declare.h index 26d2da7..53068c9 100644 --- a/src/LinkedList/LinkedList_declare.h +++ b/src/LinkedList/LinkedList_declare.h @@ -27,7 +27,7 @@ STRUCT(LinkedList(TYPE), \ ) \ \ typedef struct LinkedList_##TYPE##_functions_t { \ - destruct_t freeMembers; \ + destruct_t destruct; \ void (*removePrev)(LinkedList(TYPE)* llist, LLNode(TYPE)* nextNode, bool freeRemoved); \ void (*removeNext)(LinkedList(TYPE)* llist, LLNode(TYPE)* prevNode, bool freeRemoved); \ } LinkedList_##TYPE##_functions_t; \ diff --git a/src/LinkedList/LinkedList_define.h b/src/LinkedList/LinkedList_define.h index 503574e..e656a73 100644 --- a/src/LinkedList/LinkedList_define.h +++ b/src/LinkedList/LinkedList_define.h @@ -18,7 +18,7 @@ void LLNode_##TYPE##_destructMembers(void* _node){ \ LLNode(TYPE)* node=(LLNode(TYPE)*)_node; \ void* value_ptr=&node->value; \ if(TYPE_IS_PTR) value_ptr=*(TYPE**)value_ptr; \ - ktDescriptor_##TYPE.freeMembers(value_ptr); \ + ktDescriptor_##TYPE.destruct(value_ptr); \ } \ \ void LLNode_##TYPE##_destruct(LLNode(TYPE)* node, bool free_value){ \ @@ -69,7 +69,7 @@ void LinkedList_##TYPE##_removeNext(LinkedList(TYPE)* llist, LLNode(TYPE)* prevN } \ \ LinkedList_##TYPE##_functions_t _LinkedList_##TYPE##_functions={ \ - .freeMembers=LinkedList_##TYPE##_destructMembers, \ + .destruct=LinkedList_##TYPE##_destructMembers, \ .removePrev=LinkedList_##TYPE##_removePrev, \ .removeNext=LinkedList_##TYPE##_removeNext \ }; \ diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c index f56c761..d633376 100644 --- a/src/String/StringBuilder.c +++ b/src/String/StringBuilder.c @@ -8,7 +8,7 @@ kt_define(StringBuilder, (destruct_t)StringBuilder_destruct, NULL); void complete_buf(StringBuilder* b){ if(!b->compl_bufs) - b->compl_bufs=Autoarr_create(string,BL_C,BL_L); + b->compl_bufs=Autoarr_construct(string,BL_C,BL_L); u32 len=Autoarr_length(b->curr_buf); if(len==0) return; @@ -19,22 +19,21 @@ void complete_buf(StringBuilder* b){ ); Autoarr_add(b->compl_bufs,str); Autoarr_destruct(b->curr_buf, true); - b->curr_buf=Autoarr_create(i8,BL_C,BL_L); + b->curr_buf=Autoarr_construct(i8,BL_C,BL_L); } void StringBuilder_construct(StringBuilder* b, allocator_ptr external_al){ InternalAllocator_setExternalOrConstruct(b, external_al, LinearAllocator, 1024); b->compl_bufs=NULL; - b->curr_buf=Autoarr_create(i8,BL_C,BL_L); + b->curr_buf=Autoarr_construct(i8,BL_C,BL_L); } void StringBuilder_destruct(StringBuilder* b){ if(b->compl_bufs) Autoarr_destruct(b->compl_bufs, true); Autoarr_destruct(b->curr_buf, true); - if(InternalAllocator_isInternal(b)) - LinearAllocator_destruct((LinearAllocator*)InternalAllocator_getPtr(b)); + InternalAllocator_destructIfInternal(LinearAllocator, b); } string StringBuilder_build(StringBuilder* b){ diff --git a/src/String/StringBuilder.h b/src/String/StringBuilder.h index 181f249..9aa4c32 100644 --- a/src/String/StringBuilder.h +++ b/src/String/StringBuilder.h @@ -8,7 +8,7 @@ extern "C" { #include "string.h" STRUCT(StringBuilder, - InternalAllocator_decl(LinearAllocator); + InternalAllocator_declare(LinearAllocator); Autoarr(string)* compl_bufs; Autoarr(i8)* curr_buf; ) diff --git a/src/base/memory/CstdAllocator.c b/src/base/memory/CstdAllocator.c index c729842..f0f0de6 100644 --- a/src/base/memory/CstdAllocator.c +++ b/src/base/memory/CstdAllocator.c @@ -10,9 +10,6 @@ void CstdAllocator_free(allocator_ptr self, void* ptr){ free(ptr); } -void CstdAllocator_construct(CstdAllocator* self){ - self->base.alloc_f=CstdAllocator_alloc; - self->base.free_f=CstdAllocator_free; -} - kt_define(CstdAllocator, NULL, NULL); + +CstdAllocator CstdAllocator_instance=(CstdAllocator){.base.alloc_f=CstdAllocator_alloc, .base.free_f=CstdAllocator_free}; diff --git a/src/base/memory/allocators.h b/src/base/memory/allocators.h index 5f92d5e..78b4cf4 100644 --- a/src/base/memory/allocators.h +++ b/src/base/memory/allocators.h @@ -18,7 +18,8 @@ STRUCT(CstdAllocator, MemoryAllocator base; ); -void CstdAllocator_construct(CstdAllocator* self); +extern CstdAllocator CstdAllocator_instance; +#define CstdAllocator_instPtr (allocator_ptr)(&CstdAllocator_instance) /////////////////////////////////////////// diff --git a/src/base/memory/memory.h b/src/base/memory/memory.h index 9912e27..390dd5b 100644 --- a/src/base/memory/memory.h +++ b/src/base/memory/memory.h @@ -14,7 +14,7 @@ /////////////////////////////////////////// /// call this macro inside struct declaration -#define InternalAllocator_decl(AL_TYPE) \ +#define InternalAllocator_declare(AL_TYPE) \ AL_TYPE _internal_al; \ allocator_ptr _internal_al_ptr; @@ -28,12 +28,17 @@ #define InternalAllocator_setExternal(STRUCT_PTR, EXT_AL_PTR) (STRUCT_PTR->_internal_al_ptr = EXT_AL_PTR); /// create internal allocator and set ptr to it -#define InternalAllocator_construct(STRUCT_PTR, TYPE, CTOR_ARGS...) ({ \ +#define InternalAllocator_construct(STRUCT_PTR, TYPE, CTOR_ARGS...) { \ TYPE##_construct(&STRUCT_PTR->_internal_al, CTOR_ARGS); \ STRUCT_PTR->_internal_al_ptr = (allocator_ptr)&STRUCT_PTR->_internal_al; \ -}) +} /// if EXT_AL_PTR isn't null, set external allocator, otherwise create new #define InternalAllocator_setExternalOrConstruct(STRUCT_PTR, EXT_AL_PTR, TYPE, CTOR_ARGS...) \ if(EXT_AL_PTR!=NULL) InternalAllocator_setExternal(STRUCT_PTR, EXT_AL_PTR) \ else InternalAllocator_construct(STRUCT_PTR, TYPE, CTOR_ARGS) + +#define InternalAllocator_destructIfInternal(TYPE, STRUCT_PTR) {\ + if(InternalAllocator_isInternal(STRUCT_PTR)) \ + TYPE##_destruct((TYPE*)InternalAllocator_getPtr(STRUCT_PTR)); \ +} diff --git a/src/base/std.h b/src/base/std.h index 002035a..ec8dea6 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -25,7 +25,7 @@ typedef int64_t i64; typedef uint64_t u64; typedef float f32; typedef double f64; -/// anonymous pointer without specified freeMembers() func +/// anonymous pointer without specified destruct() func typedef void* Pointer; // Usually bool from stdbool.h is defined as macro, diff --git a/src/base/type_system/README.md b/src/base/type_system/README.md index c0066e9..0090a60 100644 --- a/src/base/type_system/README.md +++ b/src/base/type_system/README.md @@ -22,7 +22,7 @@ and register it. ## type descriptors -Every registered type should have it's own descriptor (`ktDescriptor`). It's a struct, which contains some information about type and pointers to some specific functions for this type (`toString`, `freeMembers`). +Every registered type should have it's own descriptor (`ktDescriptor`). It's a struct, which contains some information about type and pointers to some specific functions for this type (`toString`, `destruct`). ## type registration diff --git a/src/base/type_system/ktDescriptor.h b/src/base/type_system/ktDescriptor.h index 7a72a31..8f30457 100644 --- a/src/base/type_system/ktDescriptor.h +++ b/src/base/type_system/ktDescriptor.h @@ -20,14 +20,14 @@ extern "C" { .name=#TYPE, \ .id=ktid_undefined, \ .size=sizeof(TYPE), \ - .freeMembers=FREE_MEMBERS_F, \ + .destruct=FREE_MEMBERS_F, \ .toString=TOSTRING_F \ }; \ ktDescriptor ktDescriptor_##TYPE##_Ptr={\ .name=#TYPE "_Ptr", \ .id=ktid_undefined, \ .size=sizeof(TYPE), \ - .freeMembers=FREE_MEMBERS_F, \ + .destruct=FREE_MEMBERS_F, \ .toString=TOSTRING_F \ }; @@ -38,7 +38,7 @@ STRUCT(ktDescriptor, char* name; ktid id; u16 size; - destruct_t freeMembers; // NULL or function which frees all struct members + destruct_t destruct; // NULL or function which frees all struct members toString_t toString; // NULL or function which generates string representaion of object ) diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index 0758bc4..903abf0 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -22,13 +22,13 @@ char* ktDescriptor_toString(allocator_ptr al, ktDescriptor* d){ char *s0 = toString_u64(al, d->id, 0,0); char *s1 = toString_u64(al, d->size, 0,0); char *s2 = d->toString ? toString_hex(al, d->toString, sizeof(void*), 0,1,0) : n; - char *s3 = d->freeMembers ? toString_hex(al, d->freeMembers, sizeof(void*), 0,1,0) : n; + char *s3 = d->destruct ? toString_hex(al, d->destruct, sizeof(void*), 0,1,0) : n; char *rez=cptr_concat(al, "ktDescriptor {" " name:", d->name, " id:",s0, " size:",s1, " toString:",s2, - " freeMembers:",s3, + " destruct:",s3, " }"); if(s3!=n) allocator_free(al, s3); if(s2!=n) allocator_free(al, s2); @@ -56,15 +56,16 @@ ktDescriptorsState initState=NotInitialized; void kt_beginInit(){ kprintf("\e[94mtype descriptors initializing...\n"); - __descriptorPointers=Autoarr_create(Pointer, 256, 256); + Autoarr_construct(__descriptorPointers, Pointer, 256, NULL); } void kt_endInit(){ if(__descriptorPointers==NULL) throw(ERR_NULLPTR); - typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers); - Autoarr_destruct(__descriptorPointers,true); - if(typeDescriptors==NULL) throw(ERR_NULLPTR); + typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers, CstdAllocator_instPtr); + Autoarr_destruct(__descriptorPointers); + if(typeDescriptors==NULL) + throw(ERR_NULLPTR); kprintf("\e[92minitialized %u type descriptors\n", ktid_last); } diff --git a/src/base/type_system/unitype.c b/src/base/type_system/unitype.c index 986c8d6..d4531d7 100644 --- a/src/base/type_system/unitype.c +++ b/src/base/type_system/unitype.c @@ -18,8 +18,8 @@ void Unitype_destruct(Unitype* u) } ktDescriptor *type = ktDescriptor_get(u->typeId); - if (type->freeMembers) - type->freeMembers(u->VoidPtr); + if (type->destruct) + type->destruct(u->VoidPtr); if (u->allocatedInHeap) free(u->VoidPtr); } diff --git a/tests/test_allocators.c b/tests/test_allocators.c index b63220e..fd99571 100644 --- a/tests/test_allocators.c +++ b/tests/test_allocators.c @@ -28,9 +28,7 @@ void _test_allocator(allocator_ptr al){ void test_allocators(){ kprintf("\e[96m----------[test_allocators]-----------\n"); optime("test CstdAllocator", 10000, - CstdAllocator al; - CstdAllocator_construct(&al); - _test_allocator((allocator_ptr)&al); + _test_allocator(CstdAllocator_instPtr); ); optime("test LinearAllocator", 10000, LinearAllocator al; diff --git a/tests/test_autoarr-vs-vector.cpp b/tests/test_autoarr-vs-vector.cpp index 4368d52..865f48a 100644 --- a/tests/test_autoarr-vs-vector.cpp +++ b/tests/test_autoarr-vs-vector.cpp @@ -2,10 +2,11 @@ #include "../src/Autoarr/Autoarr.h" #include -i64 _autoarrVsVector(u16 chunkCount, u16 chunkLength){ - u32 count=chunkLength*chunkCount; - kprintf("\e[94mchunk count: %u chunk length: %u count: " IFWIN("%llu", "%lu") "\n", chunkCount, chunkLength, (u64)count); - Autoarr_i64* ar=Autoarr_create(i64, chunkCount, chunkLength); +i64 _autoarrVsVector(u32 count){ + kprintf("\e[94mcount: %u\n", count); + Autoarr_i64 _ar; + Autoarr_i64* ar=&_ar; + Autoarr_construct(ar, i64, count, NULL); std::vector vec=std::vector(); optime("Autoarr_add", count, Autoarr_add(ar, op_i)); @@ -16,19 +17,22 @@ i64 _autoarrVsVector(u16 chunkCount, u16 chunkLength){ t=Autoarr_get(ar, op_i)); optime("vector_get", count, t=vec[op_i]); - Autoarr_destruct(ar, true); + Autoarr_destruct(ar); return t; } void test_autoarrVsVector(){ optime(__func__, 1, kprintf("\e[96m-------[test_autoarr_vs_vector]-------\n"); - _autoarrVsVector(4, 16); - _autoarrVsVector(16, 64); - _autoarrVsVector(32, 32); - _autoarrVsVector(64, 64); - _autoarrVsVector(32, 1024); - _autoarrVsVector(256, 256); - _autoarrVsVector(512, 4096); + _autoarrVsVector(1); + _autoarrVsVector(4); + _autoarrVsVector(16); + _autoarrVsVector(64); + _autoarrVsVector(256); + _autoarrVsVector(1024); + _autoarrVsVector(4096); + _autoarrVsVector(16384); + _autoarrVsVector(65536); + _autoarrVsVector(524288); ); } diff --git a/tests/test_autoarr.c b/tests/test_autoarr.c index 0a8e0a3..985009f 100644 --- a/tests/test_autoarr.c +++ b/tests/test_autoarr.c @@ -1,53 +1,45 @@ #include "tests.h" #include "../src/Autoarr/Autoarr.h" +#define ARR_SZ 256 + static void printautoarr(Autoarr(u16)* ar){ - kprintf("\e[94mAutoarr(u16): " - IFWIN("%llu", "%lu") - "\n max_chunks_count: %u\n" - " chunks_count: %u\n" - " max_chunk_length: %u\n" - " chunk_length: %u\n" - " max_length: %u\n" - " length: %u\n", + kprintf("\e[94mAutoarr(u16): %lu\n" + " length: %u", sizeof(Autoarr(u16)), - ar->max_chunks_count, - ar->chunks_count, - ar->max_chunk_length, - ar->chunk_length, - Autoarr_max_length(ar), Autoarr_length(ar)); } static void fillar(Autoarr(u16)* ar){ - for (u16 i=0;ikey="message"; - char* tc=cptr_copy(al, text); - Unitype u=UniHeapPtr(char,tc); + char* tc=cptr_copy(CstdAllocator_instPtr, text); + Unitype u=UniHeapPtr(char, tc); k->value=u; *kptr=k; } diff --git a/tests/test_searchtree.c b/tests/test_searchtree.c index fa72925..dd91a89 100644 --- a/tests/test_searchtree.c +++ b/tests/test_searchtree.c @@ -2,9 +2,10 @@ #include "../src/SearchTree/SearchTree.h" void printstnode(STNode* node){ - kprintf("\e[94mSTNode: " - IFWIN("%llu", "%lu") - "\n address: %p\n value: ",sizeof(STNode),node); + kprintf("\e[94mSTNode: %lu\n" + " address: %p\n" + " value: ", + sizeof(STNode), node); printuni(node->value); kprintf("\n"); // prints pointers to all existing branches diff --git a/tests/test_type_system.c b/tests/test_type_system.c index 7d52799..0ee5296 100644 --- a/tests/test_type_system.c +++ b/tests/test_type_system.c @@ -3,7 +3,7 @@ void test_type_system(){ for(ktid id=0; id <= ktid_last; id++){ ktDescriptor* type=ktDescriptor_get(id); - kprintf("\e[37m{ id:%u name:%s size:%u freeMembers:%p toString:%p }\n", - type->id, type->name, type->size, type->freeMembers, type->toString); + kprintf("\e[37m{ id:%u name:%s size:%u destruct:%p toString:%p }\n", + type->id, type->name, type->size, type->destruct, type->toString); } } diff --git a/tests/tests.h b/tests/tests.h index 2b57815..4bb58a9 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -42,7 +42,7 @@ static inline void test_all(){ ); } -#define PRINT_SIZEOF(T) kprintf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T)) +#define PRINT_SIZEOF(T) kprintf("\e[94m" #T " size: \e[96m%lu\n", sizeof(T)) #if __cplusplus }