fixedmemory leaks in Autoarr

This commit is contained in:
Timerix22 2023-02-13 20:41:41 +06:00
parent 590790817b
commit 00970919d1
19 changed files with 81 additions and 125 deletions

6
.vscode/launch.json vendored
View File

@ -5,7 +5,7 @@
"name": "(gdb) Debug", "name": "(gdb) Debug",
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/bin/kerep.com", "program": "${workspaceFolder}/bin/kerep",
"cwd": "${workspaceFolder}/bin", "cwd": "${workspaceFolder}/bin",
"preLaunchTask": "build_exec_dbg", "preLaunchTask": "build_exec_dbg",
"stopAtEntry": false, "stopAtEntry": false,
@ -30,7 +30,7 @@
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"preLaunchTask": "build_exec_dbg", "preLaunchTask": "build_exec_dbg",
"program": "${workspaceFolder}/bin/kerep.com", "program": "${workspaceFolder}/bin/kerep",
"cwd": "${workspaceFolder}/bin", "cwd": "${workspaceFolder}/bin",
"stopAtEntry": false, "stopAtEntry": false,
"externalConsole": false, "externalConsole": false,
@ -48,7 +48,7 @@
"type": "cppvsdbg", "type": "cppvsdbg",
"request": "launch", "request": "launch",
"preLaunchTask": "build_dbg", "preLaunchTask": "build_dbg",
"program": "${workspaceFolder}\\bin\\kerep.com", "program": "${workspaceFolder}\\bin\\kerep",
"cwd": "${workspaceFolder}\\bin", "cwd": "${workspaceFolder}\\bin",
"stopAtEntry": false, "stopAtEntry": false,
"console": "integratedTerminal" "console": "integratedTerminal"

View File

@ -42,7 +42,7 @@ exec: build_exec
# executes $EXEC_FILE # executes $EXEC_FILE
exec_dbg: build_exec_dbg 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 # executes $EXEC_FILE with valgrind memory checker
valgrind: build_exec_dbg valgrind: build_exec_dbg

2
cbuild

@ -1 +1 @@
Subproject commit a0cdbf5522de6d4803eb135f2f52bd6cac5a3b8a Subproject commit 8e6b4336d0d2741a82758834c7fc8bbd4de85360

View File

@ -30,7 +30,7 @@ case "$OS" in
SHARED_LIB_FILE="$PROJECT.dll" SHARED_LIB_FILE="$PROJECT.dll"
;; ;;
LINUX) LINUX)
EXEC_FILE="$PROJECT.P" EXEC_FILE="$PROJECT"
SHARED_LIB_FILE="$PROJECT.so" SHARED_LIB_FILE="$PROJECT.so"
;; ;;
*) *)

View File

@ -1,15 +1,17 @@
#include "Autoarr.h" #include "Autoarr.h"
Autoarr_define(char) Autoarr_define(Pointer, true)
Autoarr_define(bool) Autoarr_define(char, false)
Autoarr_define(f32) Autoarr_define(bool, false)
Autoarr_define(f64) Autoarr_define(f32, false)
Autoarr_define(u8) Autoarr_define(f64, false)
Autoarr_define(i8) Autoarr_define(u8, false)
Autoarr_define(u16) Autoarr_define(i8, false)
Autoarr_define(i16) Autoarr_define(u16, false)
Autoarr_define(u32) Autoarr_define(i16, false)
Autoarr_define(i32) Autoarr_define(u32, false)
Autoarr_define(u64) Autoarr_define(i32, false)
Autoarr_define(i64) Autoarr_define(u64, false)
Autoarr_define(Pointer) Autoarr_define(i64, false)
Autoarr_define(Unitype, false)

View File

@ -6,8 +6,8 @@ extern "C" {
#include "Autoarr_declare.h" #include "Autoarr_declare.h"
#include "Autoarr_define.h" #include "Autoarr_define.h"
#include "Autoarr_Unitype.h"
Autoarr_declare(Pointer)
Autoarr_declare(char) Autoarr_declare(char)
Autoarr_declare(bool) Autoarr_declare(bool)
Autoarr_declare(f32) Autoarr_declare(f32)
@ -20,7 +20,23 @@ Autoarr_declare(i32)
Autoarr_declare(u32) Autoarr_declare(u32)
Autoarr_declare(i64) Autoarr_declare(i64)
Autoarr_declare(u64) 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;blockI<ar->blocks_count-1;blockI++) \
for(u32 elemI=0;elemI<ar->max_block_length;elemI++){ \
elem=ar->values[blockI][elemI]; \
(codeblock); \
} \
for(u32 elemI=0;elemI<ar->block_length;elemI++){ \
elem=ar->values[ar->blocks_count-1][elemI]; \
(codeblock); \
} \
} \
})
#if __cplusplus #if __cplusplus
} }

View File

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

View File

@ -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;blockI<ar->blocks_count-1;blockI++) \
for(u32 elemI=0;elemI<ar->max_block_length;elemI++){ \
elem=ar->values[blockI][elemI]; \
(codeblock); \
} \
for(u32 elemI=0;elemI<ar->block_length;elemI++){ \
elem=ar->values[ar->blocks_count-1][elemI]; \
(codeblock); \
} \
} \
})
#if __cplusplus
}
#endif

View File

@ -15,7 +15,8 @@ typedef struct __Autoarr_##type##_functions_list_t { \
type (*get)(struct Autoarr_##type* ar, u32 index); \ 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 (*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); \ type* (*toArray)(struct Autoarr_##type* ar); \
} __Autoarr_##type##_functions_list_t; \ } __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); \ 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##_freeWithMembers(Autoarr_##type* ar, bool freePtr); \
void ____Autoarr_##type##_free_g(void* ar); void ____Autoarr_##type##_freeWithMembers(void* ar);
#define Autoarr(type) Autoarr_##type #define Autoarr(type) Autoarr_##type
@ -47,7 +48,9 @@ void ____Autoarr_##type##_free_g(void* ar);
#define Autoarr_set(autoarr, index, element) \ #define Autoarr_set(autoarr, index, element) \
autoarr->functions->set(autoarr, index, element) autoarr->functions->set(autoarr, index, element)
#define Autoarr_free(autoarr, freePtr) \ #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) \ #define Autoarr_toArray(autoarr) \
autoarr->functions->toArray(autoarr) autoarr->functions->toArray(autoarr)

View File

@ -6,9 +6,9 @@ extern "C" {
#include "../base/base.h" #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){ \ void __Autoarr_##type##_add(Autoarr_##type* ar, type element){ \
if(!ar->values){ \ 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; \ 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; i<ar->blocks_count;i++) \ for(u16 i=0; i<ar->blocks_count;i++) \
free(ar->values[i]); \ free(ar->values[i]); \
free(ar->values); \ free(ar->values); \
if(freePtr) free(ar); \ 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=&el; \
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){ \ 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##_get, \
&__Autoarr_##type##_getPtr, \ &__Autoarr_##type##_getPtr, \
&__Autoarr_##type##_set, \ &__Autoarr_##type##_set, \
&__Autoarr_##type##_free_g, \ &__Autoarr_##type##_freeWithMembers, \
&__Autoarr_##type##_freeWithoutMembers, \
&__Autoarr_##type##_toArray \ &__Autoarr_##type##_toArray \
}; \ }; \
\ \

View File

@ -51,7 +51,7 @@ void Hashtable_expand(Hashtable* ht){
} }
// there is no need to free array values, because they are copied into new array // there is no need to free array values, because they are copied into new array
// so dont replace this incorrect auto-generated function // so dont replace this incorrect auto-generated function
__Autoarr_KVPair_free_g(ar, true); Autoarr_freeWithoutMembers(ar, true);
} }
free(ht->rows); free(ht->rows);

View File

@ -2,7 +2,7 @@
kt_define(KVPair, __KVPair_free, NULL); kt_define(KVPair, __KVPair_free, NULL);
Autoarr_define(KVPair) Autoarr_define(KVPair, false)
// proper way to clean a KVP // proper way to clean a KVP
void KVPair_free(KVPair p){ void KVPair_free(KVPair p){
@ -11,15 +11,6 @@ void KVPair_free(KVPair p){
} }
void __KVPair_free(void* p){ 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){ void printkvp(KVPair p){
kprintf("{\"%s\", ",p.key); kprintf("{\"%s\", ",p.key);
printuni(p.value); printuni(p.value);

View File

@ -18,10 +18,6 @@ Autoarr_declare(KVPair)
void KVPair_free(KVPair p); void KVPair_free(KVPair p);
void __KVPair_free(void* 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); void printkvp(KVPair p);
#if __cplusplus #if __cplusplus

View File

@ -2,7 +2,7 @@
kt_define(string, NULL, NULL); kt_define(string, NULL, NULL);
Array_define(string) Array_define(string)
Autoarr_define(string) Autoarr_define(string, false)
// copies str content to new char pointer value (adding '\0' at the end) // copies str content to new char pointer value (adding '\0' at the end)
char* string_extract(string str){ char* string_extract(string str){

View File

@ -22,6 +22,7 @@ typedef int64_t i64;
typedef uint64_t u64; typedef uint64_t u64;
typedef float f32; typedef float f32;
typedef double f64; typedef double f64;
/// anonymous pointer without specified freeMembers() func
typedef void* Pointer; typedef void* Pointer;
// Usually bool from stdbool.h is defined as macro, // Usually bool from stdbool.h is defined as macro,

View File

@ -45,6 +45,7 @@ void ktDescriptors_initKerepTypes(){
kt_register(Array_Pointer); kt_register(Array_Pointer);
// base type autoarrs // base type autoarrs
kt_register(Autoarr_Pointer);
kt_register(Autoarr_char); kt_register(Autoarr_char);
kt_register(Autoarr_bool); kt_register(Autoarr_bool);
kt_register(Autoarr_f32); kt_register(Autoarr_f32);
@ -57,17 +58,11 @@ void ktDescriptors_initKerepTypes(){
kt_register(Autoarr_u32); kt_register(Autoarr_u32);
kt_register(Autoarr_i64); kt_register(Autoarr_i64);
kt_register(Autoarr_u64); kt_register(Autoarr_u64);
kt_register(Autoarr_Pointer);
// Unitype // Unitype
kt_register(Unitype); kt_register(Unitype);
kt_register(Array_Unitype); kt_register(Array_Unitype);
kt_register(Autoarr_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 // STNode
kt_register(STNode); kt_register(STNode);
@ -75,11 +70,6 @@ void ktDescriptors_initKerepTypes(){
// KeyValuePair // KeyValuePair
kt_register(KVPair); kt_register(KVPair);
kt_register(Autoarr_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 // Hashtable
kt_register(Hashtable); kt_register(Hashtable);

View File

@ -2,7 +2,7 @@
#include "type_system.h" #include "type_system.h"
#include "base_toString.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(char,NULL, __toString_char);
kt_define(bool,NULL, __toString_bool); kt_define(bool,NULL, __toString_bool);
kt_define(f32, NULL, __toString_f32); kt_define(f32, NULL, __toString_f32);

View File

@ -33,39 +33,33 @@ void print_dtsod(Hashtable* dtsod){
} }
void test_dtsod(){ void test_dtsod(){
// optime(__func__,1,({ optime(__func__,1,({
kprintf("\e[96m-------------[test_dtsod]-------------\n"); kprintf("\e[96m-------------[test_dtsod]-------------\n");
Hashtable* dtsod; Hashtable* dtsod;
char* s; char* s;
do { optime("deserialize",1,({
// optime("deserialize",1,({
tryLast(DtsodV24_deserialize(text),r) tryLast(DtsodV24_deserialize(text),r)
dtsod=r.value.VoidPtr; dtsod=r.value.VoidPtr;
// })); }));
} while(0);
print_dtsod(dtsod); print_dtsod(dtsod);
do { optime("serialize",1,({
// optime("serialize",1,({
tryLast(DtsodV24_serialize(dtsod),r) tryLast(DtsodV24_serialize(dtsod),r)
s=r.value.VoidPtr; s=r.value.VoidPtr;
// })); }));
} while(0);
DtsodV24_free(dtsod); DtsodV24_free(dtsod);
kprintf("\e[92m%s",s); kprintf("\e[92m%s",s);
do { optime("reserialize",10,({
// optime("reserialize",10,({
tryLast(DtsodV24_deserialize(s),r) tryLast(DtsodV24_deserialize(s),r)
dtsod=r.value.VoidPtr; dtsod=r.value.VoidPtr;
free(s); free(s);
tryLast(DtsodV24_serialize(dtsod),rr) tryLast(DtsodV24_serialize(dtsod),rr)
s=rr.value.VoidPtr; s=rr.value.VoidPtr;
DtsodV24_free(dtsod); DtsodV24_free(dtsod);
// })); }));
} while(0);
free(s); free(s);
// })); }));
} }

View File

@ -28,12 +28,12 @@ inline void test_all(){
test_searchtree(); test_searchtree();
test_autoarr(); test_autoarr();
test_autoarrVsVector(); test_autoarrVsVector();
test_hash_functions();
test_hashtable();
test_dtsod();
test_rng_algorithms(); test_rng_algorithms();
test_kprint_colors(); test_kprint_colors();
test_kprint(); test_kprint();
test_hash_functions();
test_hashtable();
test_dtsod();
kprintf("\e[96m--------------------------------------\e[0m\n"); kprintf("\e[96m--------------------------------------\e[0m\n");
})); }));
} }