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