Home | History | Annotate | Line # | Download | only in linux
list.h revision 1.21
      1 /*	$NetBSD: list.h,v 1.21 2021/12/19 01:44:41 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 #include <linux/types.h>
     53 
     54 /*
     55  * Doubly-linked lists.  Type defined in <linux/types.h>.
     56  */
     57 
     58 #define	LIST_HEAD_INIT(name)	{ .prev = &(name), .next = &(name) }
     59 
     60 #define	LINUX_LIST_HEAD(name)	struct list_head name = LIST_HEAD_INIT(name)
     61 
     62 static inline void
     63 INIT_LIST_HEAD(struct list_head *head)
     64 {
     65 	head->prev = head;
     66 	head->next = head;
     67 }
     68 
     69 static inline struct list_head *
     70 list_first(const struct list_head *head)
     71 {
     72 	return head->next;
     73 }
     74 
     75 static inline struct list_head *
     76 list_last(const struct list_head *head)
     77 {
     78 	return head->prev;
     79 }
     80 
     81 static inline struct list_head *
     82 list_next(const struct list_head *node)
     83 {
     84 	return node->next;
     85 }
     86 
     87 static inline struct list_head *
     88 list_prev(const struct list_head *node)
     89 {
     90 	return node->prev;
     91 }
     92 
     93 static inline int
     94 list_empty(const struct list_head *head)
     95 {
     96 	return (head->next == head);
     97 }
     98 
     99 static inline int
    100 list_is_singular(const struct list_head *head)
    101 {
    102 
    103 	if (list_empty(head))
    104 		return false;
    105 	if (head->next != head->prev)
    106 		return false;
    107 	return true;
    108 }
    109 
    110 static inline void
    111 __list_add_between(struct list_head *prev, struct list_head *node,
    112     struct list_head *next)
    113 {
    114 	prev->next = node;
    115 	node->prev = prev;
    116 	node->next = next;
    117 	next->prev = node;
    118 }
    119 
    120 static inline void
    121 list_add(struct list_head *node, struct list_head *head)
    122 {
    123 	__list_add_between(head, node, head->next);
    124 }
    125 
    126 static inline void
    127 list_add_tail(struct list_head *node, struct list_head *head)
    128 {
    129 	__list_add_between(head->prev, node, head);
    130 }
    131 
    132 static inline void
    133 __list_del_entry(struct list_head *entry)
    134 {
    135 	entry->prev->next = entry->next;
    136 	entry->next->prev = entry->prev;
    137 }
    138 
    139 static inline void
    140 list_del(struct list_head *entry)
    141 {
    142 	__list_del_entry(entry);
    143 	entry->next = (void *)(uintptr_t)1;
    144 	entry->prev = (void *)(uintptr_t)2;
    145 }
    146 
    147 static inline void
    148 __list_splice_between(struct list_head *prev, const struct list_head *list,
    149     struct list_head *next)
    150 {
    151 	struct list_head *first = list->next;
    152 	struct list_head *last = list->prev;
    153 
    154 	first->prev = prev;
    155 	prev->next = first;
    156 
    157 	last->next = next;
    158 	next->prev = last;
    159 }
    160 
    161 static inline void
    162 list_splice(const struct list_head *list, struct list_head *head)
    163 {
    164 	if (!list_empty(list))
    165 		__list_splice_between(head, list, head->next);
    166 }
    167 
    168 static inline void
    169 list_splice_init(struct list_head *list, struct list_head *head)
    170 {
    171 	if (!list_empty(list)) {
    172 		__list_splice_between(head, list, head->next);
    173 		INIT_LIST_HEAD(list);
    174 	}
    175 }
    176 
    177 static inline void
    178 list_splice_tail(const struct list_head *list, struct list_head *head)
    179 {
    180 	if (!list_empty(list))
    181 		__list_splice_between(head->prev, list, head);
    182 }
    183 
    184 static inline void
    185 list_splice_tail_init(struct list_head *list, struct list_head *head)
    186 {
    187 	if (!list_empty(list)) {
    188 		__list_splice_between(head->prev, list, head);
    189 		INIT_LIST_HEAD(list);
    190 	}
    191 }
    192 
    193 static inline void
    194 list_move(struct list_head *node, struct list_head *head)
    195 {
    196 	list_del(node);
    197 	list_add(node, head);
    198 }
    199 
    200 static inline void
    201 list_move_tail(struct list_head *node, struct list_head *head)
    202 {
    203 	list_del(node);
    204 	list_add_tail(node, head);
    205 }
    206 
    207 static inline void
    208 list_replace(struct list_head *old, struct list_head *new)
    209 {
    210 	new->prev = old->prev;
    211 	old->prev->next = new;
    212 	new->next = old->next;
    213 	old->next->prev = new;
    214 }
    215 
    216 static inline void
    217 list_replace_init(struct list_head *old, struct list_head *new)
    218 {
    219 	list_replace(old, new);
    220 	INIT_LIST_HEAD(old);
    221 }
    222 
    223 static inline void
    224 list_del_init(struct list_head *node)
    225 {
    226 	list_del(node);
    227 	INIT_LIST_HEAD(node);
    228 }
    229 
    230 #define	list_entry(PTR, TYPE, FIELD)	container_of(PTR, TYPE, FIELD)
    231 #define	list_first_entry(PTR, TYPE, FIELD)				\
    232 	list_entry(list_first((PTR)), TYPE, FIELD)
    233 #define	list_first_entry_or_null(PTR, TYPE, FIELD)			\
    234 	(list_empty((PTR)) ? NULL : list_entry(list_first((PTR)), TYPE, FIELD))
    235 #define	list_last_entry(PTR, TYPE, FIELD)				\
    236 	list_entry(list_last((PTR)), TYPE, FIELD)
    237 #define	list_next_entry(ENTRY, FIELD)					\
    238 	list_entry(list_next(&(ENTRY)->FIELD), typeof(*(ENTRY)), FIELD)
    239 #define	list_prev_entry(ENTRY, FIELD)					\
    240 	list_entry(list_prev(&(ENTRY)->FIELD), typeof(*(ENTRY)), FIELD)
    241 
    242 #define	list_for_each(VAR, HEAD)					\
    243 	for ((VAR) = list_first((HEAD));				\
    244 		(VAR) != (HEAD);					\
    245 		(VAR) = list_next((VAR)))
    246 
    247 #define	list_for_each_safe(VAR, NEXT, HEAD)				\
    248 	for ((VAR) = list_first((HEAD));				\
    249 		((VAR) != (HEAD)) && ((NEXT) = list_next((VAR)), 1);	\
    250 		(VAR) = (NEXT))
    251 
    252 #define	list_for_each_entry(VAR, HEAD, FIELD)				\
    253 	for ((VAR) = list_entry(list_first((HEAD)), typeof(*(VAR)), FIELD); \
    254 		&(VAR)->FIELD != (HEAD);				\
    255 		(VAR) = list_entry(list_next(&(VAR)->FIELD), typeof(*(VAR)), \
    256 		    FIELD))
    257 
    258 #define	list_for_each_entry_reverse(VAR, HEAD, FIELD)			\
    259 	for ((VAR) = list_entry(list_last((HEAD)), typeof(*(VAR)), FIELD); \
    260 		&(VAR)->FIELD != (HEAD);				\
    261 		(VAR) = list_entry(list_prev(&(VAR)->FIELD), typeof(*(VAR)), \
    262 		    FIELD))
    263 
    264 #define	list_for_each_entry_safe(VAR, NEXT, HEAD, FIELD)		\
    265 	for ((VAR) = list_entry(list_first((HEAD)), typeof(*(VAR)), FIELD); \
    266 		(&(VAR)->FIELD != (HEAD)) &&				\
    267 		    ((NEXT) = list_entry(list_next(&(VAR)->FIELD),	\
    268 			typeof(*(VAR)), FIELD), 1);			\
    269 		(VAR) = (NEXT))
    270 
    271 #define	list_for_each_entry_continue(VAR, HEAD, FIELD)			\
    272 	for ((VAR) = list_next_entry((VAR), FIELD);			\
    273 		&(VAR)->FIELD != (HEAD);				\
    274 		(VAR) = list_next_entry((VAR), FIELD))
    275 
    276 #define	list_for_each_entry_continue_reverse(VAR, HEAD, FIELD)		\
    277 	for ((VAR) = list_prev_entry((VAR), FIELD);			\
    278 		&(VAR)->FIELD != (HEAD);				\
    279 		(VAR) = list_prev_entry((VAR), FIELD))
    280 
    281 #define	list_for_each_entry_safe_from(VAR, NEXT, HEAD, FIELD)		\
    282 	for (;								\
    283 		(&(VAR)->FIELD != (HEAD)) &&				\
    284 		    ((NEXT) = list_next_entry((VAR), FIELD));		\
    285 		(VAR) = (NEXT))
    286 
    287 /*
    288  * `H'ead-only/`H'ash-table doubly-linked lists.
    289  */
    290 
    291 #define	hlist_head	pslist_head
    292 #define	hlist_node	pslist_entry
    293 
    294 #define	HLIST_HEAD_INIT	PSLIST_INITIALIZER
    295 #define	INIT_HLIST_HEAD	PSLIST_INIT
    296 #define	hlist_empty(h)	(pslist_writer_first(h) == NULL)
    297 #define	hlist_first	pslist_writer_first
    298 #define	hlist_next	pslist_writer_next
    299 
    300 static inline void
    301 hlist_add_head(struct hlist_node *node, struct hlist_head *head)
    302 {
    303 
    304 	pslist_entry_init(node);
    305 	pslist_writer_insert_head(head, node);
    306 }
    307 
    308 static inline void
    309 hlist_del(struct hlist_node *node)
    310 {
    311 
    312 	pslist_writer_remove(node);
    313 	/* XXX abstraction violation */
    314 	node->ple_prevp = _PSLIST_POISON;
    315 }
    316 
    317 static inline void
    318 hlist_del_init(struct hlist_node *node)
    319 {
    320 
    321 	/* XXX abstraction violation */
    322 	if (node->ple_prevp != NULL)
    323 		pslist_writer_remove(node);
    324 }
    325 
    326 #define	hlist_entry(PTR, TYPE, FIELD)	container_of(PTR, TYPE, FIELD)
    327 #define	hlist_for_each(VAR, HEAD)					      \
    328 	for ((VAR) = hlist_first(HEAD); (VAR) != NULL; (VAR) = hlist_next(VAR))
    329 #define	hlist_for_each_safe(VAR, NEXT, HEAD)				      \
    330 	for ((VAR) = hlist_first(HEAD);					      \
    331 		(VAR) != NULL && ((NEXT) = hlist_next(HEAD), 1);	      \
    332 		(VAR) = (NEXT))
    333 #define	hlist_for_each_entry(VAR, HEAD, FIELD)				      \
    334 	for ((VAR) = (hlist_first(HEAD) == NULL ? NULL :		      \
    335 			hlist_entry(hlist_first(HEAD), typeof(*(VAR)),	      \
    336 			    FIELD));					      \
    337 		(VAR) != NULL;						      \
    338 		(VAR) = (hlist_next(&(VAR)->FIELD) == NULL ? NULL :	      \
    339 			hlist_entry(hlist_next(&(VAR)->FIELD), typeof(*(VAR)),\
    340 			    FIELD)))
    341 
    342 #define	hlist_for_each_entry_safe(VAR, NEXT, HEAD, FIELD)		      \
    343 	for ((VAR) = (hlist_first(HEAD) == NULL ? NULL :		      \
    344 			hlist_entry(hlist_first(HEAD), typeof(*(VAR)),	      \
    345 			    FIELD));					      \
    346 		((VAR) != NULL &&					      \
    347 		    ((NEXT) = hlist_next(&(VAR)->FIELD), 1));		      \
    348 	        (VAR) = hlist_entry((NEXT), typeof(*(VAR)), FIELD))
    349 
    350 static inline void
    351 hlist_add_behind_rcu(struct hlist_node *node, struct hlist_node *prev)
    352 {
    353 
    354 	pslist_entry_init(node);
    355 	pslist_writer_insert_after(prev, node);
    356 }
    357 
    358 static inline void
    359 hlist_add_head_rcu(struct hlist_node *node, struct hlist_head *head)
    360 {
    361 
    362 	pslist_entry_init(node);
    363 	pslist_writer_insert_head(head, node);
    364 }
    365 
    366 #define	hlist_del_init_rcu	hlist_del_init /* no special needs */
    367 
    368 #define	hlist_first_rcu		pslist_reader_first
    369 #define	hlist_next_rcu		pslist_reader_next
    370 
    371 #define	hlist_for_each_entry_rcu(VAR, HEAD, FIELD)			      \
    372 	for ((VAR) = (hlist_first_rcu(HEAD) == NULL ? NULL :		      \
    373 			hlist_entry(hlist_first_rcu(HEAD), typeof(*(VAR)),    \
    374 			    FIELD));					      \
    375 		(VAR) != NULL;						      \
    376 		(VAR) = (hlist_next_rcu(&(VAR)->FIELD) == NULL ? NULL :	      \
    377 			hlist_entry(hlist_next_rcu(&(VAR)->FIELD),	      \
    378 			    typeof(*(VAR)), FIELD)))
    379 
    380 #endif  /* _LINUX_LIST_H_ */
    381