list.h revision 255932
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5219820Sjeff * All rights reserved.
6219820Sjeff *
7219820Sjeff * Redistribution and use in source and binary forms, with or without
8219820Sjeff * modification, are permitted provided that the following conditions
9219820Sjeff * are met:
10219820Sjeff * 1. Redistributions of source code must retain the above copyright
11219820Sjeff *    notice unmodified, this list of conditions, and the following
12219820Sjeff *    disclaimer.
13219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
14219820Sjeff *    notice, this list of conditions and the following disclaimer in the
15219820Sjeff *    documentation and/or other materials provided with the distribution.
16219820Sjeff *
17219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27219820Sjeff */
28219820Sjeff#ifndef _LINUX_LIST_H_
29219820Sjeff#define _LINUX_LIST_H_
30219820Sjeff
31219820Sjeff/*
32219820Sjeff * Since LIST_HEAD conflicts with the linux definition we must include any
33219820Sjeff * FreeBSD header which requires it here so it is resolved with the correct
34219820Sjeff * definition prior to the undef.
35219820Sjeff */
36219820Sjeff#include <linux/types.h>
37219820Sjeff
38219820Sjeff#include <sys/param.h>
39219820Sjeff#include <sys/kernel.h>
40219820Sjeff#include <sys/queue.h>
41222813Sattilio#include <sys/cpuset.h>
42219820Sjeff#include <sys/lock.h>
43219820Sjeff#include <sys/mutex.h>
44219820Sjeff#include <sys/proc.h>
45219820Sjeff#include <sys/vnode.h>
46219820Sjeff#include <sys/conf.h>
47219820Sjeff#include <sys/socket.h>
48219820Sjeff#include <sys/mbuf.h>
49219820Sjeff
50219820Sjeff#include <net/bpf.h>
51219820Sjeff#include <net/if.h>
52219820Sjeff#include <net/if_types.h>
53219820Sjeff#include <net/if_media.h>
54219820Sjeff
55219820Sjeff#include <netinet/in.h>
56219820Sjeff#include <netinet/in_pcb.h>
57219820Sjeff
58219820Sjeff#include <netinet6/in6_var.h>
59219820Sjeff#include <netinet6/nd6.h>
60219820Sjeff
61219820Sjeff#include <vm/vm.h>
62219820Sjeff#include <vm/vm_object.h>
63219820Sjeff
64219820Sjeff#define	prefetch(x)
65219820Sjeff
66219820Sjeffstruct list_head {
67219820Sjeff	struct list_head *next;
68219820Sjeff	struct list_head *prev;
69219820Sjeff};
70219820Sjeff
71219820Sjeffstatic inline void
72219820SjeffINIT_LIST_HEAD(struct list_head *list)
73219820Sjeff{
74219820Sjeff
75219820Sjeff	list->next = list->prev = list;
76219820Sjeff}
77219820Sjeff
78219820Sjeffstatic inline int
79219820Sjefflist_empty(const struct list_head *head)
80219820Sjeff{
81219820Sjeff
82219820Sjeff	return (head->next == head);
83219820Sjeff}
84219820Sjeff
85219820Sjeffstatic inline void
86219820Sjefflist_del(struct list_head *entry)
87219820Sjeff{
88219820Sjeff
89219820Sjeff	entry->next->prev = entry->prev;
90219820Sjeff	entry->prev->next = entry->next;
91219820Sjeff}
92219820Sjeff
93219820Sjeffstatic inline void
94219820Sjeff_list_add(struct list_head *new, struct list_head *prev,
95219820Sjeff    struct list_head *next)
96219820Sjeff{
97219820Sjeff
98219820Sjeff	next->prev = new;
99219820Sjeff	new->next = next;
100219820Sjeff	new->prev = prev;
101219820Sjeff	prev->next = new;
102219820Sjeff}
103219820Sjeff
104219820Sjeffstatic inline void
105219820Sjefflist_del_init(struct list_head *entry)
106219820Sjeff{
107219820Sjeff
108219820Sjeff	list_del(entry);
109219820Sjeff	INIT_LIST_HEAD(entry);
110219820Sjeff}
111219820Sjeff
112219820Sjeff#define	list_entry(ptr, type, field)	container_of(ptr, type, field)
113219820Sjeff
114255932Salfred#define list_first_entry(ptr, type, member) \
115255932Salfred        list_entry((ptr)->next, type, member)
116255932Salfred
117219820Sjeff#define	list_for_each(p, head)						\
118219820Sjeff	for (p = (head)->next; p != (head); p = p->next)
119219820Sjeff
120219820Sjeff#define	list_for_each_safe(p, n, head)					\
121219820Sjeff	for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
122219820Sjeff
123219820Sjeff#define list_for_each_entry(p, h, field)				\
124219820Sjeff	for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
125219820Sjeff	    p = list_entry(p->field.next, typeof(*p), field))
126219820Sjeff
127219820Sjeff#define list_for_each_entry_safe(p, n, h, field)			\
128219820Sjeff	for (p = list_entry((h)->next, typeof(*p), field), 		\
129219820Sjeff	    n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
130219820Sjeff	    p = n, n = list_entry(n->field.next, typeof(*n), field))
131219820Sjeff
132219820Sjeff#define	list_for_each_entry_reverse(p, h, field)			\
133219820Sjeff	for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
134219820Sjeff	    p = list_entry(p->field.prev, typeof(*p), field))
135219820Sjeff
136219820Sjeff#define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
137219820Sjeff
138219820Sjeffstatic inline void
139219820Sjefflist_add(struct list_head *new, struct list_head *head)
140219820Sjeff{
141219820Sjeff
142219820Sjeff	_list_add(new, head, head->next);
143219820Sjeff}
144219820Sjeff
145219820Sjeffstatic inline void
146219820Sjefflist_add_tail(struct list_head *new, struct list_head *head)
147219820Sjeff{
148219820Sjeff
149219820Sjeff	_list_add(new, head->prev, head);
150219820Sjeff}
151219820Sjeff
152219820Sjeffstatic inline void
153219820Sjefflist_move(struct list_head *list, struct list_head *head)
154219820Sjeff{
155219820Sjeff
156219820Sjeff	list_del(list);
157219820Sjeff	list_add(list, head);
158219820Sjeff}
159219820Sjeff
160219820Sjeffstatic inline void
161219820Sjefflist_move_tail(struct list_head *entry, struct list_head *head)
162219820Sjeff{
163219820Sjeff
164219820Sjeff	list_del(entry);
165219820Sjeff	list_add_tail(entry, head);
166219820Sjeff}
167219820Sjeff
168219820Sjeffstatic inline void
169219820Sjeff_list_splice(const struct list_head *list, struct list_head *prev,
170219820Sjeff    struct list_head *next)
171219820Sjeff{
172219820Sjeff	struct list_head *first;
173219820Sjeff	struct list_head *last;
174219820Sjeff
175219820Sjeff	if (list_empty(list))
176219820Sjeff		return;
177219820Sjeff	first = list->next;
178219820Sjeff	last = list->prev;
179219820Sjeff	first->prev = prev;
180219820Sjeff	prev->next = first;
181219820Sjeff	last->next = next;
182219820Sjeff	next->prev = last;
183219820Sjeff}
184219820Sjeff
185219820Sjeffstatic inline void
186219820Sjefflist_splice(const struct list_head *list, struct list_head *head)
187219820Sjeff{
188219820Sjeff
189219820Sjeff	_list_splice(list, head, head->next);
190219820Sjeff}
191219820Sjeff
192219820Sjeffstatic inline void
193219820Sjefflist_splice_tail(struct list_head *list, struct list_head *head)
194219820Sjeff{
195219820Sjeff
196219820Sjeff	_list_splice(list, head->prev, head);
197219820Sjeff}
198219820Sjeff
199219820Sjeffstatic inline void
200219820Sjefflist_splice_init(struct list_head *list, struct list_head *head)
201219820Sjeff{
202219820Sjeff
203219820Sjeff	_list_splice(list, head, head->next);
204219820Sjeff	INIT_LIST_HEAD(list);
205219820Sjeff}
206219820Sjeff
207219820Sjeffstatic inline void
208219820Sjefflist_splice_tail_init(struct list_head *list, struct list_head *head)
209219820Sjeff{
210219820Sjeff
211219820Sjeff	_list_splice(list, head->prev, head);
212219820Sjeff	INIT_LIST_HEAD(list);
213219820Sjeff}
214219820Sjeff
215219820Sjeff#undef LIST_HEAD
216219820Sjeff#define LIST_HEAD(name)	struct list_head name = { &(name), &(name) }
217219820Sjeff
218219820Sjeff
219219820Sjeffstruct hlist_head {
220219820Sjeff	struct hlist_node *first;
221219820Sjeff};
222219820Sjeff
223219820Sjeffstruct hlist_node {
224219820Sjeff	struct hlist_node *next, **pprev;
225219820Sjeff};
226219820Sjeff
227219820Sjeff#define	HLIST_HEAD_INIT { }
228219820Sjeff#define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
229219820Sjeff#define	INIT_HLIST_HEAD(head) (head)->first = NULL
230219820Sjeff#define	INIT_HLIST_NODE(node)						\
231219820Sjeffdo {									\
232219820Sjeff	(node)->next = NULL;						\
233219820Sjeff	(node)->pprev = NULL;						\
234219820Sjeff} while (0)
235219820Sjeff
236219820Sjeffstatic inline int
237219820Sjeffhlist_unhashed(const struct hlist_node *h)
238219820Sjeff{
239219820Sjeff
240219820Sjeff	return !h->pprev;
241219820Sjeff}
242219820Sjeff
243219820Sjeffstatic inline int
244219820Sjeffhlist_empty(const struct hlist_head *h)
245219820Sjeff{
246219820Sjeff
247219820Sjeff	return !h->first;
248219820Sjeff}
249219820Sjeff
250219820Sjeffstatic inline void
251219820Sjeffhlist_del(struct hlist_node *n)
252219820Sjeff{
253219820Sjeff
254219820Sjeff        if (n->next)
255219820Sjeff                n->next->pprev = n->pprev;
256219820Sjeff        *n->pprev = n->next;
257219820Sjeff}
258219820Sjeff
259219820Sjeffstatic inline void
260219820Sjeffhlist_del_init(struct hlist_node *n)
261219820Sjeff{
262219820Sjeff
263219820Sjeff	if (hlist_unhashed(n))
264219820Sjeff		return;
265219820Sjeff	hlist_del(n);
266219820Sjeff	INIT_HLIST_NODE(n);
267219820Sjeff}
268219820Sjeff
269219820Sjeffstatic inline void
270219820Sjeffhlist_add_head(struct hlist_node *n, struct hlist_head *h)
271219820Sjeff{
272219820Sjeff
273219820Sjeff	n->next = h->first;
274219820Sjeff	if (h->first)
275219820Sjeff		h->first->pprev = &n->next;
276219820Sjeff	h->first = n;
277219820Sjeff	n->pprev = &h->first;
278219820Sjeff}
279219820Sjeff
280219820Sjeffstatic inline void
281219820Sjeffhlist_add_before(struct hlist_node *n, struct hlist_node *next)
282219820Sjeff{
283219820Sjeff
284219820Sjeff	n->pprev = next->pprev;
285219820Sjeff	n->next = next;
286219820Sjeff	next->pprev = &n->next;
287219820Sjeff	*(n->pprev) = n;
288219820Sjeff}
289219820Sjeff
290219820Sjeffstatic inline void
291219820Sjeffhlist_add_after(struct hlist_node *n, struct hlist_node *next)
292219820Sjeff{
293219820Sjeff
294219820Sjeff	next->next = n->next;
295219820Sjeff	n->next = next;
296219820Sjeff	next->pprev = &n->next;
297219820Sjeff	if (next->next)
298219820Sjeff		next->next->pprev = &next->next;
299219820Sjeff}
300219820Sjeff
301219820Sjeffstatic inline void
302219820Sjeffhlist_move_list(struct hlist_head *old, struct hlist_head *new)
303219820Sjeff{
304219820Sjeff
305219820Sjeff	new->first = old->first;
306219820Sjeff	if (new->first)
307219820Sjeff		new->first->pprev = &new->first;
308219820Sjeff	old->first = NULL;
309219820Sjeff}
310219820Sjeff
311219820Sjeff#define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
312219820Sjeff
313219820Sjeff#define	hlist_for_each(p, head)						\
314219820Sjeff	for (p = (head)->first; p; p = p->next)
315219820Sjeff
316219820Sjeff#define	hlist_for_each_safe(p, n, head)					\
317219820Sjeff	for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
318219820Sjeff
319219820Sjeff#define	hlist_for_each_entry(tp, p, head, field)			\
320219820Sjeff	for (p = (head)->first;						\
321219820Sjeff	    p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
322219820Sjeff
323219820Sjeff#define hlist_for_each_entry_continue(tp, p, field)			\
324219820Sjeff	for (p = (p)->next;						\
325219820Sjeff	    p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
326219820Sjeff
327219820Sjeff#define	hlist_for_each_entry_from(tp, p, field)				\
328219820Sjeff	for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
329219820Sjeff
330219820Sjeff#define	hlist_for_each_entry_safe(tp, p, n, head, field)		\
331219820Sjeff	for (p = (head)->first;	p ?					\
332219820Sjeff	    (n = p->next) | (tp = hlist_entry(p, typeof(*tp), field)) :	\
333219820Sjeff	    NULL; p = n)
334219820Sjeff
335219820Sjeff#endif /* _LINUX_LIST_H_ */
336