Modify licenses
[scp/ocu/5gnr.git] / Include / vos_linklist.h
1 /******************************************************************************
2 *
3 *   Copyright (c) 2020 ICT/CAS.
4 *
5 *   Licensed under the O-RAN Software License, Version 1.0 (the "Software License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       https://www.o-ran.org/software
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19
20
21
22 #ifndef _VOS_LINKLIST_H
23 #define _VOS_LINKLIST_H
24
25 #ifdef __cplusplus
26 extern"C"{
27 #endif
28
29 /** 链表节点 */
30 typedef struct cl_lib_listnode
31 {
32     struct cl_lib_listnode *next;
33     struct cl_lib_listnode *prev;
34     void *data;
35 }*plistnode;
36
37 /** 链表名长度 */
38 #define LIST_NAME_LEN (48)
39
40 /** 链表节 */
41 typedef struct cl_lib_list
42 {
43     struct cl_lib_listnode *head;
44     struct cl_lib_listnode *tail;
45     char name[LIST_NAME_LEN];
46     unsigned long moduleID;
47     unsigned int count;
48     int ( *cmp ) ( void *val1, void *val2 );
49     void ( *del ) ( void *val );
50 }*plist;
51
52
53 /** 获得链表头 */
54 #define cl_lib_listhead(X) ((X)->head)
55
56 /** 获得链表尾 */
57 #define cl_lib_listtail(X) ((X)->tail)
58
59 /** 获得下个节点 */
60 #define cl_lib_nextnode(X) ((X) = (X)->next)
61
62 /** 获得上个节点 */
63 #define cl_lib_prevnode(X) ((X) = (X)->prev)
64
65 /** 获得链表中节点数量 */
66 #define cl_lib_listcount(X) ((X)->count)
67
68 /** 链表为空 */
69 #define cl_lib_list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
70
71 /** 获得节点中的数据 */
72 #define cl_lib_getdata(X) ((X)->data)
73
74 /**
75  * 创建一个linklist
76  * @param[in]   name      list名  最大长度 LIST_NAME_LEN
77  * @param[in]   moduleID  创建list的模块ID
78  * @return      成功时返回新list地址,失败则返回NULL。
79  */
80 struct cl_lib_list *cl_lib_list_new (char *name,unsigned long moduleID);
81
82
83
84 /**
85  * free并删除linklist中的所有节点,然后free该list
86  * !!!注意,如果data也需要free,则需要实现list的del成员
87  * @param[in]   plist1      要删除的list
88  */
89 void cl_lib_list_delete ( struct cl_lib_list * plist1 );
90
91 /**
92  * 将数据添加到链表尾,直接传入要添加的数据,该API会自动创建节点
93  * @param[in]   plist1      待添加的list
94  * @param[in]   val         待添加的数据
95  */
96 void cl_lib_listnode_add ( struct cl_lib_list * plist1, void * val );
97
98
99 /**
100  * 将数据添加到链表头,直接传入要添加的数据,该API会自动创建节点
101  * @param[in]   plist1      待添加的list
102  * @param[in]   val         待添加的数据
103  */
104 void cl_lib_listnode_add_toHead ( struct cl_lib_list * plist1, void * val );
105
106 /**
107  * 删除数据等于val的节点,!!!不会free 数据
108  * @param[in]   plist1      待删除的list
109  * @param[in]   val         待删除的数据
110  */
111 void cl_lib_listnode_delete ( struct cl_lib_list * plist1, void * val );
112
113 /**
114  * 根据注册的cmp函数删除节点,cmd 返回值为0 时删除,!!!不会free 数据
115  * @param[in]   plist1      待删除的list
116  * @param[in]   val         cmp 函数参数
117  * @return      成功时返回被删除节点数据地址,用于free数据,失败则返回NULL。
118  */
119 void *cl_lib_listnode_cmp_delete ( struct cl_lib_list * plist1, void * val );
120
121 /**
122  * 在当前节点前插入数据
123  * @param[in]   plist1      待插入数据的list
124  * @param[in]   currentnode 当前节点
125  * @param[in]   val         待插入数据
126  */
127 void cl_lib_list_add_node_prev ( plist plist1, plistnode currentnode, void *val );
128
129 /**
130  * 在当前节点后插入数据
131  * @param[in]   plist1      待插入数据的list
132  * @param[in]   currentnode 当前节点
133  * @param[in]   val         待插入数据
134  */
135 void cl_lib_list_add_node_next ( plist plist1, plistnode currentnode, void *val );
136
137 /**
138  * 节点查找 API,根据给定val查找
139  * !!!用此API 需要确保 plist->cmp 成员已赋值,否则无法查找
140  * !!!不可修改获得节点的 prev 和 next指针
141  * !!!不可释放获得节点
142  * !!!如果替换获得节点的data数据,如果旧的data需要释放,需自己手动释放data数据
143  * @param[in]   plist1      要查找的list
144  * @param[in]   data        进行比较的数据
145  * @return      找到返回节点指针,未找到返回 NULL。
146  */
147 plistnode cl_lib_listnode_lookup_by_val( plist plist1, void *data );
148
149
150 /**
151  * 节点查找 API,根据给定val和cmp方法查找
152  * !!!不可修改获得节点的 prev 和 next指针
153  * !!!不可释放获得节点
154  * !!!如果替换获得节点的data数据,如果旧的data需要释放,需自己手动释放data数据
155  * @param[in]   plist1      要查找的list
156  * @param[in]   cmp         比较函数,当val1 与val2相等时返回0,不相等时返回非0值
157  * @param[in]   data        进行比较的数据
158  * @return      找到返回节点指针,未找到返回 NULL。
159  */
160 plistnode cl_lib_listnode_lookup_by_func( plist plist1, int (*cmp)(void *val1, void *val2),void *data);
161
162
163 /**
164  * 查找到的节点,如果想要从链表中删除,必须使用该接口
165  * !!!注意,如果data也需要free,则需要实现list的del成员
166  * @param[in]   plist1      list
167  * @param[in]   node        要删除的节点
168  */
169 void cl_lib_list_delete_node ( plist plist1, plistnode node );
170
171
172 #ifdef __cplusplus
173 }
174 #endif
175
176 #endif /* _VOS_LINKLIST_H */