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
- 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()

View File

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

View File

@ -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;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
}
#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"
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){

View File

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

View File

@ -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); \
} \
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++; \
elem_count_sum += chunk_elem_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; i<ar->chunks_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) { \
void __Autoarr_##TYPE##_destruct(Autoarr_##TYPE* ar){ \
destruct_t value_destructor=ar->type->destruct; \
if(value_destructor!=NULL) { \
Autoarr_foreach(ar, el, \
void* members_ptr=&el; \
if(TYPE_IS_PTR) members_ptr=*(type**)members_ptr; \
ktDescriptor_##type.freeMembers(members_ptr); \
TYPE* value_ptr = TYPE_IS_PTR ? *(TYPE**)(&el) : &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; i<length; i++) \
array[i]=__Autoarr_##type##_get(ar, i); \
TYPE* array=allocator_alloc(array_alloctr, length); \
Autoarr_foreach(ar, el, { \
array[el_i]=el; \
}); \
return array; \
} \
\
__Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list={ \
&__Autoarr_##type##_add, \
&__Autoarr_##type##_get, \
&__Autoarr_##type##_getPtr, \
&__Autoarr_##type##_set, \
&__Autoarr_##type##_destructWithMembers, \
&__Autoarr_##type##_destructWithoutMembers, \
&__Autoarr_##type##_toArray \
__Autoarr_##TYPE##_functions_list_t __Autoarr_##TYPE##_functions_list={ \
&__Autoarr_##TYPE##_add, \
&__Autoarr_##TYPE##_get, \
&__Autoarr_##TYPE##_getPtr, \
&__Autoarr_##TYPE##_set, \
&__Autoarr_##TYPE##_toArray \
}; \
\
Autoarr_##type* __Autoarr_##type##_create(u16 max_chunks_count, u16 max_chunk_length){ \
Autoarr_##type* ar=malloc(sizeof(Autoarr_##type)); \
*ar=(Autoarr_##type){ \
.max_chunks_count=max_chunks_count, \
.chunks_count=0, \
.max_chunk_length=max_chunk_length, \
.chunk_length=0, \
.chunks=NULL, \
.functions=&__Autoarr_##type##_functions_list \
}; \
return ar; \
}
void __Autoarr_##TYPE##_construct(Autoarr_##TYPE* ar, alloc_size_t starting_size, allocator_ptr data_allocator){ \
InternalAllocator_setExternalOrConstruct(ar, data_allocator, LinearAllocator, starting_size); \
ar->functions=&__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
}

View File

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

View File

@ -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;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;
}
@ -38,7 +38,7 @@ void Hashtable_expand(Hashtable* ht){
Autoarr(KVPair)** newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KVPair)*));
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++){
Autoarr(KVPair)* ar=ht->rows[i];

View File

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

View File

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

View File

@ -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 \
}; \

View File

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

View File

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

View File

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

View File

@ -18,7 +18,8 @@ STRUCT(CstdAllocator,
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
#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)); \
}

View File

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

View File

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

View File

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

View File

@ -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);
}

View File

@ -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);
}

View File

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

View File

@ -2,10 +2,11 @@
#include "../src/Autoarr/Autoarr.h"
#include <vector>
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<i64> vec=std::vector<i64>();
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);
);
}

View File

@ -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;i<Autoarr_max_length(ar);i++)
for (u16 i=0; ARR_SZ; i++)
Autoarr_add(ar,i);
}
static void resetar(Autoarr(u16)* ar){
for (u16 i=0;i<Autoarr_max_length(ar);i++)
Autoarr_set(ar,i,Autoarr_max_length(ar)-i-1);
for (u16 i=0; ARR_SZ; i++)
Autoarr_set(ar,i,ARR_SZ-i-1);
}
static void printallval(Autoarr(u16)* ar){
kprintf("\e[90m");
for (u16 i=0;i<Autoarr_length(ar);i++)
for (u16 i=0; i<Autoarr_length(ar); i++)
kprintf("%u ",Autoarr_get(ar,i));
kprintf("\n");
}
void test_autoarr(){
optime("test_autoarr",1,
// optime("test_autoarr",1,
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");
fillar(ar);
fillar(&ar);
kprintf("\e[92mautoarr filled up\n");
printautoarr(ar);
printallval(ar);
resetar(ar);
printautoarr(&ar);
printallval(&ar);
resetar(&ar);
kprintf("\e[92mautoarr values reset\n");
printallval(ar);
Autoarr_destruct(ar, true);
printallval(&ar);
Autoarr_destruct(&ar);
kprintf("\e[92mautoarr deleted\n");
);
// );
}

View File

@ -16,7 +16,9 @@ char data[]="iojihi2ojo8la14nhvi3311pi[jiugbja38mo0ih6gfty88tryf-drh0lanvj03";
h=hashf(h, data, sizeof(data)); \
); \
/*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_construct(&rng_state, random_seedFromTime()); \
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); \
); \
Autoarr_destruct(hashes, true); \
Autoarr_destruct(hashes); \
kprintf("\e[96m--------------------------------------\n"); \
}

View File

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

View File

@ -1,13 +1,10 @@
#include "../src/Hashtable/KeyValuePair.h"
EXPORT void CALL test_marshalling(char* text, KVPair** kptr){
CstdAllocator _al;
allocator_ptr al=(allocator_ptr)&_al;
CstdAllocator_construct(&_al);
KVPair* k=allocator_alloc(al, sizeof(KVPair));
KVPair* k=allocator_alloc(CstdAllocator_instPtr, sizeof(KVPair));
k->key="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;
}

View File

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

View File

@ -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);
}
}

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
}