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.
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_
34 #define container_of(ptr, type, member) \
36 const __typeof(((type *)0)->member) *__p = (ptr); \
37 (type *)((uintptr_t)__p - offsetof(type, member)); \
40 #define LINUX_LIST_HEAD_INIT(name) { &(name), &(name) }
42 #define LINUX_LIST_HEAD(name) \
43 struct list_head name = LINUX_LIST_HEAD_INIT(name)
48 struct list_head *next;
49 struct list_head *prev;
53 #define barrier() __asm__ __volatile__("": : :"memory")
56 #define ACCESS_ONCE(x) (*(volatile __typeof(x) *)&(x))
58 #define WRITE_ONCE(x,v) do { \
60 ACCESS_ONCE(x) = (v); \
64 #define READ_ONCE(x) ({ \
65 __typeof(x) __var = ({ \
74 INIT_LIST_HEAD(struct list_head *list)
77 list->next = list->prev = list;
81 list_empty(const struct list_head *head)
84 return (head->next == head);
88 list_empty_careful(const struct list_head *head)
90 struct list_head *next = head->next;
92 return ((next == head) && (next == head->prev));
96 __list_del(struct list_head *prev, struct list_head *next)
99 WRITE_ONCE(prev->next, next);
103 __list_del_entry(struct list_head *entry)
106 __list_del(entry->prev, entry->next);
110 list_del(struct list_head *entry)
113 __list_del(entry->prev, entry->next);
117 list_replace(struct list_head *old, struct list_head *new)
119 new->next = old->next;
120 new->next->prev = new;
121 new->prev = old->prev;
122 new->prev->next = new;
126 list_replace_init(struct list_head *old, struct list_head *new)
128 list_replace(old, new);
133 linux_list_add(struct list_head *new, struct list_head *prev,
134 struct list_head *next)
144 list_del_init(struct list_head *entry)
148 INIT_LIST_HEAD(entry);
151 #define list_entry(ptr, type, field) container_of(ptr, type, field)
153 #define list_first_entry(ptr, type, member) \
154 list_entry((ptr)->next, type, member)
156 #define list_last_entry(ptr, type, member) \
157 list_entry((ptr)->prev, type, member)
159 #define list_first_entry_or_null(ptr, type, member) \
160 (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
162 #define list_next_entry(ptr, member) \
163 list_entry(((ptr)->member.next), typeof(*(ptr)), member)
165 #define list_safe_reset_next(ptr, n, member) \
166 (n) = list_next_entry(ptr, member)
168 #define list_prev_entry(ptr, member) \
169 list_entry(((ptr)->member.prev), typeof(*(ptr)), member)
171 #define list_for_each(p, head) \
172 for (p = (head)->next; p != (head); p = (p)->next)
174 #define list_for_each_safe(p, n, head) \
175 for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next)
177 #define list_for_each_entry(p, h, field) \
178 for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \
179 p = list_entry((p)->field.next, typeof(*p), field))
181 #define list_for_each_entry_safe(p, n, h, field) \
182 for (p = list_entry((h)->next, typeof(*p), field), \
183 n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\
184 p = n, n = list_entry(n->field.next, typeof(*n), field))
186 #define list_for_each_entry_from(p, h, field) \
187 for ( ; &(p)->field != (h); \
188 p = list_entry((p)->field.next, typeof(*p), field))
190 #define list_for_each_entry_continue(p, h, field) \
191 for (p = list_next_entry((p), field); &(p)->field != (h); \
192 p = list_next_entry((p), field))
194 #define list_for_each_entry_safe_from(pos, n, head, member) \
195 for (n = list_entry((pos)->member.next, typeof(*pos), member); \
196 &(pos)->member != (head); \
197 pos = n, n = list_entry(n->member.next, typeof(*n), member))
199 #define list_for_each_entry_reverse(p, h, field) \
200 for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \
201 p = list_entry((p)->field.prev, typeof(*p), field))
203 #define list_for_each_entry_safe_reverse(p, n, h, field) \
204 for (p = list_entry((h)->prev, typeof(*p), field), \
205 n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
206 p = n, n = list_entry(n->field.prev, typeof(*n), field))
208 #define list_for_each_entry_continue_reverse(p, h, field) \
209 for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
210 p = list_entry((p)->field.prev, typeof(*p), field))
212 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev)
215 list_add(struct list_head *new, struct list_head *head)
218 linux_list_add(new, head, head->next);
222 list_add_tail(struct list_head *new, struct list_head *head)
225 linux_list_add(new, head->prev, head);
229 list_move(struct list_head *list, struct list_head *head)
233 list_add(list, head);
237 list_move_tail(struct list_head *entry, struct list_head *head)
241 list_add_tail(entry, head);
245 linux_list_splice(const struct list_head *list, struct list_head *prev,
246 struct list_head *next)
248 struct list_head *first;
249 struct list_head *last;
251 if (list_empty(list))
262 list_splice(const struct list_head *list, struct list_head *head)
265 linux_list_splice(list, head, head->next);
269 list_splice_tail(struct list_head *list, struct list_head *head)
272 linux_list_splice(list, head->prev, head);
276 list_splice_init(struct list_head *list, struct list_head *head)
279 linux_list_splice(list, head, head->next);
280 INIT_LIST_HEAD(list);
284 list_splice_tail_init(struct list_head *list, struct list_head *head)
287 linux_list_splice(list, head->prev, head);
288 INIT_LIST_HEAD(list);
292 #define LIST_HEAD(name) struct list_head name = { &(name), &(name) }
296 struct hlist_node *first;
300 struct hlist_node *next, **pprev;
303 #define HLIST_HEAD_INIT { }
304 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
305 #define INIT_HLIST_HEAD(head) (head)->first = NULL
306 #define INIT_HLIST_NODE(node) \
308 (node)->next = NULL; \
309 (node)->pprev = NULL; \
313 hlist_unhashed(const struct hlist_node *h)
320 hlist_empty(const struct hlist_head *h)
327 hlist_del(struct hlist_node *n)
331 n->next->pprev = n->pprev;
336 hlist_del_init(struct hlist_node *n)
339 if (hlist_unhashed(n))
346 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
351 h->first->pprev = &n->next;
353 n->pprev = &h->first;
357 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
360 n->pprev = next->pprev;
362 next->pprev = &n->next;
367 hlist_add_after(struct hlist_node *n, struct hlist_node *next)
370 next->next = n->next;
372 next->pprev = &n->next;
374 next->next->pprev = &next->next;
378 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
381 new->first = old->first;
383 new->first->pprev = &new->first;
387 static inline int list_is_singular(const struct list_head *head)
389 return !list_empty(head) && (head->next == head->prev);
392 static inline void __list_cut_position(struct list_head *list,
393 struct list_head *head, struct list_head *entry)
395 struct list_head *new_first = entry->next;
396 list->next = head->next;
397 list->next->prev = list;
400 head->next = new_first;
401 new_first->prev = head;
404 static inline void list_cut_position(struct list_head *list,
405 struct list_head *head, struct list_head *entry)
407 if (list_empty(head))
409 if (list_is_singular(head) &&
410 (head->next != entry && head != entry))
413 INIT_LIST_HEAD(list);
415 __list_cut_position(list, head, entry);
418 static inline int list_is_last(const struct list_head *list,
419 const struct list_head *head)
421 return list->next == head;
424 #define hlist_entry(ptr, type, field) container_of(ptr, type, field)
426 #define hlist_for_each(p, head) \
427 for (p = (head)->first; p; p = (p)->next)
429 #define hlist_for_each_safe(p, n, head) \
430 for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n)
432 #define hlist_entry_safe(ptr, type, member) \
433 ((ptr) ? hlist_entry(ptr, type, member) : NULL)
435 #define hlist_for_each_entry(pos, head, member) \
436 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
438 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
440 #define hlist_for_each_entry_continue(pos, member) \
441 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \
443 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
445 #define hlist_for_each_entry_from(pos, member) \
447 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
449 #define hlist_for_each_entry_safe(pos, n, head, member) \
450 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
451 (pos) && ({ n = (pos)->member.next; 1; }); \
452 pos = hlist_entry_safe(n, typeof(*(pos)), member))
454 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
455 struct list_head *a, struct list_head *b));
457 #endif /* _LINUX_LIST_H_ */