Skip to content

Commit

Permalink
refactor(*): use function templates to define double linked list func…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
Water-Melon committed Feb 15, 2024
1 parent a44a54f commit fbfa36f
Show file tree
Hide file tree
Showing 19 changed files with 146 additions and 147 deletions.
16 changes: 8 additions & 8 deletions docs/book/cn/double_linked_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,32 @@ Melon中有两个双向链表实现,读者可自行选用所需的一种。下
#### MLN_CHAIN_FUNC_DECLARE

```c
MLN_CHAIN_FUNC_DECLARE(prefix,type,ret_attr,func_attr);
MLN_CHAIN_FUNC_DECLARE(scope,prefix,type,func_attr);
```
描述:本宏用于对双向链表的添加操作和删除操作函数进行声明,其中:
- `scope`: 为函数的作用域关键字。
- `prefix`:为两个函数名的前缀,这是为了允许在一个源文件内为多个双向链表进行函数声明。
- `type`:链表节点的类型
- `ret_attr`:两个函数的操作域类型和返回值类型
- `func_attr`:对函数参数的约束(仅限于Linux中),若无则留空即可
#### MLN_CHAIN_FUNC_DEFINE
```c
MLN_CHAIN_FUNC_DEFINE(prefix,type,ret_attr,prev_ptr,next_ptr);
MLN_CHAIN_FUNC_DEFINE(scope,prefix,type,prev_ptr,next_ptr);
ret_attr prefix##_chain_add(type **head, type **tail, type *node);
ret_attr prefix##_chain_del(type **head, type **tail, type *node);
scope void prefix##_chain_add(type **head, type **tail, type *node);
scope void prefix##_chain_del(type **head, type **tail, type *node);
```

描述:本宏用于定义双向链表的添加和删除操作函数,其中:

- `scope`: 为函数的作用域关键字。
- `prefix`:为两个函数名的前缀,这是为了允许在一个源文件内为多个双向链表进行函数声明。
- `type`:链表节点的类型
- `ret_attr`:两个函数的操作域类型和返回值类型
- `prev_ptr`:链表节点中指向前一节点的指针名
- `next_ptr`:链表节点中指向后一节点的指针名

Expand All @@ -78,8 +78,8 @@ typedef struct chain_s {
struct chain_s *next;
} chain_t;

MLN_CHAIN_FUNC_DECLARE(test, chain_t, static inline void, );
MLN_CHAIN_FUNC_DEFINE(test, chain_t, static inline void, prev, next);
MLN_CHAIN_FUNC_DECLARE(static inline, test, chain_t, );
MLN_CHAIN_FUNC_DEFINE(static inline, test, chain_t, prev, next);

int main(void)
{
Expand Down
16 changes: 8 additions & 8 deletions docs/book/en/double_linked_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,32 @@ There are two doubly linked list implementations in Melon. We introduce them res
#### MLN_CHAIN_FUNC_DECLARE

```c
MLN_CHAIN_FUNC_DECLARE(prefix,type,ret_attr,func_attr);
MLN_CHAIN_FUNC_DECLARE(scope,prefix,type,func_attr);
```
Description: This macro is used to declare the insert operation and remove operation functions of the doubly linked list, among which:
- `scope`: The scope keyword for functions.
- `prefix`: The prefix of two function names, this is to allow function declarations for multiple doubly linked lists within a source file.
- `type`: the type of the linked list node
- `ret_attr`: the operation field type and return value type of the two functions
- `func_attr`: Constraints on function parameters (only in Linux), if not, leave it blank
#### MLN_CHAIN_FUNC_DEFINE
```c
MLN_CHAIN_FUNC_DEFINE(prefix,type,ret_attr,prev_ptr,next_ptr);
MLN_CHAIN_FUNC_DEFINE(scope,prefix,type,prev_ptr,next_ptr);
ret_attr prefix##_chain_add(type **head, type **tail, type *node);
ret_attr prefix##_chain_del(type **head, type **tail, type *node);
scope void prefix##_chain_add(type **head, type **tail, type *node);
scope void prefix##_chain_del(type **head, type **tail, type *node);
```

Description: This macro is used to define the insert and remove operation functions of the doubly linked list, where:

- `scope`: The scope keyword for functions.
- `prefix`: The prefix of two function names, this is to allow function declarations for multiple doubly linked lists within a source file.
- `type`: the type of the linked list node
- `ret_attr`: the operation field type and return value type of the two functions
- `prev_ptr`: the name of the pointer to the previous node in the linked list node
- `next_ptr`: the name of the pointer to the next node in the linked list node

Expand All @@ -77,8 +77,8 @@ typedef struct chain_s {
struct chain_s *next;
} chain_t;

MLN_CHAIN_FUNC_DECLARE(test, chain_t, static inline void, );
MLN_CHAIN_FUNC_DEFINE(test, chain_t, static inline void, prev, next);
MLN_CHAIN_FUNC_DECLARE(static inline, test, chain_t, );
MLN_CHAIN_FUNC_DEFINE(static inline, test, chain_t, prev, next);

int main(void)
{
Expand Down
10 changes: 5 additions & 5 deletions include/mln_rbtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ typedef struct rbtree_s {
} mln_rbtree_t;


MLN_CHAIN_FUNC_DECLARE(mln_rbtree, \
mln_rbtree_node_t, \
static inline void,);
MLN_CHAIN_FUNC_DEFINE(mln_rbtree, \
MLN_CHAIN_FUNC_DECLARE(static inline, \
mln_rbtree, \
mln_rbtree_node_t,);
MLN_CHAIN_FUNC_DEFINE(static inline, \
mln_rbtree, \
mln_rbtree_node_t, \
static inline void, \
prev, \
next);

Expand Down
19 changes: 9 additions & 10 deletions include/mln_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define __MLN_DEFS_H

#include <pthread.h>
#include "mln_func.h"

#ifdef __DEBUG__
#include <assert.h>
Expand Down Expand Up @@ -88,13 +89,12 @@ extern int spin_trylock(void *lock);
/*
* Chain
*/
#define MLN_CHAIN_FUNC_DECLARE(prefix,type,ret_attr,func_attr); \
ret_attr prefix##_chain_add(type **head, type **tail, type *node) func_attr;\
ret_attr prefix##_chain_del(type **head, type **tail, type *node) func_attr;
#define MLN_CHAIN_FUNC_DECLARE(scope,prefix,type,func_attr); \
scope void prefix##_chain_add(type **head, type **tail, type *node) func_attr;\
scope void prefix##_chain_del(type **head, type **tail, type *node) func_attr;

#define MLN_CHAIN_FUNC_DEFINE(prefix,type,ret_attr,prev_ptr,next_ptr); \
ret_attr prefix##_chain_add(type **head, type **tail, type *node) \
{\
#define MLN_CHAIN_FUNC_DEFINE(scope,prefix,type,prev_ptr,next_ptr); \
MLN_FUNC_VOID(scope, void, prefix##_chain_add, (type **head, type **tail, type *node), (head, tail, node), { \
if (head == NULL || tail == NULL || node == NULL) return;\
node->prev_ptr = node->next_ptr = NULL;\
if (*head == NULL) {\
Expand All @@ -104,10 +104,9 @@ extern int spin_trylock(void *lock);
(*tail)->next_ptr = node;\
node->prev_ptr = (*tail);\
*tail = node;\
}\
})\
\
ret_attr prefix##_chain_del(type **head, type **tail, type *node) \
{\
MLN_FUNC_VOID(scope, void, prefix##_chain_del, (type **head, type **tail, type *node), (head, tail, node), { \
if (head == NULL || tail == NULL || node == NULL) return;\
if (*head == node) {\
if (*tail == node) {\
Expand All @@ -126,7 +125,7 @@ extern int spin_trylock(void *lock);
}\
}\
node->prev_ptr = node->next_ptr = NULL;\
}
})

#define mln_bigendian_encode(num,buf,Blen); \
{\
Expand Down
30 changes: 15 additions & 15 deletions src/mln_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
#include "mln_func.h"


MLN_CHAIN_FUNC_DECLARE(mln_blk, \
mln_alloc_blk_t, \
static inline void,);
MLN_CHAIN_FUNC_DECLARE(mln_chunk, \
mln_alloc_chunk_t, \
static inline void,);
MLN_CHAIN_FUNC_DECLARE(mln_alloc_shm, \
mln_alloc_shm_t, \
static inline void,);
MLN_CHAIN_FUNC_DECLARE(static inline, \
mln_blk, \
mln_alloc_blk_t, );
MLN_CHAIN_FUNC_DECLARE(static inline, \
mln_chunk, \
mln_alloc_chunk_t, );
MLN_CHAIN_FUNC_DECLARE(static inline, \
mln_alloc_shm, \
mln_alloc_shm_t, );
static inline void
mln_alloc_mgr_table_init(mln_alloc_mgr_t *tbl);
static inline mln_alloc_mgr_t *
Expand Down Expand Up @@ -633,19 +633,19 @@ MLN_FUNC_VOID(static inline, void, mln_alloc_free_shm, (void *ptr), (ptr), {
/*
* chain
*/
MLN_CHAIN_FUNC_DEFINE(mln_blk, \
MLN_CHAIN_FUNC_DEFINE(static inline, \
mln_blk, \
mln_alloc_blk_t, \
static inline void, \
prev, \
next);
MLN_CHAIN_FUNC_DEFINE(mln_chunk, \
MLN_CHAIN_FUNC_DEFINE(static inline, \
mln_chunk, \
mln_alloc_chunk_t, \
static inline void, \
prev, \
next);
MLN_CHAIN_FUNC_DEFINE(mln_alloc_shm, \
MLN_CHAIN_FUNC_DEFINE(static inline, \
mln_alloc_shm, \
mln_alloc_shm_t, \
static inline void, \
prev, \
next);

10 changes: 5 additions & 5 deletions src/mln_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ MLN_DEFINE_TOKEN(static, mln_conf_lex, CONF, \
{CONF_TK_CHAR, "CONF_TK_CHAR"}, \
{CONF_TK_STRING, "CONF_TK_STRING"});

MLN_CHAIN_FUNC_DECLARE(conf_hook, \
mln_conf_hook_t, \
static inline void,);
MLN_CHAIN_FUNC_DECLARE(static inline, \
conf_hook, \
mln_conf_hook_t, );

/*
* declarations
Expand Down Expand Up @@ -1018,9 +1018,9 @@ MLN_FUNC(static, int, mln_conf_dump_domain_iterate_handler, \
/*
* chain
*/
MLN_CHAIN_FUNC_DEFINE(conf_hook, \
MLN_CHAIN_FUNC_DEFINE(static inline, \
conf_hook, \
mln_conf_hook_t, \
static inline void, \
prev, \
next);

Expand Down
20 changes: 10 additions & 10 deletions src/mln_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
#endif

/*declarations*/
MLN_CHAIN_FUNC_DECLARE(ev_fd_wait, \
mln_event_desc_t, \
static inline void,);
MLN_CHAIN_FUNC_DECLARE(ev_fd_active, \
mln_event_desc_t, \
static inline void,);
MLN_CHAIN_FUNC_DECLARE(static inline, \
ev_fd_wait, \
mln_event_desc_t, );
MLN_CHAIN_FUNC_DECLARE(static inline, \
ev_fd_active, \
mln_event_desc_t, );
static inline void
mln_event_desc_free(void *data);
static int
Expand Down Expand Up @@ -1303,13 +1303,13 @@ MLN_FUNC_VOID(static inline, void, mln_event_fheap_timer_copy, (void *k1, void *
/*
* chains
*/
MLN_CHAIN_FUNC_DEFINE(ev_fd_wait, \
MLN_CHAIN_FUNC_DEFINE(static inline, \
ev_fd_wait, \
mln_event_desc_t, \
static inline void, \
prev, \
next);
MLN_CHAIN_FUNC_DEFINE(ev_fd_active, \
MLN_CHAIN_FUNC_DEFINE(static inline, \
ev_fd_active, \
mln_event_desc_t, \
static inline void, \
act_prev, \
act_next);
10 changes: 5 additions & 5 deletions src/mln_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include <errno.h>
#include <sys/time.h>

MLN_CHAIN_FUNC_DECLARE(reg_file, \
mln_file_t, \
static inline void,);
MLN_CHAIN_FUNC_DECLARE(static inline, \
reg_file, \
mln_file_t, );
static int mln_file_set_cmp(const void *data1, const void *data2);
static void mln_file_free(void *pfile);

Expand Down Expand Up @@ -213,9 +213,9 @@ MLN_FUNC(, mln_file_t *, mln_file_tmp_open, (mln_alloc_t *pool), (pool), {
})


MLN_CHAIN_FUNC_DEFINE(reg_file, \
MLN_CHAIN_FUNC_DEFINE(static inline, \
reg_file, \
mln_file_t, \
static inline void, \
prev, \
next);

10 changes: 5 additions & 5 deletions src/mln_fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ mln_rbtree_t *worker_ipc_tree = NULL;
clr_handler rs_clr_handler = NULL;
void *rs_clr_data = NULL;

MLN_CHAIN_FUNC_DECLARE(worker_list, \
mln_fork_t, \
static inline void,);
MLN_CHAIN_FUNC_DECLARE(static inline, \
worker_list, \
mln_fork_t, );
static int
mln_fork_rbtree_cmp(const void *k1, const void *k2) __NONNULL2(1,2);
static int
Expand Down Expand Up @@ -935,9 +935,9 @@ MLN_FUNC_VOID(static, void, mln_ipc_fd_handler_worker_send, \


/*chain*/
MLN_CHAIN_FUNC_DEFINE(worker_list, \
MLN_CHAIN_FUNC_DEFINE(static inline, \
worker_list, \
mln_fork_t, \
static inline void, \
prev, \
next);
#endif
Expand Down
20 changes: 10 additions & 10 deletions src/mln_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
#include <stdlib.h>
#include <stdio.h>

MLN_CHAIN_FUNC_DECLARE(mln_gc_item, \
mln_gc_item_t, \
static inline void,);
MLN_CHAIN_FUNC_DEFINE(mln_gc_item, \
MLN_CHAIN_FUNC_DECLARE(static inline, \
mln_gc_item, \
mln_gc_item_t, );
MLN_CHAIN_FUNC_DEFINE(static inline, \
mln_gc_item, \
mln_gc_item_t, \
static inline void, \
prev, \
next);
MLN_CHAIN_FUNC_DECLARE(mln_gc_item_proc, \
mln_gc_item_t, \
static inline void,);
MLN_CHAIN_FUNC_DEFINE(mln_gc_item_proc, \
MLN_CHAIN_FUNC_DECLARE(static inline, \
mln_gc_item_proc, \
mln_gc_item_t, );
MLN_CHAIN_FUNC_DEFINE(static inline, \
mln_gc_item_proc, \
mln_gc_item_t, \
static inline void, \
proc_prev, \
proc_next);

Expand Down
20 changes: 10 additions & 10 deletions src/mln_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
#include <stdio.h>
#include <string.h>

MLN_CHAIN_FUNC_DECLARE(mln_hash_entry_iter, \
mln_hash_entry_t, \
static inline void,);
MLN_CHAIN_FUNC_DECLARE(mln_hash_entry, \
mln_hash_entry_t, \
static inline void,);
MLN_CHAIN_FUNC_DECLARE(static inline, \
mln_hash_entry_iter, \
mln_hash_entry_t, );
MLN_CHAIN_FUNC_DECLARE(static inline, \
mln_hash_entry, \
mln_hash_entry_t, );
static inline mln_hash_entry_t *
mln_hash_entry_new(mln_hash_t *h, mln_hash_mgr_t *mgr, void *key, void *val) __NONNULL4(1,2,3,4);
static inline void
Expand Down Expand Up @@ -513,14 +513,14 @@ MLN_FUNC_VOID(, void, mln_hash_reset, (mln_hash_t *h, mln_hash_flag_t flg), (h,
h->iter = h->iter_head = h->iter_tail = NULL;
})

MLN_CHAIN_FUNC_DEFINE(mln_hash_entry, \
MLN_CHAIN_FUNC_DEFINE(static inline, \
mln_hash_entry, \
mln_hash_entry_t, \
static inline void, \
prev, \
next);
MLN_CHAIN_FUNC_DEFINE(mln_hash_entry_iter, \
MLN_CHAIN_FUNC_DEFINE(static inline, \
mln_hash_entry_iter, \
mln_hash_entry_t, \
static inline void, \
iter_prev, \
iter_next);

Loading

0 comments on commit fbfa36f

Please sign in to comment.