From 1f25cac30814905661caa8675d62840f7d541c46 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 28 Feb 2022 13:51:17 +0800 Subject: [PATCH] func --- include/util/tfunctional.h | 34 +++++++++++++++++----------------- source/util/src/tfunctional.c | 25 +++++++++++-------------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/include/util/tfunctional.h b/include/util/tfunctional.h index c96f997f06..43e3cd5e48 100644 --- a/include/util/tfunctional.h +++ b/include/util/tfunctional.h @@ -12,45 +12,45 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef _TD_UTIL_FUNCTIONAL_H -#define _TD_UTIL_FUNCTIONAL_H +#ifndef _TD_UTIL_FUNCTIONAL_H_ +#define _TD_UTIL_FUNCTIONAL_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -#include "os.h" - -//TODO: hard to use, trying to rewrite it using va_list +// TODO: hard to use, trying to rewrite it using va_list typedef void* (*GenericVaFunc)(void* args[]); -typedef int32_t (*I32VaFunc) (void* args[]); -typedef void (*VoidVaFunc) (void* args[]); +typedef int32_t (*I32VaFunc)(void* args[]); +typedef void (*VoidVaFunc)(void* args[]); typedef struct GenericSavedFunc { GenericVaFunc func; - void * args[]; + void* args[]; } tGenericSavedFunc; typedef struct I32SavedFunc { I32VaFunc func; - void * args[]; + void* args[]; } tI32SavedFunc; typedef struct VoidSavedFunc { VoidVaFunc func; - void * args[]; + void* args[]; } tVoidSavedFunc; -tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int numOfArgs); -tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int numOfArgs); -tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int numOfArgs); -void* genericInvoke(tGenericSavedFunc* const pSavedFunc); -int32_t i32Invoke(tI32SavedFunc* const pSavedFunc); -void voidInvoke(tVoidSavedFunc* const pSavedFunc); +tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int32_t numOfArgs); +tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int32_t numOfArgs); +tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int32_t numOfArgs); +void* genericInvoke(tGenericSavedFunc* const pSavedFunc); +int32_t i32Invoke(tI32SavedFunc* const pSavedFunc); +void voidInvoke(tVoidSavedFunc* const pSavedFunc); #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_FUNCTIONAL_H*/ +#endif /*_TD_UTIL_FUNCTIONAL_H_*/ diff --git a/source/util/src/tfunctional.c b/source/util/src/tfunctional.c index 8b20f8fc0a..c49fbdb504 100644 --- a/source/util/src/tfunctional.c +++ b/source/util/src/tfunctional.c @@ -13,37 +13,34 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "tfunctional.h" -tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int numOfArgs) { +tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int32_t numOfArgs) { tGenericSavedFunc* pSavedFunc = malloc(sizeof(tGenericSavedFunc) + numOfArgs * (sizeof(void*))); - if(pSavedFunc == NULL) return NULL; + if (pSavedFunc == NULL) return NULL; pSavedFunc->func = func; return pSavedFunc; } -tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int numOfArgs) { - tI32SavedFunc* pSavedFunc = malloc(sizeof(tI32SavedFunc) + numOfArgs * sizeof(void *)); - if(pSavedFunc == NULL) return NULL; +tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int32_t numOfArgs) { + tI32SavedFunc* pSavedFunc = malloc(sizeof(tI32SavedFunc) + numOfArgs * sizeof(void*)); + if (pSavedFunc == NULL) return NULL; pSavedFunc->func = func; return pSavedFunc; } -tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int numOfArgs) { +tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int32_t numOfArgs) { tVoidSavedFunc* pSavedFunc = malloc(sizeof(tVoidSavedFunc) + numOfArgs * sizeof(void*)); - if(pSavedFunc == NULL) return NULL; + if (pSavedFunc == NULL) return NULL; pSavedFunc->func = func; return pSavedFunc; } -FORCE_INLINE void* genericInvoke(tGenericSavedFunc* const pSavedFunc) { - return pSavedFunc->func(pSavedFunc->args); -} +FORCE_INLINE void* genericInvoke(tGenericSavedFunc* const pSavedFunc) { return pSavedFunc->func(pSavedFunc->args); } -FORCE_INLINE int32_t i32Invoke(tI32SavedFunc* const pSavedFunc) { - return pSavedFunc->func(pSavedFunc->args); -} +FORCE_INLINE int32_t i32Invoke(tI32SavedFunc* const pSavedFunc) { return pSavedFunc->func(pSavedFunc->args); } FORCE_INLINE void voidInvoke(tVoidSavedFunc* const pSavedFunc) { - if(pSavedFunc) pSavedFunc->func(pSavedFunc->args); + if (pSavedFunc) pSavedFunc->func(pSavedFunc->args); } -- GitLab