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

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