]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/ofed/include/linux/list.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / ofed / include / linux / list.h
1 /*-
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  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
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.
17  *
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.
28  */
29 #ifndef _LINUX_LIST_H_
30 #define _LINUX_LIST_H_
31
32 /*
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.
36  */
37 #include <linux/types.h>
38
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/queue.h>
42 #include <sys/cpuset.h>
43 #include <sys/jail.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/conf.h>
49 #include <sys/socket.h>
50 #include <sys/mbuf.h>
51
52 #include <net/bpf.h>
53 #include <net/if.h>
54 #include <net/if_types.h>
55 #include <net/if_media.h>
56 #include <net/vnet.h>
57
58 #include <netinet/in.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/in_var.h>
61
62 #include <netinet6/in6_var.h>
63 #include <netinet6/nd6.h>
64
65 #include <vm/vm.h>
66 #include <vm/vm_object.h>
67
68 #define prefetch(x)
69
70 struct list_head {
71         struct list_head *next;
72         struct list_head *prev;
73 };
74
75 static inline void
76 INIT_LIST_HEAD(struct list_head *list)
77 {
78
79         list->next = list->prev = list;
80 }
81  
82 static inline int
83 list_empty(const struct list_head *head)
84 {
85
86         return (head->next == head);
87 }
88
89 static inline void
90 list_del(struct list_head *entry)
91 {
92
93         entry->next->prev = entry->prev;
94         entry->prev->next = entry->next;
95 }
96
97 static inline void
98 _list_add(struct list_head *new, struct list_head *prev,
99     struct list_head *next)
100 {
101
102         next->prev = new;
103         new->next = next;
104         new->prev = prev;
105         prev->next = new;
106 }
107
108 static inline void
109 list_del_init(struct list_head *entry)
110 {       
111
112         list_del(entry);
113         INIT_LIST_HEAD(entry);
114 }
115
116 #define list_entry(ptr, type, field)    container_of(ptr, type, field)
117
118 #define list_first_entry(ptr, type, member) \
119         list_entry((ptr)->next, type, member)
120
121 #define list_for_each(p, head)                                          \
122         for (p = (head)->next; p != (head); p = p->next)
123
124 #define list_for_each_safe(p, n, head)                                  \
125         for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
126
127 #define list_for_each_entry(p, h, field)                                \
128         for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
129             p = list_entry(p->field.next, typeof(*p), field))
130
131 #define list_for_each_entry_safe(p, n, h, field)                        \
132         for (p = list_entry((h)->next, typeof(*p), field),              \
133             n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
134             p = n, n = list_entry(n->field.next, typeof(*n), field))
135
136 #define list_for_each_entry_reverse(p, h, field)                        \
137         for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
138             p = list_entry(p->field.prev, typeof(*p), field))
139
140 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
141
142 static inline void
143 list_add(struct list_head *new, struct list_head *head)
144 {
145
146         _list_add(new, head, head->next);
147 }
148
149 static inline void
150 list_add_tail(struct list_head *new, struct list_head *head)
151 {
152
153         _list_add(new, head->prev, head);
154 }
155
156 static inline void
157 list_move(struct list_head *list, struct list_head *head)
158 {
159
160         list_del(list);
161         list_add(list, head);
162 }
163
164 static inline void
165 list_move_tail(struct list_head *entry, struct list_head *head)
166 {
167
168         list_del(entry);
169         list_add_tail(entry, head);
170 }
171
172 static inline void
173 _list_splice(const struct list_head *list, struct list_head *prev,  
174     struct list_head *next)
175 {
176         struct list_head *first;
177         struct list_head *last;
178
179         if (list_empty(list))
180                 return;
181         first = list->next;
182         last = list->prev;
183         first->prev = prev;
184         prev->next = first;
185         last->next = next;
186         next->prev = last;
187 }
188
189 static inline void
190 list_splice(const struct list_head *list, struct list_head *head)
191 {
192
193         _list_splice(list, head, head->next);
194
195
196 static inline void
197 list_splice_tail(struct list_head *list, struct list_head *head)
198 {
199
200         _list_splice(list, head->prev, head);
201 }
202  
203 static inline void
204 list_splice_init(struct list_head *list, struct list_head *head)
205 {
206
207         _list_splice(list, head, head->next);
208         INIT_LIST_HEAD(list);   
209 }
210  
211 static inline void
212 list_splice_tail_init(struct list_head *list, struct list_head *head)
213 {
214
215         _list_splice(list, head->prev, head);
216         INIT_LIST_HEAD(list);
217 }
218
219 #undef LIST_HEAD
220 #define LIST_HEAD(name) struct list_head name = { &(name), &(name) }
221
222
223 struct hlist_head {
224         struct hlist_node *first;
225 };
226
227 struct hlist_node {
228         struct hlist_node *next, **pprev;
229 };
230
231 #define HLIST_HEAD_INIT { }
232 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
233 #define INIT_HLIST_HEAD(head) (head)->first = NULL
234 #define INIT_HLIST_NODE(node)                                           \
235 do {                                                                    \
236         (node)->next = NULL;                                            \
237         (node)->pprev = NULL;                                           \
238 } while (0)
239
240 static inline int
241 hlist_unhashed(const struct hlist_node *h)
242 {
243
244         return !h->pprev;
245 }
246
247 static inline int
248 hlist_empty(const struct hlist_head *h)
249 {
250
251         return !h->first;
252 }
253
254 static inline void
255 hlist_del(struct hlist_node *n)
256 {
257
258         if (n->next)
259                 n->next->pprev = n->pprev;
260         *n->pprev = n->next;
261 }
262
263 static inline void
264 hlist_del_init(struct hlist_node *n)
265 {
266
267         if (hlist_unhashed(n))
268                 return;
269         hlist_del(n);
270         INIT_HLIST_NODE(n);
271 }
272
273 static inline void
274 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
275 {
276
277         n->next = h->first;
278         if (h->first)
279                 h->first->pprev = &n->next;
280         h->first = n;
281         n->pprev = &h->first;
282 }
283
284 static inline void
285 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
286 {
287
288         n->pprev = next->pprev;
289         n->next = next;
290         next->pprev = &n->next;
291         *(n->pprev) = n;
292 }
293  
294 static inline void
295 hlist_add_after(struct hlist_node *n, struct hlist_node *next)
296 {
297
298         next->next = n->next;
299         n->next = next;
300         next->pprev = &n->next;
301         if (next->next)
302                 next->next->pprev = &next->next;
303 }
304  
305 static inline void
306 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
307 {
308
309         new->first = old->first;
310         if (new->first)
311                 new->first->pprev = &new->first;
312         old->first = NULL;
313 }
314
315 /**
316  * list_is_singular - tests whether a list has just one entry.
317  * @head: the list to test.
318  */
319 static inline int list_is_singular(const struct list_head *head)
320 {
321         return !list_empty(head) && (head->next == head->prev);
322 }
323
324 static inline void __list_cut_position(struct list_head *list,
325                 struct list_head *head, struct list_head *entry)
326 {
327         struct list_head *new_first = entry->next;
328         list->next = head->next;
329         list->next->prev = list;
330         list->prev = entry;
331         entry->next = list;
332         head->next = new_first;
333         new_first->prev = head;
334 }
335
336 /**
337  * list_cut_position - cut a list into two
338  * @list: a new list to add all removed entries
339  * @head: a list with entries
340  * @entry: an entry within head, could be the head itself
341  *      and if so we won't cut the list
342  *
343  * This helper moves the initial part of @head, up to and
344  * including @entry, from @head to @list. You should
345  * pass on @entry an element you know is on @head. @list
346  * should be an empty list or a list you do not care about
347  * losing its data.
348  *
349  */
350 static inline void list_cut_position(struct list_head *list,
351                 struct list_head *head, struct list_head *entry)
352 {
353         if (list_empty(head))
354                 return;
355         if (list_is_singular(head) &&
356                 (head->next != entry && head != entry))
357                 return;
358         if (entry == head)
359                 INIT_LIST_HEAD(list);
360         else
361                 __list_cut_position(list, head, entry);
362 }
363
364 /**
365  *  list_is_last - tests whether @list is the last entry in list @head
366  *   @list: the entry to test
367  *    @head: the head of the list
368  */
369 static inline int list_is_last(const struct list_head *list,
370                                 const struct list_head *head)
371 {
372         return list->next == head;
373 }
374  
375 #define hlist_entry(ptr, type, field)   container_of(ptr, type, field)
376
377 #define hlist_for_each(p, head)                                         \
378         for (p = (head)->first; p; p = p->next)
379
380 #define hlist_for_each_safe(p, n, head)                                 \
381         for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
382
383 #define hlist_for_each_entry(tp, p, head, field)                        \
384         for (p = (head)->first;                                         \
385             p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
386  
387 #define hlist_for_each_entry_continue(tp, p, field)                     \
388         for (p = (p)->next;                                             \
389             p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
390
391 #define hlist_for_each_entry_from(tp, p, field)                         \
392         for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
393
394 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
395         for (pos = (head)->first;                                        \
396              (pos) != 0 && ({ n = (pos)->next; \
397                  tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \
398              pos = (n))
399
400 #endif /* _LINUX_LIST_H_ */