free() bugfix and test improvements

This commit is contained in:
Timerix22 2022-02-09 23:42:07 +03:00
parent 6a1eae90d9
commit 7150cda905
6 changed files with 39 additions and 36 deletions

View File

@ -11,7 +11,8 @@ Autoarr Autoarr_create(uint16 _max_block_count, uint16 _max_block_length, my_typ
.curr_length=0, .curr_length=0,
.values=malloc(_max_block_count*typesize(_type)) .values=malloc(_max_block_count*typesize(_type))
}; };
*ar.values=malloc(ar.max_block_length*typesize(ar.type)); *ar.values=malloc(_max_block_length*typesize(ar.type));
printf("%p %p\n",ar.values, *ar.values);
return ar; return ar;
} }
@ -157,37 +158,31 @@ void Autoarr_set_uni(Autoarr *ar, uint32 index, Unitype element){
void Autoarr_clear(Autoarr* ar){ void Autoarr_clear(Autoarr* ar){
switch (ar->type) { switch (ar->type) {
case Int8: case Int8:
for(uint16 i = 0; i < ar->curr_block_count-1;i++) for(uint16 i = 0; i < ar->curr_block_count;i++)
free(*((int8**)ar->values+i)); break; free(*((int8**)ar->values+i)); break;
case UInt8: case UInt8:
for(uint16 i = 0; i < ar->curr_block_count-1;i++) for(uint16 i = 0; i < ar->curr_block_count;i++)
free(*((uint8**)ar->values+i)); break; free(*((uint8**)ar->values+i)); break;
case Int16: case Int16:
for(uint16 i = 0; i < ar->curr_block_count-1;i++) for(uint16 i = 0; i < ar->curr_block_count;i++)
free(*((int16**)ar->values+i)); break; free(*((int16**)ar->values+i)); break;
case UInt16: case UInt16:
for(uint16 i = 0; i < ar->curr_block_count-1;i++) for(uint16 i = 0; i < ar->curr_block_count;i++)
free(*((uint16**)ar->values+i)); break; free(*((uint16**)ar->values+i)); break;
case Int32: case Int32:
for(uint16 i = 0; i < ar->curr_block_count-1;i++) for(uint16 i = 0; i < ar->curr_block_count;i++)
free(*((int32**)ar->values+i)); break; free(*((int32**)ar->values+i)); break;
case UInt32: case UInt32:
for(uint16 i = 0; i < ar->curr_block_count-1;i++) for(uint16 i = 0; i < ar->curr_block_count;i++)
free(*((uint32**)ar->values+i)); break; free(*((uint32**)ar->values+i)); break;
case Int64: case Int64:
for(uint16 i = 0; i < ar->curr_block_count-1;i++) for(uint16 i = 0; i < ar->curr_block_count;i++)
free(*((int64**)ar->values+i)); break; free(*((int64**)ar->values+i)); break;
case UInt64: case UInt64:
for(uint16 i = 0; i < ar->curr_block_count-1;i++) for(uint16 i = 0; i < ar->curr_block_count;i++)
free(*((uint64**)ar->values+i)); break; free(*((uint64**)ar->values+i)); break;
default: throw(ERR_WRONGTYPE); break; default: throw(ERR_WRONGTYPE); break;
} }
free(ar->values); free(ar->values);
ar->type=0; ar->type=Null;
ar->max_block_count=0;
ar->max_block_length=0;
ar->curr_block_count=0;
ar->curr_block_length=0;
ar->max_length=0;
ar->curr_length=0;
} }

View File

@ -11,19 +11,17 @@ STNode* STNode_create(){
void STNode_free(STNode* node){ void STNode_free(STNode* node){
if (!node) throw(ERR_NULLPTR); if (!node) throw(ERR_NULLPTR);
if(node->branches!=NULL){ if(node->branches){
for(uint8 n32 = 0;n32<8;n32++){ for(uint8 n32 = 0;n32<8;n32++){
STNode*** ptrn32=(STNode***)node->branches[n32]; STNode*** ptrn32=(STNode***)node->branches[n32];
if(ptrn32!=NULL){ if(ptrn32){
for(uint8 n4 = 0;n4<8;n4++){ for(uint8 n4 = 0;n4<8;n4++){
STNode** ptrn4=ptrn32[n4]; STNode** ptrn4=ptrn32[n4];
if (ptrn4!=NULL){ if (ptrn4){
for(uint8 rem=0;rem<4;rem++){ for(uint8 rem=0;rem<4;rem++){
STNode* ptrrem=ptrn4[rem]; STNode* ptrrem=ptrn4[rem];
if(ptrrem!=NULL){ if(ptrrem)
STNode_free(ptrrem); STNode_free(ptrrem);
free(ptrrem);
}
} }
free(ptrn4); free(ptrn4);
} }
@ -33,8 +31,9 @@ void STNode_free(STNode* node){
} }
free(node->branches); free(node->branches);
} }
if(node->value.type==UInt8Ptr|Int8Ptr) //if value is not freed ptr
free(node->value.VoidPtr); if(node->value.type>12 && node->value.type<21 && node->value.VoidPtr)
free(node->value.VoidPtr);
free(node); free(node);
} }
@ -52,24 +51,24 @@ uint8 combinei3(indexes3 i3){
return i3.n32*32+i3.n4*8; return i3.n32*32+i3.n4*8;
} }
int16 globytes=0; //int16 globytes=0;
void ST_push(STNode* node_first, const char* key, Unitype value){ void ST_push(STNode* node_first, const char* key, Unitype value){
if (!node_first) throw(ERR_NULLPTR); if (!node_first) throw(ERR_NULLPTR);
int16 bytes=sizeof(Unitype); //int16 bytes=sizeof(Unitype);
char c = *key; char c = *key;
STNode* node_last=node_first; STNode* node_last=node_first;
for (uint16 i=0;c!='\0';){ for (uint16 i=0;c!='\0';){
indexes3 i3=splitindex((uint8)c); indexes3 i3=splitindex((uint8)c);
if(!node_last->branches){ if(!node_last->branches){
node_last->branches=(STNode****)malloc(8*sizeof(STNode*)); node_last->branches=(STNode****)malloc(8*sizeof(STNode*));
bytes+=sizeof(void*)*8; //bytes+=sizeof(void*)*8;
for(uint8 i=0;i<8;i++) for(uint8 i=0;i<8;i++)
node_last->branches[i]=(STNode***)NULL; node_last->branches[i]=(STNode***)NULL;
} }
STNode*** ptrn32=(STNode***)node_last->branches[i3.n32]; STNode*** ptrn32=(STNode***)node_last->branches[i3.n32];
if(!ptrn32){ if(!ptrn32){
ptrn32=(STNode***)malloc(8*sizeof(STNode*)); ptrn32=(STNode***)malloc(8*sizeof(STNode*));
bytes+=sizeof(void*)*8; //bytes+=sizeof(void*)*8;
for(uint8 i=0;i<8;i++) for(uint8 i=0;i<8;i++)
ptrn32[i]=(STNode**)NULL; ptrn32[i]=(STNode**)NULL;
node_last->branches[i3.n32]=ptrn32; node_last->branches[i3.n32]=ptrn32;
@ -77,7 +76,7 @@ void ST_push(STNode* node_first, const char* key, Unitype value){
STNode** ptrn4=ptrn32[i3.n4]; STNode** ptrn4=ptrn32[i3.n4];
if(!ptrn4){ if(!ptrn4){
ptrn4=(STNode**)malloc(4*sizeof(STNode*)); ptrn4=(STNode**)malloc(4*sizeof(STNode*));
bytes+=sizeof(void*)*4; //bytes+=sizeof(void*)*4;
for(uint8 i=0;i<4;i++) for(uint8 i=0;i<4;i++)
ptrn4[i]=(STNode*)NULL; ptrn4[i]=(STNode*)NULL;
ptrn32[i3.n4]=ptrn4; ptrn32[i3.n4]=ptrn4;
@ -85,14 +84,14 @@ void ST_push(STNode* node_first, const char* key, Unitype value){
node_last=ptrn4[i3.rem]; node_last=ptrn4[i3.rem];
if(!node_last){ if(!node_last){
node_last=STNode_create(); node_last=STNode_create();
bytes+=sizeof(STNode); //bytes+=sizeof(STNode);
ptrn4[i3.rem]=node_last; ptrn4[i3.rem]=node_last;
} }
c=*(key+(++i)); c=*(key+(++i));
} }
node_last->value=value; node_last->value=value;
globytes+=bytes; //globytes+=bytes;
dbg(globytes); //dbg(globytes);
} }
const Unitype UnitypeNull={Null,.VoidPtr=NULL}; const Unitype UnitypeNull={Null,.VoidPtr=NULL};

View File

@ -8,7 +8,6 @@ typedef struct SearchTreeNode{
} STNode; } STNode;
STNode* STNode_create(void); STNode* STNode_create(void);
//doesn't work!
void STNode_free(STNode* node); void STNode_free(STNode* node);
void ST_push(STNode* node, const char* key, Unitype value); void ST_push(STNode* node, const char* key, Unitype value);

View File

@ -38,6 +38,7 @@ void test_autoarr(){
printallval(&ar); printallval(&ar);
printf("\n\e[92mautoarr filled up\n"); printf("\n\e[92mautoarr filled up\n");
printautoarr(&ar); printautoarr(&ar);
printf("%p %p\n",ar.values, *ar.values);
Autoarr_clear(&ar); Autoarr_clear(&ar);
printf("\e[92mautoarr cleared\n"); printf("\e[92mautoarr cleared\n");
} }

View File

@ -37,9 +37,10 @@ void printstnode(STNode* node){
printf(" \e[90m[%u]=%p\n",i,node->branches[i]); printf(" \e[90m[%u]=%p\n",i,node->branches[i]);
for (uint8 ii = 0; ii < 8; ii++){ for (uint8 ii = 0; ii < 8; ii++){
if(node->branches[i]){ if(node->branches[i]){
printf(" \e[90m[%u][%u]=%p\n",i,ii,node->branches[i][ii]); printf(" \e[90m[%u]=%p\n",ii,node->branches[i][ii]);
for (uint8 iii = 0; iii < 4; iii++) for (uint8 iii = 0; iii < 4; iii++)
if(node->branches[i][ii]) printf(" \e[90m[%u][%u][%u]=%p\n",i,ii,iii,node->branches[i][ii][iii]); if(node->branches[i][ii])
printf(" \e[90m[%u]=%p\n",iii,node->branches[i][ii][iii]);
} }
} }
} }
@ -49,6 +50,14 @@ void test_searchtree(){
printf("\e[96m-----------[test_searchtree]-----------\n"); printf("\e[96m-----------[test_searchtree]-----------\n");
STNode* node=STNode_create(); STNode* node=STNode_create();
printf("\e[92mnode created\n"); printf("\e[92mnode created\n");
Unitype v={.type=Double,.Double=-9.22003};
ST_push(node, "key_aa",v);
printuni(v);
printf(" -> push(key_aa)");
v =ST_pull(node,"key_aa");
printf("\npull(key_aa) -> ");
printuni(v);
printf("\n");
printstnode(node); printstnode(node);
STNode_free(node); STNode_free(node);
printf("\e[92mnode deleted\n"); printf("\e[92mnode deleted\n");

View File

@ -20,7 +20,7 @@ public static class TestDtsodV23
public static void TestBaseTypes() public static void TestBaseTypes()
{ {
Info.Log("b", "[TestDtsodV23/TestBaseTypes]"); Info.Log("b", "[TestDtsodV23/TestBaseTypes]");
DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Path.Sep}my_types.dtsod")); DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Path.Sep}base_types.dtsod"));
foreach (var pair in dtsod) foreach (var pair in dtsod)
Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString()); Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString());
Info.Log("g", "[test completed]"); Info.Log("g", "[test completed]");