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