Merge branch 'vendor/TNFTP'
[dragonfly.git] / sys / dev / drm / 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  * Copyright (c) 2015 François Tigeot
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef _LINUX_LIST_H_
31 #define _LINUX_LIST_H_
32
33 /*
34  * Since LIST_HEAD conflicts with the linux definition we must include any
35  * FreeBSD header which requires it here so it is resolved with the correct
36  * definition prior to the undef.
37  */
38 #include <linux/types.h>
39
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/jail.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/proc.h>
47 #include <sys/conf.h>
48 #include <sys/socket.h>
49 #include <sys/mbuf.h>
50
51 #include <net/bpf.h>
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_types.h>
55 #include <net/if_media.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/in_var.h>
60
61 #include <netinet6/in6_var.h>
62 #include <netinet6/nd6.h>
63
64 #include <vm/vm.h>
65 #include <vm/vm_object.h>
66
67 #define prefetch(x)
68
69 struct list_head {
70         struct list_head *next;
71         struct list_head *prev;
72 };
73
74 static inline void
75 INIT_LIST_HEAD(struct list_head *list)
76 {
77
78         list->next = list->prev = list;
79 }
80  
81 static inline struct list_head *
82 list_first(const struct list_head *head)
83 {
84         return head->next;
85 }
86
87 static inline struct list_head *
88 list_last(const struct list_head *head)
89 {
90         return head->prev;
91 }
92
93 static inline int
94 list_empty(const struct list_head *head)
95 {
96
97         return (head->next == head);
98 }
99
100 static inline void
101 list_del(struct list_head *entry)
102 {
103
104         entry->next->prev = entry->prev;
105         entry->prev->next = entry->next;
106 }
107
108 static inline void list_replace(struct list_head *old,
109                                 struct list_head *new)
110 {
111         new->next = old->next;
112         new->next->prev = new;
113         new->prev = old->prev;
114         new->prev->next = new;
115 }
116
117 static inline void
118 _list_add(struct list_head *new, struct list_head *prev,
119     struct list_head *next)
120 {
121
122         next->prev = new;
123         new->next = next;
124         new->prev = prev;
125         prev->next = new;
126 }
127
128 static inline void
129 list_del_init(struct list_head *entry)
130 {       
131
132         list_del(entry);
133         INIT_LIST_HEAD(entry);
134 }
135
136 #define list_entry(ptr, type, field)    container_of(ptr, type, field)
137
138 #define list_first_entry(ptr, type, member) \
139         list_entry((ptr)->next, type, member)
140
141 #define list_first_entry_or_null(ptr, type, member) \
142         (list_empty(ptr) ? NULL: list_first_entry(ptr, type, member))
143
144 #define list_last_entry(ptr, type, field) \
145         list_entry(list_last((ptr)), type, field)
146
147 #define list_next_entry(ptr, member)                                    \
148         list_entry(((ptr)->member.next), typeof(*(ptr)), member)
149
150 #define list_for_each(p, head)                                          \
151         for (p = (head)->next; p != (head); p = p->next)
152
153 #define list_for_each_safe(p, n, head)                                  \
154         for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
155
156 #define list_for_each_entry(p, h, field)                                \
157         for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
158             p = list_entry(p->field.next, typeof(*p), field))
159
160 #define list_for_each_entry_safe(p, n, h, field)                        \
161         for (p = list_entry((h)->next, typeof(*p), field),              \
162             n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
163             p = n, n = list_entry(n->field.next, typeof(*n), field))
164
165 #define list_for_each_entry_continue(p, h, field)                       \
166         for (p = list_next_entry((p), field); &p->field != (h);         \
167             p = list_next_entry((p), field))
168
169 #define list_for_each_entry_safe_from(pos, n, head, member)                     \
170         for (n = list_entry(pos->member.next, typeof(*pos), member);            \
171              &pos->member != (head);                                            \
172              pos = n, n = list_entry(n->member.next, typeof(*n), member))
173
174 #define list_for_each_entry_reverse(p, h, field)                        \
175         for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
176             p = list_entry(p->field.prev, typeof(*p), field))
177
178 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
179
180 static inline void
181 list_add(struct list_head *new, struct list_head *head)
182 {
183
184         _list_add(new, head, head->next);
185 }
186
187 static inline void
188 list_add_tail(struct list_head *new, struct list_head *head)
189 {
190
191         _list_add(new, head->prev, head);
192 }
193
194 static inline void
195 list_move(struct list_head *list, struct list_head *head)
196 {
197
198         list_del(list);
199         list_add(list, head);
200 }
201
202 static inline void
203 list_move_tail(struct list_head *entry, struct list_head *head)
204 {
205
206         list_del(entry);
207         list_add_tail(entry, head);
208 }
209
210 static inline void
211 _list_splice(const struct list_head *list, struct list_head *prev,  
212     struct list_head *next)
213 {
214         struct list_head *first;
215         struct list_head *last;
216
217         if (list_empty(list))
218                 return;
219         first = list->next;
220         last = list->prev;
221         first->prev = prev;
222         prev->next = first;
223         last->next = next;
224         next->prev = last;
225 }
226
227 static inline void
228 list_splice(const struct list_head *list, struct list_head *head)
229 {
230
231         _list_splice(list, head, head->next);
232
233
234 static inline void
235 list_splice_tail(struct list_head *list, struct list_head *head)
236 {
237
238         _list_splice(list, head->prev, head);
239 }
240  
241 static inline void
242 list_splice_init(struct list_head *list, struct list_head *head)
243 {
244
245         _list_splice(list, head, head->next);
246         INIT_LIST_HEAD(list);   
247 }
248  
249 static inline void
250 list_splice_tail_init(struct list_head *list, struct list_head *head)
251 {
252
253         _list_splice(list, head->prev, head);
254         INIT_LIST_HEAD(list);
255 }
256
257 #define LINUX_LIST_HEAD(name)   struct list_head name = { &(name), &(name) }
258
259
260 struct hlist_head {
261         struct hlist_node *first;
262 };
263
264 struct hlist_node {
265         struct hlist_node *next, **pprev;
266 };
267
268 #define HLIST_HEAD_INIT { }
269 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
270 #define INIT_HLIST_HEAD(head) (head)->first = NULL
271 #define INIT_HLIST_NODE(node)                                           \
272 do {                                                                    \
273         (node)->next = NULL;                                            \
274         (node)->pprev = NULL;                                           \
275 } while (0)
276
277 static inline int
278 hlist_unhashed(const struct hlist_node *h)
279 {
280
281         return !h->pprev;
282 }
283
284 static inline int
285 hlist_empty(const struct hlist_head *h)
286 {
287
288         return !h->first;
289 }
290
291 static inline void
292 hlist_del(struct hlist_node *n)
293 {
294
295         if (n->next)
296                 n->next->pprev = n->pprev;
297         *n->pprev = n->next;
298 }
299
300 static inline void
301 hlist_del_init(struct hlist_node *n)
302 {
303
304         if (hlist_unhashed(n))
305                 return;
306         hlist_del(n);
307         INIT_HLIST_NODE(n);
308 }
309
310 static inline void
311 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
312 {
313
314         n->next = h->first;
315         if (h->first)
316                 h->first->pprev = &n->next;
317         h->first = n;
318         n->pprev = &h->first;
319 }
320
321 static inline void
322 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
323 {
324
325         n->pprev = next->pprev;
326         n->next = next;
327         next->pprev = &n->next;
328         *(n->pprev) = n;
329 }
330  
331 static inline void
332 hlist_add_after(struct hlist_node *n, struct hlist_node *next)
333 {
334
335         next->next = n->next;
336         n->next = next;
337         next->pprev = &n->next;
338         if (next->next)
339                 next->next->pprev = &next->next;
340 }
341  
342 static inline void
343 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
344 {
345
346         new->first = old->first;
347         if (new->first)
348                 new->first->pprev = &new->first;
349         old->first = NULL;
350 }
351
352 /**
353  * list_is_singular - tests whether a list has just one entry.
354  * @head: the list to test.
355  */
356 static inline int list_is_singular(const struct list_head *head)
357 {
358         return !list_empty(head) && (head->next == head->prev);
359 }
360
361 static inline void __list_cut_position(struct list_head *list,
362                 struct list_head *head, struct list_head *entry)
363 {
364         struct list_head *new_first = entry->next;
365         list->next = head->next;
366         list->next->prev = list;
367         list->prev = entry;
368         entry->next = list;
369         head->next = new_first;
370         new_first->prev = head;
371 }
372
373 /**
374  * list_cut_position - cut a list into two
375  * @list: a new list to add all removed entries
376  * @head: a list with entries
377  * @entry: an entry within head, could be the head itself
378  *      and if so we won't cut the list
379  *
380  * This helper moves the initial part of @head, up to and
381  * including @entry, from @head to @list. You should
382  * pass on @entry an element you know is on @head. @list
383  * should be an empty list or a list you do not care about
384  * losing its data.
385  *
386  */
387 static inline void list_cut_position(struct list_head *list,
388                 struct list_head *head, struct list_head *entry)
389 {
390         if (list_empty(head))
391                 return;
392         if (list_is_singular(head) &&
393                 (head->next != entry && head != entry))
394                 return;
395         if (entry == head)
396                 INIT_LIST_HEAD(list);
397         else
398                 __list_cut_position(list, head, entry);
399 }
400
401 /**
402  *  list_is_last - tests whether @list is the last entry in list @head
403  *   @list: the entry to test
404  *    @head: the head of the list
405  */
406 static inline int list_is_last(const struct list_head *list,
407                                 const struct list_head *head)
408 {
409         return list->next == head;
410 }
411  
412 #define hlist_entry(ptr, type, field)   container_of(ptr, type, field)
413
414 #define hlist_for_each(p, head)                                         \
415         for (p = (head)->first; p; p = p->next)
416
417 #define hlist_for_each_safe(p, n, head)                                 \
418         for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
419
420 #define hlist_entry_safe(ptr, type, member) \
421         (ptr) ? hlist_entry(ptr, type, member) : NULL
422
423 #define hlist_for_each_entry(pos, head, member)                         \
424         for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
425              pos;                                                       \
426              pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
427
428 #define hlist_for_each_entry_continue(tp, p, field)                     \
429         for (p = (p)->next;                                             \
430             p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
431
432 #define hlist_for_each_entry_from(tp, p, field)                         \
433         for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
434
435 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
436         for (pos = (head)->first;                                        \
437              (pos) != 0 && ({ n = (pos)->next; \
438                  tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \
439              pos = (n))
440
441 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
442     struct list_head *a, struct list_head *b));
443
444 #define hlist_add_head_rcu(n, h)        hlist_add_head(n, h)
445
446 #define hlist_del_init_rcu(n)           hlist_del_init(n)
447
448 #endif /* _LINUX_LIST_H_ */