1*959826caSMatt Macy /*-
2*959826caSMatt Macy * Copyright (c) 2010 Isilon Systems, Inc.
3*959826caSMatt Macy * Copyright (c) 2010 iX Systems, Inc.
4*959826caSMatt Macy * Copyright (c) 2010 Panasas, Inc.
5*959826caSMatt Macy * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6*959826caSMatt Macy * All rights reserved.
7*959826caSMatt Macy *
8*959826caSMatt Macy * Redistribution and use in source and binary forms, with or without
9*959826caSMatt Macy * modification, are permitted provided that the following conditions
10*959826caSMatt Macy * are met:
11*959826caSMatt Macy * 1. Redistributions of source code must retain the above copyright
12*959826caSMatt Macy * notice unmodified, this list of conditions, and the following
13*959826caSMatt Macy * disclaimer.
14*959826caSMatt Macy * 2. Redistributions in binary form must reproduce the above copyright
15*959826caSMatt Macy * notice, this list of conditions and the following disclaimer in the
16*959826caSMatt Macy * documentation and/or other materials provided with the distribution.
17*959826caSMatt Macy *
18*959826caSMatt Macy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*959826caSMatt Macy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*959826caSMatt Macy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*959826caSMatt Macy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*959826caSMatt Macy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*959826caSMatt Macy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*959826caSMatt Macy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*959826caSMatt Macy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*959826caSMatt Macy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*959826caSMatt Macy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*959826caSMatt Macy */
29*959826caSMatt Macy #ifndef _LINUX_LIST_H_
30*959826caSMatt Macy #define _LINUX_LIST_H_
31*959826caSMatt Macy
32*959826caSMatt Macy #define container_of(ptr, type, member) \
33*959826caSMatt Macy ({ \
34*959826caSMatt Macy const __typeof(((type *)0)->member) *__p = (ptr); \
35*959826caSMatt Macy (type *)((uintptr_t)__p - offsetof(type, member)); \
36*959826caSMatt Macy })
37*959826caSMatt Macy
38*959826caSMatt Macy #define LINUX_LIST_HEAD_INIT(name) { &(name), &(name) }
39*959826caSMatt Macy
40*959826caSMatt Macy #define LINUX_LIST_HEAD(name) \
41*959826caSMatt Macy struct list_head name = LINUX_LIST_HEAD_INIT(name)
42*959826caSMatt Macy
43*959826caSMatt Macy #ifndef LIST_HEAD_DEF
44*959826caSMatt Macy #define LIST_HEAD_DEF
45*959826caSMatt Macy struct list_head {
46*959826caSMatt Macy struct list_head *next;
47*959826caSMatt Macy struct list_head *prev;
48*959826caSMatt Macy };
49*959826caSMatt Macy #endif
50*959826caSMatt Macy
51*959826caSMatt Macy #define barrier() __asm__ __volatile__("": : :"memory")
52*959826caSMatt Macy
53*959826caSMatt Macy
54*959826caSMatt Macy #define ACCESS_ONCE(x) (*(volatile __typeof(x) *)&(x))
55*959826caSMatt Macy
56*959826caSMatt Macy #define WRITE_ONCE(x,v) do { \
57*959826caSMatt Macy barrier(); \
58*959826caSMatt Macy ACCESS_ONCE(x) = (v); \
59*959826caSMatt Macy barrier(); \
60*959826caSMatt Macy } while (0)
61*959826caSMatt Macy
62*959826caSMatt Macy #define READ_ONCE(x) ({ \
63*959826caSMatt Macy __typeof(x) __var = ({ \
64*959826caSMatt Macy barrier(); \
65*959826caSMatt Macy ACCESS_ONCE(x); \
66*959826caSMatt Macy }); \
67*959826caSMatt Macy barrier(); \
68*959826caSMatt Macy __var; \
69*959826caSMatt Macy })
70*959826caSMatt Macy
71*959826caSMatt Macy static inline void
INIT_LIST_HEAD(struct list_head * list)72*959826caSMatt Macy INIT_LIST_HEAD(struct list_head *list)
73*959826caSMatt Macy {
74*959826caSMatt Macy
75*959826caSMatt Macy list->next = list->prev = list;
76*959826caSMatt Macy }
77*959826caSMatt Macy
78*959826caSMatt Macy static inline int
list_empty(const struct list_head * head)79*959826caSMatt Macy list_empty(const struct list_head *head)
80*959826caSMatt Macy {
81*959826caSMatt Macy
82*959826caSMatt Macy return (head->next == head);
83*959826caSMatt Macy }
84*959826caSMatt Macy
85*959826caSMatt Macy static inline int
list_empty_careful(const struct list_head * head)86*959826caSMatt Macy list_empty_careful(const struct list_head *head)
87*959826caSMatt Macy {
88*959826caSMatt Macy struct list_head *next = head->next;
89*959826caSMatt Macy
90*959826caSMatt Macy return ((next == head) && (next == head->prev));
91*959826caSMatt Macy }
92*959826caSMatt Macy
93*959826caSMatt Macy static inline void
__list_del(struct list_head * prev,struct list_head * next)94*959826caSMatt Macy __list_del(struct list_head *prev, struct list_head *next)
95*959826caSMatt Macy {
96*959826caSMatt Macy next->prev = prev;
97*959826caSMatt Macy WRITE_ONCE(prev->next, next);
98*959826caSMatt Macy }
99*959826caSMatt Macy
100*959826caSMatt Macy static inline void
__list_del_entry(struct list_head * entry)101*959826caSMatt Macy __list_del_entry(struct list_head *entry)
102*959826caSMatt Macy {
103*959826caSMatt Macy
104*959826caSMatt Macy __list_del(entry->prev, entry->next);
105*959826caSMatt Macy }
106*959826caSMatt Macy
107*959826caSMatt Macy static inline void
list_del(struct list_head * entry)108*959826caSMatt Macy list_del(struct list_head *entry)
109*959826caSMatt Macy {
110*959826caSMatt Macy
111*959826caSMatt Macy __list_del(entry->prev, entry->next);
112*959826caSMatt Macy }
113*959826caSMatt Macy
114*959826caSMatt Macy static inline void
list_replace(struct list_head * old,struct list_head * new)115*959826caSMatt Macy list_replace(struct list_head *old, struct list_head *new)
116*959826caSMatt Macy {
117*959826caSMatt Macy new->next = old->next;
118*959826caSMatt Macy new->next->prev = new;
119*959826caSMatt Macy new->prev = old->prev;
120*959826caSMatt Macy new->prev->next = new;
121*959826caSMatt Macy }
122*959826caSMatt Macy
123*959826caSMatt Macy static inline void
list_replace_init(struct list_head * old,struct list_head * new)124*959826caSMatt Macy list_replace_init(struct list_head *old, struct list_head *new)
125*959826caSMatt Macy {
126*959826caSMatt Macy list_replace(old, new);
127*959826caSMatt Macy INIT_LIST_HEAD(old);
128*959826caSMatt Macy }
129*959826caSMatt Macy
130*959826caSMatt Macy static inline void
linux_list_add(struct list_head * new,struct list_head * prev,struct list_head * next)131*959826caSMatt Macy linux_list_add(struct list_head *new, struct list_head *prev,
132*959826caSMatt Macy struct list_head *next)
133*959826caSMatt Macy {
134*959826caSMatt Macy
135*959826caSMatt Macy next->prev = new;
136*959826caSMatt Macy new->next = next;
137*959826caSMatt Macy new->prev = prev;
138*959826caSMatt Macy prev->next = new;
139*959826caSMatt Macy }
140*959826caSMatt Macy
141*959826caSMatt Macy static inline void
list_del_init(struct list_head * entry)142*959826caSMatt Macy list_del_init(struct list_head *entry)
143*959826caSMatt Macy {
144*959826caSMatt Macy
145*959826caSMatt Macy list_del(entry);
146*959826caSMatt Macy INIT_LIST_HEAD(entry);
147*959826caSMatt Macy }
148*959826caSMatt Macy
149*959826caSMatt Macy #define list_entry(ptr, type, field) container_of(ptr, type, field)
150*959826caSMatt Macy
151*959826caSMatt Macy #define list_first_entry(ptr, type, member) \
152*959826caSMatt Macy list_entry((ptr)->next, type, member)
153*959826caSMatt Macy
154*959826caSMatt Macy #define list_last_entry(ptr, type, member) \
155*959826caSMatt Macy list_entry((ptr)->prev, type, member)
156*959826caSMatt Macy
157*959826caSMatt Macy #define list_first_entry_or_null(ptr, type, member) \
158*959826caSMatt Macy (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
159*959826caSMatt Macy
160*959826caSMatt Macy #define list_next_entry(ptr, member) \
161*959826caSMatt Macy list_entry(((ptr)->member.next), typeof(*(ptr)), member)
162*959826caSMatt Macy
163*959826caSMatt Macy #define list_safe_reset_next(ptr, n, member) \
164*959826caSMatt Macy (n) = list_next_entry(ptr, member)
165*959826caSMatt Macy
166*959826caSMatt Macy #define list_prev_entry(ptr, member) \
167*959826caSMatt Macy list_entry(((ptr)->member.prev), typeof(*(ptr)), member)
168*959826caSMatt Macy
169*959826caSMatt Macy #define list_for_each(p, head) \
170*959826caSMatt Macy for (p = (head)->next; p != (head); p = (p)->next)
171*959826caSMatt Macy
172*959826caSMatt Macy #define list_for_each_safe(p, n, head) \
173*959826caSMatt Macy for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next)
174*959826caSMatt Macy
175*959826caSMatt Macy #define list_for_each_entry(p, h, field) \
176*959826caSMatt Macy for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \
177*959826caSMatt Macy p = list_entry((p)->field.next, typeof(*p), field))
178*959826caSMatt Macy
179*959826caSMatt Macy #define list_for_each_entry_safe(p, n, h, field) \
180*959826caSMatt Macy for (p = list_entry((h)->next, typeof(*p), field), \
181*959826caSMatt Macy n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\
182*959826caSMatt Macy p = n, n = list_entry(n->field.next, typeof(*n), field))
183*959826caSMatt Macy
184*959826caSMatt Macy #define list_for_each_entry_from(p, h, field) \
185*959826caSMatt Macy for ( ; &(p)->field != (h); \
186*959826caSMatt Macy p = list_entry((p)->field.next, typeof(*p), field))
187*959826caSMatt Macy
188*959826caSMatt Macy #define list_for_each_entry_continue(p, h, field) \
189*959826caSMatt Macy for (p = list_next_entry((p), field); &(p)->field != (h); \
190*959826caSMatt Macy p = list_next_entry((p), field))
191*959826caSMatt Macy
192*959826caSMatt Macy #define list_for_each_entry_safe_from(pos, n, head, member) \
193*959826caSMatt Macy for (n = list_entry((pos)->member.next, typeof(*pos), member); \
194*959826caSMatt Macy &(pos)->member != (head); \
195*959826caSMatt Macy pos = n, n = list_entry(n->member.next, typeof(*n), member))
196*959826caSMatt Macy
197*959826caSMatt Macy #define list_for_each_entry_reverse(p, h, field) \
198*959826caSMatt Macy for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \
199*959826caSMatt Macy p = list_entry((p)->field.prev, typeof(*p), field))
200*959826caSMatt Macy
201*959826caSMatt Macy #define list_for_each_entry_safe_reverse(p, n, h, field) \
202*959826caSMatt Macy for (p = list_entry((h)->prev, typeof(*p), field), \
203*959826caSMatt Macy n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
204*959826caSMatt Macy p = n, n = list_entry(n->field.prev, typeof(*n), field))
205*959826caSMatt Macy
206*959826caSMatt Macy #define list_for_each_entry_continue_reverse(p, h, field) \
207*959826caSMatt Macy for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
208*959826caSMatt Macy p = list_entry((p)->field.prev, typeof(*p), field))
209*959826caSMatt Macy
210*959826caSMatt Macy #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev)
211*959826caSMatt Macy
212*959826caSMatt Macy static inline void
list_add(struct list_head * new,struct list_head * head)213*959826caSMatt Macy list_add(struct list_head *new, struct list_head *head)
214*959826caSMatt Macy {
215*959826caSMatt Macy
216*959826caSMatt Macy linux_list_add(new, head, head->next);
217*959826caSMatt Macy }
218*959826caSMatt Macy
219*959826caSMatt Macy static inline void
list_add_tail(struct list_head * new,struct list_head * head)220*959826caSMatt Macy list_add_tail(struct list_head *new, struct list_head *head)
221*959826caSMatt Macy {
222*959826caSMatt Macy
223*959826caSMatt Macy linux_list_add(new, head->prev, head);
224*959826caSMatt Macy }
225*959826caSMatt Macy
226*959826caSMatt Macy static inline void
list_move(struct list_head * list,struct list_head * head)227*959826caSMatt Macy list_move(struct list_head *list, struct list_head *head)
228*959826caSMatt Macy {
229*959826caSMatt Macy
230*959826caSMatt Macy list_del(list);
231*959826caSMatt Macy list_add(list, head);
232*959826caSMatt Macy }
233*959826caSMatt Macy
234*959826caSMatt Macy static inline void
list_move_tail(struct list_head * entry,struct list_head * head)235*959826caSMatt Macy list_move_tail(struct list_head *entry, struct list_head *head)
236*959826caSMatt Macy {
237*959826caSMatt Macy
238*959826caSMatt Macy list_del(entry);
239*959826caSMatt Macy list_add_tail(entry, head);
240*959826caSMatt Macy }
241*959826caSMatt Macy
242*959826caSMatt Macy static inline void
linux_list_splice(const struct list_head * list,struct list_head * prev,struct list_head * next)243*959826caSMatt Macy linux_list_splice(const struct list_head *list, struct list_head *prev,
244*959826caSMatt Macy struct list_head *next)
245*959826caSMatt Macy {
246*959826caSMatt Macy struct list_head *first;
247*959826caSMatt Macy struct list_head *last;
248*959826caSMatt Macy
249*959826caSMatt Macy if (list_empty(list))
250*959826caSMatt Macy return;
251*959826caSMatt Macy first = list->next;
252*959826caSMatt Macy last = list->prev;
253*959826caSMatt Macy first->prev = prev;
254*959826caSMatt Macy prev->next = first;
255*959826caSMatt Macy last->next = next;
256*959826caSMatt Macy next->prev = last;
257*959826caSMatt Macy }
258*959826caSMatt Macy
259*959826caSMatt Macy static inline void
list_splice(const struct list_head * list,struct list_head * head)260*959826caSMatt Macy list_splice(const struct list_head *list, struct list_head *head)
261*959826caSMatt Macy {
262*959826caSMatt Macy
263*959826caSMatt Macy linux_list_splice(list, head, head->next);
264*959826caSMatt Macy }
265*959826caSMatt Macy
266*959826caSMatt Macy static inline void
list_splice_tail(struct list_head * list,struct list_head * head)267*959826caSMatt Macy list_splice_tail(struct list_head *list, struct list_head *head)
268*959826caSMatt Macy {
269*959826caSMatt Macy
270*959826caSMatt Macy linux_list_splice(list, head->prev, head);
271*959826caSMatt Macy }
272*959826caSMatt Macy
273*959826caSMatt Macy static inline void
list_splice_init(struct list_head * list,struct list_head * head)274*959826caSMatt Macy list_splice_init(struct list_head *list, struct list_head *head)
275*959826caSMatt Macy {
276*959826caSMatt Macy
277*959826caSMatt Macy linux_list_splice(list, head, head->next);
278*959826caSMatt Macy INIT_LIST_HEAD(list);
279*959826caSMatt Macy }
280*959826caSMatt Macy
281*959826caSMatt Macy static inline void
list_splice_tail_init(struct list_head * list,struct list_head * head)282*959826caSMatt Macy list_splice_tail_init(struct list_head *list, struct list_head *head)
283*959826caSMatt Macy {
284*959826caSMatt Macy
285*959826caSMatt Macy linux_list_splice(list, head->prev, head);
286*959826caSMatt Macy INIT_LIST_HEAD(list);
287*959826caSMatt Macy }
288*959826caSMatt Macy
289*959826caSMatt Macy #undef LIST_HEAD
290*959826caSMatt Macy #define LIST_HEAD(name) struct list_head name = { &(name), &(name) }
291*959826caSMatt Macy
292*959826caSMatt Macy
293*959826caSMatt Macy struct hlist_head {
294*959826caSMatt Macy struct hlist_node *first;
295*959826caSMatt Macy };
296*959826caSMatt Macy
297*959826caSMatt Macy struct hlist_node {
298*959826caSMatt Macy struct hlist_node *next, **pprev;
299*959826caSMatt Macy };
300*959826caSMatt Macy
301*959826caSMatt Macy #define HLIST_HEAD_INIT { }
302*959826caSMatt Macy #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
303*959826caSMatt Macy #define INIT_HLIST_HEAD(head) (head)->first = NULL
304*959826caSMatt Macy #define INIT_HLIST_NODE(node) \
305*959826caSMatt Macy do { \
306*959826caSMatt Macy (node)->next = NULL; \
307*959826caSMatt Macy (node)->pprev = NULL; \
308*959826caSMatt Macy } while (0)
309*959826caSMatt Macy
310*959826caSMatt Macy static inline int
hlist_unhashed(const struct hlist_node * h)311*959826caSMatt Macy hlist_unhashed(const struct hlist_node *h)
312*959826caSMatt Macy {
313*959826caSMatt Macy
314*959826caSMatt Macy return !h->pprev;
315*959826caSMatt Macy }
316*959826caSMatt Macy
317*959826caSMatt Macy static inline int
hlist_empty(const struct hlist_head * h)318*959826caSMatt Macy hlist_empty(const struct hlist_head *h)
319*959826caSMatt Macy {
320*959826caSMatt Macy
321*959826caSMatt Macy return !h->first;
322*959826caSMatt Macy }
323*959826caSMatt Macy
324*959826caSMatt Macy static inline void
hlist_del(struct hlist_node * n)325*959826caSMatt Macy hlist_del(struct hlist_node *n)
326*959826caSMatt Macy {
327*959826caSMatt Macy
328*959826caSMatt Macy if (n->next)
329*959826caSMatt Macy n->next->pprev = n->pprev;
330*959826caSMatt Macy *n->pprev = n->next;
331*959826caSMatt Macy }
332*959826caSMatt Macy
333*959826caSMatt Macy static inline void
hlist_del_init(struct hlist_node * n)334*959826caSMatt Macy hlist_del_init(struct hlist_node *n)
335*959826caSMatt Macy {
336*959826caSMatt Macy
337*959826caSMatt Macy if (hlist_unhashed(n))
338*959826caSMatt Macy return;
339*959826caSMatt Macy hlist_del(n);
340*959826caSMatt Macy INIT_HLIST_NODE(n);
341*959826caSMatt Macy }
342*959826caSMatt Macy
343*959826caSMatt Macy static inline void
hlist_add_head(struct hlist_node * n,struct hlist_head * h)344*959826caSMatt Macy hlist_add_head(struct hlist_node *n, struct hlist_head *h)
345*959826caSMatt Macy {
346*959826caSMatt Macy
347*959826caSMatt Macy n->next = h->first;
348*959826caSMatt Macy if (h->first)
349*959826caSMatt Macy h->first->pprev = &n->next;
350*959826caSMatt Macy h->first = n;
351*959826caSMatt Macy n->pprev = &h->first;
352*959826caSMatt Macy }
353*959826caSMatt Macy
354*959826caSMatt Macy static inline void
hlist_add_before(struct hlist_node * n,struct hlist_node * next)355*959826caSMatt Macy hlist_add_before(struct hlist_node *n, struct hlist_node *next)
356*959826caSMatt Macy {
357*959826caSMatt Macy
358*959826caSMatt Macy n->pprev = next->pprev;
359*959826caSMatt Macy n->next = next;
360*959826caSMatt Macy next->pprev = &n->next;
361*959826caSMatt Macy *(n->pprev) = n;
362*959826caSMatt Macy }
363*959826caSMatt Macy
364*959826caSMatt Macy static inline void
hlist_add_after(struct hlist_node * n,struct hlist_node * next)365*959826caSMatt Macy hlist_add_after(struct hlist_node *n, struct hlist_node *next)
366*959826caSMatt Macy {
367*959826caSMatt Macy
368*959826caSMatt Macy next->next = n->next;
369*959826caSMatt Macy n->next = next;
370*959826caSMatt Macy next->pprev = &n->next;
371*959826caSMatt Macy if (next->next)
372*959826caSMatt Macy next->next->pprev = &next->next;
373*959826caSMatt Macy }
374*959826caSMatt Macy
375*959826caSMatt Macy static inline void
hlist_move_list(struct hlist_head * old,struct hlist_head * new)376*959826caSMatt Macy hlist_move_list(struct hlist_head *old, struct hlist_head *new)
377*959826caSMatt Macy {
378*959826caSMatt Macy
379*959826caSMatt Macy new->first = old->first;
380*959826caSMatt Macy if (new->first)
381*959826caSMatt Macy new->first->pprev = &new->first;
382*959826caSMatt Macy old->first = NULL;
383*959826caSMatt Macy }
384*959826caSMatt Macy
list_is_singular(const struct list_head * head)385*959826caSMatt Macy static inline int list_is_singular(const struct list_head *head)
386*959826caSMatt Macy {
387*959826caSMatt Macy return !list_empty(head) && (head->next == head->prev);
388*959826caSMatt Macy }
389*959826caSMatt Macy
__list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)390*959826caSMatt Macy static inline void __list_cut_position(struct list_head *list,
391*959826caSMatt Macy struct list_head *head, struct list_head *entry)
392*959826caSMatt Macy {
393*959826caSMatt Macy struct list_head *new_first = entry->next;
394*959826caSMatt Macy list->next = head->next;
395*959826caSMatt Macy list->next->prev = list;
396*959826caSMatt Macy list->prev = entry;
397*959826caSMatt Macy entry->next = list;
398*959826caSMatt Macy head->next = new_first;
399*959826caSMatt Macy new_first->prev = head;
400*959826caSMatt Macy }
401*959826caSMatt Macy
list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)402*959826caSMatt Macy static inline void list_cut_position(struct list_head *list,
403*959826caSMatt Macy struct list_head *head, struct list_head *entry)
404*959826caSMatt Macy {
405*959826caSMatt Macy if (list_empty(head))
406*959826caSMatt Macy return;
407*959826caSMatt Macy if (list_is_singular(head) &&
408*959826caSMatt Macy (head->next != entry && head != entry))
409*959826caSMatt Macy return;
410*959826caSMatt Macy if (entry == head)
411*959826caSMatt Macy INIT_LIST_HEAD(list);
412*959826caSMatt Macy else
413*959826caSMatt Macy __list_cut_position(list, head, entry);
414*959826caSMatt Macy }
415*959826caSMatt Macy
list_is_last(const struct list_head * list,const struct list_head * head)416*959826caSMatt Macy static inline int list_is_last(const struct list_head *list,
417*959826caSMatt Macy const struct list_head *head)
418*959826caSMatt Macy {
419*959826caSMatt Macy return list->next == head;
420*959826caSMatt Macy }
421*959826caSMatt Macy
422*959826caSMatt Macy #define hlist_entry(ptr, type, field) container_of(ptr, type, field)
423*959826caSMatt Macy
424*959826caSMatt Macy #define hlist_for_each(p, head) \
425*959826caSMatt Macy for (p = (head)->first; p; p = (p)->next)
426*959826caSMatt Macy
427*959826caSMatt Macy #define hlist_for_each_safe(p, n, head) \
428*959826caSMatt Macy for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n)
429*959826caSMatt Macy
430*959826caSMatt Macy #define hlist_entry_safe(ptr, type, member) \
431*959826caSMatt Macy ((ptr) ? hlist_entry(ptr, type, member) : NULL)
432*959826caSMatt Macy
433*959826caSMatt Macy #define hlist_for_each_entry(pos, head, member) \
434*959826caSMatt Macy for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
435*959826caSMatt Macy pos; \
436*959826caSMatt Macy pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
437*959826caSMatt Macy
438*959826caSMatt Macy #define hlist_for_each_entry_continue(pos, member) \
439*959826caSMatt Macy for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \
440*959826caSMatt Macy (pos); \
441*959826caSMatt Macy pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
442*959826caSMatt Macy
443*959826caSMatt Macy #define hlist_for_each_entry_from(pos, member) \
444*959826caSMatt Macy for (; (pos); \
445*959826caSMatt Macy pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
446*959826caSMatt Macy
447*959826caSMatt Macy #define hlist_for_each_entry_safe(pos, n, head, member) \
448*959826caSMatt Macy for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
449*959826caSMatt Macy (pos) && ({ n = (pos)->member.next; 1; }); \
450*959826caSMatt Macy pos = hlist_entry_safe(n, typeof(*(pos)), member))
451*959826caSMatt Macy
452*959826caSMatt Macy extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
453*959826caSMatt Macy struct list_head *a, struct list_head *b));
454*959826caSMatt Macy
455*959826caSMatt Macy #endif /* _LINUX_LIST_H_ */
456