xref: /dragonfly/sys/dev/drm/include/linux/list.h (revision 7d3e9a5b)
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-2020 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 #include <linux/types.h>
34 #include <linux/stddef.h>
35 #include <linux/poison.h>
36 #include <linux/kernel.h>
37 
38 #include <sys/queue.h>
39 
40 #define	prefetch(x)
41 
42 struct list_head {
43 	struct list_head *next;
44 	struct list_head *prev;
45 };
46 
47 #define	LIST_HEAD_INIT(name)	{ .prev = &(name), .next = &(name) }
48 
49 #define DRM_LIST_HEAD(name) \
50 	struct list_head name = LIST_HEAD_INIT(name)
51 
52 static inline void
53 INIT_LIST_HEAD(struct list_head *list)
54 {
55 
56 	list->next = list->prev = list;
57 }
58 
59 static inline struct list_head *
60 list_first(const struct list_head *head)
61 {
62 	return head->next;
63 }
64 
65 static inline struct list_head *
66 list_last(const struct list_head *head)
67 {
68 	return head->prev;
69 }
70 
71 static inline int
72 list_empty(const struct list_head *head)
73 {
74 	return (head->next == head);
75 }
76 
77 static inline int
78 list_empty_careful(const struct list_head *head)
79 {
80 	return (head == head->next) && (head == head->prev);
81 }
82 
83 static inline void
84 __list_del(struct list_head *prev, struct list_head *next)
85 {
86 	next->prev = prev;
87 	WRITE_ONCE(prev->next, next);
88 }
89 
90 static inline void
91 __list_del_entry(struct list_head *entry)
92 {
93 	__list_del(entry->prev, entry->next);
94 }
95 
96 static inline void
97 list_del(struct list_head *entry)
98 {
99 
100 	entry->next->prev = entry->prev;
101 	entry->prev->next = entry->next;
102 }
103 
104 static inline void list_replace(struct list_head *old,
105 				struct list_head *new)
106 {
107 	new->next = old->next;
108 	new->next->prev = new;
109 	new->prev = old->prev;
110 	new->prev->next = new;
111 }
112 
113 static inline void list_replace_init(struct list_head *old,
114 				     struct list_head *new)
115 {
116 	list_replace(old, new);
117 	INIT_LIST_HEAD(old);
118 }
119 
120 static inline void
121 _list_add(struct list_head *new, struct list_head *prev,
122     struct list_head *next)
123 {
124 
125 	next->prev = new;
126 	new->next = next;
127 	new->prev = prev;
128 	prev->next = new;
129 }
130 
131 static inline void
132 list_del_init(struct list_head *entry)
133 {
134 
135 	list_del(entry);
136 	INIT_LIST_HEAD(entry);
137 }
138 
139 #define	list_entry(ptr, type, field)	container_of(ptr, type, field)
140 
141 #define list_first_entry(ptr, type, member) \
142         list_entry((ptr)->next, type, member)
143 
144 #define list_first_entry_or_null(ptr, type, member) \
145 	(list_empty(ptr) ? NULL: list_first_entry(ptr, type, member))
146 
147 #define list_last_entry(ptr, type, field) \
148 	list_entry(list_last((ptr)), type, field)
149 
150 #define list_next_entry(ptr, member)					\
151 	list_entry(((ptr)->member.next), typeof(*(ptr)), member)
152 
153 #define list_safe_reset_next(ptr, n, member) \
154 	(n) = list_next_entry(ptr, member)
155 
156 #define list_prev_entry(ptr, member)					\
157 	list_entry(((ptr)->member.prev), typeof(*(ptr)), member)
158 
159 #define	list_for_each(p, head)						\
160 	for (p = (head)->next; p != (head); p = p->next)
161 
162 #define	list_for_each_safe(p, n, head)					\
163 	for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
164 
165 #define list_for_each_entry(p, h, field)				\
166 	for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
167 	    p = list_entry(p->field.next, typeof(*p), field))
168 
169 #define list_for_each_entry_safe(p, n, h, field)			\
170 	for (p = list_entry((h)->next, typeof(*p), field), 		\
171 	    n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
172 	    p = n, n = list_entry(n->field.next, typeof(*n), field))
173 
174 #define list_for_each_entry_from(p, h, field) \
175 	for ( ; &(p)->field != (h); \
176 	    p = list_entry((p)->field.next, typeof(*p), field))
177 
178 #define list_for_each_entry_continue(p, h, field)			\
179 	for (p = list_next_entry((p), field); &p->field != (h);		\
180 	    p = list_next_entry((p), field))
181 
182 #define list_for_each_entry_continue_reverse(pos, head, member)         	\
183         for (pos = list_entry(pos->member.prev, __typeof(*pos), member);	\
184              &pos->member != (head);    					\
185              pos = list_entry(pos->member.prev, __typeof(*pos), member))
186 
187 #define list_for_each_entry_safe_from(pos, n, head, member) 			\
188 	for (n = list_entry(pos->member.next, typeof(*pos), member);		\
189 	     &pos->member != (head);						\
190 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
191 
192 #define	list_for_each_entry_reverse(p, h, field)			\
193 	for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
194 	    p = list_entry(p->field.prev, typeof(*p), field))
195 
196 #define	list_for_each_entry_safe_reverse(p, n, h, field)		\
197 	for (p = list_entry((h)->prev, typeof(*p), field),		\
198 	    n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
199 	    p = n, n = list_entry(n->field.prev, typeof(*n), field))
200 
201 #define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
202 
203 static inline void
204 list_add(struct list_head *new, struct list_head *head)
205 {
206 
207 	_list_add(new, head, head->next);
208 }
209 
210 static inline void
211 list_add_tail(struct list_head *new, struct list_head *head)
212 {
213 
214 	_list_add(new, head->prev, head);
215 }
216 
217 static inline void
218 list_move(struct list_head *list, struct list_head *head)
219 {
220 
221 	list_del(list);
222 	list_add(list, head);
223 }
224 
225 static inline void
226 list_move_tail(struct list_head *entry, struct list_head *head)
227 {
228 
229 	list_del(entry);
230 	list_add_tail(entry, head);
231 }
232 
233 static inline void
234 _list_splice(const struct list_head *list, struct list_head *prev,
235     struct list_head *next)
236 {
237 	struct list_head *first;
238 	struct list_head *last;
239 
240 	if (list_empty(list))
241 		return;
242 	first = list->next;
243 	last = list->prev;
244 	first->prev = prev;
245 	prev->next = first;
246 	last->next = next;
247 	next->prev = last;
248 }
249 
250 static inline void
251 list_splice(const struct list_head *list, struct list_head *head)
252 {
253 
254 	_list_splice(list, head, head->next);
255 }
256 
257 static inline void
258 list_splice_tail(struct list_head *list, struct list_head *head)
259 {
260 
261 	_list_splice(list, head->prev, head);
262 }
263 
264 static inline void
265 list_splice_init(struct list_head *list, struct list_head *head)
266 {
267 
268 	_list_splice(list, head, head->next);
269 	INIT_LIST_HEAD(list);
270 }
271 
272 static inline void
273 list_splice_tail_init(struct list_head *list, struct list_head *head)
274 {
275 
276 	_list_splice(list, head->prev, head);
277 	INIT_LIST_HEAD(list);
278 }
279 
280 #define LINUX_LIST_HEAD(name)	struct list_head name = { &(name), &(name) }
281 
282 
283 struct hlist_head {
284 	struct hlist_node *first;
285 };
286 
287 struct hlist_node {
288 	struct hlist_node *next, **pprev;
289 };
290 
291 #define	HLIST_HEAD_INIT { }
292 #define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
293 #define	INIT_HLIST_HEAD(head) (head)->first = NULL
294 #define	INIT_HLIST_NODE(node)						\
295 do {									\
296 	(node)->next = NULL;						\
297 	(node)->pprev = NULL;						\
298 } while (0)
299 
300 static inline int
301 hlist_unhashed(const struct hlist_node *h)
302 {
303 
304 	return !h->pprev;
305 }
306 
307 static inline int
308 hlist_empty(const struct hlist_head *h)
309 {
310 
311 	return !h->first;
312 }
313 
314 static inline void
315 hlist_del(struct hlist_node *n)
316 {
317 
318         if (n->next)
319                 n->next->pprev = n->pprev;
320         *n->pprev = n->next;
321 }
322 
323 static inline void
324 hlist_del_init(struct hlist_node *n)
325 {
326 
327 	if (hlist_unhashed(n))
328 		return;
329 	hlist_del(n);
330 	INIT_HLIST_NODE(n);
331 }
332 
333 static inline void
334 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
335 {
336 
337 	n->next = h->first;
338 	if (h->first)
339 		h->first->pprev = &n->next;
340 	h->first = n;
341 	n->pprev = &h->first;
342 }
343 
344 static inline void
345 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
346 {
347 
348 	n->pprev = next->pprev;
349 	n->next = next;
350 	next->pprev = &n->next;
351 	*(n->pprev) = n;
352 }
353 
354 static inline void
355 hlist_add_after(struct hlist_node *n, struct hlist_node *next)
356 {
357 
358 	next->next = n->next;
359 	n->next = next;
360 	next->pprev = &n->next;
361 	if (next->next)
362 		next->next->pprev = &next->next;
363 }
364 
365 static inline void
366 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
367 {
368 
369 	new->first = old->first;
370 	if (new->first)
371 		new->first->pprev = &new->first;
372 	old->first = NULL;
373 }
374 
375 /**
376  * list_is_singular - tests whether a list has just one entry.
377  * @head: the list to test.
378  */
379 static inline int list_is_singular(const struct list_head *head)
380 {
381 	return !list_empty(head) && (head->next == head->prev);
382 }
383 
384 static inline void __list_cut_position(struct list_head *list,
385 		struct list_head *head, struct list_head *entry)
386 {
387 	struct list_head *new_first = entry->next;
388 	list->next = head->next;
389 	list->next->prev = list;
390 	list->prev = entry;
391 	entry->next = list;
392 	head->next = new_first;
393 	new_first->prev = head;
394 }
395 
396 /**
397  * list_cut_position - cut a list into two
398  * @list: a new list to add all removed entries
399  * @head: a list with entries
400  * @entry: an entry within head, could be the head itself
401  *	and if so we won't cut the list
402  *
403  * This helper moves the initial part of @head, up to and
404  * including @entry, from @head to @list. You should
405  * pass on @entry an element you know is on @head. @list
406  * should be an empty list or a list you do not care about
407  * losing its data.
408  *
409  */
410 static inline void list_cut_position(struct list_head *list,
411 		struct list_head *head, struct list_head *entry)
412 {
413 	if (list_empty(head))
414 		return;
415 	if (list_is_singular(head) &&
416 		(head->next != entry && head != entry))
417 		return;
418 	if (entry == head)
419 		INIT_LIST_HEAD(list);
420 	else
421 		__list_cut_position(list, head, entry);
422 }
423 
424 /**
425  *  list_is_last - tests whether @list is the last entry in list @head
426  *   @list: the entry to test
427  *    @head: the head of the list
428  */
429 static inline int list_is_last(const struct list_head *list,
430                                 const struct list_head *head)
431 {
432         return list->next == head;
433 }
434 
435 #define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
436 
437 #define	hlist_for_each(p, head)						\
438 	for (p = (head)->first; p; p = p->next)
439 
440 #define	hlist_for_each_safe(p, n, head)					\
441 	for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
442 
443 #define hlist_entry_safe(ptr, type, member) \
444 	(ptr) ? hlist_entry(ptr, type, member) : NULL
445 
446 #define hlist_for_each_entry(pos, head, member)				\
447 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
448 	     pos;							\
449 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
450 
451 #define hlist_for_each_entry_continue(tp, p, field)			\
452 	for (p = (p)->next;						\
453 	    p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
454 
455 #define	hlist_for_each_entry_from(tp, p, field)				\
456 	for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
457 
458 #define hlist_for_each_entry_safe(pos, n, head, member)			\
459 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
460 	     (pos) && ({ n = (pos)->member.next; 1; });			\
461 	     pos = hlist_entry_safe(n, typeof(*(pos)), member))
462 
463 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
464     struct list_head *a, struct list_head *b));
465 
466 #define hlist_add_head_rcu(n, h)	hlist_add_head(n, h)
467 
468 #define hlist_del_init_rcu(n)		hlist_del_init(n)
469 
470 #endif /* _LINUX_LIST_H_ */
471