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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
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>
45 #include <sys/mutex.h>
48 #include <sys/socket.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>
65 #include <vm/vm_object.h>
70 struct list_head *next;
71 struct list_head *prev;
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)
87 static inline struct list_head *
88 list_last(const struct list_head *head)
94 list_empty(const struct list_head *head)
97 return (head->next == head);
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;
118 _list_add(struct list_head *new, struct list_head *prev,
119 struct list_head *next)
129 list_del_init(struct list_head *entry)
133 INIT_LIST_HEAD(entry);
136 #define list_entry(ptr, type, field) container_of(ptr, type, field)
138 #define list_first_entry(ptr, type, member) \
139 list_entry((ptr)->next, type, member)
141 #define list_first_entry_or_null(ptr, type, member) \
142 (list_empty(ptr) ? NULL: list_first_entry(ptr, type, member))
144 #define list_last_entry(ptr, type, field) \
145 list_entry(list_last((ptr)), type, field)
147 #define list_next_entry(ptr, member) \
148 list_entry(((ptr)->member.next), typeof(*(ptr)), member)
150 #define list_for_each(p, head) \
151 for (p = (head)->next; p != (head); p = p->next)
153 #define list_for_each_safe(p, n, head) \
154 for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
156 #define list_for_each_entry(p, h, field) \
157 for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
158 p = list_entry(p->field.next, typeof(*p), field))
160 #define list_for_each_entry_safe(p, n, h, field) \
161 for (p = list_entry((h)->next, typeof(*p), field), \
162 n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
163 p = n, n = list_entry(n->field.next, typeof(*n), field))
165 #define list_for_each_entry_continue(p, h, field) \
166 for (p = list_next_entry((p), field); &p->field != (h); \
167 p = list_next_entry((p), field))
169 #define list_for_each_entry_safe_from(pos, n, head, member) \
170 for (n = list_entry(pos->member.next, typeof(*pos), member); \
171 &pos->member != (head); \
172 pos = n, n = list_entry(n->member.next, typeof(*n), member))
174 #define list_for_each_entry_reverse(p, h, field) \
175 for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
176 p = list_entry(p->field.prev, typeof(*p), field))
178 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
181 list_add(struct list_head *new, struct list_head *head)
184 _list_add(new, head, head->next);
188 list_add_tail(struct list_head *new, struct list_head *head)
191 _list_add(new, head->prev, head);
195 list_move(struct list_head *list, struct list_head *head)
199 list_add(list, head);
203 list_move_tail(struct list_head *entry, struct list_head *head)
207 list_add_tail(entry, head);
211 _list_splice(const struct list_head *list, struct list_head *prev,
212 struct list_head *next)
214 struct list_head *first;
215 struct list_head *last;
217 if (list_empty(list))
228 list_splice(const struct list_head *list, struct list_head *head)
231 _list_splice(list, head, head->next);
235 list_splice_tail(struct list_head *list, struct list_head *head)
238 _list_splice(list, head->prev, head);
242 list_splice_init(struct list_head *list, struct list_head *head)
245 _list_splice(list, head, head->next);
246 INIT_LIST_HEAD(list);
250 list_splice_tail_init(struct list_head *list, struct list_head *head)
253 _list_splice(list, head->prev, head);
254 INIT_LIST_HEAD(list);
257 #define LINUX_LIST_HEAD(name) struct list_head name = { &(name), &(name) }
261 struct hlist_node *first;
265 struct hlist_node *next, **pprev;
268 #define HLIST_HEAD_INIT { }
269 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
270 #define INIT_HLIST_HEAD(head) (head)->first = NULL
271 #define INIT_HLIST_NODE(node) \
273 (node)->next = NULL; \
274 (node)->pprev = NULL; \
278 hlist_unhashed(const struct hlist_node *h)
285 hlist_empty(const struct hlist_head *h)
292 hlist_del(struct hlist_node *n)
296 n->next->pprev = n->pprev;
301 hlist_del_init(struct hlist_node *n)
304 if (hlist_unhashed(n))
311 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
316 h->first->pprev = &n->next;
318 n->pprev = &h->first;
322 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
325 n->pprev = next->pprev;
327 next->pprev = &n->next;
332 hlist_add_after(struct hlist_node *n, struct hlist_node *next)
335 next->next = n->next;
337 next->pprev = &n->next;
339 next->next->pprev = &next->next;
343 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
346 new->first = old->first;
348 new->first->pprev = &new->first;
353 * list_is_singular - tests whether a list has just one entry.
354 * @head: the list to test.
356 static inline int list_is_singular(const struct list_head *head)
358 return !list_empty(head) && (head->next == head->prev);
361 static inline void __list_cut_position(struct list_head *list,
362 struct list_head *head, struct list_head *entry)
364 struct list_head *new_first = entry->next;
365 list->next = head->next;
366 list->next->prev = list;
369 head->next = new_first;
370 new_first->prev = head;
374 * list_cut_position - cut a list into two
375 * @list: a new list to add all removed entries
376 * @head: a list with entries
377 * @entry: an entry within head, could be the head itself
378 * and if so we won't cut the list
380 * This helper moves the initial part of @head, up to and
381 * including @entry, from @head to @list. You should
382 * pass on @entry an element you know is on @head. @list
383 * should be an empty list or a list you do not care about
387 static inline void list_cut_position(struct list_head *list,
388 struct list_head *head, struct list_head *entry)
390 if (list_empty(head))
392 if (list_is_singular(head) &&
393 (head->next != entry && head != entry))
396 INIT_LIST_HEAD(list);
398 __list_cut_position(list, head, entry);
402 * list_is_last - tests whether @list is the last entry in list @head
403 * @list: the entry to test
404 * @head: the head of the list
406 static inline int list_is_last(const struct list_head *list,
407 const struct list_head *head)
409 return list->next == head;
412 #define hlist_entry(ptr, type, field) container_of(ptr, type, field)
414 #define hlist_for_each(p, head) \
415 for (p = (head)->first; p; p = p->next)
417 #define hlist_for_each_safe(p, n, head) \
418 for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
420 #define hlist_entry_safe(ptr, type, member) \
421 (ptr) ? hlist_entry(ptr, type, member) : NULL
423 #define hlist_for_each_entry(pos, head, member) \
424 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
426 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
428 #define hlist_for_each_entry_continue(tp, p, field) \
429 for (p = (p)->next; \
430 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
432 #define hlist_for_each_entry_from(tp, p, field) \
433 for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
435 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
436 for (pos = (head)->first; \
437 (pos) != 0 && ({ n = (pos)->next; \
438 tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \
441 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
442 struct list_head *a, struct list_head *b));
444 #define hlist_add_head_rcu(n, h) hlist_add_head(n, h)
446 #define hlist_del_init_rcu(n) hlist_del_init(n)
448 #endif /* _LINUX_LIST_H_ */