• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file hi_list_sdk.h
3  *
4  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
5  * Licensed under the Apache License, Version 2.0 (the "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  *     http://www.apache.org/licenses/LICENSE-2.0
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   * @defgroup iot_list List
20   * @ingroup system
21   */
22 #ifndef __HI_LIST_SDK_H__
23 #define __HI_LIST_SDK_H__
24 
25 #include <hi_types_base.h>
26 
27 /****************************************************************************/
28 HI_START_HEADER
29 
30 /*
31  * Note: This file renames the linked list interface and uses the LTOS interface to implement it again.
32  * CNcomment:备注:本文件对链表接口重新命名,使用LTOS接口重新实现。
33  */
34 typedef struct hi_list {
35     struct hi_list *prev;
36     struct hi_list *next;
37 } hi_list;
38 
39 /*
40  * Function description:
41  * Initialize the head node. Note that this node is used only for management, not the data node entered by the user.
42  * CNcomment:功能描述:初始化头节点,注意此节点仅用于管理,不是用户输入的数据节点
43  */
hi_list_init(hi_list * list)44 __attribute__((always_inline)) static inline hi_void hi_list_init(hi_list *list)
45 {
46     list->next = list;
47     list->prev = list;
48 }
49 
50 /*
51  * Function description: Inserts a node as the first node in the list.
52  * CNcomment:功能描述:把node插入为list的第一个节点
53  */
hi_list_head_insert(hi_list * node,hi_list * list)54 __attribute__((always_inline)) static inline hi_void hi_list_head_insert(hi_list *node, hi_list *list)
55 {
56     node->next = list->next;
57     node->prev = list;
58     list->next->prev = node;
59     list->next = node;
60 }
61 
62 /*
63  * Function description: Inserts a node as the last node in the list.
64  * CNcomment:功能描述:把node插入为list的最后一个节点
65  */
hi_list_tail_insert(hi_list * node,hi_list * list)66 __attribute__((always_inline)) static inline hi_void hi_list_tail_insert(hi_list *node, hi_list *list)
67 {
68     hi_list_head_insert(node, list->prev);
69 }
70 
71 /*
72  * Function description: Deletes a node from the linked list.
73  * CNcomment:功能描述:从链表中删除某个节点
74  */
hi_list_delete(hi_list * node)75 __attribute__((always_inline)) static inline hi_void hi_list_delete(hi_list *node)
76 {
77     if (node->next == HI_NULL || node->prev == HI_NULL) {
78         return;
79     }
80 
81     node->next->prev = node->prev;
82     node->prev->next = node->next;
83     node->next = (hi_list *)HI_NULL;
84     node->prev = (hi_list *)HI_NULL;
85 }
86 
87 /*
88  * Function description: Deletes the first node in the linked list without releasing the related memory.
89  * CNcomment:功能描述:删除链表的第一个节点,不释放相关内存
90  */
hi_list_delete_head(hi_list * list)91 __attribute__((always_inline)) static inline hi_list *hi_list_delete_head(hi_list *list)
92 {
93     hi_list *del_node;
94 
95     del_node = list->next;
96     if (del_node == list || del_node == HI_NULL) {
97         return HI_NULL;
98     }
99 
100     hi_list_delete(del_node);
101     return del_node;
102 }
103 
104 /*
105  * Function description: Deletes the tail node of the linked list without releasing the related memory.
106  * CNcomment:功能描述:删除链表尾部节点,不释放相关内存
107  */
hi_list_delete_tail(hi_list * list)108 __attribute__((always_inline)) static inline hi_list *hi_list_delete_tail(hi_list *list)
109 {
110     hi_list *del_node;
111 
112     del_node = list->prev;
113     if (del_node == list || del_node == HI_NULL) {
114         return HI_NULL;
115     }
116 
117     hi_list_delete(del_node);
118     return del_node;
119 }
120 
121 /*
122  * Function description: Determines whether the linked list is empty.
123  * CNcomment:功能描述:判断链表是否为空
124  */
hi_is_list_empty(hi_list * list)125 __attribute__((always_inline)) static inline hi_bool hi_is_list_empty(hi_list *list)
126 {
127     if (list->next == HI_NULL || list->prev == HI_NULL) {
128         return HI_TRUE;
129     }
130     return (hi_bool)(list->next == list);
131 }
132 
133 /*
134  * Function description: Deinitializes the linked list. The management node clears the list.
135  * Other member nodes are still connected in a bidirectional linked list.
136  * CNcomment:功能描述:去初始化链表,管理节点清空,其他成员节点首尾相连仍然是一个双向链表。
137  */
hi_list_del_init(hi_list * list)138 __attribute__((always_inline)) static inline hi_void hi_list_del_init(hi_list *list)
139 {
140     list->next->prev = list->prev;
141     list->prev->next = list->next;
142 
143     list->next = list;
144     list->prev = list;
145 }
146 
147 /*
148  * Function description: Adds linked list 2 to the end of linked list 1.
149  * CNcomment:功能描述:将链表2加入链表1的尾部
150  */
hi_list_join_tail(hi_list * list1,hi_list * list2)151 __attribute__((always_inline)) static inline hi_void hi_list_join_tail(hi_list *list1, hi_list *list2)
152 {
153     list1->prev->next = list2->next;
154     list2->next->prev = list1->prev;
155     list2->prev->next = list1;
156     list1->prev = list2->prev;
157 }
158 
159 /*
160  * Function description: Adds linked list 2 to the header of linked list 1.
161  * CNcomment:功能描述:将链表2加入链表1的头部
162  */
hi_list_join_head(hi_list * list1,hi_list * list2)163 __attribute__((always_inline)) static inline hi_void hi_list_join_head(hi_list *list1, hi_list *list2)
164 {
165     /* list2 is empty. */
166     if (list2->next == list2) {
167         return;
168     }
169 
170     list2->prev->next = list1->next;
171     list1->next->prev = list2->prev;
172     list1->next = list2->next;
173     list2->next->prev = list1;
174 }
175 
176 /*
177  * Function description: Extracts the first element from the last_node element in linked list 2 and
178  * adds the first element to the header of empty linked list 1.
179  * CNcomment:功能描述:将链表2中从第一个元素到last_node元素摘出, 加入空链表1的头部
180  */
hi_list_remove_head(hi_list * list1,hi_list * list2,hi_list * last_node)181 __attribute__((always_inline)) static inline hi_void hi_list_remove_head(hi_list *list1, hi_list *list2,
182                                                                          hi_list *last_node)
183 {
184     list1->next = list2->next;
185     list1->prev = last_node;
186 
187     list2->next = last_node->next;
188     ((hi_list *)(last_node->next))->prev = list2;
189 
190     last_node->next = list1;
191     if (last_node->prev == list2) {
192         last_node->prev = list1;
193     }
194 }
195 
196 #define hi_list_init_macro(_list_name) hi_list _list_name = { (hi_list*)&(_list_name), (hi_list*)&(_list_name) }
197 
198 /*
199  * Obtains the pointer of the first node.
200  * CNcomment:获取第一个节点指针
201  */
202 #define hi_list_first(object) ((object)->next)
203 
204 #define hi_list_last(object) ((object)->prev)
205 
206 #define hi_list_entry(item, type, member) \
207     ((type*)((char*)(item) - hi_offset_of_member(type, member)))
208 
209 #define hi_list_for_each_entry(item, list, type, member)   \
210     for ((item) = hi_list_entry((list)->next, type, member); \
211          &(item)->member != (list);                          \
212          (item) = hi_list_entry((item)->member.next, type, member))
213 
214 #define hi_list_for_each_entry_safe(list, item, pnext, type, member) \
215     for ((item) = hi_list_entry((list)->next, type, member),           \
216          (pnext) = hi_list_entry((item)->member.next, type, member);     \
217          &(item)->member != (list);                                    \
218          (item) = (pnext), (pnext) = hi_list_entry((item)->member.next, type, member))
219 
220 #define hi_list_for_each_entry_continue_safe(pitem, list, item, pnext, type, member) \
221     for ((item) = hi_list_entry((pitem)->next, type, member),                          \
222          (pnext) = hi_list_entry((item)->member.next, type, member);                     \
223          &((item)->member) != (list);                                                \
224          (item) = (pnext), (pnext) = hi_list_entry((pnext)->member.next, type, member))
225 
226 /* Simple Implementation of Bidirectional Linked List Operation */
227 #define hi_list_head(list) \
228     hi_list list = { &(list), &(list) }
229 
230 #define hi_list_for_each(item, list) \
231     for ((item) = (list)->next; (item) != (list); (item) = (item)->next)
232 
233 #define hi_list_for_each_safe(item, pnext, list)                  \
234     for ((item) = (list)->next, (pnext) = (item)->next; (item) != (list); \
235          (item) = (pnext), (pnext) = (item)->next)
236 
237 HI_END_HEADER
238 #endif  /* __HI_STDLIB_H__ */
239