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.
29 #ifndef _LINUX_LIST_H_
30 #define _LINUX_LIST_H_
33 * Since LIST_HEAD conflicts with the linux definition we must include any
34 * FreeBSD header which requires it here so it is resolved with the correct
35 * definition prior to the undef.
37 #include <linux/types.h>
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/queue.h>
44 #include <sys/mutex.h>
46 #include <sys/vnode.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;
82 list_empty(const struct list_head *head)
85 return (head->next == head);
89 list_del(struct list_head *entry)
92 entry->next->prev = entry->prev;
93 entry->prev->next = entry->next;
96 static inline void list_replace(struct list_head *old,
97 struct list_head *new)
99 new->next = old->next;
100 new->next->prev = new;
101 new->prev = old->prev;
102 new->prev->next = new;
106 _list_add(struct list_head *new, struct list_head *prev,
107 struct list_head *next)
117 list_del_init(struct list_head *entry)
121 INIT_LIST_HEAD(entry);
124 #define list_entry(ptr, type, field) container_of(ptr, type, field)
126 #define list_first_entry(ptr, type, member) \
127 list_entry((ptr)->next, type, member)
129 #define list_next_entry(ptr, member) \
130 list_entry(((ptr)->member.next), typeof(*(ptr)), member)
132 #define list_for_each(p, head) \
133 for (p = (head)->next; p != (head); p = p->next)
135 #define list_for_each_safe(p, n, head) \
136 for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
138 #define list_for_each_entry(p, h, field) \
139 for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
140 p = list_entry(p->field.next, typeof(*p), field))
142 #define list_for_each_entry_safe(p, n, h, field) \
143 for (p = list_entry((h)->next, typeof(*p), field), \
144 n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
145 p = n, n = list_entry(n->field.next, typeof(*n), field))
147 #define list_for_each_entry_continue(p, h, field) \
148 for (p = list_next_entry((p), field); &p->field != (h); \
149 p = list_next_entry((p), field))
151 #define list_for_each_entry_safe_from(pos, n, head, member) \
152 for (n = list_entry(pos->member.next, typeof(*pos), member); \
153 &pos->member != (head); \
154 pos = n, n = list_entry(n->member.next, typeof(*n), member))
156 #define list_for_each_entry_reverse(p, h, field) \
157 for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
158 p = list_entry(p->field.prev, typeof(*p), field))
160 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
163 list_add(struct list_head *new, struct list_head *head)
166 _list_add(new, head, head->next);
170 list_add_tail(struct list_head *new, struct list_head *head)
173 _list_add(new, head->prev, head);
177 list_move(struct list_head *list, struct list_head *head)
181 list_add(list, head);
185 list_move_tail(struct list_head *entry, struct list_head *head)
189 list_add_tail(entry, head);
193 _list_splice(const struct list_head *list, struct list_head *prev,
194 struct list_head *next)
196 struct list_head *first;
197 struct list_head *last;
199 if (list_empty(list))
210 list_splice(const struct list_head *list, struct list_head *head)
213 _list_splice(list, head, head->next);
217 list_splice_tail(struct list_head *list, struct list_head *head)
220 _list_splice(list, head->prev, head);
224 list_splice_init(struct list_head *list, struct list_head *head)
227 _list_splice(list, head, head->next);
228 INIT_LIST_HEAD(list);
232 list_splice_tail_init(struct list_head *list, struct list_head *head)
235 _list_splice(list, head->prev, head);
236 INIT_LIST_HEAD(list);
239 #define LINUX_LIST_HEAD(name) struct list_head name = { &(name), &(name) }
243 struct hlist_node *first;
247 struct hlist_node *next, **pprev;
250 #define HLIST_HEAD_INIT { }
251 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
252 #define INIT_HLIST_HEAD(head) (head)->first = NULL
253 #define INIT_HLIST_NODE(node) \
255 (node)->next = NULL; \
256 (node)->pprev = NULL; \
260 hlist_unhashed(const struct hlist_node *h)
267 hlist_empty(const struct hlist_head *h)
274 hlist_del(struct hlist_node *n)
278 n->next->pprev = n->pprev;
283 hlist_del_init(struct hlist_node *n)
286 if (hlist_unhashed(n))
293 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
298 h->first->pprev = &n->next;
300 n->pprev = &h->first;
304 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
307 n->pprev = next->pprev;
309 next->pprev = &n->next;
314 hlist_add_after(struct hlist_node *n, struct hlist_node *next)
317 next->next = n->next;
319 next->pprev = &n->next;
321 next->next->pprev = &next->next;
325 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
328 new->first = old->first;
330 new->first->pprev = &new->first;
335 * list_is_singular - tests whether a list has just one entry.
336 * @head: the list to test.
338 static inline int list_is_singular(const struct list_head *head)
340 return !list_empty(head) && (head->next == head->prev);
343 static inline void __list_cut_position(struct list_head *list,
344 struct list_head *head, struct list_head *entry)
346 struct list_head *new_first = entry->next;
347 list->next = head->next;
348 list->next->prev = list;
351 head->next = new_first;
352 new_first->prev = head;
356 * list_cut_position - cut a list into two
357 * @list: a new list to add all removed entries
358 * @head: a list with entries
359 * @entry: an entry within head, could be the head itself
360 * and if so we won't cut the list
362 * This helper moves the initial part of @head, up to and
363 * including @entry, from @head to @list. You should
364 * pass on @entry an element you know is on @head. @list
365 * should be an empty list or a list you do not care about
369 static inline void list_cut_position(struct list_head *list,
370 struct list_head *head, struct list_head *entry)
372 if (list_empty(head))
374 if (list_is_singular(head) &&
375 (head->next != entry && head != entry))
378 INIT_LIST_HEAD(list);
380 __list_cut_position(list, head, entry);
384 * list_is_last - tests whether @list is the last entry in list @head
385 * @list: the entry to test
386 * @head: the head of the list
388 static inline int list_is_last(const struct list_head *list,
389 const struct list_head *head)
391 return list->next == head;
394 #define hlist_entry(ptr, type, field) container_of(ptr, type, field)
396 #define hlist_for_each(p, head) \
397 for (p = (head)->first; p; p = p->next)
399 #define hlist_for_each_safe(p, n, head) \
400 for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
402 #define hlist_for_each_entry(tp, p, head, field) \
403 for (p = (head)->first; \
404 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
406 #define hlist_for_each_entry_continue(tp, p, field) \
407 for (p = (p)->next; \
408 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
410 #define hlist_for_each_entry_from(tp, p, field) \
411 for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
413 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
414 for (pos = (head)->first; \
415 (pos) != 0 && ({ n = (pos)->next; \
416 tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \
419 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
420 struct list_head *a, struct list_head *b));
422 #define hlist_for_each_entry_rcu(tp, p, head, field) \
423 hlist_for_each_entry(tp, p, head, field)
425 #define hlist_add_after_rcu(prev, n) hlist_add_after(prev, n)
427 #define hlist_add_head_rcu(n, h) hlist_add_head(n, h)
429 #define hlist_del_init_rcu(n) hlist_del_init(n)
431 #endif /* _LINUX_LIST_H_ */