1 /******************************************************************************
3 * Copyright (c) 2020 ICT/CAS.
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
9 * https://www.o-ran.org/software
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.
17 *******************************************************************************/
22 #ifndef _VOS_LINKLIST_H
23 #define _VOS_LINKLIST_H
30 typedef struct cl_lib_listnode
32 struct cl_lib_listnode *next;
33 struct cl_lib_listnode *prev;
38 #define LIST_NAME_LEN (48)
41 typedef struct cl_lib_list
43 struct cl_lib_listnode *head;
44 struct cl_lib_listnode *tail;
45 char name[LIST_NAME_LEN];
46 unsigned long moduleID;
48 int ( *cmp ) ( void *val1, void *val2 );
49 void ( *del ) ( void *val );
54 #define cl_lib_listhead(X) ((X)->head)
57 #define cl_lib_listtail(X) ((X)->tail)
60 #define cl_lib_nextnode(X) ((X) = (X)->next)
63 #define cl_lib_prevnode(X) ((X) = (X)->prev)
66 #define cl_lib_listcount(X) ((X)->count)
69 #define cl_lib_list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
72 #define cl_lib_getdata(X) ((X)->data)
76 * @param[in] name list名 最大长度 LIST_NAME_LEN
77 * @param[in] moduleID 创建list的模块ID
78 * @return 成功时返回新list地址,失败则返回NULL。
80 struct cl_lib_list *cl_lib_list_new (char *name,unsigned long moduleID);
85 * free并删除linklist中的所有节点,然后free该list
86 * !!!注意,如果data也需要free,则需要实现list的del成员
87 * @param[in] plist1 要删除的list
89 void cl_lib_list_delete ( struct cl_lib_list * plist1 );
92 * 将数据添加到链表尾,直接传入要添加的数据,该API会自动创建节点
93 * @param[in] plist1 待添加的list
94 * @param[in] val 待添加的数据
96 void cl_lib_listnode_add ( struct cl_lib_list * plist1, void * val );
100 * 将数据添加到链表头,直接传入要添加的数据,该API会自动创建节点
101 * @param[in] plist1 待添加的list
102 * @param[in] val 待添加的数据
104 void cl_lib_listnode_add_toHead ( struct cl_lib_list * plist1, void * val );
107 * 删除数据等于val的节点,!!!不会free 数据
108 * @param[in] plist1 待删除的list
109 * @param[in] val 待删除的数据
111 void cl_lib_listnode_delete ( struct cl_lib_list * plist1, void * val );
114 * 根据注册的cmp函数删除节点,cmd 返回值为0 时删除,!!!不会free 数据
115 * @param[in] plist1 待删除的list
116 * @param[in] val cmp 函数参数
117 * @return 成功时返回被删除节点数据地址,用于free数据,失败则返回NULL。
119 void *cl_lib_listnode_cmp_delete ( struct cl_lib_list * plist1, void * val );
123 * @param[in] plist1 待插入数据的list
124 * @param[in] currentnode 当前节点
125 * @param[in] val 待插入数据
127 void cl_lib_list_add_node_prev ( plist plist1, plistnode currentnode, void *val );
131 * @param[in] plist1 待插入数据的list
132 * @param[in] currentnode 当前节点
133 * @param[in] val 待插入数据
135 void cl_lib_list_add_node_next ( plist plist1, plistnode currentnode, void *val );
139 * !!!用此API 需要确保 plist->cmp 成员已赋值,否则无法查找
140 * !!!不可修改获得节点的 prev 和 next指针
142 * !!!如果替换获得节点的data数据,如果旧的data需要释放,需自己手动释放data数据
143 * @param[in] plist1 要查找的list
144 * @param[in] data 进行比较的数据
145 * @return 找到返回节点指针,未找到返回 NULL。
147 plistnode cl_lib_listnode_lookup_by_val( plist plist1, void *data );
151 * 节点查找 API,根据给定val和cmp方法查找
152 * !!!不可修改获得节点的 prev 和 next指针
154 * !!!如果替换获得节点的data数据,如果旧的data需要释放,需自己手动释放data数据
155 * @param[in] plist1 要查找的list
156 * @param[in] cmp 比较函数,当val1 与val2相等时返回0,不相等时返回非0值
157 * @param[in] data 进行比较的数据
158 * @return 找到返回节点指针,未找到返回 NULL。
160 plistnode cl_lib_listnode_lookup_by_func( plist plist1, int (*cmp)(void *val1, void *val2),void *data);
164 * 查找到的节点,如果想要从链表中删除,必须使用该接口
165 * !!!注意,如果data也需要free,则需要实现list的del成员
166 * @param[in] plist1 list
167 * @param[in] node 要删除的节点
169 void cl_lib_list_delete_node ( plist plist1, plistnode node );
176 #endif /* _VOS_LINKLIST_H */