blob: 583e149abf5aea16d21a9e7775596edcf4340a4c [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#ifndef __LIST_H
24#define __LIST_H
25
26#include <compiler.h>
27#include <stddef.h>
28#include <stdbool.h>
29
30__BEGIN_CDECLS;
31
32#define containerof(ptr, type, member) \
33 ((type *)((addr_t)(ptr) - offsetof(type, member)))
34
35struct list_node {
36 struct list_node *prev;
37 struct list_node *next;
38};
39
40#define LIST_INITIAL_VALUE(list) { &(list), &(list) }
41#define LIST_INITIAL_CLEARED_VALUE { NULL, NULL }
42
43static inline void list_initialize(struct list_node *list)
44{
45 list->prev = list->next = list;
46}
47
48static inline void list_clear_node(struct list_node *item)
49{
50 item->prev = item->next = 0;
51}
52
53static inline bool list_in_list(struct list_node *item)
54{
55 if (item->prev == 0 && item->next == 0)
56 return false;
57 else
58 return true;
59}
60
61static inline void list_add_head(struct list_node *list, struct list_node *item)
62{
63 item->next = list->next;
64 item->prev = list;
65 list->next->prev = item;
66 list->next = item;
67}
68
69#define list_add_after(entry, new_entry) list_add_head(entry, new_entry)
70
71static inline void list_add_tail(struct list_node *list, struct list_node *item)
72{
73 item->prev = list->prev;
74 item->next = list;
75 list->prev->next = item;
76 list->prev = item;
77}
78
79#define list_add_before(entry, new_entry) list_add_tail(entry, new_entry)
80
81static inline void list_delete(struct list_node *item)
82{
83 item->next->prev = item->prev;
84 item->prev->next = item->next;
85 item->prev = item->next = 0;
86}
87
88static inline struct list_node* list_remove_head(struct list_node *list) {
89 if (list->next != list) {
90 struct list_node *item = list->next;
91 list_delete(item);
92 return item;
93 } else {
94 return NULL;
95 }
96}
97
98#define list_remove_head_type(list, type, element) ({\
99 struct list_node *__nod = list_remove_head(list);\
100 type *__t;\
101 if(__nod)\
102 __t = containerof(__nod, type, element);\
103 else\
104 __t = (type *)0;\
105 __t;\
106})
107
108static inline struct list_node* list_remove_tail(struct list_node *list) {
109 if (list->prev != list) {
110 struct list_node *item = list->prev;
111 list_delete(item);
112 return item;
113 } else {
114 return NULL;
115 }
116}
117
118#define list_remove_tail_type(list, type, element) ({\
119 struct list_node *__nod = list_remove_tail(list);\
120 type *__t;\
121 if(__nod)\
122 __t = containerof(__nod, type, element);\
123 else\
124 __t = (type *)0;\
125 __t;\
126})
127
128static inline struct list_node* list_peek_head(struct list_node *list) {
129 if (list->next != list) {
130 return list->next;
131 } else {
132 return NULL;
133 }
134}
135
136#define list_peek_head_type(list, type, element) ({\
137 struct list_node *__nod = list_peek_head(list);\
138 type *__t;\
139 if(__nod)\
140 __t = containerof(__nod, type, element);\
141 else\
142 __t = (type *)0;\
143 __t;\
144})
145
146static inline struct list_node* list_peek_tail(struct list_node *list) {
147 if (list->prev != list) {
148 return list->prev;
149 } else {
150 return NULL;
151 }
152}
153
154#define list_peek_tail_type(list, type, element) ({\
155 struct list_node *__nod = list_peek_tail(list);\
156 type *__t;\
157 if(__nod)\
158 __t = containerof(__nod, type, element);\
159 else\
160 __t = (type *)0;\
161 __t;\
162})
163
164static inline struct list_node* list_prev(struct list_node *list, struct list_node *item) {
165 if (item->prev != list)
166 return item->prev;
167 else
168 return NULL;
169}
170
171#define list_prev_type(list, item, type, element) ({\
172 struct list_node *__nod = list_prev(list, item);\
173 type *__t;\
174 if(__nod)\
175 __t = containerof(__nod, type, element);\
176 else\
177 __t = (type *)0;\
178 __t;\
179})
180
181static inline struct list_node* list_prev_wrap(struct list_node *list, struct list_node *item) {
182 if (item->prev != list)
183 return item->prev;
184 else if (item->prev->prev != list)
185 return item->prev->prev;
186 else
187 return NULL;
188}
189
190#define list_prev_wrap_type(list, item, type, element) ({\
191 struct list_node *__nod = list_prev_wrap(list, item);\
192 type *__t;\
193 if(__nod)\
194 __t = containerof(__nod, type, element);\
195 else\
196 __t = (type *)0;\
197 __t;\
198})
199
200static inline struct list_node* list_next(struct list_node *list, struct list_node *item) {
201 if (item->next != list)
202 return item->next;
203 else
204 return NULL;
205}
206
207#define list_next_type(list, item, type, element) ({\
208 struct list_node *__nod = list_next(list, item);\
209 type *__t;\
210 if(__nod)\
211 __t = containerof(__nod, type, element);\
212 else\
213 __t = (type *)0;\
214 __t;\
215})
216
217static inline struct list_node* list_next_wrap(struct list_node *list, struct list_node *item) {
218 if (item->next != list)
219 return item->next;
220 else if (item->next->next != list)
221 return item->next->next;
222 else
223 return NULL;
224}
225
226#define list_next_wrap_type(list, item, type, element) ({\
227 struct list_node *__nod = list_next_wrap(list, item);\
228 type *__t;\
229 if(__nod)\
230 __t = containerof(__nod, type, element);\
231 else\
232 __t = (type *)0;\
233 __t;\
234})
235
236// iterates over the list, node should be struct list_node*
237#define list_for_every(list, node) \
238 for(node = (list)->next; node != (list); node = node->next)
239
240// iterates over the list in a safe way for deletion of current node
241// node and temp_node should be struct list_node*
242#define list_for_every_safe(list, node, temp_node) \
243 for(node = (list)->next, temp_node = (node)->next;\
244 node != (list);\
245 node = temp_node, temp_node = (node)->next)
246
247// iterates over the list, entry should be the container structure type *
248#define list_for_every_entry(list, entry, type, member) \
249 for((entry) = containerof((list)->next, type, member);\
250 &(entry)->member != (list);\
251 (entry) = containerof((entry)->member.next, type, member))
252
253// iterates over the list in a safe way for deletion of current node
254// entry and temp_entry should be the container structure type *
255#define list_for_every_entry_safe(list, entry, temp_entry, type, member) \
256 for(entry = containerof((list)->next, type, member),\
257 temp_entry = containerof((entry)->member.next, type, member);\
258 &(entry)->member != (list);\
259 entry = temp_entry, temp_entry = containerof((temp_entry)->member.next, type, member))
260
261static inline bool list_is_empty(struct list_node *list)
262{
263 return (list->next == list) ? true : false;
264}
265
266static inline size_t list_length(struct list_node *list)
267{
268 size_t cnt = 0;
269 struct list_node *node = list;
270 list_for_every(list, node) {
271 cnt++;
272 }
273
274 return cnt;
275}
276
277__END_CDECLS;
278
279#endif