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