drm: Add list_replace_init()
[dragonfly.git] / sys / dev / drm / include / linux / list.h
blob862407073b5300cf1d90be2357bdd2ec1c724564
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, 2014 Mellanox Technologies, Ltd.
6 * Copyright (c) 2015 François Tigeot
7 * All rights reserved.
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.
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.
30 #ifndef _LINUX_LIST_H_
31 #define _LINUX_LIST_H_
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.
38 #include <linux/types.h>
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/jail.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/proc.h>
47 #include <sys/conf.h>
48 #include <sys/socket.h>
49 #include <sys/mbuf.h>
51 #include <net/bpf.h>
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_types.h>
55 #include <net/if_media.h>
57 #include <netinet/in.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/in_var.h>
61 #include <netinet6/in6_var.h>
62 #include <netinet6/nd6.h>
64 #include <vm/vm.h>
65 #include <vm/vm_object.h>
67 #define prefetch(x)
69 struct list_head {
70 struct list_head *next;
71 struct list_head *prev;
74 static inline void
75 INIT_LIST_HEAD(struct list_head *list)
78 list->next = list->prev = list;
81 static inline struct list_head *
82 list_first(const struct list_head *head)
84 return head->next;
87 static inline struct list_head *
88 list_last(const struct list_head *head)
90 return head->prev;
93 static inline int
94 list_empty(const struct list_head *head)
97 return (head->next == head);
100 static inline void
101 list_del(struct list_head *entry)
104 entry->next->prev = entry->prev;
105 entry->prev->next = entry->next;
108 static inline void list_replace(struct list_head *old,
109 struct list_head *new)
111 new->next = old->next;
112 new->next->prev = new;
113 new->prev = old->prev;
114 new->prev->next = new;
117 static inline void list_replace_init(struct list_head *old,
118 struct list_head *new)
120 list_replace(old, new);
121 INIT_LIST_HEAD(old);
124 static inline void
125 _list_add(struct list_head *new, struct list_head *prev,
126 struct list_head *next)
129 next->prev = new;
130 new->next = next;
131 new->prev = prev;
132 prev->next = new;
135 static inline void
136 list_del_init(struct list_head *entry)
139 list_del(entry);
140 INIT_LIST_HEAD(entry);
143 #define list_entry(ptr, type, field) container_of(ptr, type, field)
145 #define list_first_entry(ptr, type, member) \
146 list_entry((ptr)->next, type, member)
148 #define list_first_entry_or_null(ptr, type, member) \
149 (list_empty(ptr) ? NULL: list_first_entry(ptr, type, member))
151 #define list_last_entry(ptr, type, field) \
152 list_entry(list_last((ptr)), type, field)
154 #define list_next_entry(ptr, member) \
155 list_entry(((ptr)->member.next), typeof(*(ptr)), member)
157 #define list_for_each(p, head) \
158 for (p = (head)->next; p != (head); p = p->next)
160 #define list_for_each_safe(p, n, head) \
161 for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
163 #define list_for_each_entry(p, h, field) \
164 for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
165 p = list_entry(p->field.next, typeof(*p), field))
167 #define list_for_each_entry_safe(p, n, h, field) \
168 for (p = list_entry((h)->next, typeof(*p), field), \
169 n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
170 p = n, n = list_entry(n->field.next, typeof(*n), field))
172 #define list_for_each_entry_continue(p, h, field) \
173 for (p = list_next_entry((p), field); &p->field != (h); \
174 p = list_next_entry((p), field))
176 #define list_for_each_entry_safe_from(pos, n, head, member) \
177 for (n = list_entry(pos->member.next, typeof(*pos), member); \
178 &pos->member != (head); \
179 pos = n, n = list_entry(n->member.next, typeof(*n), member))
181 #define list_for_each_entry_reverse(p, h, field) \
182 for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
183 p = list_entry(p->field.prev, typeof(*p), field))
185 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
187 static inline void
188 list_add(struct list_head *new, struct list_head *head)
191 _list_add(new, head, head->next);
194 static inline void
195 list_add_tail(struct list_head *new, struct list_head *head)
198 _list_add(new, head->prev, head);
201 static inline void
202 list_move(struct list_head *list, struct list_head *head)
205 list_del(list);
206 list_add(list, head);
209 static inline void
210 list_move_tail(struct list_head *entry, struct list_head *head)
213 list_del(entry);
214 list_add_tail(entry, head);
217 static inline void
218 _list_splice(const struct list_head *list, struct list_head *prev,
219 struct list_head *next)
221 struct list_head *first;
222 struct list_head *last;
224 if (list_empty(list))
225 return;
226 first = list->next;
227 last = list->prev;
228 first->prev = prev;
229 prev->next = first;
230 last->next = next;
231 next->prev = last;
234 static inline void
235 list_splice(const struct list_head *list, struct list_head *head)
238 _list_splice(list, head, head->next);
241 static inline void
242 list_splice_tail(struct list_head *list, struct list_head *head)
245 _list_splice(list, head->prev, head);
248 static inline void
249 list_splice_init(struct list_head *list, struct list_head *head)
252 _list_splice(list, head, head->next);
253 INIT_LIST_HEAD(list);
256 static inline void
257 list_splice_tail_init(struct list_head *list, struct list_head *head)
260 _list_splice(list, head->prev, head);
261 INIT_LIST_HEAD(list);
264 #define LINUX_LIST_HEAD(name) struct list_head name = { &(name), &(name) }
267 struct hlist_head {
268 struct hlist_node *first;
271 struct hlist_node {
272 struct hlist_node *next, **pprev;
275 #define HLIST_HEAD_INIT { }
276 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
277 #define INIT_HLIST_HEAD(head) (head)->first = NULL
278 #define INIT_HLIST_NODE(node) \
279 do { \
280 (node)->next = NULL; \
281 (node)->pprev = NULL; \
282 } while (0)
284 static inline int
285 hlist_unhashed(const struct hlist_node *h)
288 return !h->pprev;
291 static inline int
292 hlist_empty(const struct hlist_head *h)
295 return !h->first;
298 static inline void
299 hlist_del(struct hlist_node *n)
302 if (n->next)
303 n->next->pprev = n->pprev;
304 *n->pprev = n->next;
307 static inline void
308 hlist_del_init(struct hlist_node *n)
311 if (hlist_unhashed(n))
312 return;
313 hlist_del(n);
314 INIT_HLIST_NODE(n);
317 static inline void
318 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
321 n->next = h->first;
322 if (h->first)
323 h->first->pprev = &n->next;
324 h->first = n;
325 n->pprev = &h->first;
328 static inline void
329 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
332 n->pprev = next->pprev;
333 n->next = next;
334 next->pprev = &n->next;
335 *(n->pprev) = n;
338 static inline void
339 hlist_add_after(struct hlist_node *n, struct hlist_node *next)
342 next->next = n->next;
343 n->next = next;
344 next->pprev = &n->next;
345 if (next->next)
346 next->next->pprev = &next->next;
349 static inline void
350 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
353 new->first = old->first;
354 if (new->first)
355 new->first->pprev = &new->first;
356 old->first = NULL;
360 * list_is_singular - tests whether a list has just one entry.
361 * @head: the list to test.
363 static inline int list_is_singular(const struct list_head *head)
365 return !list_empty(head) && (head->next == head->prev);
368 static inline void __list_cut_position(struct list_head *list,
369 struct list_head *head, struct list_head *entry)
371 struct list_head *new_first = entry->next;
372 list->next = head->next;
373 list->next->prev = list;
374 list->prev = entry;
375 entry->next = list;
376 head->next = new_first;
377 new_first->prev = head;
381 * list_cut_position - cut a list into two
382 * @list: a new list to add all removed entries
383 * @head: a list with entries
384 * @entry: an entry within head, could be the head itself
385 * and if so we won't cut the list
387 * This helper moves the initial part of @head, up to and
388 * including @entry, from @head to @list. You should
389 * pass on @entry an element you know is on @head. @list
390 * should be an empty list or a list you do not care about
391 * losing its data.
394 static inline void list_cut_position(struct list_head *list,
395 struct list_head *head, struct list_head *entry)
397 if (list_empty(head))
398 return;
399 if (list_is_singular(head) &&
400 (head->next != entry && head != entry))
401 return;
402 if (entry == head)
403 INIT_LIST_HEAD(list);
404 else
405 __list_cut_position(list, head, entry);
409 * list_is_last - tests whether @list is the last entry in list @head
410 * @list: the entry to test
411 * @head: the head of the list
413 static inline int list_is_last(const struct list_head *list,
414 const struct list_head *head)
416 return list->next == head;
419 #define hlist_entry(ptr, type, field) container_of(ptr, type, field)
421 #define hlist_for_each(p, head) \
422 for (p = (head)->first; p; p = p->next)
424 #define hlist_for_each_safe(p, n, head) \
425 for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
427 #define hlist_entry_safe(ptr, type, member) \
428 (ptr) ? hlist_entry(ptr, type, member) : NULL
430 #define hlist_for_each_entry(pos, head, member) \
431 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
432 pos; \
433 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
435 #define hlist_for_each_entry_continue(tp, p, field) \
436 for (p = (p)->next; \
437 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
439 #define hlist_for_each_entry_from(tp, p, field) \
440 for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
442 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
443 for (pos = (head)->first; \
444 (pos) != 0 && ({ n = (pos)->next; \
445 tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \
446 pos = (n))
448 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
449 struct list_head *a, struct list_head *b));
451 #define hlist_add_head_rcu(n, h) hlist_add_head(n, h)
453 #define hlist_del_init_rcu(n) hlist_del_init(n)
455 #endif /* _LINUX_LIST_H_ */