Skip to content

Commit

Permalink
feat(func): add function pointer to the both callback functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon committed Apr 26, 2024
1 parent ee181d5 commit ffdf40d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
10 changes: 6 additions & 4 deletions docs/book/cn/func.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 所在文件行数
Expand Down Expand Up @@ -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` 所在文件行数
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions docs/book/en/func.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
36 changes: 18 additions & 18 deletions include/mln_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#include <stdio.h>

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))

Expand All @@ -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, ...) \
Expand All @@ -37,41 +37,41 @@ 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, ...) \
scope ret_type name params;\
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
Expand All @@ -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, ...) \
Expand All @@ -97,41 +97,41 @@ 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, ...) \
scope ret_type name params;\
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
Expand Down
4 changes: 2 additions & 2 deletions include/mln_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() ({\
Expand Down
4 changes: 2 additions & 2 deletions src/mln_span.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down

0 comments on commit ffdf40d

Please sign in to comment.