This commit is contained in:
timerix 2023-07-10 18:31:16 +06:00
parent 28169450cf
commit 14630e509d
31 changed files with 211 additions and 287 deletions

View File

@ -6,9 +6,11 @@
- check allocator_free call order - check allocator_free call order
- deal with StackingAllocator_free not freing memory sometimes - deal with StackingAllocator_free not freing memory sometimes
- replace LinearAllocator with StackingAllocator when possible (in DtsodV24_deserialize) - replace LinearAllocator with StackingAllocator when possible (in DtsodV24_deserialize)
- use LinkedList instead of complicated LinearAllocator logic
- configurable LinearAllocator chunk size (static/growing)
## Autoarr ## 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 - keep Autoarr_length() and Autoarr_maxLength() to old code compatibility
- toString() - toString()

View File

@ -1,17 +1,17 @@
#include "Autoarr.h" #include "Autoarr.h"
Autoarr_define(Pointer, true) // Autoarr_define(Pointer, true)
Autoarr_define(char, false) Autoarr_define(char, false)
Autoarr_define(bool, false) // Autoarr_define(bool, false)
Autoarr_define(f32, false) // Autoarr_define(f32, false)
Autoarr_define(f64, false) // Autoarr_define(f64, false)
Autoarr_define(u8, false) // Autoarr_define(u8, false)
Autoarr_define(i8, false) // Autoarr_define(i8, false)
Autoarr_define(u16, false) // Autoarr_define(u16, false)
Autoarr_define(i16, false) // Autoarr_define(i16, false)
Autoarr_define(u32, false) // Autoarr_define(u32, false)
Autoarr_define(i32, false) // Autoarr_define(i32, false)
Autoarr_define(u64, false) // Autoarr_define(u64, false)
Autoarr_define(i64, false) // Autoarr_define(i64, false)
Autoarr_define(Unitype, false) // Autoarr_define(Unitype, false)

View File

@ -23,21 +23,6 @@ Autoarr_declare(u64)
Autoarr_declare(Unitype) Autoarr_declare(Unitype)
#define Autoarr_foreach(ar, elem, codeblock...) { \
if(ar->chunks_count>0) { \
typeof(**ar->chunks) elem; \
for(u16 chunkI=0;chunkI<ar->chunks_count-1;chunkI++) \
for(u32 elemI=0;elemI<ar->max_chunk_length;elemI++){ \
elem=ar->chunks[chunkI][elemI]; \
{ codeblock; } \
} \
for(u16 elemI=0;elemI<ar->chunk_length;elemI++){ \
elem=ar->chunks[ar->chunks_count-1][elemI]; \
{ codeblock; } \
} \
} \
}
#if __cplusplus #if __cplusplus
} }
#endif #endif

View File

@ -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

View File

@ -5,7 +5,7 @@ extern "C" {
#include "Autoarr.h" #include "Autoarr.h"
EXPORT void CALL kerep_Autoarr_Unitype_create(u16 max_chunks_count, u16 max_chunk_length, Autoarr_Unitype** output){ 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){ EXPORT void CALL kerep_Autoarr_Unitype_destruct(Autoarr_Unitype* ar){

View File

@ -6,67 +6,60 @@ extern "C" {
#include "../base/base.h" #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 { \ typedef struct __Autoarr_##TYPE##_functions_list_t { \
void (*add)(struct Autoarr_##type* ar, type element); \ void (*add)(Autoarr_##TYPE* ar, TYPE element); \
type (*get)(struct Autoarr_##type* ar, u32 index); \ TYPE (*get)(Autoarr_##TYPE* ar, u32 index); \
type* (*getPtr)(struct Autoarr_##type* ar, u32 index); \ TYPE* (*getPtr)(Autoarr_##TYPE* ar, u32 index); \
void (*set)(struct Autoarr_##type* ar, u32 index, type element); \ void (*set)(Autoarr_##TYPE* ar, u32 index, TYPE element); \
void (*freeWithMembers)(struct Autoarr_##type* ar, bool freePtr); \ TYPE* (*toArray)(Autoarr_##TYPE* ar, allocator_ptr array_holder); \
void (*freeWithoutMembers)(struct Autoarr_##type* ar, bool freePtr); \ } __Autoarr_##TYPE##_functions_list_t; \
type* (*toArray)(struct Autoarr_##type* ar); \
} __Autoarr_##type##_functions_list_t; \
\ \
extern __Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list; \ STRUCT(Autoarr_##TYPE, \
\ InternalAllocator_declare(LinearAllocator); \
STRUCT(Autoarr_##type, \ __Autoarr_##TYPE##_functions_list_t* functions; \
u16 chunks_count; \ ktDescriptor* type; \
u16 max_chunks_count; \ u32 length; \
u16 chunk_length; \ TYPE* _typeof_target; \
u16 max_chunk_length; \
type** chunks; \
__Autoarr_##type##_functions_list_t* functions; \
) \ ) \
\ \
Autoarr_##type* __Autoarr_##type##_create(u16 max_chunks_count, u16 max_chunk_length); \ void __Autoarr_##TYPE##_construct(Autoarr_##TYPE* ar, alloc_size_t starting_size, allocator_ptr external_al); \
void __Autoarr_##type##_destructWithMembers(Autoarr_##type* ar, bool freePtr); \ void __Autoarr_##TYPE##_destruct(Autoarr_##TYPE* ar); \
void ____Autoarr_##type##_destructWithMembers(void* ar);
#define Autoarr(type) Autoarr_##type #define Autoarr(TYPE) Autoarr_##TYPE
#define Autoarr_create(type, max_chunks_count, max_chunk_length) \ #define Autoarr_construct(ptr, TYPE, starting_size, data_allocator) \
__Autoarr_##type##_create(max_chunks_count, max_chunk_length) __Autoarr_##TYPE##_construct(ptr, starting_size, data_allocator)
#define Autoarr_add(autoarr, element) \ #define Autoarr_destruct(autoarr) (autoarr)->type->destruct(autoarr)
autoarr->functions->add(autoarr, element) #define Autoarr_add(autoarr, element) (autoarr)->functions->add(autoarr, element)
#define Autoarr_get(autoarr, index) \ #define Autoarr_get(autoarr, index) (autoarr)->functions->get(autoarr,index)
autoarr->functions->get(autoarr,index) #define Autoarr_getPtr(autoarr, index) (autoarr)->functions->getPtr(autoarr,index)
#define Autoarr_getPtr(autoarr, index) \ #define Autoarr_set(autoarr, index, element) (autoarr)->functions->set(autoarr, index, element)
autoarr->functions->getPtr(autoarr,index) #define Autoarr_toArray(autoarr, array_alloctr) (autoarr)->functions->toArray(autoarr, array_alloctr)
#define Autoarr_set(autoarr, index, element) \ #define Autoarr_length(autoarr) (autoarr)->length
autoarr->functions->set(autoarr, index, element) #define Autoarr_pop(autoarr) { \
#define Autoarr_destruct(autoarr, freePtr) \ u32 new_len=(autoarr)->length-1; \
autoarr->functions->freeWithMembers(autoarr, freePtr) allocator_free(InternalAllocator_getPtr((autoarr)), Autoarr_getPtr((autoarr), new_len)); \
#define Autoarr_destructWithoutMembers(autoarr, freePtr) \ (autoarr)->length=new_len; \
autoarr->functions->freeWithoutMembers(autoarr, freePtr) }
#define Autoarr_toArray(autoarr) \
autoarr->functions->toArray(autoarr)
#define Autoarr_length(autoarr) \ #define Autoarr_foreach(ar, elem, codeblock...) { \
(u32)(!autoarr->chunks_count ? 0 : \ if((ar)->length > 0) { \
autoarr->max_chunk_length*(autoarr->chunks_count-1)+autoarr->chunk_length) typeof(*((ar)->_typeof_target)) elem; \
#define Autoarr_max_length(autoarr) \ LinearAllocator* al=(LinearAllocator*)InternalAllocator_getPtr(ar); \
(u32)(autoarr->max_chunk_length*autoarr->max_chunks_count) for(u16 chunk_i=0; chunk_i <= al->curr_chunk_i; chunk_i++) { \
MemoryChunk chunk = al->chunks[chunk_i]; \
#define Autoarr_pop(AR){ \ alloc_size_t chunk_elem_count = chunk.occupied_size/sizeof(elem); \
if(AR->chunk_length==1){ \ typeof((ar)->_typeof_target) chunk_data = (void*)chunk.data; \
AR->chunks_count--; \ for(u32 elem##_i=0; elem##_i < chunk_elem_count; elem##_i++) { \
AR->chunk_length=AR->max_chunk_length; \ elem = chunk_data[elem##_i]; \
free(AR->chunks[AR->chunks_count]); \ { codeblock; } \
} \
} \
} \ } \
else AR->chunk_length--; \
} }
#if __cplusplus #if __cplusplus

View File

@ -6,94 +6,79 @@ extern "C" {
#include "../base/base.h" #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)); \
void __Autoarr_##type##_add(Autoarr_##type* ar, type element){ \ *ptr=element; \
if(!ar->chunks){ \ ar->length++; \
ar->chunks=malloc(ar->max_chunks_count*sizeof(type*)); \
goto create_chunk; \
} \
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++; \
} \ } \
\ \
type __Autoarr_##type##_get(Autoarr_##type* ar, u32 index){ \ TYPE* __Autoarr_##TYPE##_getPtr(Autoarr_##TYPE* ar, u32 index){ \
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ if(index >= Autoarr_length(ar)) \
return ar->chunks[index/ar->max_chunk_length][index%ar->max_chunk_length]; \ 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; \
} \
return NULL; \
} \ } \
\ \
type* __Autoarr_##type##_getPtr(Autoarr_##type* ar, u32 index){ \ TYPE __Autoarr_##TYPE##_get(Autoarr_##TYPE* ar, u32 index){ \
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ TYPE* ptr=__Autoarr_##TYPE##_getPtr(ar, index); \
return ar->chunks[index/ar->max_chunk_length]+(index%ar->max_chunk_length); \ return *ptr; \
} \ } \
\ \
void __Autoarr_##type##_set(Autoarr_##type* ar, u32 index, type element){ \ void __Autoarr_##TYPE##_set(Autoarr_##TYPE* ar, u32 index, TYPE value){ \
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ TYPE* ptr=__Autoarr_##TYPE##_getPtr(ar, index); \
ar->chunks[index/ar->max_chunk_length][index%ar->max_chunk_length]=element; \ *ptr=value; \
} \ } \
\ \
void __Autoarr_##type##_destructWithoutMembers(Autoarr_##type* ar, bool freePtr){ \ void __Autoarr_##TYPE##_destruct(Autoarr_##TYPE* ar){ \
for(u16 i=0; i<ar->chunks_count;i++) \ destruct_t value_destructor=ar->type->destruct; \
free(ar->chunks[i]); \ if(value_destructor!=NULL) { \
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, \ Autoarr_foreach(ar, el, \
void* members_ptr=&el; \ TYPE* value_ptr = TYPE_IS_PTR ? *(TYPE**)(&el) : &el; \
if(TYPE_IS_PTR) members_ptr=*(type**)members_ptr; \ value_destructor(value_ptr); \
ktDescriptor_##type.freeMembers(members_ptr); \
); \ ); \
} \ } \
__Autoarr_##type##_destructWithoutMembers(ar, freePtr); \ InternalAllocator_destructIfInternal(LinearAllocator, ar); \
} \
void ____Autoarr_##type##_destructWithMembers(void* ar){ \
__Autoarr_##type##_destructWithMembers((Autoarr_##type*)ar, false); \
} \ } \
\ \
type* __Autoarr_##type##_toArray(Autoarr_##type* ar){ \ TYPE* __Autoarr_##TYPE##_toArray(Autoarr_##TYPE* ar, allocator_ptr array_alloctr){ \
u32 length=Autoarr_length(ar); \ u32 length=Autoarr_length(ar); \
if(length==0) \ if(length==0) \
return NULL; \ return NULL; \
type* array=malloc(length * sizeof(type)); \ TYPE* array=allocator_alloc(array_alloctr, length); \
for(u32 i=0; i<length; i++) \ Autoarr_foreach(ar, el, { \
array[i]=__Autoarr_##type##_get(ar, i); \ array[el_i]=el; \
}); \
return array; \ return array; \
} \ } \
\ \
__Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list={ \ __Autoarr_##TYPE##_functions_list_t __Autoarr_##TYPE##_functions_list={ \
&__Autoarr_##type##_add, \ &__Autoarr_##TYPE##_add, \
&__Autoarr_##type##_get, \ &__Autoarr_##TYPE##_get, \
&__Autoarr_##type##_getPtr, \ &__Autoarr_##TYPE##_getPtr, \
&__Autoarr_##type##_set, \ &__Autoarr_##TYPE##_set, \
&__Autoarr_##type##_destructWithMembers, \ &__Autoarr_##TYPE##_toArray \
&__Autoarr_##type##_destructWithoutMembers, \
&__Autoarr_##type##_toArray \
}; \ }; \
\ \
Autoarr_##type* __Autoarr_##type##_create(u16 max_chunks_count, u16 max_chunk_length){ \ void __Autoarr_##TYPE##_construct(Autoarr_##TYPE* ar, alloc_size_t starting_size, allocator_ptr data_allocator){ \
Autoarr_##type* ar=malloc(sizeof(Autoarr_##type)); \ InternalAllocator_setExternalOrConstruct(ar, data_allocator, LinearAllocator, starting_size); \
*ar=(Autoarr_##type){ \ ar->functions=&__Autoarr_##TYPE##_functions_list; \
.max_chunks_count=max_chunks_count, \ ar->type = TYPE_IS_PTR ? &ktDescriptor_namePtr(TYPE) : &ktDescriptor_name(TYPE); \
.chunks_count=0, \ ar->length=0; \
.max_chunk_length=max_chunk_length, \ } \
.chunk_length=0, \ \
.chunks=NULL, \ kt_define(Autoarr_##TYPE, (destruct_t)__Autoarr_##TYPE##_destruct, NULL);
.functions=&__Autoarr_##type##_functions_list \
}; \
return ar; \
}
#if __cplusplus #if __cplusplus
} }

View File

@ -1,8 +1,7 @@
#include "DtsodV24.h" #include "DtsodV24.h"
#include "../String/StringBuilder.h" #include "../String/StringBuilder.h"
#define ARR_BC 64 #define ARR_SZ_START 64
#define ARR_BL 1024
typedef struct DeserializeSharedData{ typedef struct DeserializeSharedData{
@ -149,19 +148,20 @@ Maybe __ReadString(DeserializeSharedData* shared){
#define ReadString() __ReadString(shared) #define ReadString() __ReadString(shared)
Maybe __ReadList(DeserializeSharedData* 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; bool readingList=true;
while (true){ while (true){
try(ReadValue((&readingList)), m_val, Autoarr_destruct(list, true)) try(ReadValue((&readingList)), m_val, Autoarr_destruct(&list))
Autoarr_add(list,m_val.value); Autoarr_add(&list, m_val.value);
if (!readingList){ if (!readingList){
if(Unitype_isUniNull(m_val.value)) if(Unitype_isUniNull(m_val.value))
Autoarr_pop(list); Autoarr_pop(&list);
break; break;
} }
} }
return SUCCESS(UniHeapPtr(Autoarr_Unitype,list)); return SUCCESS(UniHeapPtr(Autoarr_Unitype, &list));
}; };
#define ReadList() __ReadList(shared) #define ReadList() __ReadList(shared)
@ -320,7 +320,8 @@ Maybe __deserialize(char** _text, bool _calledRecursively, allocator_ptr _tmp_al
// allocator_free(tmp_al, nameCPtr); // allocator_free(tmp_al, nameCPtr);
} }
else{ 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)); Hashtable_add(dict,nameCPtr,UniHeapPtr(Autoarr_Unitype,list));
} }
Autoarr_add(list,val.value); Autoarr_add(list,val.value);

View File

@ -15,7 +15,7 @@ Hashtable* Hashtable_create(){
ht->hein=HT_HEIN_MIN; ht->hein=HT_HEIN_MIN;
ht->rows=malloc(HT_HEIGHTS[HT_HEIN_MIN]*sizeof(Autoarr(KVPair)*)); ht->rows=malloc(HT_HEIGHTS[HT_HEIN_MIN]*sizeof(Autoarr(KVPair)*));
for(u16 i=0;i<HT_HEIGHTS[HT_HEIN_MIN];i++) for(u16 i=0;i<HT_HEIGHTS[HT_HEIN_MIN];i++)
ht->rows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL); ht->rows[i]=Autoarr_construct(KVPair,ARR_BC,ARR_BL);
return ht; return ht;
} }
@ -38,7 +38,7 @@ void Hashtable_expand(Hashtable* ht){
Autoarr(KVPair)** newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KVPair)*)); Autoarr(KVPair)** newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KVPair)*));
for(u16 i=0;i<HT_HEIGHTS[ht->hein];i++) for(u16 i=0;i<HT_HEIGHTS[ht->hein];i++)
newrows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL); newrows[i]=Autoarr_construct(KVPair,ARR_BC,ARR_BL);
for(u16 i=0;i<HT_HEIGHTS[ht->hein-1];i++){ for(u16 i=0;i<HT_HEIGHTS[ht->hein-1];i++){
Autoarr(KVPair)* ar=ht->rows[i]; Autoarr(KVPair)* ar=ht->rows[i];

View File

@ -16,7 +16,7 @@ extern "C" {
#define LLNode_create(TYPE, VALUE) LLNode_##TYPE##_create(VALUE) #define LLNode_create(TYPE, VALUE) LLNode_##TYPE##_create(VALUE)
#define LinkedList_create(TYPE) LinkedList_##TYPE##_create() #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); void LinkedList_addToBeginning(void* _llist, void* _new_node);

View File

@ -27,7 +27,7 @@ STRUCT(LinkedList(TYPE), \
) \ ) \
\ \
typedef struct LinkedList_##TYPE##_functions_t { \ typedef struct LinkedList_##TYPE##_functions_t { \
destruct_t freeMembers; \ destruct_t destruct; \
void (*removePrev)(LinkedList(TYPE)* llist, LLNode(TYPE)* nextNode, bool freeRemoved); \ void (*removePrev)(LinkedList(TYPE)* llist, LLNode(TYPE)* nextNode, bool freeRemoved); \
void (*removeNext)(LinkedList(TYPE)* llist, LLNode(TYPE)* prevNode, bool freeRemoved); \ void (*removeNext)(LinkedList(TYPE)* llist, LLNode(TYPE)* prevNode, bool freeRemoved); \
} LinkedList_##TYPE##_functions_t; \ } LinkedList_##TYPE##_functions_t; \

View File

@ -18,7 +18,7 @@ void LLNode_##TYPE##_destructMembers(void* _node){ \
LLNode(TYPE)* node=(LLNode(TYPE)*)_node; \ LLNode(TYPE)* node=(LLNode(TYPE)*)_node; \
void* value_ptr=&node->value; \ void* value_ptr=&node->value; \
if(TYPE_IS_PTR) value_ptr=*(TYPE**)value_ptr; \ 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){ \ 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={ \ LinkedList_##TYPE##_functions_t _LinkedList_##TYPE##_functions={ \
.freeMembers=LinkedList_##TYPE##_destructMembers, \ .destruct=LinkedList_##TYPE##_destructMembers, \
.removePrev=LinkedList_##TYPE##_removePrev, \ .removePrev=LinkedList_##TYPE##_removePrev, \
.removeNext=LinkedList_##TYPE##_removeNext \ .removeNext=LinkedList_##TYPE##_removeNext \
}; \ }; \

View File

@ -8,7 +8,7 @@ kt_define(StringBuilder, (destruct_t)StringBuilder_destruct, NULL);
void complete_buf(StringBuilder* b){ void complete_buf(StringBuilder* b){
if(!b->compl_bufs) 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); u32 len=Autoarr_length(b->curr_buf);
if(len==0) if(len==0)
return; return;
@ -19,22 +19,21 @@ void complete_buf(StringBuilder* b){
); );
Autoarr_add(b->compl_bufs,str); Autoarr_add(b->compl_bufs,str);
Autoarr_destruct(b->curr_buf, true); 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){ void StringBuilder_construct(StringBuilder* b, allocator_ptr external_al){
InternalAllocator_setExternalOrConstruct(b, external_al, LinearAllocator, 1024); InternalAllocator_setExternalOrConstruct(b, external_al, LinearAllocator, 1024);
b->compl_bufs=NULL; 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){ void StringBuilder_destruct(StringBuilder* b){
if(b->compl_bufs) if(b->compl_bufs)
Autoarr_destruct(b->compl_bufs, true); Autoarr_destruct(b->compl_bufs, true);
Autoarr_destruct(b->curr_buf, true); Autoarr_destruct(b->curr_buf, true);
if(InternalAllocator_isInternal(b)) InternalAllocator_destructIfInternal(LinearAllocator, b);
LinearAllocator_destruct((LinearAllocator*)InternalAllocator_getPtr(b));
} }
string StringBuilder_build(StringBuilder* b){ string StringBuilder_build(StringBuilder* b){

View File

@ -8,7 +8,7 @@ extern "C" {
#include "string.h" #include "string.h"
STRUCT(StringBuilder, STRUCT(StringBuilder,
InternalAllocator_decl(LinearAllocator); InternalAllocator_declare(LinearAllocator);
Autoarr(string)* compl_bufs; Autoarr(string)* compl_bufs;
Autoarr(i8)* curr_buf; Autoarr(i8)* curr_buf;
) )

View File

@ -10,9 +10,6 @@ void CstdAllocator_free(allocator_ptr self, void* ptr){
free(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); kt_define(CstdAllocator, NULL, NULL);
CstdAllocator CstdAllocator_instance=(CstdAllocator){.base.alloc_f=CstdAllocator_alloc, .base.free_f=CstdAllocator_free};

View File

@ -18,7 +18,8 @@ STRUCT(CstdAllocator,
MemoryAllocator base; MemoryAllocator base;
); );
void CstdAllocator_construct(CstdAllocator* self); extern CstdAllocator CstdAllocator_instance;
#define CstdAllocator_instPtr (allocator_ptr)(&CstdAllocator_instance)
/////////////////////////////////////////// ///////////////////////////////////////////

View File

@ -14,7 +14,7 @@
/////////////////////////////////////////// ///////////////////////////////////////////
/// call this macro inside struct declaration /// call this macro inside struct declaration
#define InternalAllocator_decl(AL_TYPE) \ #define InternalAllocator_declare(AL_TYPE) \
AL_TYPE _internal_al; \ AL_TYPE _internal_al; \
allocator_ptr _internal_al_ptr; 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); #define InternalAllocator_setExternal(STRUCT_PTR, EXT_AL_PTR) (STRUCT_PTR->_internal_al_ptr = EXT_AL_PTR);
/// create internal allocator and set ptr to it /// 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); \ TYPE##_construct(&STRUCT_PTR->_internal_al, CTOR_ARGS); \
STRUCT_PTR->_internal_al_ptr = (allocator_ptr)&STRUCT_PTR->_internal_al; \ STRUCT_PTR->_internal_al_ptr = (allocator_ptr)&STRUCT_PTR->_internal_al; \
}) }
/// if EXT_AL_PTR isn't null, set external allocator, otherwise create new /// if EXT_AL_PTR isn't null, set external allocator, otherwise create new
#define InternalAllocator_setExternalOrConstruct(STRUCT_PTR, EXT_AL_PTR, TYPE, CTOR_ARGS...) \ #define InternalAllocator_setExternalOrConstruct(STRUCT_PTR, EXT_AL_PTR, TYPE, CTOR_ARGS...) \
if(EXT_AL_PTR!=NULL) InternalAllocator_setExternal(STRUCT_PTR, EXT_AL_PTR) \ if(EXT_AL_PTR!=NULL) InternalAllocator_setExternal(STRUCT_PTR, EXT_AL_PTR) \
else InternalAllocator_construct(STRUCT_PTR, TYPE, CTOR_ARGS) 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)); \
}

View File

@ -25,7 +25,7 @@ typedef int64_t i64;
typedef uint64_t u64; typedef uint64_t u64;
typedef float f32; typedef float f32;
typedef double f64; typedef double f64;
/// anonymous pointer without specified freeMembers() func /// anonymous pointer without specified destruct() func
typedef void* Pointer; typedef void* Pointer;
// Usually bool from stdbool.h is defined as macro, // Usually bool from stdbool.h is defined as macro,

View File

@ -22,7 +22,7 @@ and register it.
## type descriptors ## 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 ## type registration

View File

@ -20,14 +20,14 @@ extern "C" {
.name=#TYPE, \ .name=#TYPE, \
.id=ktid_undefined, \ .id=ktid_undefined, \
.size=sizeof(TYPE), \ .size=sizeof(TYPE), \
.freeMembers=FREE_MEMBERS_F, \ .destruct=FREE_MEMBERS_F, \
.toString=TOSTRING_F \ .toString=TOSTRING_F \
}; \ }; \
ktDescriptor ktDescriptor_##TYPE##_Ptr={\ ktDescriptor ktDescriptor_##TYPE##_Ptr={\
.name=#TYPE "_Ptr", \ .name=#TYPE "_Ptr", \
.id=ktid_undefined, \ .id=ktid_undefined, \
.size=sizeof(TYPE), \ .size=sizeof(TYPE), \
.freeMembers=FREE_MEMBERS_F, \ .destruct=FREE_MEMBERS_F, \
.toString=TOSTRING_F \ .toString=TOSTRING_F \
}; };
@ -38,7 +38,7 @@ STRUCT(ktDescriptor,
char* name; char* name;
ktid id; ktid id;
u16 size; 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 toString_t toString; // NULL or function which generates string representaion of object
) )

View File

@ -22,13 +22,13 @@ char* ktDescriptor_toString(allocator_ptr al, ktDescriptor* d){
char *s0 = toString_u64(al, d->id, 0,0); char *s0 = toString_u64(al, d->id, 0,0);
char *s1 = toString_u64(al, d->size, 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 *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 {" char *rez=cptr_concat(al, "ktDescriptor {"
" name:", d->name, " name:", d->name,
" id:",s0, " id:",s0,
" size:",s1, " size:",s1,
" toString:",s2, " toString:",s2,
" freeMembers:",s3, " destruct:",s3,
" }"); " }");
if(s3!=n) allocator_free(al, s3); if(s3!=n) allocator_free(al, s3);
if(s2!=n) allocator_free(al, s2); if(s2!=n) allocator_free(al, s2);
@ -56,15 +56,16 @@ ktDescriptorsState initState=NotInitialized;
void kt_beginInit(){ void kt_beginInit(){
kprintf("\e[94mtype descriptors initializing...\n"); kprintf("\e[94mtype descriptors initializing...\n");
__descriptorPointers=Autoarr_create(Pointer, 256, 256); Autoarr_construct(__descriptorPointers, Pointer, 256, NULL);
} }
void kt_endInit(){ void kt_endInit(){
if(__descriptorPointers==NULL) if(__descriptorPointers==NULL)
throw(ERR_NULLPTR); throw(ERR_NULLPTR);
typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers); typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers, CstdAllocator_instPtr);
Autoarr_destruct(__descriptorPointers,true); Autoarr_destruct(__descriptorPointers);
if(typeDescriptors==NULL) throw(ERR_NULLPTR); if(typeDescriptors==NULL)
throw(ERR_NULLPTR);
kprintf("\e[92minitialized %u type descriptors\n", ktid_last); kprintf("\e[92minitialized %u type descriptors\n", ktid_last);
} }

View File

@ -18,8 +18,8 @@ void Unitype_destruct(Unitype* u)
} }
ktDescriptor *type = ktDescriptor_get(u->typeId); ktDescriptor *type = ktDescriptor_get(u->typeId);
if (type->freeMembers) if (type->destruct)
type->freeMembers(u->VoidPtr); type->destruct(u->VoidPtr);
if (u->allocatedInHeap) if (u->allocatedInHeap)
free(u->VoidPtr); free(u->VoidPtr);
} }

View File

@ -28,9 +28,7 @@ void _test_allocator(allocator_ptr al){
void test_allocators(){ void test_allocators(){
kprintf("\e[96m----------[test_allocators]-----------\n"); kprintf("\e[96m----------[test_allocators]-----------\n");
optime("test CstdAllocator", 10000, optime("test CstdAllocator", 10000,
CstdAllocator al; _test_allocator(CstdAllocator_instPtr);
CstdAllocator_construct(&al);
_test_allocator((allocator_ptr)&al);
); );
optime("test LinearAllocator", 10000, optime("test LinearAllocator", 10000,
LinearAllocator al; LinearAllocator al;

View File

@ -2,10 +2,11 @@
#include "../src/Autoarr/Autoarr.h" #include "../src/Autoarr/Autoarr.h"
#include <vector> #include <vector>
i64 _autoarrVsVector(u16 chunkCount, u16 chunkLength){ i64 _autoarrVsVector(u32 count){
u32 count=chunkLength*chunkCount; kprintf("\e[94mcount: %u\n", count);
kprintf("\e[94mchunk count: %u chunk length: %u count: " IFWIN("%llu", "%lu") "\n", chunkCount, chunkLength, (u64)count); Autoarr_i64 _ar;
Autoarr_i64* ar=Autoarr_create(i64, chunkCount, chunkLength); Autoarr_i64* ar=&_ar;
Autoarr_construct(ar, i64, count, NULL);
std::vector<i64> vec=std::vector<i64>(); std::vector<i64> vec=std::vector<i64>();
optime("Autoarr_add", count, optime("Autoarr_add", count,
Autoarr_add(ar, op_i)); Autoarr_add(ar, op_i));
@ -16,19 +17,22 @@ i64 _autoarrVsVector(u16 chunkCount, u16 chunkLength){
t=Autoarr_get(ar, op_i)); t=Autoarr_get(ar, op_i));
optime("vector_get", count, optime("vector_get", count,
t=vec[op_i]); t=vec[op_i]);
Autoarr_destruct(ar, true); Autoarr_destruct(ar);
return t; return t;
} }
void test_autoarrVsVector(){ void test_autoarrVsVector(){
optime(__func__, 1, optime(__func__, 1,
kprintf("\e[96m-------[test_autoarr_vs_vector]-------\n"); kprintf("\e[96m-------[test_autoarr_vs_vector]-------\n");
_autoarrVsVector(4, 16); _autoarrVsVector(1);
_autoarrVsVector(16, 64); _autoarrVsVector(4);
_autoarrVsVector(32, 32); _autoarrVsVector(16);
_autoarrVsVector(64, 64); _autoarrVsVector(64);
_autoarrVsVector(32, 1024); _autoarrVsVector(256);
_autoarrVsVector(256, 256); _autoarrVsVector(1024);
_autoarrVsVector(512, 4096); _autoarrVsVector(4096);
_autoarrVsVector(16384);
_autoarrVsVector(65536);
_autoarrVsVector(524288);
); );
} }

View File

@ -1,31 +1,22 @@
#include "tests.h" #include "tests.h"
#include "../src/Autoarr/Autoarr.h" #include "../src/Autoarr/Autoarr.h"
#define ARR_SZ 256
static void printautoarr(Autoarr(u16)* ar){ static void printautoarr(Autoarr(u16)* ar){
kprintf("\e[94mAutoarr(u16): " kprintf("\e[94mAutoarr(u16): %lu\n"
IFWIN("%llu", "%lu") " length: %u",
"\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",
sizeof(Autoarr(u16)), sizeof(Autoarr(u16)),
ar->max_chunks_count,
ar->chunks_count,
ar->max_chunk_length,
ar->chunk_length,
Autoarr_max_length(ar),
Autoarr_length(ar)); Autoarr_length(ar));
} }
static void fillar(Autoarr(u16)* ar){ static void fillar(Autoarr(u16)* ar){
for (u16 i=0;i<Autoarr_max_length(ar);i++) for (u16 i=0; ARR_SZ; i++)
Autoarr_add(ar,i); Autoarr_add(ar,i);
} }
static void resetar(Autoarr(u16)* ar){ static void resetar(Autoarr(u16)* ar){
for (u16 i=0;i<Autoarr_max_length(ar);i++) for (u16 i=0; ARR_SZ; i++)
Autoarr_set(ar,i,Autoarr_max_length(ar)-i-1); Autoarr_set(ar,i,ARR_SZ-i-1);
} }
static void printallval(Autoarr(u16)* ar){ static void printallval(Autoarr(u16)* ar){
@ -36,18 +27,19 @@ static void printallval(Autoarr(u16)* ar){
} }
void test_autoarr(){ void test_autoarr(){
optime("test_autoarr",1, // optime("test_autoarr",1,
kprintf("\e[96m------------[test_autoarr]------------\n"); kprintf("\e[96m------------[test_autoarr]------------\n");
Autoarr(u16)* ar=Autoarr_create(u16,10,16); Autoarr(u16) ar;
Autoarr_construct(&ar, u16, ARR_SZ, NULL);
kprintf("\e[92mautoarr created\n"); kprintf("\e[92mautoarr created\n");
fillar(ar); fillar(&ar);
kprintf("\e[92mautoarr filled up\n"); kprintf("\e[92mautoarr filled up\n");
printautoarr(ar); printautoarr(&ar);
printallval(ar); printallval(&ar);
resetar(ar); resetar(&ar);
kprintf("\e[92mautoarr values reset\n"); kprintf("\e[92mautoarr values reset\n");
printallval(ar); printallval(&ar);
Autoarr_destruct(ar, true); Autoarr_destruct(&ar);
kprintf("\e[92mautoarr deleted\n"); kprintf("\e[92mautoarr deleted\n");
); // );
} }

View File

@ -16,7 +16,9 @@ char data[]="iojihi2ojo8la14nhvi3311pi[jiugbja38mo0ih6gfty88tryf-drh0lanvj03";
h=hashf(h, data, sizeof(data)); \ h=hashf(h, data, sizeof(data)); \
); \ ); \
/*kprintf("\e[94mhash of \"\e[90m%s\e[94m\": \e[92m%x\n",data, h);*/ \ /*kprintf("\e[94mhash of \"\e[90m%s\e[94m\": \e[92m%x\n",data, h);*/ \
Autoarr(hasht)* hashes=Autoarr_create(hasht,1,COLLISION_TESTS); \ Autoarr(hasht) _hashes; \
Autoarr(hasht)* hashes = &_hashes; \
Autoarr_construct(hashes, hasht, COLLISION_TESTS*sizeof(hasht), NULL); \
splitmix64_state rng_state; \ splitmix64_state rng_state; \
splitmix64_construct(&rng_state, random_seedFromTime()); \ splitmix64_construct(&rng_state, random_seedFromTime()); \
optime("collision test",1, \ optime("collision test",1, \
@ -38,7 +40,7 @@ char data[]="iojihi2ojo8la14nhvi3311pi[jiugbja38mo0ih6gfty88tryf-drh0lanvj03";
} \ } \
kprintf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS); \ kprintf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS); \
); \ ); \
Autoarr_destruct(hashes, true); \ Autoarr_destruct(hashes); \
kprintf("\e[96m--------------------------------------\n"); \ kprintf("\e[96m--------------------------------------\n"); \
} }

View File

@ -2,9 +2,8 @@
#include "../src/Hashtable/Hashtable.h" #include "../src/Hashtable/Hashtable.h"
void print_hashtable(Hashtable* ht){ void print_hashtable(Hashtable* ht){
kprintf("\e[94mHashtable: " kprintf("\e[94mHashtable: %lu\n"
IFWIN("%llu", "%lu") " hein: %u\n"
"\n hein: %u\n"
" height: %u\n" " height: %u\n"
" rows: %p\n", " rows: %p\n",
sizeof(Hashtable), sizeof(Hashtable),

View File

@ -1,12 +1,9 @@
#include "../src/Hashtable/KeyValuePair.h" #include "../src/Hashtable/KeyValuePair.h"
EXPORT void CALL test_marshalling(char* text, KVPair** kptr){ EXPORT void CALL test_marshalling(char* text, KVPair** kptr){
CstdAllocator _al; KVPair* k=allocator_alloc(CstdAllocator_instPtr, sizeof(KVPair));
allocator_ptr al=(allocator_ptr)&_al;
CstdAllocator_construct(&_al);
KVPair* k=allocator_alloc(al, sizeof(KVPair));
k->key="message"; k->key="message";
char* tc=cptr_copy(al, text); char* tc=cptr_copy(CstdAllocator_instPtr, text);
Unitype u=UniHeapPtr(char, tc); Unitype u=UniHeapPtr(char, tc);
k->value=u; k->value=u;
*kptr=k; *kptr=k;

View File

@ -2,9 +2,10 @@
#include "../src/SearchTree/SearchTree.h" #include "../src/SearchTree/SearchTree.h"
void printstnode(STNode* node){ void printstnode(STNode* node){
kprintf("\e[94mSTNode: " kprintf("\e[94mSTNode: %lu\n"
IFWIN("%llu", "%lu") " address: %p\n"
"\n address: %p\n value: ",sizeof(STNode),node); " value: ",
sizeof(STNode), node);
printuni(node->value); printuni(node->value);
kprintf("\n"); kprintf("\n");
// prints pointers to all existing branches // prints pointers to all existing branches

View File

@ -3,7 +3,7 @@
void test_type_system(){ void test_type_system(){
for(ktid id=0; id <= ktid_last; id++){ for(ktid id=0; id <= ktid_last; id++){
ktDescriptor* type=ktDescriptor_get(id); ktDescriptor* type=ktDescriptor_get(id);
kprintf("\e[37m{ id:%u name:%s size:%u freeMembers:%p toString:%p }\n", kprintf("\e[37m{ id:%u name:%s size:%u destruct:%p toString:%p }\n",
type->id, type->name, type->size, type->freeMembers, type->toString); type->id, type->name, type->size, type->destruct, type->toString);
} }
} }

View File

@ -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 #if __cplusplus
} }