commit 4aab4275ccfef0fef0ca89fbf4ea5fddf85f7395 parent 3757d42266e14cb4711837adfba28fd0ca019551 Author: Remy Noulin <loader2x@gmail.com> Date: Fri, 3 Jan 2020 17:12:04 +0100 add clean* macros for cleaning allocated variables (cleanSmallArrayP(name), cleanSmallArray(name), ...), add CLEANUP macro completion.txt | 26 ++++++++++++++ documentation.md | 61 +++++++++++++++++++++++++++++++++ release/json/libsheepyCSmallArray.h | 15 ++++++++ release/json/libsheepyCSmallBool.h | 16 +++++++++ release/json/libsheepyCSmallBytes.h | 16 +++++++++ release/json/libsheepyCSmallContainer.h | 16 +++++++++ release/json/libsheepyCSmallDict.h | 15 ++++++++ release/json/libsheepyCSmallDouble.h | 16 +++++++++ release/json/libsheepyCSmallInt.h | 16 +++++++++ release/json/libsheepyCSmallJson.h | 16 +++++++++ release/json/libsheepyCSmallString.h | 16 +++++++++ release/json/libsheepyCUndefined.h | 17 +++++++++ release/libsheepy.c | 20 +++++++++++ release/libsheepy.h | 43 +++++++++++++++++++++++ src/json/libsheepyCSmallArray.c | 12 +++++++ src/json/libsheepyCSmallArray.h | 15 ++++++++ src/json/libsheepyCSmallBool.c | 12 +++++++ src/json/libsheepyCSmallBool.h | 16 +++++++++ src/json/libsheepyCSmallBytes.c | 12 +++++++ src/json/libsheepyCSmallBytes.h | 16 +++++++++ src/json/libsheepyCSmallContainer.c | 12 +++++++ src/json/libsheepyCSmallContainer.h | 16 +++++++++ src/json/libsheepyCSmallDict.c | 12 +++++++ src/json/libsheepyCSmallDict.h | 15 ++++++++ src/json/libsheepyCSmallDouble.c | 12 +++++++ src/json/libsheepyCSmallDouble.h | 16 +++++++++ src/json/libsheepyCSmallInt.c | 12 +++++++ src/json/libsheepyCSmallInt.h | 16 +++++++++ src/json/libsheepyCSmallJson.c | 14 ++++++++ src/json/libsheepyCSmallJson.h | 16 +++++++++ src/json/libsheepyCSmallString.c | 12 +++++++ src/json/libsheepyCSmallString.h | 16 +++++++++ src/json/libsheepyCUndefined.c | 12 +++++++ src/json/libsheepyCUndefined.h | 17 +++++++++ src/libsheepy.c | 20 +++++++++++ src/libsheepy.h | 43 +++++++++++++++++++++++ 36 files changed, 653 insertions(+) Diffstat:
36 files changed, 653 insertions(+), 0 deletions(-)
diff --git a/completion.txt b/completion.txt @@ -117,6 +117,12 @@ TODO(message) TOKENPASTE2(a, b) TOKENPASTE(a, b) UNIQVAR(name) +void cleanUpCharFree(char **val) +cleanCharP(name) +void cleanUpListFree(char*** val) +cleanListP(name) +void cleanUpFileFree(FILE **val) +cleanFileP(name) logVar(var, format) logMVar(mask, var, format) logBoolVar(var) @@ -1349,8 +1355,10 @@ usSleep(time) msSleep(time) DEPRECATED PACKED +CLEANUP(func) DEPRECATED PACKED +CLEANUP initiateG(self) allocG(value) freeO(obj) @@ -3349,6 +3357,8 @@ cDi(self) ( (smallDictt*) self ) SMALLDICTFUNCTIONST createSmallDict(obj) ;smallDictt obj; initiateSmallDict(&obj) createAllocateSmallDict(obj) ;smallDictt *obj; initiateAllocateSmallDict(&obj) +cleanSmallDictP(name) smallDictt *name CLEANUP(cleanUpSmallDictTerminateG) +cleanSmallDict(name) smallDictt name CLEANUP(cleanUpSmallDictFreeG); initiateSmallDict(&name) forEachSmallDict(smallDict, key, value) forEachTypeSmallDict(type, smallDict, key, value) @@ -3360,6 +3370,8 @@ cAr(self) ( (smallArrayt*) self ) SMALLARRAYFUNCTIONST createSmallArray(obj) ;smallArrayt obj; initiateSmallArray(&obj) createAllocateSmallArray(obj) ;smallArrayt *obj; initiateAllocateSmallArray(&obj) +cleanSmallArrayP(name) smallArrayt *name CLEANUP(cleanUpSmallArrayTerminateG) +cleanSmallArray(name) smallArrayt name CLEANUP(cleanUpSmallArrayFreeG); initiateSmallArray(&name) createSA(...) createSAF("", __VA_ARGS__, NULL) forEachSmallArray(array, element) forEachTypeSmallArray(type, array, element) @@ -3381,6 +3393,8 @@ SMALLJSONFUNCTIONST createSmallJson(obj) ;smallJsont obj; initiateSmallJson(&obj) createAllocateSmallJson(obj) ;smallJsont *obj; initiateAllocateSmallJson(&obj) createSJ(...) createSJF("", __VA_ARGS__, NULL) +cleanSmallJsonP(name) smallJsont *name CLEANUP(cleanUpSmallJsonTerminateG) +cleanSmallJson(name) smallJsont name CLEANUP(cleanUpSmallJsonFreeG); initiateSmallJson(&name) forEachSmallJson(array, element) forEachDictSmallJson(smallJson, key, value) enumerateSmallJson(array, element, index) @@ -3393,6 +3407,8 @@ cBy(self) ( (smallBytest*) self ) SMALLBYTESFUNCTIONST createSmallBytes(obj) ;smallBytest obj; initiateSmallBytes(&obj) createAllocateSmallBytes(obj) ;smallBytest *obj; initiateAllocateSmallBytes(&obj) +cleanSmallBytesP(name) smallBytest *name CLEANUP(cleanUpSmallBytesTerminateG) +cleanSmallBytes(name) smallBytest name CLEANUP(cleanUpSmallBytesFreeG); initiateSmallBytes(&name) Class SmallBool @@ -3402,6 +3418,8 @@ cBo(self) ( (smallBoolt*) self ) SMALLBOOLFUNCTIONST createSmallBool(obj) ;smallBoolt obj; initiateSmallBool(&obj) createAllocateSmallBool(obj) ;smallBoolt *obj; initiateAllocateSmallBool(&obj) +cleanSmallBoolP(name) smallBoolt *name CLEANUP(cleanUpSmallBoolTerminateG) +cleanSmallBool(name) smallBoolt name CLEANUP(cleanUpSmallBoolFreeG); initiateSmallBool(&name) Class SmallContainer @@ -3411,6 +3429,8 @@ cCo(self) ( (smallContainert*) self ) SMALLCONTAINERFUNCTIONST createSmallContainer(obj) ;smallContainert obj; initiateSmallContainer(&obj) createAllocateSmallContainer(obj) ;smallContainert *obj; initiateAllocateSmallContainer(&obj) +cleanSmallContainerP(name) smallContainert *name CLEANUP(cleanUpSmallContainerTerminateG) +cleanSmallContainer(name) smallContainert name CLEANUP(cleanUpSmallContainerFreeG); initiateSmallContainer(&name) Class SmallDouble @@ -3420,6 +3440,8 @@ cDo(self) ( (smallDoublet*) self ) SMALLDOUBLEFUNCTIONST createSmallDouble(obj) ;smallDoublet obj; initiateSmallDouble(&obj) createAllocateSmallDouble(obj) ;smallDoublet *obj; initiateAllocateSmallDouble(&obj) +cleanSmallDoubleP(name) smallDoublet *name CLEANUP(cleanUpSmallDoubleTerminateG) +cleanSmallDouble(name) smallDoublet name CLEANUP(cleanUpSmallDoubleFreeG); initiateSmallDouble(&name) Class SmallInt @@ -3429,6 +3451,8 @@ cIn(self) ( (smallIntt*) self ) SMALLINTFUNCTIONST createSmallInt(obj) ;smallIntt obj; initiateSmallInt(&obj) createAllocateSmallInt(obj) ;smallIntt *obj; initiateAllocateSmallInt(&obj) +cleanSmallIntP(name) smallIntt *name CLEANUP(cleanUpSmallIntTerminateG) +cleanSmallInt(name) smallIntt name CLEANUP(cleanUpSmallIntFreeG); initiateSmallInt(&name) Class SmallString @@ -3438,6 +3462,8 @@ cSt(self) ( (smallStringt*) self ) SMALLSTRINGFUNCTIONST createSmallString(obj) ;smallStringt obj; initiateSmallString(&obj) createAllocateSmallString(obj) ;smallStringt *obj; initiateAllocateSmallString(&obj) +cleanSmallStringP(name) smallStringt *name CLEANUP(cleanUpSmallStringTerminateG) +cleanSmallString(name) smallStringt name CLEANUP(cleanUpSmallStringFreeG); initiateSmallString(&name) createS(...) createSF("", __VA_ARGS__, NULL) extern undefinedt* undefined; diff --git a/documentation.md b/documentation.md @@ -148,6 +148,12 @@ TODO(message) TOKENPASTE2(a, b) TOKENPASTE(a, b) UNIQVAR(name) +void cleanUpCharFree(char **val) +cleanCharP(name) +void cleanUpListFree(char*** val) +cleanListP(name) +void cleanUpFileFree(FILE **val) +cleanFileP(name) logVar(var, format) logMVar(mask, var, format) logBoolVar(var) @@ -1380,8 +1386,10 @@ usSleep(time) msSleep(time) DEPRECATED PACKED +CLEANUP(func) DEPRECATED PACKED +CLEANUP ``` ## Prototypes and macros @@ -1826,6 +1834,24 @@ extern jmp_buf tryJumpBuffers[maxTryThrowCount]; #define TOKENPASTE2(a, b) a ## b #define TOKENPASTE(a, b) TOKENPASTE2(a, b) #define UNIQVAR(name) TOKENPASTE(name, __LINE__) +// free val when it is out of scope +void cleanUpCharFree(char **val); +/* + * declare pointer name with type char* and free name when it is out of scope + */ +#define cleanCharP(name) char *name CLEANUP(cleanUpCharFree) +// free val when it is out of scope +void cleanUpListFree(char*** val); +/* + * declare pointer name with type char* and free name when it is out of scope + */ +#define cleanListP(name) char **name CLEANUP(cleanUpListFree) +// free val when it is out of scope +void cleanUpFileFree(FILE **val); +/* + * declare pointer name with type FILE and free name when it is out of scope + */ +#define cleanFileP(name) FILE *name CLEANUP(cleanUpFileFree) /* * log variable and its value * @@ -6342,8 +6368,25 @@ int nanoSleepF(uint64_t time); #define msSleep(time) pError0(nanoSleepF(1000000 * (uint64_t)time)) #define DEPRECATED __attribute__ ((deprecated)) #define PACKED __attribute__((__packed__)) +/* + * cleanup calls a function when the variable is out of scope + * + * Also checkout the clean* macros using the cleanup attribute (cleanCharP, ...) + * + * Example: + * void cleanUp(int *val) { + * logVarG(*val); + * } + * int a CLEANUP(cleanUp) = 1; + * a++; + * + * The cleanUp function is called when the variable a is out of scope + * + */ +#define CLEANUP(func) __attribute__((cleanup(func))) #define DEPRECATED #define PACKED +#define CLEANUP // vim: set expandtab ts=2 sw=2: ``` @@ -17195,6 +17238,8 @@ cDi(self) ( (smallDictt*) self ) SMALLDICTFUNCTIONST createSmallDict(obj) ;smallDictt obj; initiateSmallDict(&obj) createAllocateSmallDict(obj) ;smallDictt *obj; initiateAllocateSmallDict(&obj) +cleanSmallDictP(name) smallDictt *name CLEANUP(cleanUpSmallDictTerminateG) +cleanSmallDict(name) smallDictt name CLEANUP(cleanUpSmallDictFreeG); initiateSmallDict(&name) forEachSmallDict(smallDict, key, value) forEachTypeSmallDict(type, smallDict, key, value) @@ -17206,6 +17251,8 @@ cAr(self) ( (smallArrayt*) self ) SMALLARRAYFUNCTIONST createSmallArray(obj) ;smallArrayt obj; initiateSmallArray(&obj) createAllocateSmallArray(obj) ;smallArrayt *obj; initiateAllocateSmallArray(&obj) +cleanSmallArrayP(name) smallArrayt *name CLEANUP(cleanUpSmallArrayTerminateG) +cleanSmallArray(name) smallArrayt name CLEANUP(cleanUpSmallArrayFreeG); initiateSmallArray(&name) createSA(...) createSAF("", __VA_ARGS__, NULL) forEachSmallArray(array, element) forEachTypeSmallArray(type, array, element) @@ -17227,6 +17274,8 @@ SMALLJSONFUNCTIONST createSmallJson(obj) ;smallJsont obj; initiateSmallJson(&obj) createAllocateSmallJson(obj) ;smallJsont *obj; initiateAllocateSmallJson(&obj) createSJ(...) createSJF("", __VA_ARGS__, NULL) +cleanSmallJsonP(name) smallJsont *name CLEANUP(cleanUpSmallJsonTerminateG) +cleanSmallJson(name) smallJsont name CLEANUP(cleanUpSmallJsonFreeG); initiateSmallJson(&name) forEachSmallJson(array, element) forEachDictSmallJson(smallJson, key, value) enumerateSmallJson(array, element, index) @@ -17239,6 +17288,8 @@ cBy(self) ( (smallBytest*) self ) SMALLBYTESFUNCTIONST createSmallBytes(obj) ;smallBytest obj; initiateSmallBytes(&obj) createAllocateSmallBytes(obj) ;smallBytest *obj; initiateAllocateSmallBytes(&obj) +cleanSmallBytesP(name) smallBytest *name CLEANUP(cleanUpSmallBytesTerminateG) +cleanSmallBytes(name) smallBytest name CLEANUP(cleanUpSmallBytesFreeG); initiateSmallBytes(&name) Class SmallBool @@ -17248,6 +17299,8 @@ cBo(self) ( (smallBoolt*) self ) SMALLBOOLFUNCTIONST createSmallBool(obj) ;smallBoolt obj; initiateSmallBool(&obj) createAllocateSmallBool(obj) ;smallBoolt *obj; initiateAllocateSmallBool(&obj) +cleanSmallBoolP(name) smallBoolt *name CLEANUP(cleanUpSmallBoolTerminateG) +cleanSmallBool(name) smallBoolt name CLEANUP(cleanUpSmallBoolFreeG); initiateSmallBool(&name) Class SmallContainer @@ -17257,6 +17310,8 @@ cCo(self) ( (smallContainert*) self ) SMALLCONTAINERFUNCTIONST createSmallContainer(obj) ;smallContainert obj; initiateSmallContainer(&obj) createAllocateSmallContainer(obj) ;smallContainert *obj; initiateAllocateSmallContainer(&obj) +cleanSmallContainerP(name) smallContainert *name CLEANUP(cleanUpSmallContainerTerminateG) +cleanSmallContainer(name) smallContainert name CLEANUP(cleanUpSmallContainerFreeG); initiateSmallContainer(&name) Class SmallDouble @@ -17266,6 +17321,8 @@ cDo(self) ( (smallDoublet*) self ) SMALLDOUBLEFUNCTIONST createSmallDouble(obj) ;smallDoublet obj; initiateSmallDouble(&obj) createAllocateSmallDouble(obj) ;smallDoublet *obj; initiateAllocateSmallDouble(&obj) +cleanSmallDoubleP(name) smallDoublet *name CLEANUP(cleanUpSmallDoubleTerminateG) +cleanSmallDouble(name) smallDoublet name CLEANUP(cleanUpSmallDoubleFreeG); initiateSmallDouble(&name) Class SmallInt @@ -17275,6 +17332,8 @@ cIn(self) ( (smallIntt*) self ) SMALLINTFUNCTIONST createSmallInt(obj) ;smallIntt obj; initiateSmallInt(&obj) createAllocateSmallInt(obj) ;smallIntt *obj; initiateAllocateSmallInt(&obj) +cleanSmallIntP(name) smallIntt *name CLEANUP(cleanUpSmallIntTerminateG) +cleanSmallInt(name) smallIntt name CLEANUP(cleanUpSmallIntFreeG); initiateSmallInt(&name) Class SmallString @@ -17284,6 +17343,8 @@ cSt(self) ( (smallStringt*) self ) SMALLSTRINGFUNCTIONST createSmallString(obj) ;smallStringt obj; initiateSmallString(&obj) createAllocateSmallString(obj) ;smallStringt *obj; initiateAllocateSmallString(&obj) +cleanSmallStringP(name) smallStringt *name CLEANUP(cleanUpSmallStringTerminateG) +cleanSmallString(name) smallStringt name CLEANUP(cleanUpSmallStringFreeG); initiateSmallString(&name) createS(...) createSF("", __VA_ARGS__, NULL) ``` diff --git a/release/json/libsheepyCSmallArray.h b/release/json/libsheepyCSmallArray.h @@ -1841,6 +1841,21 @@ smallArrayt* allocSmallArray(void); smallArrayt* allocArraySmallArray(char **array); smallArrayt* allocCArraySmallArray(const char **array); +// terminate smallArrayt val when it is out of scope +void cleanUpSmallArrayTerminateG(smallArrayt **val); +// free smallArrayt val when it is out of scope +void cleanUpSmallArrayFreeG(smallArrayt *val); + +/** + * declare pointer name with type smallArrayt and terminate name when it is out of scope + */ +#define cleanSmallArrayP(name) smallArrayt *name CLEANUP(cleanUpSmallArrayTerminateG) + +/** + * declare local object name with type smallArrayt and free name when it is out of scope + */ +#define cleanSmallArray(name) smallArrayt name CLEANUP(cleanUpSmallArrayFreeG); initiateSmallArray(&name) + /** * create String Array Function * diff --git a/release/json/libsheepyCSmallBool.h b/release/json/libsheepyCSmallBool.h @@ -244,6 +244,22 @@ void registerMethodsSmallBool(smallBoolFunctionst *f); smallBoolt* allocSmallBool(bool value); +// terminate smallBoolt val when it is out of scope +void cleanUpSmallBoolTerminateG(smallBoolt **val); + +// free smallBoolt val when it is out of scope +void cleanUpSmallBoolFreeG(smallBoolt *val); + +/** + * declare pointer name with type smallBoolt and terminate name when it is out of scope + */ +#define cleanSmallBoolP(name) smallBoolt *name CLEANUP(cleanUpSmallBoolTerminateG) + +/** + * declare local object name with type smallBoolt and free name when it is out of scope + */ +#define cleanSmallBool(name) smallBoolt name CLEANUP(cleanUpSmallBoolFreeG); initiateSmallBool(&name) + smallBoolt* duplicateSmallBoolG (smallBoolt *self); void freeSmallBoolG (smallBoolt *self); diff --git a/release/json/libsheepyCSmallBytes.h b/release/json/libsheepyCSmallBytes.h @@ -247,6 +247,22 @@ void registerMethodsSmallBytes(smallBytesFunctionst *f); smallBytest* allocSmallBytes(void *data, uint32_t size); +// terminate smallBytest val when it is out of scope +void cleanUpSmallBytesTerminateG(smallBytest **val); + +// free smallBytest val when it is out of scope +void cleanUpSmallBytesFreeG(smallBytest *val); + +/** + * declare pointer name with type smallBytest and terminate name when it is out of scope + */ +#define cleanSmallBytesP(name) smallBytest *name CLEANUP(cleanUpSmallBytesTerminateG) + +/** + * declare local object name with type smallBytest and free name when it is out of scope + */ +#define cleanSmallBytes(name) smallBytest name CLEANUP(cleanUpSmallBytesFreeG); initiateSmallBytes(&name) + smallBytest* duplicateSmallBytesG (smallBytest *self); void freeSmallBytesG (smallBytest *self); diff --git a/release/json/libsheepyCSmallContainer.h b/release/json/libsheepyCSmallContainer.h @@ -214,6 +214,22 @@ void registerMethodsSmallContainer(smallContainerFunctionst *f); smallContainert* allocSmallContainer(void *data); +// terminate smallContainert val when it is out of scope +void cleanUpSmallContainerTerminateG(smallContainert **val); + +// free smallContainert val when it is out of scope +void cleanUpSmallContainerFreeG(smallContainert *val); + +/** + * declare pointer name with type smallContainert and terminate name when it is out of scope + */ +#define cleanSmallContainerP(name) smallContainert *name CLEANUP(cleanUpSmallContainerTerminateG) + +/** + * declare local object name with type smallContainert and free name when it is out of scope + */ +#define cleanSmallContainer(name) smallContainert name CLEANUP(cleanUpSmallContainerFreeG); initiateSmallContainer(&name) + smallContainert* duplicateSmallContainerG (smallContainert *self); void freeSmallContainerG(smallContainert *self); diff --git a/release/json/libsheepyCSmallDict.h b/release/json/libsheepyCSmallDict.h @@ -1046,6 +1046,21 @@ void registerMethodsSmallDict(smallDictFunctionst *f); smallDictt* allocSmallDict(void); +// terminate smallDictt val when it is out of scope +void cleanUpSmallDictTerminateG(smallDictt **val); +// free smallDictt val when it is out of scope +void cleanUpSmallDictFreeG(smallDictt *val); + +/** + * declare pointer name with type smallDictt and terminate name when it is out of scope + */ +#define cleanSmallDictP(name) smallDictt *name CLEANUP(cleanUpSmallDictTerminateG) + +/** + * declare local object name with type smallDictt and free name when it is out of scope + */ +#define cleanSmallDict(name) smallDictt name CLEANUP(cleanUpSmallDictFreeG); initiateSmallDict(&name) + /** * forEach - loop macro on smallDict keys */ diff --git a/release/json/libsheepyCSmallDouble.h b/release/json/libsheepyCSmallDouble.h @@ -242,6 +242,22 @@ void registerMethodsSmallDouble(smallDoubleFunctionst *f); smallDoublet* allocSmallDouble(double value); +// terminate smallDoublet val when it is out of scope +void cleanUpSmallDoubleTerminateG(smallDoublet **val); + +// free smallDoublet val when it is out of scope +void cleanUpSmallDoubleFreeG(smallDoublet *val); + +/** + * declare pointer name with type smallDoublet and terminate name when it is out of scope + */ +#define cleanSmallDoubleP(name) smallDoublet *name CLEANUP(cleanUpSmallDoubleTerminateG) + +/** + * declare local object name with type smallDoublet and free name when it is out of scope + */ +#define cleanSmallDouble(name) smallDoublet name CLEANUP(cleanUpSmallDoubleFreeG); initiateSmallDouble(&name) + smallDoublet* duplicateSmallDoubleG (smallDoublet *self); void freeSmallDoubleG (smallDoublet *self); diff --git a/release/json/libsheepyCSmallInt.h b/release/json/libsheepyCSmallInt.h @@ -244,6 +244,22 @@ void registerMethodsSmallInt(smallIntFunctionst *f); smallIntt* allocSmallInt(int64_t value); +// terminate smallIntt val when it is out of scope +void cleanUpSmallIntTerminateG(smallIntt **val); + +// free smallIntt val when it is out of scope +void cleanUpSmallIntFreeG(smallIntt *val); + +/** + * declare pointer name with type smallIntt and terminate name when it is out of scope + */ +#define cleanSmallIntP(name) smallIntt *name CLEANUP(cleanUpSmallIntTerminateG) + +/** + * declare local object name with type smallIntt and free name when it is out of scope + */ +#define cleanSmallInt(name) smallIntt name CLEANUP(cleanUpSmallIntFreeG); initiateSmallInt(&name) + smallIntt* duplicateSmallIntG (smallIntt *self); void freeSmallIntG (smallIntt *self); diff --git a/release/json/libsheepyCSmallJson.h b/release/json/libsheepyCSmallJson.h @@ -3426,6 +3426,22 @@ smallJsont* createSJF(const char *paramType, ...); smallJsont* allocSmallJsonG(smallJsont *self UNUSED); +// terminate smallJsont val when it is out of scope +void cleanUpSmallJsonTerminateG(smallJsont **val); + +// free smallJsont val when it is out of scope +void cleanUpSmallJsonFreeG(smallJsont *val); + +/** + * declare pointer name with type smallJsont and terminate name when it is out of scope + */ +#define cleanSmallJsonP(name) smallJsont *name CLEANUP(cleanUpSmallJsonTerminateG) + +/** + * declare local object name with Type smallJsont and free name when it is out of scope + */ +#define cleanSmallJson(name) smallJsont name CLEANUP(cleanUpSmallJsonFreeG); initiateSmallJson(&name) + smallJsont* duplicateSmallJsonG (smallJsont *self); void freeSmallJsonG (smallJsont *self); diff --git a/release/json/libsheepyCSmallString.h b/release/json/libsheepyCSmallString.h @@ -1282,6 +1282,22 @@ void registerMethodsSmallString(smallStringFunctionst *f); smallStringt* allocSmallString(const char *string); +// terminate smallStringt val when it is out of scope +void cleanUpSmallStringTerminateG(smallStringt **val); + +// free smallStringt val when it is out of scope +void cleanUpSmallStringFreeG(smallStringt *val); + +/** + * declare pointer name with type smallStringt and terminate name when it is out of scope + */ +#define cleanSmallStringP(name) smallStringt *name CLEANUP(cleanUpSmallStringTerminateG) + +/** + * declare local object name with type smallStringt and free name when it is out of scope + */ +#define cleanSmallString(name) smallStringt name CLEANUP(cleanUpSmallStringFreeG); initiateSmallString(&name) + /** * create String: f("qwd ", str," werr ", str2) * like catS, concatenate strings to a new smallString diff --git a/release/json/libsheepyCUndefined.h b/release/json/libsheepyCUndefined.h @@ -71,6 +71,23 @@ void finalizeUndefined(void); undefinedt* allocUndefined(void); +// terminate undefinedt val when it is out of scope +void cleanUpUndefinedTerminateG(undefinedt **val); + +// free undefinedt val when it is out of scope +void cleanUpUndefinedFreeG(undefinedt *val); + +/** + * declare pointer name with type undefinedt and terminate name when it is out of scope + */ +#define cleanUndefinedP(name) undefinedt *name CLEANUP(cleanUpUndefinedTerminateG) + +/** + * declare local object name with type undefinedt and free name when it is out of scope + */ +#define cleanUndefined(name) undefinedt name CLEANUP(cleanUpUndefinedFreeG); initiateUndefined(&name) + + undefinedt* duplicateUndefinedG (undefinedt *self); void freeUndefinedG (undefinedt *self); diff --git a/release/libsheepy.c b/release/libsheepy.c @@ -78,6 +78,9 @@ typedef enum { LOCALE_NORMAL, LOCALE_TURKIC, LOCALE_LITHUANIAN } localeType; internal void segfault_sigaction(int signal, siginfo_t *si, void *arg); internal void segfault_sigaction(int signal UNUSED, siginfo_t *si UNUSED, void *arg UNUSED); #endif +void cleanUpCharFree(char **val); +void cleanUpListFree(char* **val); +void cleanUpFileFree(FILE **val); uint64_t shStopwatch(uint8_t op); int getLogSymbols(void); void setLogSymbols(int mode); @@ -848,6 +851,23 @@ internal void segfault_sigaction(int signal UNUSED, siginfo_t *si UNUSED, void * #endif +void cleanUpCharFree(char **val) { + + free(*val); +} + + +void cleanUpListFree(char* **val) { + + listFreeS(*val); +} + + +void cleanUpFileFree(FILE **val) { + + free(*val); +} + /** * nanosecond stopwatch * diff --git a/release/libsheepy.h b/release/libsheepy.h @@ -660,6 +660,31 @@ extern jmp_buf tryJumpBuffers[maxTryThrowCount]; #define TOKENPASTE(a, b) TOKENPASTE2(a, b) #define UNIQVAR(name) TOKENPASTE(name, __LINE__) + +// free val when it is out of scope +void cleanUpCharFree(char **val); + +/** + * declare pointer name with type char* and free name when it is out of scope + */ +#define cleanCharP(name) char *name CLEANUP(cleanUpCharFree) + +// free val when it is out of scope +void cleanUpListFree(char*** val); + +/** + * declare pointer name with type char* and free name when it is out of scope + */ +#define cleanListP(name) char **name CLEANUP(cleanUpListFree) + +// free val when it is out of scope +void cleanUpFileFree(FILE **val); + +/** + * declare pointer name with type FILE and free name when it is out of scope + */ +#define cleanFileP(name) FILE *name CLEANUP(cleanUpFileFree) + /** * log variable and its value * @@ -7520,10 +7545,28 @@ int nanoSleepF(uint64_t time); #define UNUSED __attribute__ ((unused)) #define DEPRECATED __attribute__ ((deprecated)) #define PACKED __attribute__((__packed__)) + +/** + * cleanup calls a function when the variable is out of scope + * + * Also checkout the clean* macros using the cleanup attribute (cleanCharP, ...) + * + * Example: + * void cleanUp(int *val) { + * logVarG(*val); + * } + * int a CLEANUP(cleanUp) = 1; + * a++; + * + * The cleanUp function is called when the variable a is out of scope + * + */ +#define CLEANUP(func) __attribute__((cleanup(func))) #else #define UNUSED #define DEPRECATED #define PACKED +#define CLEANUP #endif #endif // _libsheepyH diff --git a/src/json/libsheepyCSmallArray.c b/src/json/libsheepyCSmallArray.c @@ -40,6 +40,8 @@ void initiateAllocateSmallArray(smallArrayt **self); smallArrayt* allocSmallArray(void); smallArrayt* allocArraySmallArray(char **array); smallArrayt* allocCArraySmallArray(const char **array); +void cleanUpSmallArrayTerminateG(smallArrayt **val); +void cleanUpSmallArrayFreeG(smallArrayt *val); smallArrayt* createSAF(const char *paramType, ...); void finalizeRecycleSmallArray(void *arg UNUSED); void finalizeSmallArray(void); @@ -1393,6 +1395,16 @@ smallArrayt* allocCArraySmallArray(const char **array) { return(allocArraySmallArray((char**)array)); } +void cleanUpSmallArrayTerminateG(smallArrayt **val) { + + terminateG(*val); +} + +void cleanUpSmallArrayFreeG(smallArrayt *val) { + + freeG(val); +} + smallArrayt* createSAF(const char *paramType, ...) { va_list pl; smallArrayt *r = NULL; diff --git a/src/json/libsheepyCSmallArray.h b/src/json/libsheepyCSmallArray.h @@ -1841,6 +1841,21 @@ smallArrayt* allocSmallArray(void); smallArrayt* allocArraySmallArray(char **array); smallArrayt* allocCArraySmallArray(const char **array); +// terminate smallArrayt val when it is out of scope +void cleanUpSmallArrayTerminateG(smallArrayt **val); +// free smallArrayt val when it is out of scope +void cleanUpSmallArrayFreeG(smallArrayt *val); + +/** + * declare pointer name with type smallArrayt and terminate name when it is out of scope + */ +#define cleanSmallArrayP(name) smallArrayt *name CLEANUP(cleanUpSmallArrayTerminateG) + +/** + * declare local object name with type smallArrayt and free name when it is out of scope + */ +#define cleanSmallArray(name) smallArrayt name CLEANUP(cleanUpSmallArrayFreeG); initiateSmallArray(&name) + /** * create String Array Function * diff --git a/src/json/libsheepyCSmallBool.c b/src/json/libsheepyCSmallBool.c @@ -38,6 +38,8 @@ void initiateAllocateSmallBool(smallBoolt **self); void finalizeRecycleSmallBool(void *arg UNUSED); void finalizeSmallBool(void); smallBoolt* allocSmallBool(bool value); +void cleanUpSmallBoolTerminateG(smallBoolt **val); +void cleanUpSmallBoolFreeG(smallBoolt *val); internal void freeSmallBool(smallBoolt *self); internal void terminateSmallBool(smallBoolt **self); internal char* toStringSmallBool(smallBoolt *self); @@ -230,6 +232,16 @@ smallBoolt* allocSmallBool(bool value) { return(r); } +void cleanUpSmallBoolTerminateG(smallBoolt **val) { + + terminateG(*val); +} + +void cleanUpSmallBoolFreeG(smallBoolt *val) { + + freeG(val); +} + internal void freeSmallBool(smallBoolt *self) { if (self->value) { diff --git a/src/json/libsheepyCSmallBool.h b/src/json/libsheepyCSmallBool.h @@ -244,6 +244,22 @@ void registerMethodsSmallBool(smallBoolFunctionst *f); smallBoolt* allocSmallBool(bool value); +// terminate smallBoolt val when it is out of scope +void cleanUpSmallBoolTerminateG(smallBoolt **val); + +// free smallBoolt val when it is out of scope +void cleanUpSmallBoolFreeG(smallBoolt *val); + +/** + * declare pointer name with type smallBoolt and terminate name when it is out of scope + */ +#define cleanSmallBoolP(name) smallBoolt *name CLEANUP(cleanUpSmallBoolTerminateG) + +/** + * declare local object name with type smallBoolt and free name when it is out of scope + */ +#define cleanSmallBool(name) smallBoolt name CLEANUP(cleanUpSmallBoolFreeG); initiateSmallBool(&name) + smallBoolt* duplicateSmallBoolG (smallBoolt *self); void freeSmallBoolG (smallBoolt *self); diff --git a/src/json/libsheepyCSmallBytes.c b/src/json/libsheepyCSmallBytes.c @@ -38,6 +38,8 @@ void initiateAllocateSmallBytes(smallBytest **self); void finalizeRecycleSmallBytes(void *arg UNUSED); void finalizeSmallBytes(void); smallBytest* allocSmallBytes(void *data, uint32_t size); +void cleanUpSmallBytesTerminateG(smallBytest **val); +void cleanUpSmallBytesFreeG(smallBytest *val); internal void freeSmallBytes(smallBytest *self); internal void terminateSmallBytes(smallBytest **self); #if (NFreeStackCheck) @@ -196,6 +198,16 @@ smallBytest* allocSmallBytes(void *data, uint32_t size) { return(r); } +void cleanUpSmallBytesTerminateG(smallBytest **val) { + + terminateG(*val); +} + +void cleanUpSmallBytesFreeG(smallBytest *val) { + + freeG(val); +} + internal void freeSmallBytes(smallBytest *self) { diff --git a/src/json/libsheepyCSmallBytes.h b/src/json/libsheepyCSmallBytes.h @@ -247,6 +247,22 @@ void registerMethodsSmallBytes(smallBytesFunctionst *f); smallBytest* allocSmallBytes(void *data, uint32_t size); +// terminate smallBytest val when it is out of scope +void cleanUpSmallBytesTerminateG(smallBytest **val); + +// free smallBytest val when it is out of scope +void cleanUpSmallBytesFreeG(smallBytest *val); + +/** + * declare pointer name with type smallBytest and terminate name when it is out of scope + */ +#define cleanSmallBytesP(name) smallBytest *name CLEANUP(cleanUpSmallBytesTerminateG) + +/** + * declare local object name with type smallBytest and free name when it is out of scope + */ +#define cleanSmallBytes(name) smallBytest name CLEANUP(cleanUpSmallBytesFreeG); initiateSmallBytes(&name) + smallBytest* duplicateSmallBytesG (smallBytest *self); void freeSmallBytesG (smallBytest *self); diff --git a/src/json/libsheepyCSmallContainer.c b/src/json/libsheepyCSmallContainer.c @@ -36,6 +36,8 @@ void initiateAllocateSmallContainer(smallContainert **self); void finalizeRecycleSmallContainer(void *arg UNUSED); void finalizeSmallContainer(void); smallContainert* allocSmallContainer(void *data); +void cleanUpSmallContainerTerminateG(smallContainert **val); +void cleanUpSmallContainerFreeG(smallContainert *val); internal void freeSmallContainer(smallContainert *self); internal void terminateSmallContainer(smallContainert **self); #if (NFreeStackCheck) @@ -135,6 +137,16 @@ smallContainert* allocSmallContainer(void *data) { return(r); } +void cleanUpSmallContainerTerminateG(smallContainert **val) { + + terminateG(*val); +} + +void cleanUpSmallContainerFreeG(smallContainert *val) { + + freeG(val); +} + internal void freeSmallContainer(smallContainert *self) { if (self->data) { diff --git a/src/json/libsheepyCSmallContainer.h b/src/json/libsheepyCSmallContainer.h @@ -214,6 +214,22 @@ void registerMethodsSmallContainer(smallContainerFunctionst *f); smallContainert* allocSmallContainer(void *data); +// terminate smallContainert val when it is out of scope +void cleanUpSmallContainerTerminateG(smallContainert **val); + +// free smallContainert val when it is out of scope +void cleanUpSmallContainerFreeG(smallContainert *val); + +/** + * declare pointer name with type smallContainert and terminate name when it is out of scope + */ +#define cleanSmallContainerP(name) smallContainert *name CLEANUP(cleanUpSmallContainerTerminateG) + +/** + * declare local object name with type smallContainert and free name when it is out of scope + */ +#define cleanSmallContainer(name) smallContainert name CLEANUP(cleanUpSmallContainerFreeG); initiateSmallContainer(&name) + smallContainert* duplicateSmallContainerG (smallContainert *self); void freeSmallContainerG(smallContainert *self); diff --git a/src/json/libsheepyCSmallDict.c b/src/json/libsheepyCSmallDict.c @@ -39,6 +39,8 @@ void initiateAllocateSmallDict(smallDictt **self); void finalizeRecycleSmallDict(void *arg UNUSED); void finalizeSmallDict(void); smallDictt* allocSmallDict(void); +void cleanUpSmallDictTerminateG(smallDictt **val); +void cleanUpSmallDictFreeG(smallDictt *val); internal void freeSmallDict(smallDictt *self); internal void terminateSmallDict(smallDictt **self); internal char* toStringSmallDict(smallDictt *self); @@ -966,6 +968,16 @@ smallDictt* allocSmallDict(void) { return(r); } +void cleanUpSmallDictTerminateG(smallDictt **val) { + + terminateG(*val); +} + +void cleanUpSmallDictFreeG(smallDictt *val) { + + freeG(val); +} + internal void freeSmallDict(smallDictt *self) { sFree((smallt *)self->d); diff --git a/src/json/libsheepyCSmallDict.h b/src/json/libsheepyCSmallDict.h @@ -1046,6 +1046,21 @@ void registerMethodsSmallDict(smallDictFunctionst *f); smallDictt* allocSmallDict(void); +// terminate smallDictt val when it is out of scope +void cleanUpSmallDictTerminateG(smallDictt **val); +// free smallDictt val when it is out of scope +void cleanUpSmallDictFreeG(smallDictt *val); + +/** + * declare pointer name with type smallDictt and terminate name when it is out of scope + */ +#define cleanSmallDictP(name) smallDictt *name CLEANUP(cleanUpSmallDictTerminateG) + +/** + * declare local object name with type smallDictt and free name when it is out of scope + */ +#define cleanSmallDict(name) smallDictt name CLEANUP(cleanUpSmallDictFreeG); initiateSmallDict(&name) + /** * forEach - loop macro on smallDict keys */ diff --git a/src/json/libsheepyCSmallDouble.c b/src/json/libsheepyCSmallDouble.c @@ -37,6 +37,8 @@ void initiateAllocateSmallDouble(smallDoublet **self); void finalizeRecycleSmallDouble(void *arg UNUSED); void finalizeSmallDouble(void); smallDoublet* allocSmallDouble(double value); +void cleanUpSmallDoubleTerminateG(smallDoublet **val); +void cleanUpSmallDoubleFreeG(smallDoublet *val); internal void freeSmallDouble(smallDoublet *self); internal void terminateSmallDouble(smallDoublet **self); internal char* toStringSmallDouble(smallDoublet *self); @@ -229,6 +231,16 @@ smallDoublet* allocSmallDouble(double value) { return(r); } +void cleanUpSmallDoubleTerminateG(smallDoublet **val) { + + terminateG(*val); +} + +void cleanUpSmallDoubleFreeG(smallDoublet *val) { + + freeG(val); +} + internal void freeSmallDouble(smallDoublet *self) { if (self->value) { diff --git a/src/json/libsheepyCSmallDouble.h b/src/json/libsheepyCSmallDouble.h @@ -242,6 +242,22 @@ void registerMethodsSmallDouble(smallDoubleFunctionst *f); smallDoublet* allocSmallDouble(double value); +// terminate smallDoublet val when it is out of scope +void cleanUpSmallDoubleTerminateG(smallDoublet **val); + +// free smallDoublet val when it is out of scope +void cleanUpSmallDoubleFreeG(smallDoublet *val); + +/** + * declare pointer name with type smallDoublet and terminate name when it is out of scope + */ +#define cleanSmallDoubleP(name) smallDoublet *name CLEANUP(cleanUpSmallDoubleTerminateG) + +/** + * declare local object name with type smallDoublet and free name when it is out of scope + */ +#define cleanSmallDouble(name) smallDoublet name CLEANUP(cleanUpSmallDoubleFreeG); initiateSmallDouble(&name) + smallDoublet* duplicateSmallDoubleG (smallDoublet *self); void freeSmallDoubleG (smallDoublet *self); diff --git a/src/json/libsheepyCSmallInt.c b/src/json/libsheepyCSmallInt.c @@ -38,6 +38,8 @@ void initiateAllocateSmallInt(smallIntt **self); void finalizeRecycleSmallInt(void *arg UNUSED); void finalizeSmallInt(void); smallIntt* allocSmallInt(int64_t value); +void cleanUpSmallIntTerminateG(smallIntt **val); +void cleanUpSmallIntFreeG(smallIntt *val); internal void freeSmallInt(smallIntt *self); internal void terminateSmallInt(smallIntt **self); internal char* toStringSmallInt(smallIntt *self); @@ -235,6 +237,16 @@ smallIntt* allocSmallInt(int64_t value) { return(r); } +void cleanUpSmallIntTerminateG(smallIntt **val) { + + terminateG(*val); +} + +void cleanUpSmallIntFreeG(smallIntt *val) { + + freeG(val); +} + internal void freeSmallInt(smallIntt *self) { if (self->value) { diff --git a/src/json/libsheepyCSmallInt.h b/src/json/libsheepyCSmallInt.h @@ -244,6 +244,22 @@ void registerMethodsSmallInt(smallIntFunctionst *f); smallIntt* allocSmallInt(int64_t value); +// terminate smallIntt val when it is out of scope +void cleanUpSmallIntTerminateG(smallIntt **val); + +// free smallIntt val when it is out of scope +void cleanUpSmallIntFreeG(smallIntt *val); + +/** + * declare pointer name with type smallIntt and terminate name when it is out of scope + */ +#define cleanSmallIntP(name) smallIntt *name CLEANUP(cleanUpSmallIntTerminateG) + +/** + * declare local object name with type smallIntt and free name when it is out of scope + */ +#define cleanSmallInt(name) smallIntt name CLEANUP(cleanUpSmallIntFreeG); initiateSmallInt(&name) + smallIntt* duplicateSmallIntG (smallIntt *self); void freeSmallIntG (smallIntt *self); diff --git a/src/json/libsheepyCSmallJson.c b/src/json/libsheepyCSmallJson.c @@ -47,6 +47,8 @@ smallJsont* createSJF(const char *paramType, ...); void finalizeRecycleSmallJson(void *arg UNUSED); void finalizeSmallJson(void); smallJsont* allocSmallJson(void); +void cleanUpSmallJsonTerminateG(smallJsont **val); +void cleanUpSmallJsonFreeG(smallJsont *val); internal void freeSmallJson(smallJsont *self); internal void terminateSmallJson(smallJsont **self); internal char* toStringSmallJson(smallJsont *self); @@ -2593,6 +2595,18 @@ smallJsont* allocSmallJson(void) { return(r); } +void cleanUpSmallJsonTerminateG(smallJsont **val) { + + terminateG(*val); +} + + +void cleanUpSmallJsonFreeG(smallJsont *val) { + + freeG(val); +} + + internal void freeSmallJson(smallJsont *self) { self->topIsA = SMALLJSON_IS_EMPTY; diff --git a/src/json/libsheepyCSmallJson.h b/src/json/libsheepyCSmallJson.h @@ -3426,6 +3426,22 @@ smallJsont* createSJF(const char *paramType, ...); smallJsont* allocSmallJsonG(smallJsont *self UNUSED); +// terminate smallJsont val when it is out of scope +void cleanUpSmallJsonTerminateG(smallJsont **val); + +// free smallJsont val when it is out of scope +void cleanUpSmallJsonFreeG(smallJsont *val); + +/** + * declare pointer name with type smallJsont and terminate name when it is out of scope + */ +#define cleanSmallJsonP(name) smallJsont *name CLEANUP(cleanUpSmallJsonTerminateG) + +/** + * declare local object name with Type smallJsont and free name when it is out of scope + */ +#define cleanSmallJson(name) smallJsont name CLEANUP(cleanUpSmallJsonFreeG); initiateSmallJson(&name) + smallJsont* duplicateSmallJsonG (smallJsont *self); void freeSmallJsonG (smallJsont *self); diff --git a/src/json/libsheepyCSmallString.c b/src/json/libsheepyCSmallString.c @@ -41,6 +41,8 @@ void initiateAllocateSmallString(smallStringt **self); void finalizeRecycleSmallString(void *arg UNUSED); void finalizeSmallString(void); smallStringt* allocSmallString(const char *string); +void cleanUpSmallStringTerminateG(smallStringt **val); +void cleanUpSmallStringFreeG(smallStringt *val); smallStringt* createSF(const char *paramType, ...); internal void freeSmallString(smallStringt *self); internal void terminateSmallString(smallStringt **self); @@ -820,6 +822,16 @@ smallStringt* allocSmallString(const char *string) { return(r); } +void cleanUpSmallStringTerminateG(smallStringt **val) { + + terminateG(*val); +} + +void cleanUpSmallStringFreeG(smallStringt *val) { + + freeG(val); +} + smallStringt* createSF(const char *paramType, ...) { va_list pl; smallStringt *r = NULL; diff --git a/src/json/libsheepyCSmallString.h b/src/json/libsheepyCSmallString.h @@ -1282,6 +1282,22 @@ void registerMethodsSmallString(smallStringFunctionst *f); smallStringt* allocSmallString(const char *string); +// terminate smallStringt val when it is out of scope +void cleanUpSmallStringTerminateG(smallStringt **val); + +// free smallStringt val when it is out of scope +void cleanUpSmallStringFreeG(smallStringt *val); + +/** + * declare pointer name with type smallStringt and terminate name when it is out of scope + */ +#define cleanSmallStringP(name) smallStringt *name CLEANUP(cleanUpSmallStringTerminateG) + +/** + * declare local object name with type smallStringt and free name when it is out of scope + */ +#define cleanSmallString(name) smallStringt name CLEANUP(cleanUpSmallStringFreeG); initiateSmallString(&name) + /** * create String: f("qwd ", str," werr ", str2) * like catS, concatenate strings to a new smallString diff --git a/src/json/libsheepyCUndefined.c b/src/json/libsheepyCUndefined.c @@ -35,6 +35,8 @@ void initiateAllocateUndefined(undefinedt **self); void finalizeRecycleUndefined(void *arg UNUSED); void finalizeUndefined(void); undefinedt* allocUndefined(void); +void cleanUpUndefinedTerminateG(undefinedt **val); +void cleanUpUndefinedFreeG(undefinedt *val); internal void freeUndefined(undefinedt *self UNUSED); internal void terminateUndefined(undefinedt **self); internal char* toStringUndefined(undefinedt *self UNUSED); @@ -99,6 +101,16 @@ undefinedt* allocUndefined(void) { return(r); } +void cleanUpUndefinedTerminateG(undefinedt **val) { + + terminateG(*val); +} + +void cleanUpUndefinedFreeG(undefinedt *val) { + + freeG(val); +} + internal void freeUndefined(undefinedt *self UNUSED) { return; diff --git a/src/json/libsheepyCUndefined.h b/src/json/libsheepyCUndefined.h @@ -71,6 +71,23 @@ void finalizeUndefined(void); undefinedt* allocUndefined(void); +// terminate undefinedt val when it is out of scope +void cleanUpUndefinedTerminateG(undefinedt **val); + +// free undefinedt val when it is out of scope +void cleanUpUndefinedFreeG(undefinedt *val); + +/** + * declare pointer name with type undefinedt and terminate name when it is out of scope + */ +#define cleanUndefinedP(name) undefinedt *name CLEANUP(cleanUpUndefinedTerminateG) + +/** + * declare local object name with type undefinedt and free name when it is out of scope + */ +#define cleanUndefined(name) undefinedt name CLEANUP(cleanUpUndefinedFreeG); initiateUndefined(&name) + + undefinedt* duplicateUndefinedG (undefinedt *self); void freeUndefinedG (undefinedt *self); diff --git a/src/libsheepy.c b/src/libsheepy.c @@ -80,6 +80,9 @@ void *myRealloc(void *buffer, size_t l); internal void segfault_sigaction(int signal, siginfo_t *si, void *arg); internal void segfault_sigaction(int signal UNUSED, siginfo_t *si UNUSED, void *arg UNUSED); #endif +void cleanUpCharFree(char **val); +void cleanUpListFree(char* **val); +void cleanUpFileFree(FILE **val); uint64_t shStopwatch(uint8_t op); int getLogSymbols(void); void setLogSymbols(int mode); @@ -904,6 +907,23 @@ internal void segfault_sigaction(int signal UNUSED, siginfo_t *si UNUSED, void * #endif +void cleanUpCharFree(char **val) { + + free(*val); +} + + +void cleanUpListFree(char* **val) { + + listFreeS(*val); +} + + +void cleanUpFileFree(FILE **val) { + + free(*val); +} + /** * nanosecond stopwatch * diff --git a/src/libsheepy.h b/src/libsheepy.h @@ -660,6 +660,31 @@ extern jmp_buf tryJumpBuffers[maxTryThrowCount]; #define TOKENPASTE(a, b) TOKENPASTE2(a, b) #define UNIQVAR(name) TOKENPASTE(name, __LINE__) + +// free val when it is out of scope +void cleanUpCharFree(char **val); + +/** + * declare pointer name with type char* and free name when it is out of scope + */ +#define cleanCharP(name) char *name CLEANUP(cleanUpCharFree) + +// free val when it is out of scope +void cleanUpListFree(char*** val); + +/** + * declare pointer name with type char* and free name when it is out of scope + */ +#define cleanListP(name) char **name CLEANUP(cleanUpListFree) + +// free val when it is out of scope +void cleanUpFileFree(FILE **val); + +/** + * declare pointer name with type FILE and free name when it is out of scope + */ +#define cleanFileP(name) FILE *name CLEANUP(cleanUpFileFree) + /** * log variable and its value * @@ -7520,10 +7545,28 @@ int nanoSleepF(uint64_t time); #define UNUSED __attribute__ ((unused)) #define DEPRECATED __attribute__ ((deprecated)) #define PACKED __attribute__((__packed__)) + +/** + * cleanup calls a function when the variable is out of scope + * + * Also checkout the clean* macros using the cleanup attribute (cleanCharP, ...) + * + * Example: + * void cleanUp(int *val) { + * logVarG(*val); + * } + * int a CLEANUP(cleanUp) = 1; + * a++; + * + * The cleanUp function is called when the variable a is out of scope + * + */ +#define CLEANUP(func) __attribute__((cleanup(func))) #else #define UNUSED #define DEPRECATED #define PACKED +#define CLEANUP #endif #endif // _libsheepyH