list.h revision 1.8 1 /* $NetBSD: list.h,v 1.8 2018/08/27 06:51:38 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 * - The `_rcu' routines here are not actually pserialize(9)-safe.
40 * They need dependent read memory barriers added. Please fix this
41 * if you need to use them with pserialize(9).
42 */
43
44 #ifndef _LINUX_LIST_H_
45 #define _LINUX_LIST_H_
46
47 #include <sys/null.h>
48 #include <sys/pslist.h>
49 #include <sys/queue.h>
50
51 #include <linux/kernel.h>
52
53 /*
54 * Doubly-linked lists.
55 */
56
57 struct list_head {
58 struct list_head *prev;
59 struct list_head *next;
60 };
61
62 #define LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) }
63
64 static inline void
65 INIT_LIST_HEAD(struct list_head *head)
66 {
67 head->prev = head;
68 head->next = head;
69 }
70
71 static inline struct list_head *
72 list_first(const struct list_head *head)
73 {
74 return head->next;
75 }
76
77 static inline struct list_head *
78 list_last(const struct list_head *head)
79 {
80 return head->prev;
81 }
82
83 static inline struct list_head *
84 list_next(const struct list_head *node)
85 {
86 return node->next;
87 }
88
89 static inline struct list_head *
90 list_prev(const struct list_head *node)
91 {
92 return node->prev;
93 }
94
95 static inline int
96 list_empty(const struct list_head *head)
97 {
98 return (head->next == head);
99 }
100
101 static inline int
102 list_is_singular(const struct list_head *head)
103 {
104
105 if (list_empty(head))
106 return false;
107 if (head->next != head->prev)
108 return false;
109 return true;
110 }
111
112 static inline void
113 __list_add_between(struct list_head *prev, struct list_head *node,
114 struct list_head *next)
115 {
116 prev->next = node;
117 node->prev = prev;
118 node->next = next;
119 next->prev = node;
120 }
121
122 static inline void
123 list_add(struct list_head *node, struct list_head *head)
124 {
125 __list_add_between(head, node, head->next);
126 }
127
128 static inline void
129 list_add_tail(struct list_head *node, struct list_head *head)
130 {
131 __list_add_between(head->prev, node, head);
132 }
133
134 static inline void
135 list_del(struct list_head *entry)
136 {
137 entry->prev->next = entry->next;
138 entry->next->prev = entry->prev;
139 }
140
141 static inline void
142 __list_splice_between(struct list_head *prev, const struct list_head *list,
143 struct list_head *next)
144 {
145 struct list_head *first = list->next;
146 struct list_head *last = list->prev;
147
148 first->prev = prev;
149 prev->next = first;
150
151 last->next = next;
152 next->prev = last;
153 }
154
155 static inline void
156 list_splice(const struct list_head *list, struct list_head *head)
157 {
158 if (!list_empty(list))
159 __list_splice_between(head, list, head->next);
160 }
161
162 static inline void
163 list_splice_tail(const struct list_head *list, struct list_head *head)
164 {
165 if (!list_empty(list))
166 __list_splice_between(head->prev, list, head);
167 }
168
169 static inline void
170 list_move(struct list_head *node, struct list_head *head)
171 {
172 list_del(node);
173 list_add(node, head);
174 }
175
176 static inline void
177 list_move_tail(struct list_head *node, struct list_head *head)
178 {
179 list_del(node);
180 list_add_tail(node, head);
181 }
182
183 static inline void
184 list_replace(struct list_head *old, struct list_head *new)
185 {
186 new->prev = old->prev;
187 old->prev->next = new;
188 new->next = old->next;
189 old->next->prev = new;
190 }
191
192 static inline void
193 list_del_init(struct list_head *node)
194 {
195 list_del(node);
196 INIT_LIST_HEAD(node);
197 }
198
199 #define list_entry(PTR, TYPE, FIELD) container_of(PTR, TYPE, FIELD)
200 #define list_first_entry(PTR, TYPE, FIELD) \
201 list_entry(list_first((PTR)), TYPE, FIELD)
202 #define list_last_entry(PTR, TYPE, FIELD) \
203 list_entry(list_last((PTR)), TYPE, FIELD)
204 #define list_next_entry(ENTRY, FIELD) \
205 list_entry(list_next(&(ENTRY)->FIELD), typeof(*(ENTRY)), FIELD)
206 #define list_prev_entry(ENTRY, FIELD) \
207 list_entry(list_prev(&(ENTRY)->FIELD), typeof(*(ENTRY)), FIELD)
208
209 #define list_for_each(VAR, HEAD) \
210 for ((VAR) = list_first((HEAD)); \
211 (VAR) != (HEAD); \
212 (VAR) = list_next((VAR)))
213
214 #define list_for_each_safe(VAR, NEXT, HEAD) \
215 for ((VAR) = list_first((HEAD)); \
216 ((VAR) != (HEAD)) && ((NEXT) = list_next((VAR)), 1); \
217 (VAR) = (NEXT))
218
219 #define list_for_each_entry(VAR, HEAD, FIELD) \
220 for ((VAR) = list_entry(list_first((HEAD)), typeof(*(VAR)), FIELD); \
221 &(VAR)->FIELD != (HEAD); \
222 (VAR) = list_entry(list_next(&(VAR)->FIELD), typeof(*(VAR)), \
223 FIELD))
224
225 #define list_for_each_entry_reverse(VAR, HEAD, FIELD) \
226 for ((VAR) = list_entry(list_last((HEAD)), typeof(*(VAR)), FIELD); \
227 &(VAR)->FIELD != (HEAD); \
228 (VAR) = list_entry(list_prev(&(VAR)->FIELD), typeof(*(VAR)), \
229 FIELD))
230
231 #define list_for_each_entry_safe(VAR, NEXT, HEAD, FIELD) \
232 for ((VAR) = list_entry(list_first((HEAD)), typeof(*(VAR)), FIELD); \
233 (&(VAR)->FIELD != (HEAD)) && \
234 ((NEXT) = list_entry(list_next(&(VAR)->FIELD), \
235 typeof(*(VAR)), FIELD), 1); \
236 (VAR) = (NEXT))
237
238 #define list_for_each_entry_continue(VAR, HEAD, FIELD) \
239 for ((VAR) = list_next_entry((VAR), FIELD); \
240 &(VAR)->FIELD != (HEAD); \
241 (VAR) = list_next_entry((VAR), FIELD))
242
243 #define list_for_each_entry_continue_reverse(VAR, HEAD, FIELD) \
244 for ((VAR) = list_prev_entry((VAR), FIELD); \
245 &(VAR)->FIELD != (HEAD); \
246 (VAR) = list_prev_entry((VAR), FIELD))
247
248 #define list_for_each_entry_safe_from(VAR, NEXT, HEAD, FIELD) \
249 for (; \
250 (&(VAR)->FIELD != (HEAD)) && \
251 ((NEXT) = list_next_entry((VAR), FIELD)); \
252 (VAR) = (NEXT))
253
254 /*
255 * `H'ead-only/`H'ash-table doubly-linked lists.
256 */
257
258 #define hlist_head pslist_head
259 #define hlist_node pslist_entry
260
261 #define HLIST_HEAD_INIT PSLIST_INITIALIZER
262 #define INIT_HLIST_HEAD PSLIST_INIT
263 #define hlist_empty(h) (pslist_writer_first(h) == NULL)
264 #define hlist_first pslist_writer_first
265 #define hlist_next pslist_writer_next
266 #define hlist_add_head(n, h) pslist_writer_insert_head(h, n)
267
268 static inline void
269 hlist_del(struct hlist_node *node)
270 {
271
272 pslist_writer_remove(node);
273 /* XXX abstraction violation */
274 node->ple_prevp = _PSLIST_POISON;
275 }
276
277 static inline void
278 hlist_del_init(struct hlist_node *node)
279 {
280
281 /* XXX abstraction violation */
282 if (node->ple_prevp != NULL)
283 pslist_writer_remove(node);
284 }
285
286 #define hlist_entry(PTR, TYPE, FIELD) container_of(PTR, TYPE, FIELD)
287 #define hlist_for_each(VAR, HEAD) \
288 for ((VAR) = hlist_first(HEAD); (VAR) != NULL; (VAR) = hlist_next(VAR))
289 #define hlist_for_each_safe(VAR, NEXT, HEAD) \
290 for ((VAR) = hlist_first(HEAD), \
291 (NEXT) = ((VAR) == NULL ? NULL : hlist_next(HEAD)); \
292 (VAR) != NULL; \
293 (VAR) = (NEXT))
294 #define hlist_for_each_entry(VAR, HEAD, FIELD) \
295 for ((VAR) = (hlist_first(HEAD) == NULL ? NULL : \
296 hlist_entry(hlist_first(HEAD), typeof(*(VAR)), \
297 FIELD)); \
298 (VAR) != NULL; \
299 (VAR) = (hlist_next(VAR) == NULL ? NULL : \
300 hlist_entry(hlist_next(&(VAR)->FIELD), typeof(*(VAR)),\
301 FIELD)))
302
303 #define hlist_for_each_entry_safe(VAR, NEXT, HEAD, FIELD) \
304 for ((VAR) = (hlist_first(HEAD) == NULL ? NULL : \
305 hlist_entry(hlist_first(HEAD), typeof(*(VAR)), \
306 FIELD)), \
307 (NEXT) = ((VAR) == NULL ? NULL : \
308 hlist_entry(hlist_next(&(VAR)->FIELD), typeof(*(VAR)),\
309 FIELD)); \
310 (VAR) != NULL; \
311 (VAR) = (NEXT))
312
313 #define hlist_add_behind_rcu(n, p) pslist_writer_insert_after(p, n)
314 #define hlist_add_head_rcu pslist_writer_insert_head
315 #define hlist_del_init_rcu hlist_del_init /* no special needs */
316
317 #define hlist_first_rcu pslist_reader_first
318 #define hlist_next_rcu pslist_reader_next
319
320 #define hlist_for_each_entry_rcu(VAR, HEAD, FIELD) \
321 for ((VAR) = (hlist_first_rcu(HEAD) == NULL ? NULL : \
322 hlist_entry(hlist_first_rcu(HEAD), typeof(*(VAR)), \
323 FIELD)); \
324 (VAR) != NULL; \
325 (VAR) = (hlist_next_rcu(VAR) == NULL ? NULL : \
326 hlist_entry(hlist_next_rcu(VAR), typeof(*(VAR)), \
327 FIELD)))
328
329 #endif /* _LINUX_LIST_H_ */
330