1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef SEC_UTILS_LIST_H
17 #define SEC_UTILS_LIST_H
18
19 typedef struct TagListHead {
20 struct TagListHead *next;
21 struct TagListHead *prev;
22 } ListHead;
23
24 typedef ListHead ListNode;
25
AddListNode(ListNode * item,ListNode * where)26 static inline void AddListNode(ListNode *item, ListNode *where)
27 {
28 item->next = where->next;
29 item->prev = where;
30 where->next = item;
31 item->next->prev = item;
32 }
33
AddListNodeBefore(ListNode * item,ListNode * where)34 static inline void AddListNodeBefore(ListNode *item, ListNode *where)
35 {
36 AddListNode(item, where->prev);
37 }
38
RemoveListNode(ListNode * item)39 static inline void RemoveListNode(ListNode *item)
40 {
41 item->prev->next = item->next;
42 item->next->prev = item->prev;
43 }
44
IsEmptyList(ListHead * head)45 static inline int IsEmptyList(ListHead *head)
46 {
47 return head->next == head;
48 }
49
InitListHead(ListHead * head)50 static inline void InitListHead(ListHead *head)
51 {
52 head->next = head;
53 head->prev = head;
54 }
55
56 #define INIT_LIST(list) \
57 { \
58 &(list), &(list) \
59 }
60
61 #define FOREACH_LIST_NODE(item, head) for ((item) = (head)->next; (item) != (head); (item) = (item)->next)
62
63 #define FOREACH_LIST_NODE_SAFE(item, head, temp) \
64 for ((item) = (head)->next, (temp) = (item)->next; (item) != (head); (item) = (temp), (temp) = (item)->next)
65
66 #define LIST_ENTRY(item, type, member) ((type *)((char *)(item) - (char *)(&((type *)0)->member)))
67
68 #endif /* SEC_UTILS_LIST_H */