1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 * Description: List Header
15 */
16
17 #ifndef LIST_H
18 #define LIST_H
19
20 #include "common_def.h"
21
22 /** @defgroup DRIVER_PLATFORM_LIST CHIP Platform LIST Driver
23 * @ingroup DRIVER_PLATFORM
24 * @{
25 */
26 #define LIST_POISON1 ((void *)0x10100100)
27 #define LIST_POISON2 ((void *)0x20200200)
28
29 struct list_head {
30 struct list_head *next, *prev;
31 };
32
INIT_LIST_HEAD(struct list_head * node)33 static inline void INIT_LIST_HEAD(struct list_head *node)
34 {
35 node->next = node;
36 node->prev = node;
37 }
38
list_add_node(struct list_head * cur,struct list_head * prev,struct list_head * next)39 static inline void list_add_node(struct list_head *cur, struct list_head *prev, struct list_head *next)
40 {
41 next->prev = cur;
42 cur->next = next;
43 cur->prev = prev;
44 prev->next = cur;
45 }
46
list_add(struct list_head * cur,struct list_head * head)47 static inline void list_add(struct list_head *cur, struct list_head *head)
48 {
49 list_add_node(cur, head, head->next);
50 }
51
list_add_tail(struct list_head * cur,struct list_head * head)52 static inline void list_add_tail(struct list_head *cur, struct list_head *head)
53 {
54 list_add_node(cur, head->prev, head);
55 }
56
list_del_node(struct list_head * prev,struct list_head * next)57 static inline void list_del_node(struct list_head *prev, struct list_head *next)
58 {
59 next->prev = prev;
60 prev->next = next;
61 }
62
list_del(struct list_head * node)63 static inline void list_del(struct list_head *node)
64 {
65 list_del_node(node->prev, node->next);
66 node->next = (struct list_head*)LIST_POISON1;
67 node->prev = (struct list_head*)LIST_POISON2;
68 }
69
list_empty(const struct list_head * list)70 static inline int list_empty(const struct list_head *list)
71 {
72 return list->next == list;
73 }
74
75 #ifndef LOSCFG_LIB_LIBC
76 #ifndef offsetof
77 #define offsetof(type, member) ((unsigned int)&((type *)0)->member)
78 #endif
79 #endif
80
81 #ifndef container_of
82 #define container_of(cur, type, member) ((type *)((char *)(cur) - offsetof(type, member)))
83 #endif
84
85 #define list_entry(cur, type, member) container_of(cur, type, member)
86
87 #define list_first_entry(cur, type, member) container_of((cur)->next, type, member)
88
89 #define list_next_entry(cur, type, member) container_of((cur)->member.next, type, member)
90
91 #define list_for_each_entry(cur, node_type, head, member) \
92 for ((cur) = list_first_entry(head, node_type, member); \
93 &(cur)->member != (head); \
94 (cur) = list_next_entry(cur, node_type, member))
95
96 #define list_for_each_entry_safe(cur, cur_next, node_type, head, member) \
97 for ((cur) = list_first_entry(head, node_type, member), \
98 (cur_next) = list_next_entry(cur, node_type, member); \
99 &(cur)->member != (head); \
100 (cur) = (cur_next), (cur_next) = list_next_entry(cur, node_type, member))
101
102 /**
103 * @}
104 */
105 #endif
106