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