From d62405ccff6e002ecfa4db6e15787a7c6927608a Mon Sep 17 00:00:00 2001 From: timerix Date: Thu, 25 Aug 2022 01:37:14 +0600 Subject: [PATCH] registred all types --- src/Autoarr/Autoarr.c | 30 +++--- src/Autoarr/Autoarr.h | 12 ++- src/Autoarr/Autoarr_KVPair_exported.c | 2 +- src/Autoarr/Autoarr_Unitype_exported.c | 2 +- src/Autoarr/Autoarr_declare.h | 13 +-- src/Autoarr/Autoarr_define.h | 7 +- src/DtsodParser/DtsodV24_deserialize.c | 2 +- src/Hashtable/Hashtable.c | 10 +- src/Hashtable/Hashtable.h | 1 + src/Hashtable/KeyValuePair.c | 8 +- src/Hashtable/KeyValuePair.h | 8 +- src/SearchTree/SearchTree.c | 7 +- src/SearchTree/SearchTree.h | 1 + src/String/StringBuilder.c | 10 +- src/String/StringBuilder.h | 3 + src/base/errors.h | 2 +- src/base/init.c | 131 +++++++++++++++---------- src/base/init.h | 4 +- src/base/std.h | 4 +- src/base/types.c | 4 +- src/base/types.c.old | 113 --------------------- src/base/types.h | 3 +- src/base/unitype.c | 33 ++++++- src/base/unitype.h | 4 +- 24 files changed, 201 insertions(+), 213 deletions(-) delete mode 100644 src/base/types.c.old diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index 48d595a..8dde7ab 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -1,19 +1,23 @@ #include "Autoarr.h" -Autoarr_define(uint8); -Autoarr_define(int8); -Autoarr_define(uint16); -Autoarr_define(int16); -Autoarr_define(uint32); -Autoarr_define(int32); -Autoarr_define(uint64); -Autoarr_define(int64); -Autoarr_define(float); -Autoarr_define(double); -Autoarr_define(Unitype); +Autoarr_define(char) +Autoarr_define(bool) +Autoarr_define(float32) +Autoarr_define(float64) +Autoarr_define(uint8) +Autoarr_define(int8) +Autoarr_define(uint16) +Autoarr_define(int16) +Autoarr_define(uint32) +Autoarr_define(int32) +Autoarr_define(uint64) +Autoarr_define(int64) + +Autoarr_define(Unitype) // right func to clear array of unitype values -void Autoarr_free_Unitype(Autoarr(Unitype)* ar){ +void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){ Autoarr_foreach(ar, u,Unitype_free(u)); - Autoarr_free(ar); + if(freePtr) Autoarr_free(ar, freePtr); } +void ____Autoarr_free_Unitype_(void* ar) { __Autoarr_free_Unitype_((Autoarr(Unitype)*)ar, false); } \ No newline at end of file diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index 725b174..88459d6 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -7,6 +7,8 @@ extern "C" { #include "Autoarr_declare.h" #include "Autoarr_define.h" +Autoarr_declare(char) +Autoarr_declare(bool) Autoarr_declare(float32) Autoarr_declare(float64) Autoarr_declare(int8) @@ -17,7 +19,6 @@ Autoarr_declare(int32) Autoarr_declare(uint32) Autoarr_declare(int64) Autoarr_declare(uint64) -Autoarr_declare(Unitype) kerepType_declare(AutoarrChar); kerepType_declare(AutoarrBool); @@ -45,8 +46,13 @@ kerepType_declare(AutoarrUInt32Ptr); kerepType_declare(AutoarrInt64Ptr); kerepType_declare(AutoarrUInt64Ptr); -// right func to clear array of unitype values -void Autoarr_free_Unitype(Autoarr(Unitype)* ar); +Autoarr_declare(Unitype) +kerepType_declare(AutoarrUnitype); +kerepType_declare(AutoarrUnitypePtr); + +// this function is injected in kerep_init() +void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr); +void ____Autoarr_free_Unitype_(void* ar); #define Autoarr_foreach(ar,elem,codeblock)({\ if(ar->blocks_count>0) {\ diff --git a/src/Autoarr/Autoarr_KVPair_exported.c b/src/Autoarr/Autoarr_KVPair_exported.c index 18fa4aa..8bfbd56 100644 --- a/src/Autoarr/Autoarr_KVPair_exported.c +++ b/src/Autoarr/Autoarr_KVPair_exported.c @@ -10,7 +10,7 @@ EXPORT void CALL kerep_Autoarr_KVPair_create(uint16 max_blocks_count, uint16 max } EXPORT void CALL kerep_Autoarr_KVPair_free(Autoarr_KVPair* ar){ - Autoarr_free_KVPair(ar); + Autoarr_free(ar, true); } EXPORT void CALL kerep_Autoarr_KVPair_get(Autoarr_KVPair* ar, uint32 index, KVPair* output){ diff --git a/src/Autoarr/Autoarr_Unitype_exported.c b/src/Autoarr/Autoarr_Unitype_exported.c index 8ad5efd..2aded2e 100644 --- a/src/Autoarr/Autoarr_Unitype_exported.c +++ b/src/Autoarr/Autoarr_Unitype_exported.c @@ -9,7 +9,7 @@ EXPORT void CALL kerep_Autoarr_Unitype_create(uint16 max_blocks_count, uint16 ma } EXPORT void CALL kerep_Autoarr_Unitype_free(Autoarr_Unitype* ar){ - Autoarr_free_Unitype(ar); + Autoarr_free(ar, true); } EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, uint32 index, Unitype* output){ diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h index 9b15bd7..c9434a3 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -15,7 +15,7 @@ typedef struct {\ type (*get)(struct Autoarr_##type* ar, uint32 index);\ type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\ void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\ - void (*_free)(struct Autoarr_##type* ar);\ + void (*freear)(struct Autoarr_##type* ar, bool freePtr);\ type* (*toArray)(struct Autoarr_##type* ar);\ } __functions_list_t_##type;\ \ @@ -28,10 +28,13 @@ typedef struct Autoarr_##type{\ __functions_list_t_##type* functions;\ } Autoarr_##type;\ \ -Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length); +Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);\ +void ____Autoarr_free_##type(void* ar); #define Autoarr(type) Autoarr_##type +#define Autoarr_create(type, max_blocks_count, max_block_length)\ + __Autoarr_create_##type(max_blocks_count, max_block_length) #define Autoarr_add(autoarr, element)\ autoarr->functions->add(autoarr, element) #define Autoarr_get(autoarr, index)\ @@ -40,10 +43,8 @@ Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_bloc autoarr->functions->getptr(autoarr,index) #define Autoarr_set(autoarr, index, element)\ autoarr->functions->set(autoarr, index, element) -#define Autoarr_free(autoarr)\ - autoarr->functions->_free(autoarr) -#define Autoarr_create(type, max_blocks_count, max_block_length)\ - __Autoarr_create_##type(max_blocks_count, max_block_length) +#define Autoarr_free(autoarr, freePtr)\ + autoarr->functions->freear(autoarr, freePtr) #define Autoarr_toArray(autoarr)\ autoarr->functions->toArray(autoarr) diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h index ffc50e1..b70ad86 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -39,11 +39,14 @@ void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element){\ ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\ }\ \ -void __Autoarr_free_##type(Autoarr_##type* ar){\ +void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){\ for(uint16 i=0; iblocks_count;i++)\ free(ar->values[i]); \ free(ar->values);\ - free(ar);\ + if(freePtr) free(ar);\ +}\ +void ____Autoarr_free_##type(void* ar){\ + __Autoarr_free_##type((Autoarr_##type*)ar, false);\ }\ \ type* __Autoarr_toArray_##type(Autoarr_##type* ar){\ diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index f8dd4d0..6a9862b 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -154,7 +154,7 @@ Maybe __ReadList(DeserializeSharedData* shared){ Autoarr(Unitype)* list=Autoarr_create(Unitype,ARR_BC,ARR_BL); bool readingList=true; while (true){ - try(ReadValue((&readingList)), val, Autoarr_free_Unitype(list)) + try(ReadValue((&readingList)), val, Autoarr_free(list, true)) Autoarr_add(list,val.value); if (!readingList){ if(val.value.type==Null) diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index 992ef5e..9a914c4 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -17,10 +17,14 @@ Hashtable* Hashtable_create(){ return ht; } -void Hashtable_free(Hashtable* ht){ +void __Hashtable_free(void* _ht){ + Hashtable* ht=_ht; for(uint16 i=0;ihein];i++) - Autoarr_free_KVPair(ht->rows[i]); + Autoarr_free(ht->rows[i], true); free(ht->rows); +} +void Hashtable_free(Hashtable* ht){ + __Hashtable_free(ht); free(ht); } @@ -43,7 +47,7 @@ void Hashtable_expand(Hashtable* ht){ Autoarr(KVPair)* newar=newrows[newrown]; Autoarr_add(newar,p); } - Autoarr_free(ar); + Autoarr_free(ar, true); } free(ht->rows); diff --git a/src/Hashtable/Hashtable.h b/src/Hashtable/Hashtable.h index ef8b651..5bf4a81 100644 --- a/src/Hashtable/Hashtable.h +++ b/src/Hashtable/Hashtable.h @@ -16,6 +16,7 @@ kerepType_declare(HashtablePtr); Hashtable* Hashtable_create(); void Hashtable_free(Hashtable* ht); +void __Hashtable_free(void* ht); // amount of rows uint16 Hashtable_height(Hashtable* ht); diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index cbe22dd..93ad54a 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -8,11 +8,15 @@ void KVPair_free(KVPair p){ free(p.key); Unitype_free(p.value); } +void __KVPair_free(void* p){ KVPair_free(*(KVPair*)p); } // func for KVP array clearing -void Autoarr_free_KVPair(Autoarr_KVPair* ar){ +void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr){ Autoarr_foreach(ar,k,KVPair_free(k)); - Autoarr_free(ar); + Autoarr_free(ar, freePtr); +} +void ____Autoarr_free_KVPair_(void* ar){ + __Autoarr_free_KVPair_((Autoarr_KVPair*)ar, false); } void printkvp(KVPair p){ diff --git a/src/Hashtable/KeyValuePair.h b/src/Hashtable/KeyValuePair.h index 19cc277..48e606f 100644 --- a/src/Hashtable/KeyValuePair.h +++ b/src/Hashtable/KeyValuePair.h @@ -11,14 +11,20 @@ typedef struct KVPair{ char* key; Unitype value; } KVPair; +kerepType_declare(KVPair); +kerepType_declare(KVPairPtr); Autoarr_declare(KVPair) +kerepType_declare(AutoarrKVPair); +kerepType_declare(AutoarrKVPairPtr); // proper way to clear a KVP void KVPair_free(KVPair p); +void __KVPair_free(void* p); // func to clear KVP array -void Autoarr_free_KVPair(Autoarr_KVPair* ar); +void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr); +void ____Autoarr_free_KVPair_(void* ar); void printkvp(KVPair p); diff --git a/src/SearchTree/SearchTree.c b/src/SearchTree/SearchTree.c index e7cfac6..0c95125 100644 --- a/src/SearchTree/SearchTree.c +++ b/src/SearchTree/SearchTree.c @@ -8,7 +8,8 @@ STNode* STNode_create(){ return node; } -void STNode_free(STNode* node){ +void __STNode_free(void* _node){ + STNode* node=_node; if (!node) throw(ERR_NULLPTR); if(node->branches){ for(uint8 n32 = 0;n32<8;n32++){ @@ -34,6 +35,10 @@ void STNode_free(STNode* node){ Unitype_free(node->value); free(node); } +void STNode_free(STNode* node){ + __STNode_free(node); + free(node); +} typedef struct {uint8 n32, n4, rem;} indexes3; diff --git a/src/SearchTree/SearchTree.h b/src/SearchTree/SearchTree.h index b02cbe0..c0f1c3f 100644 --- a/src/SearchTree/SearchTree.h +++ b/src/SearchTree/SearchTree.h @@ -16,6 +16,7 @@ kerepType_declare(STNodePtr); STNode* STNode_create(); void STNode_free(STNode* node); +void __STNode_free(void* node); void ST_push(STNode* node, char* key, Unitype value); void ST_pushString(STNode* node, string key, Unitype value); diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c index c0b46a9..652c9b8 100644 --- a/src/String/StringBuilder.c +++ b/src/String/StringBuilder.c @@ -17,7 +17,7 @@ void complete_buf(StringBuilder* b){ str.ptr[i++]=c; })); Autoarr_add(b->compl_bufs,str); - Autoarr_free(b->curr_buf); + Autoarr_free(b->curr_buf, true); b->curr_buf=Autoarr_create(int8,BL_C,BL_L); } @@ -34,9 +34,13 @@ StringBuilder* StringBuilder_create(){ return b; } +void __StringBuilder_free(void* _b){ + StringBuilder* b=_b; + if(b->compl_bufs) Autoarr_free(b->compl_bufs, true); + Autoarr_free(b->curr_buf, true); +} void StringBuilder_free(StringBuilder* b){ - if(b->compl_bufs) Autoarr_free(b->compl_bufs); - Autoarr_free(b->curr_buf); + __StringBuilder_free(b); free(b); } diff --git a/src/String/StringBuilder.h b/src/String/StringBuilder.h index 8e19420..0fe1696 100644 --- a/src/String/StringBuilder.h +++ b/src/String/StringBuilder.h @@ -13,9 +13,12 @@ typedef struct StringBuilder{ Autoarr(string)* compl_bufs; Autoarr(int8)* curr_buf; } StringBuilder; +kerepType_declare(StringBuilder); +kerepType_declare(StringBuilderPtr); StringBuilder* StringBuilder_create(void); void StringBuilder_free(StringBuilder* b); +void __StringBuilder_free(void* b); // Joins all strings from compl_bufs. // Returns zero-terminated string. // No need to call string_extract()! diff --git a/src/base/errors.h b/src/base/errors.h index de7c713..4109782 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -5,7 +5,7 @@ extern "C" { #endif #include "std.h" -#include "types.h" +#include "unitype.h" typedef enum ErrorId { SUCCESS, // not an error diff --git a/src/base/init.c b/src/base/init.c index f764aad..73073ab 100644 --- a/src/base/init.c +++ b/src/base/init.c @@ -1,66 +1,95 @@ #include "base.h" +#include "../Autoarr/Autoarr.h" +#include "../SearchTree/SearchTree.h" +#include "../Hashtable/Hashtable.h" +#include "../String/StringBuilder.h" -void kerepInit(){ +void kerepTypeDescriptors_initKerepTypes(){ + // null kerepType_register(NULL, Null, NULL); - - kerepType_register(char, Char, NULL); - kerepType_register(bool, Bool, NULL); + // base types + kerepType_register(char, Char, NULL); + kerepType_register(bool, Bool, NULL); kerepType_register(float32, Float32, NULL); kerepType_register(float64, Float64, NULL); - kerepType_register(int8, Int8, NULL); - kerepType_register(uint8, UInt8, NULL); - kerepType_register(int16, Int16, NULL); - kerepType_register(uint16, UInt16, NULL); - kerepType_register(int32, Int32, NULL); - kerepType_register(uint32, UInt32, NULL); - kerepType_register(int64, Int64, NULL); - kerepType_register(uint64, UInt64, NULL); - - kerepType_register(char*, CharPtr, NULL); - kerepType_register(bool*, BoolPtr, NULL); + kerepType_register(int8, Int8, NULL); + kerepType_register(uint8, UInt8, NULL); + kerepType_register(int16, Int16, NULL); + kerepType_register(uint16, UInt16, NULL); + kerepType_register(int32, Int32, NULL); + kerepType_register(uint32, UInt32, NULL); + kerepType_register(int64, Int64, NULL); + kerepType_register(uint64, UInt64, NULL); + // base type pointers + kerepType_register(char*, CharPtr, NULL); + kerepType_register(bool*, BoolPtr, NULL); kerepType_register(float32*, Float32Ptr, NULL); kerepType_register(float64*, Float64Ptr, NULL); - kerepType_register(int8*, Int8Ptr, NULL); - kerepType_register(uint8*, UInt8Ptr, NULL); - kerepType_register(int16*, Int16Ptr, NULL); - kerepType_register(uint16*, UInt16Ptr, NULL); - kerepType_register(int32*, Int32Ptr, NULL); - kerepType_register(uint32*, UInt32Ptr, NULL); - kerepType_register(int64*, Int64Ptr, NULL); - kerepType_register(uint64*, UInt64Ptr, NULL); + kerepType_register(int8*, Int8Ptr, NULL); + kerepType_register(uint8*, UInt8Ptr, NULL); + kerepType_register(int16*, Int16Ptr, NULL); + kerepType_register(uint16*, UInt16Ptr, NULL); + kerepType_register(int32*, Int32Ptr, NULL); + kerepType_register(uint32*, UInt32Ptr, NULL); + kerepType_register(int64*, Int64Ptr, NULL); + kerepType_register(uint64*, UInt64Ptr, NULL); + // base type autoarrs + kerepType_register(Autoarr_char, AutoarrChar, ____Autoarr_free_char); + kerepType_register(Autoarr_bool, AutoarrBool, ____Autoarr_free_bool); + kerepType_register(Autoarr_float32, AutoarrFloat32, ____Autoarr_free_float32); + kerepType_register(Autoarr_float64, AutoarrFloat64, ____Autoarr_free_float64); + kerepType_register(Autoarr_int8, AutoarrInt8, ____Autoarr_free_int8); + kerepType_register(Autoarr_uint8, AutoarrUInt8, ____Autoarr_free_uint8); + kerepType_register(Autoarr_int16, AutoarrInt16, ____Autoarr_free_int16); + kerepType_register(Autoarr_uint16, AutoarrUInt16, ____Autoarr_free_uint16); + kerepType_register(Autoarr_int32, AutoarrInt32, ____Autoarr_free_int32); + kerepType_register(Autoarr_uint32, AutoarrUInt32, ____Autoarr_free_uint32); + kerepType_register(Autoarr_int64, AutoarrInt64, ____Autoarr_free_int64); + kerepType_register(Autoarr_uint64, AutoarrUInt64, ____Autoarr_free_uint64); + // base type autoarr pointers + kerepType_register(Autoarr_char*, AutoarrCharPtr, ____Autoarr_free_char); + kerepType_register(Autoarr_bool*, AutoarrBoolPtr, ____Autoarr_free_bool); + kerepType_register(Autoarr_float32*, AutoarrFloat32Ptr, ____Autoarr_free_float32); + kerepType_register(Autoarr_float64*, AutoarrFloat64Ptr, ____Autoarr_free_float64); + kerepType_register(Autoarr_int8*, AutoarrInt8Ptr, ____Autoarr_free_int8); + kerepType_register(Autoarr_uint8*, AutoarrUInt8Ptr, ____Autoarr_free_uint8); + kerepType_register(Autoarr_int16*, AutoarrInt16Ptr, ____Autoarr_free_int16); + kerepType_register(Autoarr_uint16*, AutoarrUInt16Ptr, ____Autoarr_free_uint16); + kerepType_register(Autoarr_int32*, AutoarrInt32Ptr, ____Autoarr_free_int32); + kerepType_register(Autoarr_uint32*, AutoarrUInt32Ptr, ____Autoarr_free_uint32); + kerepType_register(Autoarr_int64*, AutoarrInt64Ptr, ____Autoarr_free_int64); + kerepType_register(Autoarr_uint64*, AutoarrUInt64Ptr, ____Autoarr_free_uint64); + + // Unitype kerepType_register(Unitype, Unitype, __UnitypePtr_free); kerepType_register(Unitype*, UnitypePtr, __UnitypePtr_free); + kerepType_register(Autoarr_Unitype, AutoarrUnitype, ____Autoarr_free_Unitype_); + kerepType_register(Autoarr_Unitype*, AutoarrUnitypePtr, ____Autoarr_free_Unitype_); + // replacing autogenerated freear() function to custom + Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1); + _uar->functions->freear=__Autoarr_free_Unitype_; + Autoarr_free(_uar, true); - kerepType_register(, AutoarrChar, Autoarr_free); - kerepType_register(, AutoarrBool, Autoarr_free); - kerepType_register(, AutoarrFloat32, Autoarr_free); - kerepType_register(, AutoarrFloat64, Autoarr_free); - kerepType_register(, AutoarrInt8, Autoarr_free); - kerepType_register(, AutoarrUInt8, Autoarr_free); - kerepType_register(, AutoarrInt16, Autoarr_free); - kerepType_register(, AutoarrUInt16, Autoarr_free); - kerepType_register(, AutoarrInt32, Autoarr_free); - kerepType_register(, AutoarrUInt32, Autoarr_free); - kerepType_register(, AutoarrInt64, Autoarr_free); - kerepType_register(, AutoarrUInt64, Autoarr_free); + // SearchTreeNode + kerepType_register(STNode, STNode, __STNode_free); + kerepType_register(STNode*, STNodePtr, __STNode_free); - kerepType_register(, AutoarrCharPtr, Autoarr_free); - kerepType_register(, AutoarrBoolPtr, Autoarr_free); - kerepType_register(, AutoarrFloat32Ptr, Autoarr_free); - kerepType_register(, AutoarrFloat64Ptr, Autoarr_free); - kerepType_register(, AutoarrInt8Ptr, Autoarr_free); - kerepType_register(, AutoarrUInt8Ptr, Autoarr_free); - kerepType_register(, AutoarrInt16Ptr, Autoarr_free); - kerepType_register(, AutoarrUInt16Ptr, Autoarr_free); - kerepType_register(, AutoarrInt32Ptr, Autoarr_free); - kerepType_register(, AutoarrUInt32Ptr, Autoarr_free); - kerepType_register(, AutoarrInt64Ptr, Autoarr_free); - kerepType_register(, AutoarrUInt64Ptr, Autoarr_free); + // KeyValuePair + kerepType_register(KVPair, KVPair, __KVPair_free); + kerepType_register(KVPair*, KVPairPtr, __KVPair_free); + kerepType_register(Autoarr_KVPair, AutoarrKVPair, ____Autoarr_free_KVPair_); + kerepType_register(Autoarr_KVPair*, AutoarrKVPairPtr, ____Autoarr_free_KVPair_); + // replacing autogenerated freear() function to custom + Autoarr_KVPair* _kvpar=Autoarr_create(KVPair, 1, 1); + _kvpar->functions->freear=__Autoarr_free_KVPair_; + Autoarr_free(_kvpar, true); - kerepType_register(, STNode, STNode_free); - kerepType_register(, STNodePtr, STNode_free); + // Hashtable + kerepType_register(Hashtable, Hashtable, __Hashtable_free); + kerepType_register(Hashtable*, HashtablePtr, __Hashtable_free); - kerepType_register(, Hashtable, Hashtable_free); - kerepType_register(, HashtablePtr, Hashtable_free); + // StringBuilder + kerepType_register(StringBuilder, StringBuilder, __StringBuilder_free); + kerepType_register(StringBuilder*, StringBuilderPtr, __StringBuilder_free); } diff --git a/src/base/init.h b/src/base/init.h index 0faa5f2..dfe5e69 100644 --- a/src/base/init.h +++ b/src/base/init.h @@ -4,8 +4,8 @@ extern "C" { #endif -// call this between kerepTypesInitBegin() and kerepTypesInitEnd() -void kerepInit(); +// call this between kerepTypeDescriptors_beginInit() and kerepTypeDescriptors_endInit() +void kerepTypeDescriptors_initKerepTypes(); #if __cplusplus } diff --git a/src/base/std.h b/src/base/std.h index baf6a86..de0b9e9 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -44,7 +44,6 @@ typedef double float64; #pragma GCC error "unknown compiler" #endif - #ifdef _MSC_VER #define IFWIN(YES, NO) YES #define IFMSC(YES, NO) YES @@ -58,6 +57,9 @@ typedef double float64; #pragma GCC error "unknown compiler" #endif +#ifndef sprintf_s + #define sprintf_s(BUF, BUFSIZE, FORMAT, ...) sprintf(BUF, FORMAT, ## __VA_ARGS__) +#endif #if __cplusplus } diff --git a/src/base/types.c b/src/base/types.c index 2f0e1a7..1c7a055 100644 --- a/src/base/types.c +++ b/src/base/types.c @@ -22,7 +22,7 @@ void kerepTypeDescriptors_beginInit(){ void kerepTypeDescriptors_endInit(){ typeDescriptors=Autoarr_toArray(__kerepTypeDescriptors); - Autoarr_free(__kerepTypeDescriptors); + Autoarr_free(__kerepTypeDescriptors,true); if(typeDescriptors==NULL) throw(ERR_NULLPTR); } void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){ @@ -35,6 +35,6 @@ void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){ Autoarr_add(__kerepTypeDescriptors, typeDesc); } -kerepTypeDescriptor typeDescriptor_get(kerepTypeId id){ +kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id){ return typeDescriptors[id]; } diff --git a/src/base/types.c.old b/src/base/types.c.old deleted file mode 100644 index 3c6ef1e..0000000 --- a/src/base/types.c.old +++ /dev/null @@ -1,113 +0,0 @@ -#include "types.h" -#include "errors.h" -#include "../Autoarr/Autoarr.h" -#include "../Hashtable/Hashtable.h" -#include "../SearchTree/SearchTree.h" - -// frees VoidPtr value or does nothing if type isn't pointer -void Unitype_free(Unitype u){ - switch (u.type) { - case Null: - case Float32: - case Float64: - case Char: - case Bool: - case Int8: - case UInt8: - case Int16: - case UInt16: - case Int32: - case UInt32: - case Int64: - case UInt64: - break; - case Int8Ptr: - case UInt8Ptr: - case Int16Ptr: - case UInt16Ptr: - case Int32Ptr: - case UInt32Ptr: - case Int64Ptr: - case UInt64Ptr: - case CharPtr: - free(u.VoidPtr); - break; - case HashtablePtr: - Hashtable_free(u.VoidPtr); - break; - case STNodePtr: - STNode_free(u.VoidPtr); - break; - case AutoarrInt8Ptr: - __Autoarr_free_int8(u.VoidPtr); - break; - case AutoarrUInt8Ptr: - __Autoarr_free_uint8(u.VoidPtr); - break; - case AutoarrInt16Ptr: - __Autoarr_free_int16(u.VoidPtr); - break; - case AutoarrUInt16Ptr: - __Autoarr_free_uint16(u.VoidPtr); - break; - case AutoarrInt32Ptr: - __Autoarr_free_int32(u.VoidPtr); - break; - case AutoarrUInt32Ptr: - __Autoarr_free_uint32(u.VoidPtr); - break; - case AutoarrInt64Ptr: - __Autoarr_free_int64(u.VoidPtr); - break; - case AutoarrUInt64Ptr: - __Autoarr_free_uint64(u.VoidPtr); - break; - case AutoarrUnitypePtr: - Autoarr_free_Unitype(u.VoidPtr); - break; - case AutoarrKVPairPtr: - Autoarr_free_KVPair(u.VoidPtr); - break; - default: throw(ERR_WRONGTYPE); - } -} - -#define BUFSIZE 64 -char* sprintuni(Unitype v){ - char* buf=malloc(BUFSIZE); - IFMSC( - switch (v.type) { - case Null: sprintf_s(buf, BUFSIZE, "{Null}");break; - case Float64: sprintf_s(buf, BUFSIZE, "{%s : %lf}", my_type_name(v.type),v.Float64);break; - case Bool: - case UInt64: sprintf_s(buf, BUFSIZE, "{%s : %lu}", my_type_name(v.type),v.UInt64);break; - case Int64: sprintf_s(buf, BUFSIZE, "{%s : %ld}", my_type_name(v.type),v.Int64);break; - case CharPtr: ; - size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; - buf=realloc(buf, newBUFSIZE); - sprintf_s(buf, newBUFSIZE, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr); - break; - default: sprintf_s(buf, BUFSIZE, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break; - }, - switch (v.type) { - case Null: sprintf(buf, "{Null}"); break; - case Float64: sprintf(buf, "{%s : %lf}", my_type_name(v.type),v.Float64); break; - case Bool: - case UInt64: sprintf(buf, "{%s : " IFWIN("%llu", "%lu") "}", my_type_name(v.type),v.UInt64); break; - case Int64: sprintf(buf, "{%s : " IFWIN("%lld", "%ld") "}", my_type_name(v.type),v.Int64); break; - case CharPtr: ; - size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; - buf=realloc(buf, newBUFSIZE); - sprintf(buf, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr); - break; - default: sprintf(buf, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break; - } - ); - return buf; -} - -void printuni(Unitype v){ - char* s=sprintuni(v); - fputs(s, stdout); - free(s); -} \ No newline at end of file diff --git a/src/base/types.h b/src/base/types.h index 2722e17..2e58287 100644 --- a/src/base/types.h +++ b/src/base/types.h @@ -7,6 +7,7 @@ extern "C" { #include "std.h" typedef uint16 kerepTypeId; + typedef struct kerepTypeDescriptor{ void (*free_members)(void*); // NULL or function which frees all struct members char* name; @@ -26,7 +27,7 @@ void __kerepType_register(char* name, int16 size, void (*free_members)(void*)); void kerepTypeDescriptors_beginInit(); void kerepTypeDescriptors_endInit(); -kerepTypeDescriptor typeDescriptor_get(kerepTypeId id); +kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id); kerepType_declare(Null); diff --git a/src/base/unitype.c b/src/base/unitype.c index a2feb13..1f18751 100644 --- a/src/base/unitype.c +++ b/src/base/unitype.c @@ -1,10 +1,37 @@ -#include "unitype.h" +#include "base.h" void Unitype_free(Unitype u){ - kerepTypeDescriptor type=typeDescriptor_get(u.typeId); + kerepTypeDescriptor type=kerepTypeDescriptor_get(u.typeId); if(type.free_members) type.free_members(u.VoidPtr); if(u.allocatedInHeap) free(u.VoidPtr); } -void __UnitypePtr_free(Unitype* u) { Unitype_free(&u); } \ No newline at end of file +void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); } + +#define BUFSIZE 64 +char* sprintuni(Unitype v){ + char* buf=malloc(BUFSIZE); + kerepTypeDescriptor type=kerepTypeDescriptor_get(v.typeId); + if(v.typeId==kerepTypeId_Null) + sprintf_s(buf, BUFSIZE, "{Null}"); + else if(v.typeId==kerepTypeId_Float64) + sprintf_s(buf, BUFSIZE, "{%s ) %lf}", type.name,v.Float64); + else if(v.typeId==kerepTypeId_Bool || v.typeId==kerepTypeId_UInt64) + sprintf_s(buf, BUFSIZE, "{%s ) " IFWIN("%llu", "%lu") "}", type.name,v.UInt64); + else if(v.typeId==kerepTypeId_Int64) + sprintf_s(buf, BUFSIZE, "{%s ) " IFWIN("%lld", "%ld") "}", type.name,v.Int64); + else if(v.typeId==kerepTypeId_CharPtr){ + size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; + buf=realloc(buf, newBUFSIZE); + sprintf_s(buf, BUFSIZE, "{%s ) \"%s\"}", type.name,(char*)v.VoidPtr); + } + else sprintf_s(buf, BUFSIZE, "{%s ) %p}", type.name,v.VoidPtr); + return buf; +} + +void printuni(Unitype v){ + char* s=sprintuni(v); + fputs(s, stdout); + free(s); +} \ No newline at end of file diff --git a/src/base/unitype.h b/src/base/unitype.h index 65c953f..0d28ed8 100644 --- a/src/base/unitype.h +++ b/src/base/unitype.h @@ -23,7 +23,7 @@ kerepType_declare(UnitypePtr); #define __Uni(TYPE,VAL) (Unitype){\ - .TYPE_NAME=VAL, .type=kerepTypeId_##TYPE, .allocatedInHeap=false} + .TYPE_NAME=VAL, .typeId=kerepTypeId_##TYPE, .allocatedInHeap=false} #define UniInt64(VAL) __Uni(Int64, VAL) #define UniUInt64(VAL) __Uni(UInt64, VAL) @@ -41,7 +41,7 @@ kerepType_declare(UnitypePtr); // frees VoidPtr value or does nothing if type isn't pointer void Unitype_free(Unitype u); -void __UnitypePtr_free(Unitype* u); +void __UnitypePtr_free(void* u); void printuni(Unitype v); char* sprintuni(Unitype v);