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