list.h revision 1.31 1 /* $NetBSD: list.h,v 1.31 2021/12/19 11:38:03 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 static inline bool
113 list_is_last(const struct list_head *entry, const struct list_head *head)
114 {
115 return head == entry->next;
116 }
117
118 static inline void
119 __list_add_between(struct list_head *prev, struct list_head *node,
120 struct list_head *next)
121 {
122 prev->next = node;
123 node->prev = prev;
124 node->next = next;
125 next->prev = node;
126 }
127
128 static inline void
129 list_add(struct list_head *node, struct list_head *head)
130 {
131 __list_add_between(head, node, head->next);
132 }
133
134 static inline void
135 list_add_rcu(struct list_head *node, struct list_head *head)
136 {
137 struct list_head *next = head->next;
138
139 /* Initialize the new node first. */
140 node->next = next;
141 node->prev = head;
142
143 /* Now publish it. */
144 atomic_store_release(&head->next, node);
145
146 /* Fix up back pointers, not protected by RCU. */
147 next->prev = node;
148 }
149
150 static inline void
151 list_add_tail(struct list_head *node, struct list_head *head)
152 {
153 __list_add_between(head->prev, node, head);
154 }
155
156 static inline void
157 __list_del_entry(struct list_head *entry)
158 {
159 entry->prev->next = entry->next;
160 entry->next->prev = entry->prev;
161 }
162
163 static inline void
164 list_del(struct list_head *entry)
165 {
166 __list_del_entry(entry);
167 entry->next = (void *)(uintptr_t)1;
168 entry->prev = (void *)(uintptr_t)2;
169 }
170
171 static inline void
172 __list_splice_between(struct list_head *prev, const struct list_head *list,
173 struct list_head *next)
174 {
175 struct list_head *first = list->next;
176 struct list_head *last = list->prev;
177
178 first->prev = prev;
179 prev->next = first;
180
181 last->next = next;
182 next->prev = last;
183 }
184
185 static inline void
186 list_splice(const struct list_head *list, struct list_head *head)
187 {
188 if (!list_empty(list))
189 __list_splice_between(head, list, head->next);
190 }
191
192 static inline void
193 list_splice_init(struct list_head *list, struct list_head *head)
194 {
195 if (!list_empty(list)) {
196 __list_splice_between(head, list, head->next);
197 INIT_LIST_HEAD(list);
198 }
199 }
200
201 static inline void
202 list_splice_tail(const struct list_head *list, struct list_head *head)
203 {
204 if (!list_empty(list))
205 __list_splice_between(head->prev, list, head);
206 }
207
208 static inline void
209 list_splice_tail_init(struct list_head *list, struct list_head *head)
210 {
211 if (!list_empty(list)) {
212 __list_splice_between(head->prev, list, head);
213 INIT_LIST_HEAD(list);
214 }
215 }
216
217 static inline void
218 list_move(struct list_head *node, struct list_head *head)
219 {
220 list_del(node);
221 list_add(node, head);
222 }
223
224 static inline void
225 list_move_tail(struct list_head *node, struct list_head *head)
226 {
227 list_del(node);
228 list_add_tail(node, head);
229 }
230
231 static inline void
232 list_bulk_move_tail(struct list_head *head, struct list_head *first,
233 struct list_head *last)
234 {
235
236 first->prev->next = last->next;
237 last->next->prev = first->prev;
238
239 head->prev->next = first;
240 first->prev = head->prev;
241
242 last->next = head;
243 head->prev = last;
244 }
245
246 static inline void
247 list_replace(struct list_head *old, struct list_head *new)
248 {
249 new->prev = old->prev;
250 old->prev->next = new;
251 new->next = old->next;
252 old->next->prev = new;
253 }
254
255 static inline void
256 list_replace_init(struct list_head *old, struct list_head *new)
257 {
258 list_replace(old, new);
259 INIT_LIST_HEAD(old);
260 }
261
262 static inline void
263 list_del_init(struct list_head *node)
264 {
265 list_del(node);
266 INIT_LIST_HEAD(node);
267 }
268
269 #define list_entry(PTR, TYPE, FIELD) container_of(PTR, TYPE, FIELD)
270 #define list_first_entry(PTR, TYPE, FIELD) \
271 list_entry(list_first((PTR)), TYPE, FIELD)
272 #define list_first_entry_or_null(PTR, TYPE, FIELD) \
273 (list_empty((PTR)) ? NULL : list_entry(list_first((PTR)), TYPE, FIELD))
274 #define list_last_entry(PTR, TYPE, FIELD) \
275 list_entry(list_last((PTR)), TYPE, FIELD)
276 #define list_next_entry(ENTRY, FIELD) \
277 list_entry(list_next(&(ENTRY)->FIELD), typeof(*(ENTRY)), FIELD)
278 #define list_prev_entry(ENTRY, FIELD) \
279 list_entry(list_prev(&(ENTRY)->FIELD), typeof(*(ENTRY)), FIELD)
280
281 #define list_for_each(VAR, HEAD) \
282 for ((VAR) = list_first((HEAD)); \
283 (VAR) != (HEAD); \
284 (VAR) = list_next((VAR)))
285
286 #define list_for_each_prev(VAR, HEAD) \
287 for ((VAR) = list_last((HEAD)); \
288 (VAR) != (HEAD); \
289 (VAR) = list_prev((VAR)))
290
291 #define list_for_each_safe(VAR, NEXT, HEAD) \
292 for ((VAR) = list_first((HEAD)); \
293 ((VAR) != (HEAD)) && ((NEXT) = list_next((VAR)), 1); \
294 (VAR) = (NEXT))
295
296 #define list_safe_reset_next(VAR, NEXT, FIELD) \
297 (NEXT) = list_next_entry(VAR, FIELD)
298
299 #define list_for_each_entry(VAR, HEAD, FIELD) \
300 for ((VAR) = list_entry(list_first((HEAD)), typeof(*(VAR)), FIELD); \
301 &(VAR)->FIELD != (HEAD); \
302 (VAR) = list_entry(list_next(&(VAR)->FIELD), typeof(*(VAR)), \
303 FIELD))
304
305 #define list_for_each_entry_safe(VAR, NEXT, HEAD, FIELD) \
306 for ((VAR) = list_entry(list_first((HEAD)), typeof(*(VAR)), FIELD); \
307 (&(VAR)->FIELD != (HEAD)) && \
308 ((NEXT) = list_entry(list_next(&(VAR)->FIELD), \
309 typeof(*(VAR)), FIELD), 1); \
310 (VAR) = (NEXT))
311
312 #define list_for_each_entry_safe_reverse(VAR, NEXT, HEAD, FIELD) \
313 for ((VAR) = list_entry(list_last((HEAD)), typeof(*(VAR)), FIELD); \
314 (&(VAR)->FIELD != (HEAD)) && \
315 ((NEXT) = list_entry(list_prev(&(VAR)->FIELD), \
316 typeof(*(VAR)), FIELD), 1); \
317 (VAR) = (NEXT))
318
319 #define list_for_each_entry_reverse(VAR, HEAD, FIELD) \
320 for ((VAR) = list_entry(list_last((HEAD)), typeof(*(VAR)), FIELD); \
321 &(VAR)->FIELD != (HEAD); \
322 (VAR) = list_entry(list_prev(&(VAR)->FIELD), typeof(*(VAR)), \
323 FIELD))
324
325 #define list_for_each_entry_continue(VAR, HEAD, FIELD) \
326 for ((VAR) = list_next_entry((VAR), FIELD); \
327 &(VAR)->FIELD != (HEAD); \
328 (VAR) = list_next_entry((VAR), FIELD))
329
330 #define list_for_each_entry_continue_reverse(VAR, HEAD, FIELD) \
331 for ((VAR) = list_prev_entry((VAR), FIELD); \
332 &(VAR)->FIELD != (HEAD); \
333 (VAR) = list_prev_entry((VAR), FIELD))
334
335 #define list_for_each_entry_from(VAR, HEAD, FIELD) \
336 for (; \
337 (&(VAR)->FIELD != (HEAD)); \
338 (VAR) = list_next_entry((VAR), FIELD))
339
340 #define list_for_each_entry_from_reverse(VAR, HEAD, FIELD) \
341 for (; \
342 (&(VAR)->FIELD != (HEAD)); \
343 (VAR) = list_prev_entry((VAR), FIELD))
344
345 #define list_for_each_entry_safe_from(VAR, NEXT, HEAD, FIELD) \
346 for (; \
347 (&(VAR)->FIELD != (HEAD)) && \
348 ((NEXT) = list_next_entry((VAR), FIELD)); \
349 (VAR) = (NEXT))
350
351 /*
352 * `H'ead-only/`H'ash-table doubly-linked lists.
353 */
354
355 #define hlist_head pslist_head
356 #define hlist_node pslist_entry
357
358 #define HLIST_HEAD_INIT PSLIST_INITIALIZER
359 #define INIT_HLIST_HEAD PSLIST_INIT
360 #define hlist_empty(h) (pslist_writer_first(h) == NULL)
361 #define hlist_first pslist_writer_first
362 #define hlist_next pslist_writer_next
363
364 static inline void
365 hlist_add_head(struct hlist_node *node, struct hlist_head *head)
366 {
367
368 pslist_entry_init(node);
369 pslist_writer_insert_head(head, node);
370 }
371
372 static inline void
373 hlist_del(struct hlist_node *node)
374 {
375
376 pslist_writer_remove(node);
377 /* XXX abstraction violation */
378 node->ple_prevp = _PSLIST_POISON;
379 }
380
381 static inline void
382 hlist_del_init(struct hlist_node *node)
383 {
384
385 /* XXX abstraction violation */
386 if (node->ple_prevp != NULL)
387 pslist_writer_remove(node);
388 }
389
390 #define hlist_entry(PTR, TYPE, FIELD) container_of(PTR, TYPE, FIELD)
391 #define hlist_for_each(VAR, HEAD) \
392 for ((VAR) = hlist_first(HEAD); (VAR) != NULL; (VAR) = hlist_next(VAR))
393 #define hlist_for_each_safe(VAR, NEXT, HEAD) \
394 for ((VAR) = hlist_first(HEAD); \
395 (VAR) != NULL && ((NEXT) = hlist_next(HEAD), 1); \
396 (VAR) = (NEXT))
397 #define hlist_for_each_entry(VAR, HEAD, FIELD) \
398 for ((VAR) = (hlist_first(HEAD) == NULL ? NULL : \
399 hlist_entry(hlist_first(HEAD), typeof(*(VAR)), \
400 FIELD)); \
401 (VAR) != NULL; \
402 (VAR) = (hlist_next(&(VAR)->FIELD) == NULL ? NULL : \
403 hlist_entry(hlist_next(&(VAR)->FIELD), typeof(*(VAR)),\
404 FIELD)))
405
406 #define hlist_for_each_entry_safe(VAR, NEXT, HEAD, FIELD) \
407 for ((VAR) = (hlist_first(HEAD) == NULL ? NULL : \
408 hlist_entry(hlist_first(HEAD), typeof(*(VAR)), \
409 FIELD)); \
410 ((VAR) != NULL && \
411 ((NEXT) = hlist_next(&(VAR)->FIELD), 1)); \
412 (VAR) = hlist_entry((NEXT), typeof(*(VAR)), FIELD))
413
414 static inline void
415 hlist_add_behind_rcu(struct hlist_node *node, struct hlist_node *prev)
416 {
417
418 pslist_entry_init(node);
419 pslist_writer_insert_after(prev, node);
420 }
421
422 static inline void
423 hlist_add_head_rcu(struct hlist_node *node, struct hlist_head *head)
424 {
425
426 pslist_entry_init(node);
427 pslist_writer_insert_head(head, node);
428 }
429
430 #define hlist_del_init_rcu hlist_del_init /* no special needs */
431
432 #define hlist_first_rcu pslist_reader_first
433 #define hlist_next_rcu pslist_reader_next
434
435 #define hlist_for_each_entry_rcu(VAR, HEAD, FIELD) \
436 for ((VAR) = (hlist_first_rcu(HEAD) == NULL ? NULL : \
437 hlist_entry(hlist_first_rcu(HEAD), typeof(*(VAR)), \
438 FIELD)); \
439 (VAR) != NULL; \
440 (VAR) = (hlist_next_rcu(&(VAR)->FIELD) == NULL ? NULL : \
441 hlist_entry(hlist_next_rcu(&(VAR)->FIELD), \
442 typeof(*(VAR)), FIELD)))
443
444 #endif /* _LINUX_LIST_H_ */
445