list.h revision 257176
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>
52257176Sglebius#include <net/if_var.h>
53219820Sjeff#include <net/if_types.h>
54219820Sjeff#include <net/if_media.h>
55219820Sjeff
56219820Sjeff#include <netinet/in.h>
57219820Sjeff#include <netinet/in_pcb.h>
58219820Sjeff
59219820Sjeff#include <netinet6/in6_var.h>
60219820Sjeff#include <netinet6/nd6.h>
61219820Sjeff
62219820Sjeff#include <vm/vm.h>
63219820Sjeff#include <vm/vm_object.h>
64219820Sjeff
65219820Sjeff#define	prefetch(x)
66219820Sjeff
67219820Sjeffstruct list_head {
68219820Sjeff	struct list_head *next;
69219820Sjeff	struct list_head *prev;
70219820Sjeff};
71219820Sjeff
72219820Sjeffstatic inline void
73219820SjeffINIT_LIST_HEAD(struct list_head *list)
74219820Sjeff{
75219820Sjeff
76219820Sjeff	list->next = list->prev = list;
77219820Sjeff}
78219820Sjeff
79219820Sjeffstatic inline int
80219820Sjefflist_empty(const struct list_head *head)
81219820Sjeff{
82219820Sjeff
83219820Sjeff	return (head->next == head);
84219820Sjeff}
85219820Sjeff
86219820Sjeffstatic inline void
87219820Sjefflist_del(struct list_head *entry)
88219820Sjeff{
89219820Sjeff
90219820Sjeff	entry->next->prev = entry->prev;
91219820Sjeff	entry->prev->next = entry->next;
92219820Sjeff}
93219820Sjeff
94219820Sjeffstatic inline void
95219820Sjeff_list_add(struct list_head *new, struct list_head *prev,
96219820Sjeff    struct list_head *next)
97219820Sjeff{
98219820Sjeff
99219820Sjeff	next->prev = new;
100219820Sjeff	new->next = next;
101219820Sjeff	new->prev = prev;
102219820Sjeff	prev->next = new;
103219820Sjeff}
104219820Sjeff
105219820Sjeffstatic inline void
106219820Sjefflist_del_init(struct list_head *entry)
107219820Sjeff{
108219820Sjeff
109219820Sjeff	list_del(entry);
110219820Sjeff	INIT_LIST_HEAD(entry);
111219820Sjeff}
112219820Sjeff
113219820Sjeff#define	list_entry(ptr, type, field)	container_of(ptr, type, field)
114219820Sjeff
115255932Salfred#define list_first_entry(ptr, type, member) \
116255932Salfred        list_entry((ptr)->next, type, member)
117255932Salfred
118219820Sjeff#define	list_for_each(p, head)						\
119219820Sjeff	for (p = (head)->next; p != (head); p = p->next)
120219820Sjeff
121219820Sjeff#define	list_for_each_safe(p, n, head)					\
122219820Sjeff	for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
123219820Sjeff
124219820Sjeff#define list_for_each_entry(p, h, field)				\
125219820Sjeff	for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
126219820Sjeff	    p = list_entry(p->field.next, typeof(*p), field))
127219820Sjeff
128219820Sjeff#define list_for_each_entry_safe(p, n, h, field)			\
129219820Sjeff	for (p = list_entry((h)->next, typeof(*p), field), 		\
130219820Sjeff	    n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
131219820Sjeff	    p = n, n = list_entry(n->field.next, typeof(*n), field))
132219820Sjeff
133219820Sjeff#define	list_for_each_entry_reverse(p, h, field)			\
134219820Sjeff	for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
135219820Sjeff	    p = list_entry(p->field.prev, typeof(*p), field))
136219820Sjeff
137219820Sjeff#define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
138219820Sjeff
139219820Sjeffstatic inline void
140219820Sjefflist_add(struct list_head *new, struct list_head *head)
141219820Sjeff{
142219820Sjeff
143219820Sjeff	_list_add(new, head, head->next);
144219820Sjeff}
145219820Sjeff
146219820Sjeffstatic inline void
147219820Sjefflist_add_tail(struct list_head *new, struct list_head *head)
148219820Sjeff{
149219820Sjeff
150219820Sjeff	_list_add(new, head->prev, head);
151219820Sjeff}
152219820Sjeff
153219820Sjeffstatic inline void
154219820Sjefflist_move(struct list_head *list, struct list_head *head)
155219820Sjeff{
156219820Sjeff
157219820Sjeff	list_del(list);
158219820Sjeff	list_add(list, head);
159219820Sjeff}
160219820Sjeff
161219820Sjeffstatic inline void
162219820Sjefflist_move_tail(struct list_head *entry, struct list_head *head)
163219820Sjeff{
164219820Sjeff
165219820Sjeff	list_del(entry);
166219820Sjeff	list_add_tail(entry, head);
167219820Sjeff}
168219820Sjeff
169219820Sjeffstatic inline void
170219820Sjeff_list_splice(const struct list_head *list, struct list_head *prev,
171219820Sjeff    struct list_head *next)
172219820Sjeff{
173219820Sjeff	struct list_head *first;
174219820Sjeff	struct list_head *last;
175219820Sjeff
176219820Sjeff	if (list_empty(list))
177219820Sjeff		return;
178219820Sjeff	first = list->next;
179219820Sjeff	last = list->prev;
180219820Sjeff	first->prev = prev;
181219820Sjeff	prev->next = first;
182219820Sjeff	last->next = next;
183219820Sjeff	next->prev = last;
184219820Sjeff}
185219820Sjeff
186219820Sjeffstatic inline void
187219820Sjefflist_splice(const struct list_head *list, struct list_head *head)
188219820Sjeff{
189219820Sjeff
190219820Sjeff	_list_splice(list, head, head->next);
191219820Sjeff}
192219820Sjeff
193219820Sjeffstatic inline void
194219820Sjefflist_splice_tail(struct list_head *list, struct list_head *head)
195219820Sjeff{
196219820Sjeff
197219820Sjeff	_list_splice(list, head->prev, head);
198219820Sjeff}
199219820Sjeff
200219820Sjeffstatic inline void
201219820Sjefflist_splice_init(struct list_head *list, struct list_head *head)
202219820Sjeff{
203219820Sjeff
204219820Sjeff	_list_splice(list, head, head->next);
205219820Sjeff	INIT_LIST_HEAD(list);
206219820Sjeff}
207219820Sjeff
208219820Sjeffstatic inline void
209219820Sjefflist_splice_tail_init(struct list_head *list, struct list_head *head)
210219820Sjeff{
211219820Sjeff
212219820Sjeff	_list_splice(list, head->prev, head);
213219820Sjeff	INIT_LIST_HEAD(list);
214219820Sjeff}
215219820Sjeff
216219820Sjeff#undef LIST_HEAD
217219820Sjeff#define LIST_HEAD(name)	struct list_head name = { &(name), &(name) }
218219820Sjeff
219219820Sjeff
220219820Sjeffstruct hlist_head {
221219820Sjeff	struct hlist_node *first;
222219820Sjeff};
223219820Sjeff
224219820Sjeffstruct hlist_node {
225219820Sjeff	struct hlist_node *next, **pprev;
226219820Sjeff};
227219820Sjeff
228219820Sjeff#define	HLIST_HEAD_INIT { }
229219820Sjeff#define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
230219820Sjeff#define	INIT_HLIST_HEAD(head) (head)->first = NULL
231219820Sjeff#define	INIT_HLIST_NODE(node)						\
232219820Sjeffdo {									\
233219820Sjeff	(node)->next = NULL;						\
234219820Sjeff	(node)->pprev = NULL;						\
235219820Sjeff} while (0)
236219820Sjeff
237219820Sjeffstatic inline int
238219820Sjeffhlist_unhashed(const struct hlist_node *h)
239219820Sjeff{
240219820Sjeff
241219820Sjeff	return !h->pprev;
242219820Sjeff}
243219820Sjeff
244219820Sjeffstatic inline int
245219820Sjeffhlist_empty(const struct hlist_head *h)
246219820Sjeff{
247219820Sjeff
248219820Sjeff	return !h->first;
249219820Sjeff}
250219820Sjeff
251219820Sjeffstatic inline void
252219820Sjeffhlist_del(struct hlist_node *n)
253219820Sjeff{
254219820Sjeff
255219820Sjeff        if (n->next)
256219820Sjeff                n->next->pprev = n->pprev;
257219820Sjeff        *n->pprev = n->next;
258219820Sjeff}
259219820Sjeff
260219820Sjeffstatic inline void
261219820Sjeffhlist_del_init(struct hlist_node *n)
262219820Sjeff{
263219820Sjeff
264219820Sjeff	if (hlist_unhashed(n))
265219820Sjeff		return;
266219820Sjeff	hlist_del(n);
267219820Sjeff	INIT_HLIST_NODE(n);
268219820Sjeff}
269219820Sjeff
270219820Sjeffstatic inline void
271219820Sjeffhlist_add_head(struct hlist_node *n, struct hlist_head *h)
272219820Sjeff{
273219820Sjeff
274219820Sjeff	n->next = h->first;
275219820Sjeff	if (h->first)
276219820Sjeff		h->first->pprev = &n->next;
277219820Sjeff	h->first = n;
278219820Sjeff	n->pprev = &h->first;
279219820Sjeff}
280219820Sjeff
281219820Sjeffstatic inline void
282219820Sjeffhlist_add_before(struct hlist_node *n, struct hlist_node *next)
283219820Sjeff{
284219820Sjeff
285219820Sjeff	n->pprev = next->pprev;
286219820Sjeff	n->next = next;
287219820Sjeff	next->pprev = &n->next;
288219820Sjeff	*(n->pprev) = n;
289219820Sjeff}
290219820Sjeff
291219820Sjeffstatic inline void
292219820Sjeffhlist_add_after(struct hlist_node *n, struct hlist_node *next)
293219820Sjeff{
294219820Sjeff
295219820Sjeff	next->next = n->next;
296219820Sjeff	n->next = next;
297219820Sjeff	next->pprev = &n->next;
298219820Sjeff	if (next->next)
299219820Sjeff		next->next->pprev = &next->next;
300219820Sjeff}
301219820Sjeff
302219820Sjeffstatic inline void
303219820Sjeffhlist_move_list(struct hlist_head *old, struct hlist_head *new)
304219820Sjeff{
305219820Sjeff
306219820Sjeff	new->first = old->first;
307219820Sjeff	if (new->first)
308219820Sjeff		new->first->pprev = &new->first;
309219820Sjeff	old->first = NULL;
310219820Sjeff}
311219820Sjeff
312219820Sjeff#define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
313219820Sjeff
314219820Sjeff#define	hlist_for_each(p, head)						\
315219820Sjeff	for (p = (head)->first; p; p = p->next)
316219820Sjeff
317219820Sjeff#define	hlist_for_each_safe(p, n, head)					\
318219820Sjeff	for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
319219820Sjeff
320219820Sjeff#define	hlist_for_each_entry(tp, p, head, field)			\
321219820Sjeff	for (p = (head)->first;						\
322219820Sjeff	    p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
323219820Sjeff
324219820Sjeff#define hlist_for_each_entry_continue(tp, p, field)			\
325219820Sjeff	for (p = (p)->next;						\
326219820Sjeff	    p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
327219820Sjeff
328219820Sjeff#define	hlist_for_each_entry_from(tp, p, field)				\
329219820Sjeff	for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
330219820Sjeff
331219820Sjeff#define	hlist_for_each_entry_safe(tp, p, n, head, field)		\
332219820Sjeff	for (p = (head)->first;	p ?					\
333219820Sjeff	    (n = p->next) | (tp = hlist_entry(p, typeof(*tp), field)) :	\
334219820Sjeff	    NULL; p = n)
335219820Sjeff
336219820Sjeff#endif /* _LINUX_LIST_H_ */
337