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