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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef _LINUX_LIST_H_
32 #define _LINUX_LIST_H_
35 * Since LIST_HEAD conflicts with the linux definition we must include any
36 * FreeBSD header which requires it here so it is resolved with the correct
37 * definition prior to the undef.
39 #include <linux/types.h>
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/queue.h>
44 #include <sys/cpuset.h>
47 #include <sys/mutex.h>
49 #include <sys/vnode.h>
51 #include <sys/socket.h>
56 #include <net/if_var.h>
57 #include <net/if_types.h>
58 #include <net/if_media.h>
61 #include <netinet/in.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_var.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet6/nd6.h>
69 #include <vm/vm_object.h>
71 #include <machine/pmap.h>
76 struct list_head *next;
77 struct list_head *prev;
81 INIT_LIST_HEAD(struct list_head *list)
84 list->next = list->prev = list;
88 list_empty(const struct list_head *head)
91 return (head->next == head);
95 list_del(struct list_head *entry)
98 entry->next->prev = entry->prev;
99 entry->prev->next = entry->next;
103 list_replace(struct list_head *old, struct list_head *new)
105 new->next = old->next;
106 new->next->prev = new;
107 new->prev = old->prev;
108 new->prev->next = new;
112 _list_add(struct list_head *new, struct list_head *prev,
113 struct list_head *next)
123 list_del_init(struct list_head *entry)
127 INIT_LIST_HEAD(entry);
130 #define list_entry(ptr, type, field) container_of(ptr, type, field)
132 #define list_first_entry(ptr, type, member) \
133 list_entry((ptr)->next, type, member)
135 #define list_next_entry(ptr, member) \
136 list_entry(((ptr)->member.next), typeof(*(ptr)), member)
138 #define list_for_each(p, head) \
139 for (p = (head)->next; p != (head); p = p->next)
141 #define list_for_each_safe(p, n, head) \
142 for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
144 #define list_for_each_entry(p, h, field) \
145 for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
146 p = list_entry(p->field.next, typeof(*p), field))
148 #define list_for_each_entry_safe(p, n, h, field) \
149 for (p = list_entry((h)->next, typeof(*p), field), \
150 n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
151 p = n, n = list_entry(n->field.next, typeof(*n), field))
153 #define list_for_each_entry_continue(p, h, field) \
154 for (p = list_next_entry((p), field); &p->field != (h); \
155 p = list_next_entry((p), field))
157 #define list_for_each_entry_safe_from(pos, n, head, member) \
158 for (n = list_entry(pos->member.next, typeof(*pos), member); \
159 &pos->member != (head); \
160 pos = n, n = list_entry(n->member.next, typeof(*n), member))
162 #define list_for_each_entry_reverse(p, h, field) \
163 for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
164 p = list_entry(p->field.prev, typeof(*p), field))
166 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
169 list_add(struct list_head *new, struct list_head *head)
172 _list_add(new, head, head->next);
176 list_add_tail(struct list_head *new, struct list_head *head)
179 _list_add(new, head->prev, head);
183 list_move(struct list_head *list, struct list_head *head)
187 list_add(list, head);
191 list_move_tail(struct list_head *entry, struct list_head *head)
195 list_add_tail(entry, head);
199 _list_splice(const struct list_head *list, struct list_head *prev,
200 struct list_head *next)
202 struct list_head *first;
203 struct list_head *last;
205 if (list_empty(list))
216 list_splice(const struct list_head *list, struct list_head *head)
219 _list_splice(list, head, head->next);
223 list_splice_tail(struct list_head *list, struct list_head *head)
226 _list_splice(list, head->prev, head);
230 list_splice_init(struct list_head *list, struct list_head *head)
233 _list_splice(list, head, head->next);
234 INIT_LIST_HEAD(list);
238 list_splice_tail_init(struct list_head *list, struct list_head *head)
241 _list_splice(list, head->prev, head);
242 INIT_LIST_HEAD(list);
246 #define LIST_HEAD(name) struct list_head name = { &(name), &(name) }
250 struct hlist_node *first;
254 struct hlist_node *next, **pprev;
257 #define HLIST_HEAD_INIT { }
258 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
259 #define INIT_HLIST_HEAD(head) (head)->first = NULL
260 #define INIT_HLIST_NODE(node) \
262 (node)->next = NULL; \
263 (node)->pprev = NULL; \
267 hlist_unhashed(const struct hlist_node *h)
274 hlist_empty(const struct hlist_head *h)
281 hlist_del(struct hlist_node *n)
285 n->next->pprev = n->pprev;
290 hlist_del_init(struct hlist_node *n)
293 if (hlist_unhashed(n))
300 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
305 h->first->pprev = &n->next;
307 n->pprev = &h->first;
311 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
314 n->pprev = next->pprev;
316 next->pprev = &n->next;
321 hlist_add_after(struct hlist_node *n, struct hlist_node *next)
324 next->next = n->next;
326 next->pprev = &n->next;
328 next->next->pprev = &next->next;
332 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
335 new->first = old->first;
337 new->first->pprev = &new->first;
342 * list_is_singular - tests whether a list has just one entry.
343 * @head: the list to test.
345 static inline int list_is_singular(const struct list_head *head)
347 return !list_empty(head) && (head->next == head->prev);
350 static inline void __list_cut_position(struct list_head *list,
351 struct list_head *head, struct list_head *entry)
353 struct list_head *new_first = entry->next;
354 list->next = head->next;
355 list->next->prev = list;
358 head->next = new_first;
359 new_first->prev = head;
363 * list_cut_position - cut a list into two
364 * @list: a new list to add all removed entries
365 * @head: a list with entries
366 * @entry: an entry within head, could be the head itself
367 * and if so we won't cut the list
369 * This helper moves the initial part of @head, up to and
370 * including @entry, from @head to @list. You should
371 * pass on @entry an element you know is on @head. @list
372 * should be an empty list or a list you do not care about
376 static inline void list_cut_position(struct list_head *list,
377 struct list_head *head, struct list_head *entry)
379 if (list_empty(head))
381 if (list_is_singular(head) &&
382 (head->next != entry && head != entry))
385 INIT_LIST_HEAD(list);
387 __list_cut_position(list, head, entry);
391 * list_is_last - tests whether @list is the last entry in list @head
392 * @list: the entry to test
393 * @head: the head of the list
395 static inline int list_is_last(const struct list_head *list,
396 const struct list_head *head)
398 return list->next == head;
401 #define hlist_entry(ptr, type, field) container_of(ptr, type, field)
403 #define hlist_for_each(p, head) \
404 for (p = (head)->first; p; p = p->next)
406 #define hlist_for_each_safe(p, n, head) \
407 for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
409 #define hlist_entry_safe(ptr, type, member) \
410 ((ptr) ? hlist_entry(ptr, type, member) : NULL)
412 #define hlist_for_each_entry(pos, head, member) \
413 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
415 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
417 #define hlist_for_each_entry_continue(tp, p, field) \
418 for (p = (p)->next; \
419 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
421 #define hlist_for_each_entry_from(tp, p, field) \
422 for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
424 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
425 for (pos = (head)->first; \
426 (pos) != 0 && ({ n = (pos)->next; \
427 tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \
430 #define hlist_add_head_rcu(n, h) hlist_add_head(n, h)
432 #define hlist_del_init_rcu(n) hlist_del_init(n)
434 #endif /* _LINUX_LIST_H_ */