From ffdf40d747ee9eed9cd7dbac1915dbb061e6c943 Mon Sep 17 00:00:00 2001 From: Water-Melon Date: Fri, 26 Apr 2024 15:17:24 +0000 Subject: [PATCH] feat(func): add function pointer to the both callback functions --- docs/book/cn/func.md | 10 ++++++---- docs/book/en/func.md | 10 ++++++---- include/mln_func.h | 36 ++++++++++++++++++------------------ include/mln_span.h | 4 ++-- src/mln_span.c | 4 ++-- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/docs/book/cn/func.md b/docs/book/cn/func.md index b2caf65..a6bbbd0 100644 --- a/docs/book/cn/func.md +++ b/docs/book/cn/func.md @@ -27,11 +27,12 @@ ```c void mln_func_entry_callback_set(mln_func_entry_cb_t cb); -typedef int (*mln_func_entry_cb_t)(const char *file, const char *func, int line, ...); +typedef int (*mln_func_entry_cb_t)(void *fptr, const char *file, const char *func, int line, ...); ``` 描述:设置函数入口的回调函数。`mln_func_entry_cb_t`的参数和返回值含义: +- `fptr` 函数指针,也就是`func`对应的函数 - `file` 函数所在文件 - `func` 函数名 - `line` 所在文件行数 @@ -59,11 +60,12 @@ typedef int (*mln_func_entry_cb_t)(const char *file, const char *func, int line, ```c void mln_func_exit_callback_set(mln_func_exit_cb_t cb); -typedef void (*mln_func_exit_cb_t)(const char *file, const char *func, int line, void *ret, ...); +typedef void (*mln_func_exit_cb_t)(void *fptr, const char *file, const char *func, int line, void *ret, ...); ``` 描述:设置函数出口的回调函数。`mln_func_exit_cb_t`的参数含义: +- `fptr` 函数指针,也就是`func`对应的函数 - `file` 函数所在文件 - `func` 函数名 - `line` 所在文件行数 @@ -201,7 +203,7 @@ MLN_FUNC(static, int, bar, (void), (), { return 0; }) -static int my_entry(const char *file, const char *func, int line, ...) +static int my_entry(void *fptr, const char *file, const char *func, int line, ...) { if (!strcmp(func, "bar")) { printf("%s won't be executed\n", func); @@ -223,7 +225,7 @@ static int my_entry(const char *file, const char *func, int line, ...) return 0; } -static void my_exit(const char *file, const char *func, int line, void *ret, ...) +static void my_exit(void *fptr, const char *file, const char *func, int line, void *ret, ...) { if (!strcmp(func, "bar")) return; diff --git a/docs/book/en/func.md b/docs/book/en/func.md index 586e7a8..f249fb6 100644 --- a/docs/book/en/func.md +++ b/docs/book/en/func.md @@ -27,11 +27,12 @@ The MLN_FUNC macro will generate special function code only when the macro MLN_F ```c void mln_func_entry_callback_set(mln_func_entry_cb_t cb); -typedef int (*mln_func_entry_cb_t)(const char *file, const char *func, int line, ...); +typedef int (*mln_func_entry_cb_t)(void *fptr, const char *file, const char *func, int line, ...); ``` Description: Set the callback function for the function entry. Description of the parameters and return value of `mln_func_entry_cb_t`: +- `fptr` is a function pointer, which points to the function corresponding to `func`. - `file`: The file where the function is located. - `func`: The name of the function. - `line`: The line number in the file. @@ -59,11 +60,12 @@ Return value: Pointer to the entry function ```c void mln_func_exit_callback_set(mln_func_exit_cb_t cb); -typedef void (*mln_func_exit_cb_t)(const char *file, const char *func, int line, void *ret, ...); +typedef void (*mln_func_exit_cb_t)(void *fptr, const char *file, const char *func, int line, void *ret, ...); ``` Description: Set the callback function for the function exit. Description of the parameters of `mln_func_exit_cb_t`: +- `fptr` is a function pointer, which points to the function corresponding to `func`. - `file`: The file where the function is located. - `func`: The name of the function. - `line`: The line number in the file. @@ -204,7 +206,7 @@ MLN_FUNC(static, int, bar, (void), (), { return 0; }) -static int my_entry(const char *file, const char *func, int line, ...) +static int my_entry(void *fptr, const char *file, const char *func, int line, ...) { if (!strcmp(func, "bar")) { printf("%s won't be executed\n", func); @@ -226,7 +228,7 @@ static int my_entry(const char *file, const char *func, int line, ...) return 0; } -static void my_exit(const char *file, const char *func, int line, void *ret, ...) +static void my_exit(void *fptr, const char *file, const char *func, int line, void *ret, ...) { if (!strcmp(func, "bar")) return; diff --git a/include/mln_func.h b/include/mln_func.h index fe1db10..193b3a9 100644 --- a/include/mln_func.h +++ b/include/mln_func.h @@ -7,8 +7,8 @@ #include -typedef int (*mln_func_entry_cb_t)(const char *file, const char *func, int line, ...); -typedef void (*mln_func_exit_cb_t)(const char *file, const char *func, int line, void *ret, ...); +typedef int (*mln_func_entry_cb_t)(void *fptr, const char *file, const char *func, int line, ...); +typedef void (*mln_func_exit_cb_t)(void *fptr, const char *file, const char *func, int line, void *ret, ...); #define MLN_FUNC_ERROR (~((long)0)) @@ -21,14 +21,14 @@ typedef void (*mln_func_exit_cb_t)(const char *file, const char *func, int line, scope ret_type name params {\ ret_type _r;\ if (mln_func_entry != NULL) {\ - if (mln_func_entry(__FILE__, __FUNCTION__, __LINE__ MLN_FUNC_STRIP args) < 0) {\ + if (mln_func_entry(name, __FILE__, __FUNCTION__, __LINE__ MLN_FUNC_STRIP args) < 0) {\ _r = (ret_type)MLN_FUNC_ERROR;\ goto out;\ }\ }\ _r = __mln_func_##name args;\ out:\ - if (mln_func_exit != NULL) mln_func_exit(__FILE__, __FUNCTION__, __LINE__, &_r MLN_FUNC_STRIP args);\ + if (mln_func_exit != NULL) mln_func_exit(name, __FILE__, __FUNCTION__, __LINE__, &_r MLN_FUNC_STRIP args);\ return _r;\ } #define MLN_FUNC_VOID(scope, ret_type, name, params, args, ...) \ @@ -37,27 +37,27 @@ out:\ scope ret_type name params {\ int _r = 0;\ if (mln_func_entry != NULL) {\ - if (mln_func_entry(__FILE__, __FUNCTION__, __LINE__ MLN_FUNC_STRIP args) < 0) {\ + if (mln_func_entry(name, __FILE__, __FUNCTION__, __LINE__ MLN_FUNC_STRIP args) < 0) {\ _r = (int)MLN_FUNC_ERROR;\ goto out;\ }\ }\ __mln_func_##name args;\ out:\ - if (mln_func_exit != NULL) mln_func_exit(__FILE__, __FUNCTION__, __LINE__, _r? &_r: NULL MLN_FUNC_STRIP args);\ + if (mln_func_exit != NULL) mln_func_exit(name, __FILE__, __FUNCTION__, __LINE__, _r? &_r: NULL MLN_FUNC_STRIP args);\ } #define MLN_FUNC_CUSTOM(entry, exit, scope, ret_type, name, params, args, ...) \ scope ret_type name params;\ scope ret_type __mln_func_##name params __VA_ARGS__\ scope ret_type name params {\ ret_type _r;\ - if (entry(__FILE__, __FUNCTION__, __LINE__ MLN_FUNC_STRIP args) < 0) {\ + if (entry(name, __FILE__, __FUNCTION__, __LINE__ MLN_FUNC_STRIP args) < 0) {\ _r = (ret_type)MLN_FUNC_ERROR;\ goto out;\ }\ _r = __mln_func_##name args;\ out:\ - exit(__FILE__, __FUNCTION__, __LINE__, &_r MLN_FUNC_STRIP args);\ + exit(name, __FILE__, __FUNCTION__, __LINE__, &_r MLN_FUNC_STRIP args);\ return _r;\ } #define MLN_FUNC_VOID_CUSTOM(entry, exit, scope, ret_type, name, params, args, ...) \ @@ -65,13 +65,13 @@ out:\ scope ret_type __mln_func_##name params __VA_ARGS__\ scope ret_type name params {\ int _r = 0;\ - if (entry(__FILE__, __FUNCTION__, __LINE__ MLN_FUNC_STRIP args) < 0) {\ + if (entry(name, __FILE__, __FUNCTION__, __LINE__ MLN_FUNC_STRIP args) < 0) {\ _r = (int)MLN_FUNC_ERROR;\ goto out;\ }\ __mln_func_##name args;\ out:\ - exit(__FILE__, __FUNCTION__, __LINE__, _r? &_r: NULL MLN_FUNC_STRIP args);\ + exit(name, __FILE__, __FUNCTION__, __LINE__, _r? &_r: NULL MLN_FUNC_STRIP args);\ } #else @@ -81,14 +81,14 @@ out:\ scope ret_type name params {\ ret_type _r;\ if (mln_func_entry != NULL) {\ - if (mln_func_entry(__FILE__, __FUNCTION__, __LINE__) < 0) {\ + if (mln_func_entry(name, __FILE__, __FUNCTION__, __LINE__) < 0) {\ _r = (ret_type)MLN_FUNC_ERROR;\ goto out;\ }\ }\ _r = __mln_func_##name args;\ out:\ - if (mln_func_exit != NULL) mln_func_exit(__FILE__, __FUNCTION__, __LINE__, &_r);\ + if (mln_func_exit != NULL) mln_func_exit(name, __FILE__, __FUNCTION__, __LINE__, &_r);\ return _r;\ } #define MLN_FUNC_VOID(scope, ret_type, name, params, args, ...) \ @@ -97,27 +97,27 @@ out:\ scope ret_type name params {\ int _r = 0;\ if (mln_func_entry != NULL) {\ - if (mln_func_entry(__FILE__, __FUNCTION__, __LINE__) < 0) {\ + if (mln_func_entry(name, __FILE__, __FUNCTION__, __LINE__) < 0) {\ _r = (int)MLN_FUNC_ERROR;\ goto out;\ }\ }\ __mln_func_##name args;\ out:\ - if (mln_func_exit != NULL) mln_func_exit(__FILE__, __FUNCTION__, __LINE__, _r? &_r: NULL);\ + if (mln_func_exit != NULL) mln_func_exit(name, __FILE__, __FUNCTION__, __LINE__, _r? &_r: NULL);\ } #define MLN_FUNC_CUSTOM(entry, exit, scope, ret_type, name, params, args, ...) \ scope ret_type name params;\ scope ret_type __mln_func_##name params __VA_ARGS__\ scope ret_type name params {\ ret_type _r;\ - if (entry(__FILE__, __FUNCTION__, __LINE__) < 0) {\ + if (entry(name, __FILE__, __FUNCTION__, __LINE__) < 0) {\ _r = (ret_type)MLN_FUNC_ERROR;\ goto out;\ }\ _r = __mln_func_##name args;\ out:\ - exit(__FILE__, __FUNCTION__, __LINE__, &_r);\ + exit(name, __FILE__, __FUNCTION__, __LINE__, &_r);\ return _r;\ } #define MLN_FUNC_VOID_CUSTOM(entry, exit, scope, ret_type, name, params, args, ...) \ @@ -125,13 +125,13 @@ out:\ scope ret_type __mln_func_##name params __VA_ARGS__\ scope ret_type name params {\ int _r = 0;\ - if (entry(__FILE__, __FUNCTION__, __LINE__) < 0) {\ + if (entry(name, __FILE__, __FUNCTION__, __LINE__) < 0) {\ _r = (int)MLN_FUNC_ERROR;\ goto out;\ }\ __mln_func_##name args;\ out:\ - exit(__FILE__, __FUNCTION__, __LINE__, _r? &_r: NULL);\ + exit(name, __FILE__, __FUNCTION__, __LINE__, _r? &_r: NULL);\ } #endif #else diff --git a/include/mln_span.h b/include/mln_span.h index a874448..f5322e0 100644 --- a/include/mln_span.h +++ b/include/mln_span.h @@ -46,8 +46,8 @@ extern pthread_t mln_span_registered_thread; extern void mln_span_stack_free(void); extern mln_span_t *mln_span_new(mln_span_t *parent, const char *file, const char *func, int line); extern void mln_span_free(mln_span_t *s); -extern int mln_span_entry(const char *file, const char *func, int line, ...); -extern void mln_span_exit(const char *file, const char *func, int line, void *ret, ...); +extern int mln_span_entry(void *fptr, const char *file, const char *func, int line, ...); +extern void mln_span_exit(void *fptr, const char *file, const char *func, int line, void *ret, ...); #if defined(__WIN32__) #define mln_span_start() ({\ diff --git a/src/mln_span.c b/src/mln_span.c index f584f54..124a776 100644 --- a/src/mln_span.c +++ b/src/mln_span.c @@ -145,7 +145,7 @@ void mln_span_free(mln_span_t *s) free(s); } -int mln_span_entry(const char *file, const char *func, int line, ...) +int mln_span_entry(void *fptr, const char *file, const char *func, int line, ...) { mln_span_t *span; @@ -168,7 +168,7 @@ int mln_span_entry(const char *file, const char *func, int line, ...) return 0; } -void mln_span_exit(const char *file, const char *func, int line, void *ret, ...) +void mln_span_exit(void *fptr, const char *file, const char *func, int line, void *ret, ...) { #if defined(__WIN32__) if (mln_span_registered_thread != GetCurrentThreadId()) return;