segfault fixed

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

View File

@@ -1,12 +1,16 @@
#include "Autoarr.h"
Autoarr_define(Unitype);
Autoarr_define(Unitype)
// right func to clear array of unitype values
void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){
u32 free_calls=0;
// right func to clean array of unitype values
void __Autoarr_Unitype_free_fixed(Autoarr(Unitype)* ar, bool freePtr){
Autoarr_foreach(ar, u,Unitype_free(u));
__Autoarr_free_Unitype(ar, freePtr);
__Autoarr_Unitype_free_g(ar, freePtr);
free_calls++;
kprintf("free_calls: %u\n", free_calls);
}
void ____Autoarr_free_Unitype_(void* ar) {
__Autoarr_free_Unitype_((Autoarr(Unitype)*)ar, false);
void ____Autoarr_Unitype_free_fixed(void* ar) {
__Autoarr_Unitype_free_fixed((Autoarr(Unitype)*)ar, false);
}

View File

@@ -10,8 +10,8 @@ extern "C" {
Autoarr_declare(Unitype)
// this function is injected in kerep_init()
void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr);
void ____Autoarr_free_Unitype_(void* ar);
void __Autoarr_Unitype_free_fixed(Autoarr(Unitype)* ar, bool freePtr);
void ____Autoarr_Unitype_free_fixed(void* ar);
#define Autoarr_foreach(ar,elem,codeblock)({ \
if(ar->blocks_count>0) { \

View File

@@ -10,14 +10,16 @@ extern "C" {
\
struct Autoarr_##type; \
\
STRUCT(__functions_list_t_##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); \
type* (*getPtr)(struct Autoarr_##type* ar, u32 index); \
void (*set)(struct Autoarr_##type* ar, u32 index, type element); \
void (*freear)(struct Autoarr_##type* ar, bool freePtr); \
type* (*toArray)(struct Autoarr_##type* ar); \
) \
} __Autoarr_##type##_functions_list_t; \
\
extern __Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list; \
\
STRUCT(Autoarr_##type, \
u16 blocks_count; \
@@ -25,23 +27,23 @@ STRUCT(Autoarr_##type, \
u16 block_length; \
u16 max_block_length; \
type** values; \
__functions_list_t_##type* functions; \
__Autoarr_##type##_functions_list_t* functions; \
) \
\
Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length); \
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr); \
void ____Autoarr_free_##type(void* ar);
Autoarr_##type* __Autoarr_##type##_create(u16 max_blocks_count, u16 max_block_length); \
void __Autoarr_##type##_free_g(Autoarr_##type* ar, bool freePtr); \
void ____Autoarr_##type##_free_g(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)
__Autoarr_##type##_create(max_blocks_count, max_block_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_getPtr(autoarr, index) \
autoarr->functions->getPtr(autoarr,index)
#define Autoarr_set(autoarr, index, element) \
autoarr->functions->set(autoarr, index, element)
#define Autoarr_free(autoarr, freePtr) \

View File

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

View File

@@ -12,13 +12,13 @@ void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){
// checks for dtsod contains value or dont
bool DtsodV24_contains(Hashtable* dtsod, char* key){
Unitype* val=Hashtable_getptr(dtsod, key);
Unitype* val=Hashtable_getPtr(dtsod, key);
return val!=NULL;
}
// replaces value with UniNull if key exists in dtsod
bool DtsodV24_remove(Hashtable* dtsod, char* key){
Unitype* val=Hashtable_getptr(dtsod, key);
Unitype* val=Hashtable_getPtr(dtsod, key);
if (!val) return false;
*val=UniNull;
return true;

View File

@@ -50,7 +50,8 @@ void Hashtable_expand(Hashtable* ht){
Autoarr_add(newar,p);
}
// there is no need to free array values, because they are copied into new array
__Autoarr_free_KVPair(ar, true);
// so dont replace this incorrect auto-generated function
__Autoarr_KVPair_free_g(ar, true);
}
free(ht->rows);
@@ -72,11 +73,11 @@ void Hashtable_add(Hashtable* ht, char* key, Unitype u){
}
// returns null or pointer to value in hashtable
Unitype* Hashtable_getptr(Hashtable* ht, char* key){
Unitype* Hashtable_getPtr(Hashtable* ht, char* key){
Autoarr(KVPair)* ar=getrow(ht,key,false);
u32 arlen=Autoarr_length(ar);
for(u32 i=0;i<arlen;i++){
KVPair* p=Autoarr_getptr(ar,i);
KVPair* p=Autoarr_getPtr(ar,i);
if(cptr_compare(key,p->key)) return &p->value;
}
return NULL;
@@ -99,7 +100,7 @@ bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
}
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){
Unitype* val=Hashtable_getptr(ht, key);
Unitype* val=Hashtable_getPtr(ht, key);
if(val) *val=u;
else Hashtable_add(ht, key, u);
}

View File

@@ -28,7 +28,7 @@ void Hashtable_add(Hashtable* ht, char* key, Unitype u);
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u);
// returns null or pointer to value in hashtable
Unitype* Hashtable_getptr(Hashtable* ht, char* key);
Unitype* Hashtable_getPtr(Hashtable* ht, char* key);
Unitype Hashtable_get(Hashtable* ht, char* key);
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output);

View File

@@ -4,20 +4,20 @@ kt_define(KVPair, __KVPair_free, NULL);
Autoarr_define(KVPair)
// proper way to clear a KVP
// proper way to clean a KVP
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, bool freePtr){
// func for KVP array cleaning
void __Autoarr_KVPair_free_fixed(Autoarr_KVPair* ar, bool freePtr){
Autoarr_foreach(ar,k,KVPair_free(k));
__Autoarr_free_KVPair(ar, freePtr);
__Autoarr_KVPair_free_g(ar, freePtr);
}
void ____Autoarr_free_KVPair_(void* ar){
__Autoarr_free_KVPair_((Autoarr_KVPair*)ar, false);
void ____Autoarr_KVPair_free_fixed(void* ar){
__Autoarr_KVPair_free_fixed((Autoarr_KVPair*)ar, false);
}
void printkvp(KVPair p){

View File

@@ -14,13 +14,13 @@ STRUCT(KVPair,
Autoarr_declare(KVPair)
// proper way to clear a KVP
// proper way to clean a KVP
void KVPair_free(KVPair p);
void __KVPair_free(void* p);
// func to clear KVP array
void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr);
void ____Autoarr_free_KVPair_(void* ar);
// func to clean KVP array
void __Autoarr_KVPair_free_fixed(Autoarr_KVPair* ar, bool freePtr);
void ____Autoarr_KVPair_free_fixed(void* ar);
void printkvp(KVPair p);

View File

@@ -68,7 +68,7 @@ void StringBuilder_rmchar(StringBuilder* b){
Autoarr_pop(b->curr_buf)
else {
if(!b->compl_bufs) throw(ERR_NULLPTR);
string* lastcb=Autoarr_getptr(b->compl_bufs, (Autoarr_length(b->compl_bufs)-1));
string* lastcb=Autoarr_getPtr(b->compl_bufs, (Autoarr_length(b->compl_bufs)-1));
lastcb->length--;
}
}

View File

@@ -3,11 +3,12 @@
#include "../../kprint/kprint_format.h"
// accepts char* (ptr to char) and char** (ptr to string)
// accepts char* (ptr to char) and char* (ptr to string)
// uses format kp_s and kp_c to determine what type is <c> argument
char* __toString_char(void* c, u32 fmt) {
// *c=char*
if(kp_fmt_dataFormat(fmt)==kp_s){
return cptr_copy(*(char**)c); // to avoid segmentation fault on free() when *c allocalet on stack
return cptr_copy((char*)c); // to avoid segmentation fault on free() when *c allocalet on stack
}
// *c=char
if(kp_fmt_dataFormat(fmt)==kp_c){

View File

@@ -6,7 +6,7 @@ extern "C" {
#include "../errors.h"
// accepts char* (ptr to char) and char** (ptr to string)
// accepts char* (ptr to char) and char* (ptr to string)
// uses format kp_s and kp_c to determine what type is <c> argument
char* __toString_char(void* c, u32 fmt);

View File

@@ -63,10 +63,11 @@ void ktDescriptors_initKerepTypes(){
kt_register(Unitype);
kt_register(Array_Unitype);
kt_register(Autoarr_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);
// replacing autogenerated freear() function to custom
// in autoarr functions list
__Autoarr_Unitype_functions_list.freear=__Autoarr_Unitype_free_fixed;
// and in type descriptor
ktDescriptor_Autoarr_Unitype.freeMembers=____Autoarr_Unitype_free_fixed;
// STNode
kt_register(STNode);
@@ -74,10 +75,11 @@ void ktDescriptors_initKerepTypes(){
// KeyValuePair
kt_register(KVPair);
kt_register(Autoarr_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);
// replacing autogenerated freear() function to custom
// in autoarr functions list
__Autoarr_KVPair_functions_list.freear=__Autoarr_KVPair_free_fixed;
// and in type descriptor
ktDescriptor_Autoarr_KVPair.freeMembers=____Autoarr_KVPair_free_fixed;
// Hashtable
kt_register(Hashtable);

View File

@@ -1,90 +1,100 @@
#include "../base.h"
#include "../../kprint/kprint_format.h"
#include "../base.h"
char* __Unitype_toString(void* _u, u32 fmt){
return Unitype_toString(*(Unitype*)_u, fmt);
char *__Unitype_toString(void *_u, u32 fmt)
{
return Unitype_toString(*(Unitype *)_u, fmt);
}
kt_define(Unitype, __UnitypePtr_free, __Unitype_toString);
void Unitype_free(Unitype u){
if(u.typeId==ktid_undefined){
if(u.VoidPtr!=NULL)
void Unitype_free(Unitype u)
{
if (u.typeId == ktid_undefined)
{
if (u.VoidPtr != NULL)
throw("unitype with undefined typeId has value");
return;
}
ktDescriptor* type=ktDescriptor_get(u.typeId);
if(type->freeMembers)
ktDescriptor *type = ktDescriptor_get(u.typeId);
if (type->freeMembers)
type->freeMembers(u.VoidPtr);
if(u.allocatedInHeap)
if (u.allocatedInHeap)
free(u.VoidPtr);
}
void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
void __UnitypePtr_free(void *u)
{
Unitype_free(*(Unitype *)u);
}
char* Unitype_toString(Unitype u, u32 fmt){
if(u.typeId==ktid_undefined){
if(u.VoidPtr!=NULL)
char *Unitype_toString(Unitype u, u32 fmt)
{
if (u.typeId == ktid_undefined)
{
if (u.VoidPtr != NULL)
throw("unitype with undefined typeId has value");
return cptr_copy("{ERROR_TYPE}");
}
if(fmt==0) {
if(u.typeId==ktid_name(bool) ||
u.typeId==ktid_name(i8) || u.typeId==ktid_name(i16) ||
u.typeId==ktid_name(i32) || u.typeId==ktid_name(i64)){
if (fmt == 0)
{
if (u.typeId == ktid_name(bool) || u.typeId == ktid_name(i8) || u.typeId == ktid_name(i16) ||
u.typeId == ktid_name(i32) || u.typeId == ktid_name(i64))
{
// auto format set
fmt=kp_i;
fmt = kp_i;
// replaces value with pointer to value to pass into toString_i64(void*, u32)
i64 value=u.Int64;
u.VoidPtr=&value;
i64 value = u.Int64;
u.VoidPtr = &value;
}
else if(u.typeId==ktid_name(u8) || u.typeId==ktid_name(u16) ||
u.typeId==ktid_name(u32) || u.typeId==ktid_name(u64)){
fmt=kp_u;
u64 value=u.UInt64;
u.VoidPtr=&value;
else if (u.typeId == ktid_name(u8) || u.typeId == ktid_name(u16) || u.typeId == ktid_name(u32) ||
u.typeId == ktid_name(u64))
{
fmt = kp_u;
u64 value = u.UInt64;
u.VoidPtr = &value;
}
else if(u.typeId==ktid_name(f32) || u.typeId==ktid_name(f64)){
fmt=kp_f;
f64 value=u.Float64;
u.VoidPtr=&value;
else if (u.typeId == ktid_name(f32) || u.typeId == ktid_name(f64))
{
fmt = kp_f;
f64 value = u.Float64;
u.VoidPtr = &value;
}
else if(u.typeId==ktid_name(char)){
fmt=kp_c;
i64 value=u.Int64;
u.VoidPtr=&value;
else if (u.typeId == ktid_name(char))
{
fmt = kp_c;
i64 value = u.Int64;
u.VoidPtr = &value;
}
else if(u.typeId==ktid_ptrName(char)){
char* value=u.VoidPtr;
u.VoidPtr=&value;
fmt=kp_s;
else if (u.typeId == ktid_ptrName(char))
{
fmt = kp_s;
}
else if(u.typeId==ktid_name(Pointer)){
if(u.VoidPtr==NULL)
else if (u.typeId == ktid_name(Pointer))
{
if (u.VoidPtr == NULL)
return cptr_copy("{ UniNull }");
fmt=kp_h;
fmt = kp_h;
}
}
ktDescriptor* type=ktDescriptor_get(u.typeId);
char* valuestr;
if(type->toString)
valuestr=type->toString(u.VoidPtr, fmt);
else valuestr="ERR_NO_TOSTRING_FUNC";
char* rezult=cptr_concat("{ type: ", type->name,
", allocated on heap: ", (u.allocatedInHeap ? "true" : "false"),
", value:", valuestr, " }");
if(type->toString)
ktDescriptor *type = ktDescriptor_get(u.typeId);
char *valuestr;
if (type->toString)
valuestr = type->toString(u.VoidPtr, fmt);
else
valuestr = "ERR_NO_TOSTRING_FUNC";
char *rezult = cptr_concat("{ type: ", type->name, ", allocated on heap: ", (u.allocatedInHeap ? "true" : "false"),
", value:", valuestr, " }");
if (type->toString)
free(valuestr);
return rezult;
}
void printuni(Unitype v){
char* s=Unitype_toString(v,0);
void printuni(Unitype v)
{
char *s = Unitype_toString(v, 0);
fputs(s, stdout);
fputc('\n',stdout);
free(s);
}

View File

@@ -19,15 +19,19 @@ ktid __typeFromFormat(kp_fmt f){
case kp_s:
return ktid_ptrName(char);
default:
return -1;
return ktid_undefined;
}
}
Maybe __next_toString(kp_fmt f, __kp_value_union* object){
Maybe __next_toString(kp_fmt f, void* object){
// detecting type
ktid typeId=__typeFromFormat(f);
if(typeId==ktid_undefined)
safethrow("typeId is undefined, can't autodetect type",;);
if(typeId==ktid_ptrName(char))
object=*(char**)object; // dereferencing char** to char*
ktDescriptor* type=ktDescriptor_get(typeId);
if(!type->toString)
safethrow("type descriptor doesnt have toString() func",;);