Home | History | Annotate | Line # | Download | only in linux
list.h revision 1.14
      1 /*	$NetBSD: list.h,v 1.14 2018/08/27 07:21:59 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Notes on porting:
     34  *
     35  * - LIST_HEAD(x) means a declaration `struct list_head x =
     36  *   LIST_HEAD_INIT(x)' in Linux, but something else in NetBSD.
     37  *   Replace by the expansion.
     38  *
     39  * - The `_rcu' routines here are not actually pserialize(9)-safe.
     40  *   They need dependent read memory barriers added.  Please fix this
     41  *   if you need to use them with pserialize(9).
     42  */
     43 
     44 #ifndef _LINUX_LIST_H_
     45 #define _LINUX_LIST_H_
     46 
     47 #include <sys/null.h>
     48 #include <sys/pslist.h>
     49 #include <sys/queue.h>
     50 
     51 #include <linux/kernel.h>
     52 
     53 /*
     54  * Doubly-linked lists.
     55  */
     56 
     57 struct list_head {
     58 	struct list_head *prev;
     59 	struct list_head *next;
     60 };
     61 
     62 #define	LIST_HEAD_INIT(name)	{ .prev = &(name), .next = &(name) }
     63 
     64 static inline void
     65 INIT_LIST_HEAD(struct list_head *head)
     66 {
     67 	head->prev = head;
     68 	head->next = head;
     69 }
     70 
     71 static inline struct list_head *
     72 list_first(const struct list_head *head)
     73 {
     74 	return head->next;
     75 }
     76 
     77 static inline struct list_head *
     78 list_last(const struct list_head *head)
     79 {
     80 	return head->prev;
     81 }
     82 
     83 static inline struct list_head *
     84 list_next(const struct list_head *node)
     85 {
     86 	return node->next;
     87 }
     88 
     89 static inline struct list_head *
     90 list_prev(const struct list_head *node)
     91 {
     92 	return node->prev;
     93 }
     94 
     95 static inline int
     96 list_empty(const struct list_head *head)
     97 {
     98 	return (head->next == head);
     99 }
    100 
    101 static inline int
    102 list_is_singular(const struct list_head *head)
    103 {
    104 
    105 	if (list_empty(head))
    106 		return false;
    107 	if (head->next != head->prev)
    108 		return false;
    109 	return true;
    110 }
    111 
    112 static inline void
    113 __list_add_between(struct list_head *prev, struct list_head *node,
    114     struct list_head *next)
    115 {
    116 	prev->next = node;
    117 	node->prev = prev;
    118 	node->next = next;
    119 	next->prev = node;
    120 }
    121 
    122 static inline void
    123 list_add(struct list_head *node, struct list_head *head)
    124 {
    125 	__list_add_between(head, node, head->next);
    126 }
    127 
    128 static inline void
    129 list_add_tail(struct list_head *node, struct list_head *head)
    130 {
    131 	__list_add_between(head->prev, node, head);
    132 }
    133 
    134 static inline void
    135 list_del(struct list_head *entry)
    136 {
    137 	entry->prev->next = entry->next;
    138 	entry->next->prev = entry->prev;
    139 }
    140 
    141 static inline void
    142 __list_splice_between(struct list_head *prev, const struct list_head *list,
    143     struct list_head *next)
    144 {
    145 	struct list_head *first = list->next;
    146 	struct list_head *last = list->prev;
    147 
    148 	first->prev = prev;
    149 	prev->next = first;
    150 
    151 	last->next = next;
    152 	next->prev = last;
    153 }
    154 
    155 static inline void
    156 list_splice(const struct list_head *list, struct list_head *head)
    157 {
    158 	if (!list_empty(list))
    159 		__list_splice_between(head, list, head->next);
    160 }
    161 
    162 static inline void
    163 list_splice_tail(const struct list_head *list, struct list_head *head)
    164 {
    165 	if (!list_empty(list))
    166 		__list_splice_between(head->prev, list, head);
    167 }
    168 
    169 static inline void
    170 list_move(struct list_head *node, struct list_head *head)
    171 {
    172 	list_del(node);
    173 	list_add(node, head);
    174 }
    175 
    176 static inline void
    177 list_move_tail(struct list_head *node, struct list_head *head)
    178 {
    179 	list_del(node);
    180 	list_add_tail(node, head);
    181 }
    182 
    183 static inline void
    184 list_replace(struct list_head *old, struct list_head *new)
    185 {
    186 	new->prev = old->prev;
    187 	old->prev->next = new;
    188 	new->next = old->next;
    189 	old->next->prev = new;
    190 }
    191 
    192 static inline void
    193 list_replace_init(struct list_head *old, struct list_head *new)
    194 {
    195 	list_replace(old, new);
    196 	INIT_LIST_HEAD(old);
    197 }
    198 
    199 static inline void
    200 list_del_init(struct list_head *node)
    201 {
    202 	list_del(node);
    203 	INIT_LIST_HEAD(node);
    204 }
    205 
    206 #define	list_entry(PTR, TYPE, FIELD)	container_of(PTR, TYPE, FIELD)
    207 #define	list_first_entry(PTR, TYPE, FIELD)				\
    208 	list_entry(list_first((PTR)), TYPE, FIELD)
    209 #define	list_first_entry_or_null(PTR, TYPE, FIELD)			\
    210 	(list_empty((PTR)) ? NULL : list_entry(list_first((PTR)), TYPE, FIELD))
    211 #define	list_last_entry(PTR, TYPE, FIELD)				\
    212 	list_entry(list_last((PTR)), TYPE, FIELD)
    213 #define	list_next_entry(ENTRY, FIELD)					\
    214 	list_entry(list_next(&(ENTRY)->FIELD), typeof(*(ENTRY)), FIELD)
    215 #define	list_prev_entry(ENTRY, FIELD)					\
    216 	list_entry(list_prev(&(ENTRY)->FIELD), typeof(*(ENTRY)), FIELD)
    217 
    218 #define	list_for_each(VAR, HEAD)					\
    219 	for ((VAR) = list_first((HEAD));				\
    220 		(VAR) != (HEAD);					\
    221 		(VAR) = list_next((VAR)))
    222 
    223 #define	list_for_each_safe(VAR, NEXT, HEAD)				\
    224 	for ((VAR) = list_first((HEAD));				\
    225 		((VAR) != (HEAD)) && ((NEXT) = list_next((VAR)), 1);	\
    226 		(VAR) = (NEXT))
    227 
    228 #define	list_for_each_entry(VAR, HEAD, FIELD)				\
    229 	for ((VAR) = list_entry(list_first((HEAD)), typeof(*(VAR)), FIELD); \
    230 		&(VAR)->FIELD != (HEAD);				\
    231 		(VAR) = list_entry(list_next(&(VAR)->FIELD), typeof(*(VAR)), \
    232 		    FIELD))
    233 
    234 #define	list_for_each_entry_reverse(VAR, HEAD, FIELD)			\
    235 	for ((VAR) = list_entry(list_last((HEAD)), typeof(*(VAR)), FIELD); \
    236 		&(VAR)->FIELD != (HEAD);				\
    237 		(VAR) = list_entry(list_prev(&(VAR)->FIELD), typeof(*(VAR)), \
    238 		    FIELD))
    239 
    240 #define	list_for_each_entry_safe(VAR, NEXT, HEAD, FIELD)		\
    241 	for ((VAR) = list_entry(list_first((HEAD)), typeof(*(VAR)), FIELD); \
    242 		(&(VAR)->FIELD != (HEAD)) &&				\
    243 		    ((NEXT) = list_entry(list_next(&(VAR)->FIELD),	\
    244 			typeof(*(VAR)), FIELD), 1);			\
    245 		(VAR) = (NEXT))
    246 
    247 #define	list_for_each_entry_continue(VAR, HEAD, FIELD)			\
    248 	for ((VAR) = list_next_entry((VAR), FIELD);			\
    249 		&(VAR)->FIELD != (HEAD);				\
    250 		(VAR) = list_next_entry((VAR), FIELD))
    251 
    252 #define	list_for_each_entry_continue_reverse(VAR, HEAD, FIELD)		\
    253 	for ((VAR) = list_prev_entry((VAR), FIELD);			\
    254 		&(VAR)->FIELD != (HEAD);				\
    255 		(VAR) = list_prev_entry((VAR), FIELD))
    256 
    257 #define	list_for_each_entry_safe_from(VAR, NEXT, HEAD, FIELD)		\
    258 	for (;								\
    259 		(&(VAR)->FIELD != (HEAD)) &&				\
    260 		    ((NEXT) = list_next_entry((VAR), FIELD));		\
    261 		(VAR) = (NEXT))
    262 
    263 /*
    264  * `H'ead-only/`H'ash-table doubly-linked lists.
    265  */
    266 
    267 #define	hlist_head	pslist_head
    268 #define	hlist_node	pslist_entry
    269 
    270 #define	HLIST_HEAD_INIT	PSLIST_INITIALIZER
    271 #define	INIT_HLIST_HEAD	PSLIST_INIT
    272 #define	hlist_empty(h)	(pslist_writer_first(h) == NULL)
    273 #define	hlist_first	pslist_writer_first
    274 #define	hlist_next	pslist_writer_next
    275 #define	hlist_add_head(n, h)	pslist_writer_insert_head(h, n)
    276 
    277 static inline void
    278 hlist_del(struct hlist_node *node)
    279 {
    280 
    281 	pslist_writer_remove(node);
    282 	/* XXX abstraction violation */
    283 	node->ple_prevp = _PSLIST_POISON;
    284 }
    285 
    286 static inline void
    287 hlist_del_init(struct hlist_node *node)
    288 {
    289 
    290 	/* XXX abstraction violation */
    291 	if (node->ple_prevp != NULL)
    292 		pslist_writer_remove(node);
    293 }
    294 
    295 #define	hlist_entry(PTR, TYPE, FIELD)	container_of(PTR, TYPE, FIELD)
    296 #define	hlist_for_each(VAR, HEAD)					      \
    297 	for ((VAR) = hlist_first(HEAD); (VAR) != NULL; (VAR) = hlist_next(VAR))
    298 #define	hlist_for_each_safe(VAR, NEXT, HEAD)				      \
    299 	for ((VAR) = hlist_first(HEAD),					      \
    300 		    (NEXT) = ((VAR) == NULL ? NULL : hlist_next(HEAD));	      \
    301 		(VAR) != NULL;						      \
    302 		(VAR) = (NEXT))
    303 #define	hlist_for_each_entry(VAR, HEAD, FIELD)				      \
    304 	for ((VAR) = (hlist_first(HEAD) == NULL ? NULL :		      \
    305 			hlist_entry(hlist_first(HEAD), typeof(*(VAR)),	      \
    306 			    FIELD));					      \
    307 		(VAR) != NULL;						      \
    308 		(VAR) = (hlist_next(&(VAR)->FIELD) == NULL ? NULL :	      \
    309 			hlist_entry(hlist_next(&(VAR)->FIELD), typeof(*(VAR)),\
    310 			    FIELD)))
    311 
    312 #define	hlist_for_each_entry_safe(VAR, NEXT, HEAD, FIELD)		      \
    313 	for ((VAR) = (hlist_first(HEAD) == NULL ? NULL :		      \
    314 			hlist_entry(hlist_first(HEAD), typeof(*(VAR)),	      \
    315 			    FIELD)),					      \
    316 		    (NEXT) = ((VAR) == NULL ? NULL :			      \
    317 			hlist_next(&(VAR)->FIELD));			      \
    318 		(VAR) != NULL;						      \
    319 	        (VAR) = ((NEXT) == NULL ? NULL :			      \
    320 			    hlist_entry((NEXT), typeof(*(VAR)), FIELD)))
    321 
    322 #define	hlist_add_behind_rcu(n, p)	pslist_writer_insert_after(p, n)
    323 #define	hlist_add_head_rcu(n, h)	pslist_writer_insert_head(h, n)
    324 #define	hlist_del_init_rcu		hlist_del_init /* no special needs */
    325 
    326 #define	hlist_first_rcu		pslist_reader_first
    327 #define	hlist_next_rcu		pslist_reader_next
    328 
    329 #define	hlist_for_each_entry_rcu(VAR, HEAD, FIELD)			      \
    330 	for ((VAR) = (hlist_first_rcu(HEAD) == NULL ? NULL :		      \
    331 			hlist_entry(hlist_first_rcu(HEAD), typeof(*(VAR)),    \
    332 			    FIELD));					      \
    333 		(VAR) != NULL;						      \
    334 		(VAR) = (hlist_next_rcu(&(VAR)->FIELD) == NULL ? NULL :	      \
    335 			hlist_entry(hlist_next_rcu(&(VAR)->FIELD),	      \
    336 			    typeof(*(VAR)), FIELD)))
    337 
    338 #endif  /* _LINUX_LIST_H_ */
    339