From 00970919d18361b1f9844ec89d0682234e9d9a1f Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 13 Feb 2023 20:41:41 +0600 Subject: [PATCH] fixedmemory leaks in Autoarr --- .vscode/launch.json | 6 +++--- Makefile | 2 +- cbuild | 2 +- default.config | 2 +- src/Autoarr/Autoarr.c | 28 ++++++++++++------------ src/Autoarr/Autoarr.h | 20 +++++++++++++++-- src/Autoarr/Autoarr_Unitype.c | 16 -------------- src/Autoarr/Autoarr_Unitype.h | 33 ----------------------------- src/Autoarr/Autoarr_declare.h | 11 ++++++---- src/Autoarr/Autoarr_define.h | 24 +++++++++++++++------ src/Hashtable/Hashtable.c | 2 +- src/Hashtable/KeyValuePair.c | 11 +--------- src/Hashtable/KeyValuePair.h | 4 ---- src/String/string.c | 2 +- src/base/std.h | 1 + src/base/type_system/init.c | 12 +---------- src/base/type_system/kt_functions.c | 2 +- tests/test_dtsod.c | 22 +++++++------------ tests/tests.h | 6 +++--- 19 files changed, 81 insertions(+), 125 deletions(-) delete mode 100644 src/Autoarr/Autoarr_Unitype.c delete mode 100644 src/Autoarr/Autoarr_Unitype.h diff --git a/.vscode/launch.json b/.vscode/launch.json index 887a884..44f903a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "name": "(gdb) Debug", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/bin/kerep.com", + "program": "${workspaceFolder}/bin/kerep", "cwd": "${workspaceFolder}/bin", "preLaunchTask": "build_exec_dbg", "stopAtEntry": false, @@ -30,7 +30,7 @@ "type": "cppdbg", "request": "launch", "preLaunchTask": "build_exec_dbg", - "program": "${workspaceFolder}/bin/kerep.com", + "program": "${workspaceFolder}/bin/kerep", "cwd": "${workspaceFolder}/bin", "stopAtEntry": false, "externalConsole": false, @@ -48,7 +48,7 @@ "type": "cppvsdbg", "request": "launch", "preLaunchTask": "build_dbg", - "program": "${workspaceFolder}\\bin\\kerep.com", + "program": "${workspaceFolder}\\bin\\kerep", "cwd": "${workspaceFolder}\\bin", "stopAtEntry": false, "console": "integratedTerminal" diff --git a/Makefile b/Makefile index 3a38c5c..7f1ba15 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,7 @@ exec: build_exec # executes $EXEC_FILE exec_dbg: build_exec_dbg - @cbuild/call_task.sh exec_dbg 2>&1 | tee -a make_raw.log + @cbuild/call_task.sh exec 2>&1 | tee -a make_raw.log # executes $EXEC_FILE with valgrind memory checker valgrind: build_exec_dbg diff --git a/cbuild b/cbuild index a0cdbf5..8e6b433 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit a0cdbf5522de6d4803eb135f2f52bd6cac5a3b8a +Subproject commit 8e6b4336d0d2741a82758834c7fc8bbd4de85360 diff --git a/default.config b/default.config index 31e6241..a9313e2 100644 --- a/default.config +++ b/default.config @@ -30,7 +30,7 @@ case "$OS" in SHARED_LIB_FILE="$PROJECT.dll" ;; LINUX) - EXEC_FILE="$PROJECT.P" + EXEC_FILE="$PROJECT" SHARED_LIB_FILE="$PROJECT.so" ;; *) diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index b8a57e4..f2159c8 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -1,15 +1,17 @@ #include "Autoarr.h" -Autoarr_define(char) -Autoarr_define(bool) -Autoarr_define(f32) -Autoarr_define(f64) -Autoarr_define(u8) -Autoarr_define(i8) -Autoarr_define(u16) -Autoarr_define(i16) -Autoarr_define(u32) -Autoarr_define(i32) -Autoarr_define(u64) -Autoarr_define(i64) -Autoarr_define(Pointer) +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(Unitype, false) diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index e75da8f..405e018 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -6,8 +6,8 @@ extern "C" { #include "Autoarr_declare.h" #include "Autoarr_define.h" -#include "Autoarr_Unitype.h" +Autoarr_declare(Pointer) Autoarr_declare(char) Autoarr_declare(bool) Autoarr_declare(f32) @@ -20,7 +20,23 @@ Autoarr_declare(i32) Autoarr_declare(u32) Autoarr_declare(i64) Autoarr_declare(u64) -Autoarr_declare(Pointer) + +Autoarr_declare(Unitype) + +#define Autoarr_foreach(ar,elem,codeblock)({ \ + if(ar->blocks_count>0) { \ + typeof(**ar->values) elem; \ + for(u32 blockI=0;blockIblocks_count-1;blockI++) \ + for(u32 elemI=0;elemImax_block_length;elemI++){ \ + elem=ar->values[blockI][elemI]; \ + (codeblock); \ + } \ + for(u32 elemI=0;elemIblock_length;elemI++){ \ + elem=ar->values[ar->blocks_count-1][elemI]; \ + (codeblock); \ + } \ + } \ +}) #if __cplusplus } diff --git a/src/Autoarr/Autoarr_Unitype.c b/src/Autoarr/Autoarr_Unitype.c deleted file mode 100644 index c16e531..0000000 --- a/src/Autoarr/Autoarr_Unitype.c +++ /dev/null @@ -1,16 +0,0 @@ -#include "Autoarr.h" - -Autoarr_define(Unitype) - -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_Unitype_free_g(ar, freePtr); - free_calls++; - kprintf("free_calls: %u\n", free_calls); -} -void ____Autoarr_Unitype_free_fixed(void* ar) { - __Autoarr_Unitype_free_fixed((Autoarr(Unitype)*)ar, false); -} diff --git a/src/Autoarr/Autoarr_Unitype.h b/src/Autoarr/Autoarr_Unitype.h deleted file mode 100644 index b58bdfa..0000000 --- a/src/Autoarr/Autoarr_Unitype.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "Autoarr_declare.h" -#include "Autoarr_define.h" - -Autoarr_declare(Unitype) - -// this function is injected in kerep_init() -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) { \ - typeof(**ar->values) elem; \ - for(u32 blockI=0;blockIblocks_count-1;blockI++) \ - for(u32 elemI=0;elemImax_block_length;elemI++){ \ - elem=ar->values[blockI][elemI]; \ - (codeblock); \ - } \ - for(u32 elemI=0;elemIblock_length;elemI++){ \ - elem=ar->values[ar->blocks_count-1][elemI]; \ - (codeblock); \ - } \ - } \ -}) - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h index e209115..d996063 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -15,7 +15,8 @@ typedef struct __Autoarr_##type##_functions_list_t { \ 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 (*freear)(struct Autoarr_##type* ar, bool freePtr); \ + 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; \ \ @@ -31,8 +32,8 @@ STRUCT(Autoarr_##type, \ ) \ \ 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); +void __Autoarr_##type##_freeWithMembers(Autoarr_##type* ar, bool freePtr); \ +void ____Autoarr_##type##_freeWithMembers(void* ar); #define Autoarr(type) Autoarr_##type @@ -47,7 +48,9 @@ void ____Autoarr_##type##_free_g(void* ar); #define Autoarr_set(autoarr, index, element) \ autoarr->functions->set(autoarr, index, element) #define Autoarr_free(autoarr, freePtr) \ - autoarr->functions->freear(autoarr, freePtr) + autoarr->functions->freeWithMembers(autoarr, freePtr) +#define Autoarr_freeWithoutMembers(autoarr, freePtr) \ + autoarr->functions->freeWithoutMembers(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 94b4747..6c7df17 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -6,9 +6,9 @@ extern "C" { #include "../base/base.h" -#define Autoarr_define(type) \ +#define Autoarr_define(type, TYPE_IS_PTR) \ \ -kt_define(Autoarr_##type, ____Autoarr_##type##_free_g, NULL); \ +kt_define(Autoarr_##type, ____Autoarr_##type##_freeWithMembers, NULL); \ \ void __Autoarr_##type##_add(Autoarr_##type* ar, type element){ \ if(!ar->values){ \ @@ -41,14 +41,25 @@ void __Autoarr_##type##_set(Autoarr_##type* ar, u32 index, type element){ \ ar->values[index/ar->max_block_length][index%ar->max_block_length]=element; \ } \ \ -void __Autoarr_##type##_free_g(Autoarr_##type* ar, bool freePtr){ \ +void __Autoarr_##type##_freeWithoutMembers(Autoarr_##type* ar, bool freePtr){ \ for(u16 i=0; iblocks_count;i++) \ free(ar->values[i]); \ free(ar->values); \ if(freePtr) free(ar); \ } \ -void ____Autoarr_##type##_free_g(void* ar){ \ - __Autoarr_##type##_free_g((Autoarr_##type*)ar, false); \ +\ +void __Autoarr_##type##_freeWithMembers(Autoarr_##type* ar, bool freePtr){ \ + if(ktDescriptor_##type.freeMembers!=NULL) { \ + Autoarr_foreach(ar, el, ({ \ + void* members_ptr=⪙ \ + if(TYPE_IS_PTR) members_ptr=*(type**)members_ptr; \ + ktDescriptor_##type.freeMembers(members_ptr); \ + })); \ + } \ + __Autoarr_##type##_freeWithoutMembers(ar, freePtr);\ +} \ +void ____Autoarr_##type##_freeWithMembers(void* ar){ \ + __Autoarr_##type##_freeWithMembers((Autoarr_##type*)ar, false); \ } \ \ type* __Autoarr_##type##_toArray(Autoarr_##type* ar){ \ @@ -64,7 +75,8 @@ __Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list={ \ &__Autoarr_##type##_get, \ &__Autoarr_##type##_getPtr, \ &__Autoarr_##type##_set, \ - &__Autoarr_##type##_free_g, \ + &__Autoarr_##type##_freeWithMembers, \ + &__Autoarr_##type##_freeWithoutMembers, \ &__Autoarr_##type##_toArray \ }; \ \ diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index 2022084..c1d3e20 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -51,7 +51,7 @@ void Hashtable_expand(Hashtable* ht){ } // there is no need to free array values, because they are copied into new array // so dont replace this incorrect auto-generated function - __Autoarr_KVPair_free_g(ar, true); + Autoarr_freeWithoutMembers(ar, true); } free(ht->rows); diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index 724f490..952ebe8 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -2,7 +2,7 @@ kt_define(KVPair, __KVPair_free, NULL); -Autoarr_define(KVPair) +Autoarr_define(KVPair, false) // proper way to clean a KVP void KVPair_free(KVPair p){ @@ -11,15 +11,6 @@ void KVPair_free(KVPair p){ } void __KVPair_free(void* p){ KVPair_free(*(KVPair*)p); } -// func for KVP array cleaning -void __Autoarr_KVPair_free_fixed(Autoarr_KVPair* ar, bool freePtr){ - Autoarr_foreach(ar,k,KVPair_free(k)); - __Autoarr_KVPair_free_g(ar, freePtr); -} -void ____Autoarr_KVPair_free_fixed(void* ar){ - __Autoarr_KVPair_free_fixed((Autoarr_KVPair*)ar, false); -} - void printkvp(KVPair p){ kprintf("{\"%s\", ",p.key); printuni(p.value); diff --git a/src/Hashtable/KeyValuePair.h b/src/Hashtable/KeyValuePair.h index 2d3fb32..99d8117 100644 --- a/src/Hashtable/KeyValuePair.h +++ b/src/Hashtable/KeyValuePair.h @@ -18,10 +18,6 @@ Autoarr_declare(KVPair) void KVPair_free(KVPair p); void __KVPair_free(void* p); -// 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); #if __cplusplus diff --git a/src/String/string.c b/src/String/string.c index 36c2684..7e2f084 100644 --- a/src/String/string.c +++ b/src/String/string.c @@ -2,7 +2,7 @@ kt_define(string, NULL, NULL); Array_define(string) -Autoarr_define(string) +Autoarr_define(string, false) // copies str content to new char pointer value (adding '\0' at the end) char* string_extract(string str){ diff --git a/src/base/std.h b/src/base/std.h index 1fc07e1..0055fd1 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -22,6 +22,7 @@ typedef int64_t i64; typedef uint64_t u64; typedef float f32; typedef double f64; +/// anonymous pointer without specified freeMembers() func typedef void* Pointer; // Usually bool from stdbool.h is defined as macro, diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index 9078be2..7b619cb 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -45,6 +45,7 @@ void ktDescriptors_initKerepTypes(){ kt_register(Array_Pointer); // base type autoarrs + kt_register(Autoarr_Pointer); kt_register(Autoarr_char); kt_register(Autoarr_bool); kt_register(Autoarr_f32); @@ -57,17 +58,11 @@ void ktDescriptors_initKerepTypes(){ kt_register(Autoarr_u32); kt_register(Autoarr_i64); kt_register(Autoarr_u64); - kt_register(Autoarr_Pointer); // Unitype kt_register(Unitype); kt_register(Array_Unitype); kt_register(Autoarr_Unitype); - // 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); @@ -75,11 +70,6 @@ void ktDescriptors_initKerepTypes(){ // KeyValuePair kt_register(KVPair); kt_register(Autoarr_KVPair); - // 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); diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index bccaf07..b73eca0 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -2,7 +2,7 @@ #include "type_system.h" #include "base_toString.h" -kt_define(Pointer, free, __toString_u64); +kt_define(Pointer, NULL, __toString_u64); kt_define(char,NULL, __toString_char); kt_define(bool,NULL, __toString_bool); kt_define(f32, NULL, __toString_f32); diff --git a/tests/test_dtsod.c b/tests/test_dtsod.c index 71b38f9..8af576c 100644 --- a/tests/test_dtsod.c +++ b/tests/test_dtsod.c @@ -33,39 +33,33 @@ void print_dtsod(Hashtable* dtsod){ } void test_dtsod(){ - // optime(__func__,1,({ + optime(__func__,1,({ kprintf("\e[96m-------------[test_dtsod]-------------\n"); Hashtable* dtsod; char* s; - do { - // optime("deserialize",1,({ + optime("deserialize",1,({ tryLast(DtsodV24_deserialize(text),r) dtsod=r.value.VoidPtr; - // })); - } while(0); + })); print_dtsod(dtsod); - do { - // optime("serialize",1,({ + optime("serialize",1,({ tryLast(DtsodV24_serialize(dtsod),r) s=r.value.VoidPtr; - // })); - } while(0); + })); DtsodV24_free(dtsod); kprintf("\e[92m%s",s); - do { - // optime("reserialize",10,({ + optime("reserialize",10,({ tryLast(DtsodV24_deserialize(s),r) dtsod=r.value.VoidPtr; free(s); tryLast(DtsodV24_serialize(dtsod),rr) s=rr.value.VoidPtr; DtsodV24_free(dtsod); - // })); - } while(0); + })); free(s); - // })); + })); } \ No newline at end of file diff --git a/tests/tests.h b/tests/tests.h index c65a4ba..616721a 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -28,12 +28,12 @@ inline void test_all(){ test_searchtree(); test_autoarr(); test_autoarrVsVector(); - test_hash_functions(); - test_hashtable(); - test_dtsod(); test_rng_algorithms(); test_kprint_colors(); test_kprint(); + test_hash_functions(); + test_hashtable(); + test_dtsod(); kprintf("\e[96m--------------------------------------\e[0m\n"); })); }