xref: /dflybsd-src/sys/dev/drm/include/linux/list.h (revision 11699d20ad593415e7d8ff1bbb21f8603f3aa217)
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_prev_entry(ptr, member)					\
165 	list_entry(((ptr)->member.prev), typeof(*(ptr)), member)
166 
167 #define	list_for_each(p, head)						\
168 	for (p = (head)->next; p != (head); p = p->next)
169 
170 #define	list_for_each_safe(p, n, head)					\
171 	for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
172 
173 #define list_for_each_entry(p, h, field)				\
174 	for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
175 	    p = list_entry(p->field.next, typeof(*p), field))
176 
177 #define list_for_each_entry_safe(p, n, h, field)			\
178 	for (p = list_entry((h)->next, typeof(*p), field), 		\
179 	    n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
180 	    p = n, n = list_entry(n->field.next, typeof(*n), field))
181 
182 #define list_for_each_entry_continue(p, h, field)			\
183 	for (p = list_next_entry((p), field); &p->field != (h);		\
184 	    p = list_next_entry((p), field))
185 
186 #define list_for_each_entry_continue_reverse(pos, head, member)         	\
187         for (pos = list_entry(pos->member.prev, __typeof(*pos), member);	\
188              &pos->member != (head);    					\
189              pos = list_entry(pos->member.prev, __typeof(*pos), member))
190 
191 #define list_for_each_entry_safe_from(pos, n, head, member) 			\
192 	for (n = list_entry(pos->member.next, typeof(*pos), member);		\
193 	     &pos->member != (head);						\
194 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
195 
196 #define	list_for_each_entry_reverse(p, h, field)			\
197 	for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
198 	    p = list_entry(p->field.prev, typeof(*p), field))
199 
200 #define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
201 
202 static inline void
203 list_add(struct list_head *new, struct list_head *head)
204 {
205 
206 	_list_add(new, head, head->next);
207 }
208 
209 static inline void
210 list_add_tail(struct list_head *new, struct list_head *head)
211 {
212 
213 	_list_add(new, head->prev, head);
214 }
215 
216 static inline void
217 list_move(struct list_head *list, struct list_head *head)
218 {
219 
220 	list_del(list);
221 	list_add(list, head);
222 }
223 
224 static inline void
225 list_move_tail(struct list_head *entry, struct list_head *head)
226 {
227 
228 	list_del(entry);
229 	list_add_tail(entry, head);
230 }
231 
232 static inline void
233 _list_splice(const struct list_head *list, struct list_head *prev,
234     struct list_head *next)
235 {
236 	struct list_head *first;
237 	struct list_head *last;
238 
239 	if (list_empty(list))
240 		return;
241 	first = list->next;
242 	last = list->prev;
243 	first->prev = prev;
244 	prev->next = first;
245 	last->next = next;
246 	next->prev = last;
247 }
248 
249 static inline void
250 list_splice(const struct list_head *list, struct list_head *head)
251 {
252 
253 	_list_splice(list, head, head->next);
254 }
255 
256 static inline void
257 list_splice_tail(struct list_head *list, struct list_head *head)
258 {
259 
260 	_list_splice(list, head->prev, head);
261 }
262 
263 static inline void
264 list_splice_init(struct list_head *list, struct list_head *head)
265 {
266 
267 	_list_splice(list, head, head->next);
268 	INIT_LIST_HEAD(list);
269 }
270 
271 static inline void
272 list_splice_tail_init(struct list_head *list, struct list_head *head)
273 {
274 
275 	_list_splice(list, head->prev, head);
276 	INIT_LIST_HEAD(list);
277 }
278 
279 #define LINUX_LIST_HEAD(name)	struct list_head name = { &(name), &(name) }
280 
281 
282 struct hlist_head {
283 	struct hlist_node *first;
284 };
285 
286 struct hlist_node {
287 	struct hlist_node *next, **pprev;
288 };
289 
290 #define	HLIST_HEAD_INIT { }
291 #define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
292 #define	INIT_HLIST_HEAD(head) (head)->first = NULL
293 #define	INIT_HLIST_NODE(node)						\
294 do {									\
295 	(node)->next = NULL;						\
296 	(node)->pprev = NULL;						\
297 } while (0)
298 
299 static inline int
300 hlist_unhashed(const struct hlist_node *h)
301 {
302 
303 	return !h->pprev;
304 }
305 
306 static inline int
307 hlist_empty(const struct hlist_head *h)
308 {
309 
310 	return !h->first;
311 }
312 
313 static inline void
314 hlist_del(struct hlist_node *n)
315 {
316 
317         if (n->next)
318                 n->next->pprev = n->pprev;
319         *n->pprev = n->next;
320 }
321 
322 static inline void
323 hlist_del_init(struct hlist_node *n)
324 {
325 
326 	if (hlist_unhashed(n))
327 		return;
328 	hlist_del(n);
329 	INIT_HLIST_NODE(n);
330 }
331 
332 static inline void
333 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
334 {
335 
336 	n->next = h->first;
337 	if (h->first)
338 		h->first->pprev = &n->next;
339 	h->first = n;
340 	n->pprev = &h->first;
341 }
342 
343 static inline void
344 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
345 {
346 
347 	n->pprev = next->pprev;
348 	n->next = next;
349 	next->pprev = &n->next;
350 	*(n->pprev) = n;
351 }
352 
353 static inline void
354 hlist_add_after(struct hlist_node *n, struct hlist_node *next)
355 {
356 
357 	next->next = n->next;
358 	n->next = next;
359 	next->pprev = &n->next;
360 	if (next->next)
361 		next->next->pprev = &next->next;
362 }
363 
364 static inline void
365 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
366 {
367 
368 	new->first = old->first;
369 	if (new->first)
370 		new->first->pprev = &new->first;
371 	old->first = NULL;
372 }
373 
374 /**
375  * list_is_singular - tests whether a list has just one entry.
376  * @head: the list to test.
377  */
378 static inline int list_is_singular(const struct list_head *head)
379 {
380 	return !list_empty(head) && (head->next == head->prev);
381 }
382 
383 static inline void __list_cut_position(struct list_head *list,
384 		struct list_head *head, struct list_head *entry)
385 {
386 	struct list_head *new_first = entry->next;
387 	list->next = head->next;
388 	list->next->prev = list;
389 	list->prev = entry;
390 	entry->next = list;
391 	head->next = new_first;
392 	new_first->prev = head;
393 }
394 
395 /**
396  * list_cut_position - cut a list into two
397  * @list: a new list to add all removed entries
398  * @head: a list with entries
399  * @entry: an entry within head, could be the head itself
400  *	and if so we won't cut the list
401  *
402  * This helper moves the initial part of @head, up to and
403  * including @entry, from @head to @list. You should
404  * pass on @entry an element you know is on @head. @list
405  * should be an empty list or a list you do not care about
406  * losing its data.
407  *
408  */
409 static inline void list_cut_position(struct list_head *list,
410 		struct list_head *head, struct list_head *entry)
411 {
412 	if (list_empty(head))
413 		return;
414 	if (list_is_singular(head) &&
415 		(head->next != entry && head != entry))
416 		return;
417 	if (entry == head)
418 		INIT_LIST_HEAD(list);
419 	else
420 		__list_cut_position(list, head, entry);
421 }
422 
423 /**
424  *  list_is_last - tests whether @list is the last entry in list @head
425  *   @list: the entry to test
426  *    @head: the head of the list
427  */
428 static inline int list_is_last(const struct list_head *list,
429                                 const struct list_head *head)
430 {
431         return list->next == head;
432 }
433 
434 #define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
435 
436 #define	hlist_for_each(p, head)						\
437 	for (p = (head)->first; p; p = p->next)
438 
439 #define	hlist_for_each_safe(p, n, head)					\
440 	for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
441 
442 #define hlist_entry_safe(ptr, type, member) \
443 	(ptr) ? hlist_entry(ptr, type, member) : NULL
444 
445 #define hlist_for_each_entry(pos, head, member)				\
446 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
447 	     pos;							\
448 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
449 
450 #define hlist_for_each_entry_continue(tp, p, field)			\
451 	for (p = (p)->next;						\
452 	    p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
453 
454 #define	hlist_for_each_entry_from(tp, p, field)				\
455 	for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
456 
457 #define hlist_for_each_entry_safe(pos, n, head, member)			\
458 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
459 	     (pos) && ({ n = (pos)->member.next; 1; });			\
460 	     pos = hlist_entry_safe(n, typeof(*(pos)), member))
461 
462 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
463     struct list_head *a, struct list_head *b));
464 
465 #define hlist_add_head_rcu(n, h)	hlist_add_head(n, h)
466 
467 #define hlist_del_init_rcu(n)		hlist_del_init(n)
468 
469 #endif /* _LINUX_LIST_H_ */
470