xref: /dragonfly/sys/dev/drm/include/linux/list.h (revision 6491ace5)
1 /*
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6  * Copyright (c) 2015-2019 François Tigeot <ftigeot@wolfpond.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef _LINUX_LIST_H_
31 #define _LINUX_LIST_H_
32 
33 /*
34  * Since LIST_HEAD conflicts with the linux definition we must include any
35  * FreeBSD header which requires it here so it is resolved with the correct
36  * definition prior to the undef.
37  */
38 #include <linux/types.h>
39 #include <linux/stddef.h>
40 #include <linux/kernel.h>
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/queue.h>
45 #include <sys/jail.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/proc.h>
49 #include <sys/conf.h>
50 #include <sys/socket.h>
51 #include <sys/mbuf.h>
52 
53 #include <net/bpf.h>
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/if_types.h>
57 #include <net/if_media.h>
58 
59 #include <netinet/in.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/in_var.h>
62 
63 #include <netinet6/in6_var.h>
64 #include <netinet6/nd6.h>
65 
66 #include <vm/vm.h>
67 #include <vm/vm_object.h>
68 
69 #define	prefetch(x)
70 
71 struct list_head {
72 	struct list_head *next;
73 	struct list_head *prev;
74 };
75 
76 static inline void
77 INIT_LIST_HEAD(struct list_head *list)
78 {
79 
80 	list->next = list->prev = list;
81 }
82 
83 static inline struct list_head *
84 list_first(const struct list_head *head)
85 {
86 	return head->next;
87 }
88 
89 static inline struct list_head *
90 list_last(const struct list_head *head)
91 {
92 	return head->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_empty_careful(const struct list_head *head)
103 {
104 	return (head == head->next) && (head == head->prev);
105 }
106 
107 static inline void
108 list_del(struct list_head *entry)
109 {
110 
111 	entry->next->prev = entry->prev;
112 	entry->prev->next = entry->next;
113 }
114 
115 static inline void list_replace(struct list_head *old,
116 				struct list_head *new)
117 {
118 	new->next = old->next;
119 	new->next->prev = new;
120 	new->prev = old->prev;
121 	new->prev->next = new;
122 }
123 
124 static inline void list_replace_init(struct list_head *old,
125 				     struct list_head *new)
126 {
127 	list_replace(old, new);
128 	INIT_LIST_HEAD(old);
129 }
130 
131 static inline void
132 _list_add(struct list_head *new, struct list_head *prev,
133     struct list_head *next)
134 {
135 
136 	next->prev = new;
137 	new->next = next;
138 	new->prev = prev;
139 	prev->next = new;
140 }
141 
142 static inline void
143 list_del_init(struct list_head *entry)
144 {
145 
146 	list_del(entry);
147 	INIT_LIST_HEAD(entry);
148 }
149 
150 #define	list_entry(ptr, type, field)	container_of(ptr, type, field)
151 
152 #define list_first_entry(ptr, type, member) \
153         list_entry((ptr)->next, type, member)
154 
155 #define list_first_entry_or_null(ptr, type, member) \
156 	(list_empty(ptr) ? NULL: list_first_entry(ptr, type, member))
157 
158 #define list_last_entry(ptr, type, field) \
159 	list_entry(list_last((ptr)), type, field)
160 
161 #define list_next_entry(ptr, member)					\
162 	list_entry(((ptr)->member.next), typeof(*(ptr)), member)
163 
164 #define	list_for_each(p, head)						\
165 	for (p = (head)->next; p != (head); p = p->next)
166 
167 #define	list_for_each_safe(p, n, head)					\
168 	for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
169 
170 #define list_for_each_entry(p, h, field)				\
171 	for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
172 	    p = list_entry(p->field.next, typeof(*p), field))
173 
174 #define list_for_each_entry_safe(p, n, h, field)			\
175 	for (p = list_entry((h)->next, typeof(*p), field), 		\
176 	    n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
177 	    p = n, n = list_entry(n->field.next, typeof(*n), field))
178 
179 #define list_for_each_entry_continue(p, h, field)			\
180 	for (p = list_next_entry((p), field); &p->field != (h);		\
181 	    p = list_next_entry((p), field))
182 
183 #define list_for_each_entry_continue_reverse(pos, head, member)         	\
184         for (pos = list_entry(pos->member.prev, __typeof(*pos), member);	\
185              &pos->member != (head);    					\
186              pos = list_entry(pos->member.prev, __typeof(*pos), member))
187 
188 #define list_for_each_entry_safe_from(pos, n, head, member) 			\
189 	for (n = list_entry(pos->member.next, typeof(*pos), member);		\
190 	     &pos->member != (head);						\
191 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
192 
193 #define	list_for_each_entry_reverse(p, h, field)			\
194 	for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
195 	    p = list_entry(p->field.prev, typeof(*p), field))
196 
197 #define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
198 
199 static inline void
200 list_add(struct list_head *new, struct list_head *head)
201 {
202 
203 	_list_add(new, head, head->next);
204 }
205 
206 static inline void
207 list_add_tail(struct list_head *new, struct list_head *head)
208 {
209 
210 	_list_add(new, head->prev, head);
211 }
212 
213 static inline void
214 list_move(struct list_head *list, struct list_head *head)
215 {
216 
217 	list_del(list);
218 	list_add(list, head);
219 }
220 
221 static inline void
222 list_move_tail(struct list_head *entry, struct list_head *head)
223 {
224 
225 	list_del(entry);
226 	list_add_tail(entry, head);
227 }
228 
229 static inline void
230 _list_splice(const struct list_head *list, struct list_head *prev,
231     struct list_head *next)
232 {
233 	struct list_head *first;
234 	struct list_head *last;
235 
236 	if (list_empty(list))
237 		return;
238 	first = list->next;
239 	last = list->prev;
240 	first->prev = prev;
241 	prev->next = first;
242 	last->next = next;
243 	next->prev = last;
244 }
245 
246 static inline void
247 list_splice(const struct list_head *list, struct list_head *head)
248 {
249 
250 	_list_splice(list, head, head->next);
251 }
252 
253 static inline void
254 list_splice_tail(struct list_head *list, struct list_head *head)
255 {
256 
257 	_list_splice(list, head->prev, head);
258 }
259 
260 static inline void
261 list_splice_init(struct list_head *list, struct list_head *head)
262 {
263 
264 	_list_splice(list, head, head->next);
265 	INIT_LIST_HEAD(list);
266 }
267 
268 static inline void
269 list_splice_tail_init(struct list_head *list, struct list_head *head)
270 {
271 
272 	_list_splice(list, head->prev, head);
273 	INIT_LIST_HEAD(list);
274 }
275 
276 #define LINUX_LIST_HEAD(name)	struct list_head name = { &(name), &(name) }
277 
278 
279 struct hlist_head {
280 	struct hlist_node *first;
281 };
282 
283 struct hlist_node {
284 	struct hlist_node *next, **pprev;
285 };
286 
287 #define	HLIST_HEAD_INIT { }
288 #define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
289 #define	INIT_HLIST_HEAD(head) (head)->first = NULL
290 #define	INIT_HLIST_NODE(node)						\
291 do {									\
292 	(node)->next = NULL;						\
293 	(node)->pprev = NULL;						\
294 } while (0)
295 
296 static inline int
297 hlist_unhashed(const struct hlist_node *h)
298 {
299 
300 	return !h->pprev;
301 }
302 
303 static inline int
304 hlist_empty(const struct hlist_head *h)
305 {
306 
307 	return !h->first;
308 }
309 
310 static inline void
311 hlist_del(struct hlist_node *n)
312 {
313 
314         if (n->next)
315                 n->next->pprev = n->pprev;
316         *n->pprev = n->next;
317 }
318 
319 static inline void
320 hlist_del_init(struct hlist_node *n)
321 {
322 
323 	if (hlist_unhashed(n))
324 		return;
325 	hlist_del(n);
326 	INIT_HLIST_NODE(n);
327 }
328 
329 static inline void
330 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
331 {
332 
333 	n->next = h->first;
334 	if (h->first)
335 		h->first->pprev = &n->next;
336 	h->first = n;
337 	n->pprev = &h->first;
338 }
339 
340 static inline void
341 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
342 {
343 
344 	n->pprev = next->pprev;
345 	n->next = next;
346 	next->pprev = &n->next;
347 	*(n->pprev) = n;
348 }
349 
350 static inline void
351 hlist_add_after(struct hlist_node *n, struct hlist_node *next)
352 {
353 
354 	next->next = n->next;
355 	n->next = next;
356 	next->pprev = &n->next;
357 	if (next->next)
358 		next->next->pprev = &next->next;
359 }
360 
361 static inline void
362 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
363 {
364 
365 	new->first = old->first;
366 	if (new->first)
367 		new->first->pprev = &new->first;
368 	old->first = NULL;
369 }
370 
371 /**
372  * list_is_singular - tests whether a list has just one entry.
373  * @head: the list to test.
374  */
375 static inline int list_is_singular(const struct list_head *head)
376 {
377 	return !list_empty(head) && (head->next == head->prev);
378 }
379 
380 static inline void __list_cut_position(struct list_head *list,
381 		struct list_head *head, struct list_head *entry)
382 {
383 	struct list_head *new_first = entry->next;
384 	list->next = head->next;
385 	list->next->prev = list;
386 	list->prev = entry;
387 	entry->next = list;
388 	head->next = new_first;
389 	new_first->prev = head;
390 }
391 
392 /**
393  * list_cut_position - cut a list into two
394  * @list: a new list to add all removed entries
395  * @head: a list with entries
396  * @entry: an entry within head, could be the head itself
397  *	and if so we won't cut the list
398  *
399  * This helper moves the initial part of @head, up to and
400  * including @entry, from @head to @list. You should
401  * pass on @entry an element you know is on @head. @list
402  * should be an empty list or a list you do not care about
403  * losing its data.
404  *
405  */
406 static inline void list_cut_position(struct list_head *list,
407 		struct list_head *head, struct list_head *entry)
408 {
409 	if (list_empty(head))
410 		return;
411 	if (list_is_singular(head) &&
412 		(head->next != entry && head != entry))
413 		return;
414 	if (entry == head)
415 		INIT_LIST_HEAD(list);
416 	else
417 		__list_cut_position(list, head, entry);
418 }
419 
420 /**
421  *  list_is_last - tests whether @list is the last entry in list @head
422  *   @list: the entry to test
423  *    @head: the head of the list
424  */
425 static inline int list_is_last(const struct list_head *list,
426                                 const struct list_head *head)
427 {
428         return list->next == head;
429 }
430 
431 #define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
432 
433 #define	hlist_for_each(p, head)						\
434 	for (p = (head)->first; p; p = p->next)
435 
436 #define	hlist_for_each_safe(p, n, head)					\
437 	for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
438 
439 #define hlist_entry_safe(ptr, type, member) \
440 	(ptr) ? hlist_entry(ptr, type, member) : NULL
441 
442 #define hlist_for_each_entry(pos, head, member)				\
443 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
444 	     pos;							\
445 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
446 
447 #define hlist_for_each_entry_continue(tp, p, field)			\
448 	for (p = (p)->next;						\
449 	    p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
450 
451 #define	hlist_for_each_entry_from(tp, p, field)				\
452 	for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
453 
454 #define hlist_for_each_entry_safe(pos, n, head, member)			\
455 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
456 	     (pos) && ({ n = (pos)->member.next; 1; });			\
457 	     pos = hlist_entry_safe(n, typeof(*(pos)), member))
458 
459 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
460     struct list_head *a, struct list_head *b));
461 
462 #define hlist_add_head_rcu(n, h)	hlist_add_head(n, h)
463 
464 #define hlist_del_init_rcu(n)		hlist_del_init(n)
465 
466 #endif /* _LINUX_LIST_H_ */
467