list.h revision 1.25 1 /* $NetBSD: list.h,v 1.25 2021/12/19 10:39:28 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Notes on porting:
34 *
35 * - LIST_HEAD(x) means a declaration `struct list_head x =
36 * LIST_HEAD_INIT(x)' in Linux, but something else in NetBSD.
37 * Replace by the expansion.
38 */
39
40 #ifndef _LINUX_LIST_H_
41 #define _LINUX_LIST_H_
42
43 #include <sys/null.h>
44 #include <sys/pslist.h>
45 #include <sys/queue.h>
46
47 #include <linux/kernel.h>
48 #include <linux/types.h>
49
50 /*
51 * Doubly-linked lists. Type defined in <linux/types.h>.
52 */
53
54 #define LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) }
55
56 #define LINUX_LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
57
58 static inline void
59 INIT_LIST_HEAD(struct list_head *head)
60 {
61 head->prev = head;
62 head->next = head;
63 }
64
65 static inline struct list_head *
66 list_first(const struct list_head *head)
67 {
68 return head->next;
69 }
70
71 static inline struct list_head *
72 list_last(const struct list_head *head)
73 {
74 return head->prev;
75 }
76
77 static inline struct list_head *
78 list_next(const struct list_head *node)
79 {
80 return node->next;
81 }
82
83 static inline struct list_head *
84 list_prev(const struct list_head *node)
85 {
86 return node->prev;
87 }
88
89 static inline int
90 list_empty(const struct list_head *head)
91 {
92 return (head->next == head);
93 }
94
95 static inline int
96 list_is_singular(const struct list_head *head)
97 {
98
99 if (list_empty(head))
100 return false;
101 if (head->next != head->prev)
102 return false;
103 return true;
104 }
105
106 static inline bool
107 list_is_first(const struct list_head *entry, const struct list_head *head)
108 {
109 return head == entry->prev;
110
111 }
112
113 static inline bool
114 list_is_last(const struct list_head *entry, const struct list_head *head)
115 {
116 return head == entry->next;
117 }
118
119 static inline void
120 __list_add_between(struct list_head *prev, struct list_head *node,
121 struct list_head *next)
122 {
123 prev->next = node;
124 node->prev = prev;
125 node->next = next;
126 next->prev = node;
127 }
128
129 static inline void
130 list_add(struct list_head *node, struct list_head *head)
131 {
132 __list_add_between(head, node, head->next);
133 }
134
135 static inline void
136 list_add_tail(struct list_head *node, struct list_head *head)
137 {
138 __list_add_between(head->prev, node, head);
139 }
140
141 static inline void
142 __list_del_entry(struct list_head *entry)
143 {
144 entry->prev->next = entry->next;
145 entry->next->prev = entry->prev;
146 }
147
148 static inline void
149 list_del(struct list_head *entry)
150 {
151 __list_del_entry(entry);
152 entry->next = (void *)(uintptr_t)1;
153 entry->prev = (void *)(uintptr_t)2;
154 }
155
156 static inline void
157 __list_splice_between(struct list_head *prev, const struct list_head *list,
158 struct list_head *next)
159 {
160 struct list_head *first = list->next;
161 struct list_head *last = list->prev;
162
163 first->prev = prev;
164 prev->next = first;
165
166 last->next = next;
167 next->prev = last;
168 }
169
170 static inline void
171 list_splice(const struct list_head *list, struct list_head *head)
172 {
173 if (!list_empty(list))
174 __list_splice_between(head, list, head->next);
175 }
176
177 static inline void
178 list_splice_init(struct list_head *list, struct list_head *head)
179 {
180 if (!list_empty(list)) {
181 __list_splice_between(head, list, head->next);
182 INIT_LIST_HEAD(list);
183 }
184 }
185
186 static inline void
187 list_splice_tail(const struct list_head *list, struct list_head *head)
188 {
189 if (!list_empty(list))
190 __list_splice_between(head->prev, list, head);
191 }
192
193 static inline void
194 list_splice_tail_init(struct list_head *list, struct list_head *head)
195 {
196 if (!list_empty(list)) {
197 __list_splice_between(head->prev, list, head);
198 INIT_LIST_HEAD(list);
199 }
200 }
201
202 static inline void
203 list_move(struct list_head *node, struct list_head *head)
204 {
205 list_del(node);
206 list_add(node, head);
207 }
208
209 static inline void
210 list_move_tail(struct list_head *node, struct list_head *head)
211 {
212 list_del(node);
213 list_add_tail(node, head);
214 }
215
216 static inline void
217 list_replace(struct list_head *old, struct list_head *new)
218 {
219 new->prev = old->prev;
220 old->prev->next = new;
221 new->next = old->next;
222 old->next->prev = new;
223 }
224
225 static inline void
226 list_replace_init(struct list_head *old, struct list_head *new)
227 {
228 list_replace(old, new);
229 INIT_LIST_HEAD(old);
230 }
231
232 static inline void
233 list_del_init(struct list_head *node)
234 {
235 list_del(node);
236 INIT_LIST_HEAD(node);
237 }
238
239 #define list_entry(PTR, TYPE, FIELD) container_of(PTR, TYPE, FIELD)
240 #define list_first_entry(PTR, TYPE, FIELD) \
241 list_entry(list_first((PTR)), TYPE, FIELD)
242 #define list_first_entry_or_null(PTR, TYPE, FIELD) \
243 (list_empty((PTR)) ? NULL : list_entry(list_first((PTR)), TYPE, FIELD))
244 #define list_last_entry(PTR, TYPE, FIELD) \
245 list_entry(list_last((PTR)), TYPE, FIELD)
246 #define list_next_entry(ENTRY, FIELD) \
247 list_entry(list_next(&(ENTRY)->FIELD), typeof(*(ENTRY)), FIELD)
248 #define list_prev_entry(ENTRY, FIELD) \
249 list_entry(list_prev(&(ENTRY)->FIELD), typeof(*(ENTRY)), FIELD)
250
251 #define list_for_each(VAR, HEAD) \
252 for ((VAR) = list_first((HEAD)); \
253 (VAR) != (HEAD); \
254 (VAR) = list_next((VAR)))
255
256 #define list_for_each_safe(VAR, NEXT, HEAD) \
257 for ((VAR) = list_first((HEAD)); \
258 ((VAR) != (HEAD)) && ((NEXT) = list_next((VAR)), 1); \
259 (VAR) = (NEXT))
260
261 #define list_for_each_entry(VAR, HEAD, FIELD) \
262 for ((VAR) = list_entry(list_first((HEAD)), typeof(*(VAR)), FIELD); \
263 &(VAR)->FIELD != (HEAD); \
264 (VAR) = list_entry(list_next(&(VAR)->FIELD), typeof(*(VAR)), \
265 FIELD))
266
267 #define list_for_each_entry_reverse(VAR, HEAD, FIELD) \
268 for ((VAR) = list_entry(list_last((HEAD)), typeof(*(VAR)), FIELD); \
269 &(VAR)->FIELD != (HEAD); \
270 (VAR) = list_entry(list_prev(&(VAR)->FIELD), typeof(*(VAR)), \
271 FIELD))
272
273 #define list_for_each_entry_safe(VAR, NEXT, HEAD, FIELD) \
274 for ((VAR) = list_entry(list_first((HEAD)), typeof(*(VAR)), FIELD); \
275 (&(VAR)->FIELD != (HEAD)) && \
276 ((NEXT) = list_entry(list_next(&(VAR)->FIELD), \
277 typeof(*(VAR)), FIELD), 1); \
278 (VAR) = (NEXT))
279
280 #define list_for_each_entry_continue(VAR, HEAD, FIELD) \
281 for ((VAR) = list_next_entry((VAR), FIELD); \
282 &(VAR)->FIELD != (HEAD); \
283 (VAR) = list_next_entry((VAR), FIELD))
284
285 #define list_for_each_entry_continue_reverse(VAR, HEAD, FIELD) \
286 for ((VAR) = list_prev_entry((VAR), FIELD); \
287 &(VAR)->FIELD != (HEAD); \
288 (VAR) = list_prev_entry((VAR), FIELD))
289
290 #define list_for_each_entry_from(VAR, HEAD, FIELD) \
291 for (; \
292 (&(VAR)->FIELD != (HEAD)); \
293 (VAR) = list_next_entry((VAR), FIELD))
294
295 #define list_for_each_entry_safe_from(VAR, NEXT, HEAD, FIELD) \
296 for (; \
297 (&(VAR)->FIELD != (HEAD)) && \
298 ((NEXT) = list_next_entry((VAR), FIELD)); \
299 (VAR) = (NEXT))
300
301 /*
302 * `H'ead-only/`H'ash-table doubly-linked lists.
303 */
304
305 #define hlist_head pslist_head
306 #define hlist_node pslist_entry
307
308 #define HLIST_HEAD_INIT PSLIST_INITIALIZER
309 #define INIT_HLIST_HEAD PSLIST_INIT
310 #define hlist_empty(h) (pslist_writer_first(h) == NULL)
311 #define hlist_first pslist_writer_first
312 #define hlist_next pslist_writer_next
313
314 static inline void
315 hlist_add_head(struct hlist_node *node, struct hlist_head *head)
316 {
317
318 pslist_entry_init(node);
319 pslist_writer_insert_head(head, node);
320 }
321
322 static inline void
323 hlist_del(struct hlist_node *node)
324 {
325
326 pslist_writer_remove(node);
327 /* XXX abstraction violation */
328 node->ple_prevp = _PSLIST_POISON;
329 }
330
331 static inline void
332 hlist_del_init(struct hlist_node *node)
333 {
334
335 /* XXX abstraction violation */
336 if (node->ple_prevp != NULL)
337 pslist_writer_remove(node);
338 }
339
340 #define hlist_entry(PTR, TYPE, FIELD) container_of(PTR, TYPE, FIELD)
341 #define hlist_for_each(VAR, HEAD) \
342 for ((VAR) = hlist_first(HEAD); (VAR) != NULL; (VAR) = hlist_next(VAR))
343 #define hlist_for_each_safe(VAR, NEXT, HEAD) \
344 for ((VAR) = hlist_first(HEAD); \
345 (VAR) != NULL && ((NEXT) = hlist_next(HEAD), 1); \
346 (VAR) = (NEXT))
347 #define hlist_for_each_entry(VAR, HEAD, FIELD) \
348 for ((VAR) = (hlist_first(HEAD) == NULL ? NULL : \
349 hlist_entry(hlist_first(HEAD), typeof(*(VAR)), \
350 FIELD)); \
351 (VAR) != NULL; \
352 (VAR) = (hlist_next(&(VAR)->FIELD) == NULL ? NULL : \
353 hlist_entry(hlist_next(&(VAR)->FIELD), typeof(*(VAR)),\
354 FIELD)))
355
356 #define hlist_for_each_entry_safe(VAR, NEXT, HEAD, FIELD) \
357 for ((VAR) = (hlist_first(HEAD) == NULL ? NULL : \
358 hlist_entry(hlist_first(HEAD), typeof(*(VAR)), \
359 FIELD)); \
360 ((VAR) != NULL && \
361 ((NEXT) = hlist_next(&(VAR)->FIELD), 1)); \
362 (VAR) = hlist_entry((NEXT), typeof(*(VAR)), FIELD))
363
364 static inline void
365 hlist_add_behind_rcu(struct hlist_node *node, struct hlist_node *prev)
366 {
367
368 pslist_entry_init(node);
369 pslist_writer_insert_after(prev, node);
370 }
371
372 static inline void
373 hlist_add_head_rcu(struct hlist_node *node, struct hlist_head *head)
374 {
375
376 pslist_entry_init(node);
377 pslist_writer_insert_head(head, node);
378 }
379
380 #define hlist_del_init_rcu hlist_del_init /* no special needs */
381
382 #define hlist_first_rcu pslist_reader_first
383 #define hlist_next_rcu pslist_reader_next
384
385 #define hlist_for_each_entry_rcu(VAR, HEAD, FIELD) \
386 for ((VAR) = (hlist_first_rcu(HEAD) == NULL ? NULL : \
387 hlist_entry(hlist_first_rcu(HEAD), typeof(*(VAR)), \
388 FIELD)); \
389 (VAR) != NULL; \
390 (VAR) = (hlist_next_rcu(&(VAR)->FIELD) == NULL ? NULL : \
391 hlist_entry(hlist_next_rcu(&(VAR)->FIELD), \
392 typeof(*(VAR)), FIELD)))
393
394 #endif /* _LINUX_LIST_H_ */
395