xref: /openbmc/linux/tools/include/linux/list.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2d1b39d41SJosh Poimboeuf #ifndef __TOOLS_LINUX_LIST_H
3d1b39d41SJosh Poimboeuf #define __TOOLS_LINUX_LIST_H
4d1b39d41SJosh Poimboeuf 
54fc62a89SWang Nan #include <linux/types.h>
6d1b39d41SJosh Poimboeuf #include <linux/poison.h>
7d1b39d41SJosh Poimboeuf #include <linux/kernel.h>
8d1b39d41SJosh Poimboeuf #include <linux/compiler.h>
94fc62a89SWang Nan 
10d1b39d41SJosh Poimboeuf /*
11d1b39d41SJosh Poimboeuf  * Simple doubly linked list implementation.
12d1b39d41SJosh Poimboeuf  *
13d1b39d41SJosh Poimboeuf  * Some of the internal functions ("__xxx") are useful when
14d1b39d41SJosh Poimboeuf  * manipulating whole lists rather than single entries, as
15d1b39d41SJosh Poimboeuf  * sometimes we already know the next/prev entries and we can
16d1b39d41SJosh Poimboeuf  * generate better code by using them directly rather than
17d1b39d41SJosh Poimboeuf  * using the generic single-entry routines.
18d1b39d41SJosh Poimboeuf  */
194fc62a89SWang Nan 
20d1b39d41SJosh Poimboeuf #define LIST_HEAD_INIT(name) { &(name), &(name) }
21d1b39d41SJosh Poimboeuf 
22d1b39d41SJosh Poimboeuf #define LIST_HEAD(name) \
23d1b39d41SJosh Poimboeuf 	struct list_head name = LIST_HEAD_INIT(name)
24d1b39d41SJosh Poimboeuf 
INIT_LIST_HEAD(struct list_head * list)25d1b39d41SJosh Poimboeuf static inline void INIT_LIST_HEAD(struct list_head *list)
26d1b39d41SJosh Poimboeuf {
27d1b39d41SJosh Poimboeuf 	list->next = list;
28d1b39d41SJosh Poimboeuf 	list->prev = list;
29d1b39d41SJosh Poimboeuf }
30d1b39d41SJosh Poimboeuf 
31d1b39d41SJosh Poimboeuf /*
32d1b39d41SJosh Poimboeuf  * Insert a new entry between two known consecutive entries.
33d1b39d41SJosh Poimboeuf  *
34d1b39d41SJosh Poimboeuf  * This is only for internal list manipulation where we know
35d1b39d41SJosh Poimboeuf  * the prev/next entries already!
36d1b39d41SJosh Poimboeuf  */
37d1b39d41SJosh Poimboeuf #ifndef CONFIG_DEBUG_LIST
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)38d1b39d41SJosh Poimboeuf static inline void __list_add(struct list_head *new,
39d1b39d41SJosh Poimboeuf 			      struct list_head *prev,
40d1b39d41SJosh Poimboeuf 			      struct list_head *next)
41d1b39d41SJosh Poimboeuf {
42d1b39d41SJosh Poimboeuf 	next->prev = new;
43d1b39d41SJosh Poimboeuf 	new->next = next;
44d1b39d41SJosh Poimboeuf 	new->prev = prev;
45d1b39d41SJosh Poimboeuf 	prev->next = new;
46d1b39d41SJosh Poimboeuf }
47d1b39d41SJosh Poimboeuf #else
48d1b39d41SJosh Poimboeuf extern void __list_add(struct list_head *new,
49d1b39d41SJosh Poimboeuf 			      struct list_head *prev,
50d1b39d41SJosh Poimboeuf 			      struct list_head *next);
51d1b39d41SJosh Poimboeuf #endif
52d1b39d41SJosh Poimboeuf 
53d1b39d41SJosh Poimboeuf /**
54d1b39d41SJosh Poimboeuf  * list_add - add a new entry
55d1b39d41SJosh Poimboeuf  * @new: new entry to be added
56d1b39d41SJosh Poimboeuf  * @head: list head to add it after
57d1b39d41SJosh Poimboeuf  *
58d1b39d41SJosh Poimboeuf  * Insert a new entry after the specified head.
59d1b39d41SJosh Poimboeuf  * This is good for implementing stacks.
60d1b39d41SJosh Poimboeuf  */
list_add(struct list_head * new,struct list_head * head)61d1b39d41SJosh Poimboeuf static inline void list_add(struct list_head *new, struct list_head *head)
62d1b39d41SJosh Poimboeuf {
63d1b39d41SJosh Poimboeuf 	__list_add(new, head, head->next);
64d1b39d41SJosh Poimboeuf }
65d1b39d41SJosh Poimboeuf 
66d1b39d41SJosh Poimboeuf 
67d1b39d41SJosh Poimboeuf /**
68d1b39d41SJosh Poimboeuf  * list_add_tail - add a new entry
69d1b39d41SJosh Poimboeuf  * @new: new entry to be added
70d1b39d41SJosh Poimboeuf  * @head: list head to add it before
71d1b39d41SJosh Poimboeuf  *
72d1b39d41SJosh Poimboeuf  * Insert a new entry before the specified head.
73d1b39d41SJosh Poimboeuf  * This is useful for implementing queues.
74d1b39d41SJosh Poimboeuf  */
list_add_tail(struct list_head * new,struct list_head * head)75d1b39d41SJosh Poimboeuf static inline void list_add_tail(struct list_head *new, struct list_head *head)
76d1b39d41SJosh Poimboeuf {
77d1b39d41SJosh Poimboeuf 	__list_add(new, head->prev, head);
78d1b39d41SJosh Poimboeuf }
79d1b39d41SJosh Poimboeuf 
80d1b39d41SJosh Poimboeuf /*
81d1b39d41SJosh Poimboeuf  * Delete a list entry by making the prev/next entries
82d1b39d41SJosh Poimboeuf  * point to each other.
83d1b39d41SJosh Poimboeuf  *
84d1b39d41SJosh Poimboeuf  * This is only for internal list manipulation where we know
85d1b39d41SJosh Poimboeuf  * the prev/next entries already!
86d1b39d41SJosh Poimboeuf  */
__list_del(struct list_head * prev,struct list_head * next)87d1b39d41SJosh Poimboeuf static inline void __list_del(struct list_head * prev, struct list_head * next)
88d1b39d41SJosh Poimboeuf {
89d1b39d41SJosh Poimboeuf 	next->prev = prev;
90d1b39d41SJosh Poimboeuf 	WRITE_ONCE(prev->next, next);
91d1b39d41SJosh Poimboeuf }
92d1b39d41SJosh Poimboeuf 
93d1b39d41SJosh Poimboeuf /**
94d1b39d41SJosh Poimboeuf  * list_del - deletes entry from list.
95d1b39d41SJosh Poimboeuf  * @entry: the element to delete from the list.
96d1b39d41SJosh Poimboeuf  * Note: list_empty() on entry does not return true after this, the entry is
97d1b39d41SJosh Poimboeuf  * in an undefined state.
98d1b39d41SJosh Poimboeuf  */
99d1b39d41SJosh Poimboeuf #ifndef CONFIG_DEBUG_LIST
__list_del_entry(struct list_head * entry)100d1b39d41SJosh Poimboeuf static inline void __list_del_entry(struct list_head *entry)
101d1b39d41SJosh Poimboeuf {
102d1b39d41SJosh Poimboeuf 	__list_del(entry->prev, entry->next);
103d1b39d41SJosh Poimboeuf }
104d1b39d41SJosh Poimboeuf 
list_del(struct list_head * entry)105d1b39d41SJosh Poimboeuf static inline void list_del(struct list_head *entry)
106d1b39d41SJosh Poimboeuf {
107d1b39d41SJosh Poimboeuf 	__list_del(entry->prev, entry->next);
108d1b39d41SJosh Poimboeuf 	entry->next = LIST_POISON1;
109d1b39d41SJosh Poimboeuf 	entry->prev = LIST_POISON2;
110d1b39d41SJosh Poimboeuf }
111d1b39d41SJosh Poimboeuf #else
112d1b39d41SJosh Poimboeuf extern void __list_del_entry(struct list_head *entry);
113d1b39d41SJosh Poimboeuf extern void list_del(struct list_head *entry);
114d1b39d41SJosh Poimboeuf #endif
115d1b39d41SJosh Poimboeuf 
116d1b39d41SJosh Poimboeuf /**
117d1b39d41SJosh Poimboeuf  * list_replace - replace old entry by new one
118d1b39d41SJosh Poimboeuf  * @old : the element to be replaced
119d1b39d41SJosh Poimboeuf  * @new : the new element to insert
120d1b39d41SJosh Poimboeuf  *
121d1b39d41SJosh Poimboeuf  * If @old was empty, it will be overwritten.
122d1b39d41SJosh Poimboeuf  */
list_replace(struct list_head * old,struct list_head * new)123d1b39d41SJosh Poimboeuf static inline void list_replace(struct list_head *old,
124d1b39d41SJosh Poimboeuf 				struct list_head *new)
125d1b39d41SJosh Poimboeuf {
126d1b39d41SJosh Poimboeuf 	new->next = old->next;
127d1b39d41SJosh Poimboeuf 	new->next->prev = new;
128d1b39d41SJosh Poimboeuf 	new->prev = old->prev;
129d1b39d41SJosh Poimboeuf 	new->prev->next = new;
130d1b39d41SJosh Poimboeuf }
131d1b39d41SJosh Poimboeuf 
list_replace_init(struct list_head * old,struct list_head * new)132d1b39d41SJosh Poimboeuf static inline void list_replace_init(struct list_head *old,
133d1b39d41SJosh Poimboeuf 					struct list_head *new)
134d1b39d41SJosh Poimboeuf {
135d1b39d41SJosh Poimboeuf 	list_replace(old, new);
136d1b39d41SJosh Poimboeuf 	INIT_LIST_HEAD(old);
137d1b39d41SJosh Poimboeuf }
138d1b39d41SJosh Poimboeuf 
139d1b39d41SJosh Poimboeuf /**
140d1b39d41SJosh Poimboeuf  * list_del_init - deletes entry from list and reinitialize it.
141d1b39d41SJosh Poimboeuf  * @entry: the element to delete from the list.
142d1b39d41SJosh Poimboeuf  */
list_del_init(struct list_head * entry)143d1b39d41SJosh Poimboeuf static inline void list_del_init(struct list_head *entry)
144d1b39d41SJosh Poimboeuf {
145d1b39d41SJosh Poimboeuf 	__list_del_entry(entry);
146d1b39d41SJosh Poimboeuf 	INIT_LIST_HEAD(entry);
147d1b39d41SJosh Poimboeuf }
148d1b39d41SJosh Poimboeuf 
149d1b39d41SJosh Poimboeuf /**
150d1b39d41SJosh Poimboeuf  * list_move - delete from one list and add as another's head
151d1b39d41SJosh Poimboeuf  * @list: the entry to move
152d1b39d41SJosh Poimboeuf  * @head: the head that will precede our entry
153d1b39d41SJosh Poimboeuf  */
list_move(struct list_head * list,struct list_head * head)154d1b39d41SJosh Poimboeuf static inline void list_move(struct list_head *list, struct list_head *head)
155d1b39d41SJosh Poimboeuf {
156d1b39d41SJosh Poimboeuf 	__list_del_entry(list);
157d1b39d41SJosh Poimboeuf 	list_add(list, head);
158d1b39d41SJosh Poimboeuf }
159d1b39d41SJosh Poimboeuf 
160d1b39d41SJosh Poimboeuf /**
161d1b39d41SJosh Poimboeuf  * list_move_tail - delete from one list and add as another's tail
162d1b39d41SJosh Poimboeuf  * @list: the entry to move
163d1b39d41SJosh Poimboeuf  * @head: the head that will follow our entry
164d1b39d41SJosh Poimboeuf  */
list_move_tail(struct list_head * list,struct list_head * head)165d1b39d41SJosh Poimboeuf static inline void list_move_tail(struct list_head *list,
166d1b39d41SJosh Poimboeuf 				  struct list_head *head)
167d1b39d41SJosh Poimboeuf {
168d1b39d41SJosh Poimboeuf 	__list_del_entry(list);
169d1b39d41SJosh Poimboeuf 	list_add_tail(list, head);
170d1b39d41SJosh Poimboeuf }
171d1b39d41SJosh Poimboeuf 
172d1b39d41SJosh Poimboeuf /**
173d1b39d41SJosh Poimboeuf  * list_is_last - tests whether @list is the last entry in list @head
174d1b39d41SJosh Poimboeuf  * @list: the entry to test
175d1b39d41SJosh Poimboeuf  * @head: the head of the list
176d1b39d41SJosh Poimboeuf  */
list_is_last(const struct list_head * list,const struct list_head * head)177d1b39d41SJosh Poimboeuf static inline int list_is_last(const struct list_head *list,
178d1b39d41SJosh Poimboeuf 				const struct list_head *head)
179d1b39d41SJosh Poimboeuf {
180d1b39d41SJosh Poimboeuf 	return list->next == head;
181d1b39d41SJosh Poimboeuf }
182d1b39d41SJosh Poimboeuf 
183d1b39d41SJosh Poimboeuf /**
184d1b39d41SJosh Poimboeuf  * list_empty - tests whether a list is empty
185d1b39d41SJosh Poimboeuf  * @head: the list to test.
186d1b39d41SJosh Poimboeuf  */
list_empty(const struct list_head * head)187d1b39d41SJosh Poimboeuf static inline int list_empty(const struct list_head *head)
188d1b39d41SJosh Poimboeuf {
189d1b39d41SJosh Poimboeuf 	return head->next == head;
190d1b39d41SJosh Poimboeuf }
191d1b39d41SJosh Poimboeuf 
192d1b39d41SJosh Poimboeuf /**
193d1b39d41SJosh Poimboeuf  * list_empty_careful - tests whether a list is empty and not being modified
194d1b39d41SJosh Poimboeuf  * @head: the list to test
195d1b39d41SJosh Poimboeuf  *
196d1b39d41SJosh Poimboeuf  * Description:
197d1b39d41SJosh Poimboeuf  * tests whether a list is empty _and_ checks that no other CPU might be
198d1b39d41SJosh Poimboeuf  * in the process of modifying either member (next or prev)
199d1b39d41SJosh Poimboeuf  *
200d1b39d41SJosh Poimboeuf  * NOTE: using list_empty_careful() without synchronization
201d1b39d41SJosh Poimboeuf  * can only be safe if the only activity that can happen
202d1b39d41SJosh Poimboeuf  * to the list entry is list_del_init(). Eg. it cannot be used
203d1b39d41SJosh Poimboeuf  * if another CPU could re-list_add() it.
204d1b39d41SJosh Poimboeuf  */
list_empty_careful(const struct list_head * head)205d1b39d41SJosh Poimboeuf static inline int list_empty_careful(const struct list_head *head)
206d1b39d41SJosh Poimboeuf {
207d1b39d41SJosh Poimboeuf 	struct list_head *next = head->next;
208d1b39d41SJosh Poimboeuf 	return (next == head) && (next == head->prev);
209d1b39d41SJosh Poimboeuf }
210d1b39d41SJosh Poimboeuf 
211d1b39d41SJosh Poimboeuf /**
212d1b39d41SJosh Poimboeuf  * list_rotate_left - rotate the list to the left
213d1b39d41SJosh Poimboeuf  * @head: the head of the list
214d1b39d41SJosh Poimboeuf  */
list_rotate_left(struct list_head * head)215d1b39d41SJosh Poimboeuf static inline void list_rotate_left(struct list_head *head)
216d1b39d41SJosh Poimboeuf {
217d1b39d41SJosh Poimboeuf 	struct list_head *first;
218d1b39d41SJosh Poimboeuf 
219d1b39d41SJosh Poimboeuf 	if (!list_empty(head)) {
220d1b39d41SJosh Poimboeuf 		first = head->next;
221d1b39d41SJosh Poimboeuf 		list_move_tail(first, head);
222d1b39d41SJosh Poimboeuf 	}
223d1b39d41SJosh Poimboeuf }
224d1b39d41SJosh Poimboeuf 
225d1b39d41SJosh Poimboeuf /**
226d1b39d41SJosh Poimboeuf  * list_is_singular - tests whether a list has just one entry.
227d1b39d41SJosh Poimboeuf  * @head: the list to test.
228d1b39d41SJosh Poimboeuf  */
list_is_singular(const struct list_head * head)229d1b39d41SJosh Poimboeuf static inline int list_is_singular(const struct list_head *head)
230d1b39d41SJosh Poimboeuf {
231d1b39d41SJosh Poimboeuf 	return !list_empty(head) && (head->next == head->prev);
232d1b39d41SJosh Poimboeuf }
233d1b39d41SJosh Poimboeuf 
__list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)234d1b39d41SJosh Poimboeuf static inline void __list_cut_position(struct list_head *list,
235d1b39d41SJosh Poimboeuf 		struct list_head *head, struct list_head *entry)
236d1b39d41SJosh Poimboeuf {
237d1b39d41SJosh Poimboeuf 	struct list_head *new_first = entry->next;
238d1b39d41SJosh Poimboeuf 	list->next = head->next;
239d1b39d41SJosh Poimboeuf 	list->next->prev = list;
240d1b39d41SJosh Poimboeuf 	list->prev = entry;
241d1b39d41SJosh Poimboeuf 	entry->next = list;
242d1b39d41SJosh Poimboeuf 	head->next = new_first;
243d1b39d41SJosh Poimboeuf 	new_first->prev = head;
244d1b39d41SJosh Poimboeuf }
245d1b39d41SJosh Poimboeuf 
246d1b39d41SJosh Poimboeuf /**
247d1b39d41SJosh Poimboeuf  * list_cut_position - cut a list into two
248d1b39d41SJosh Poimboeuf  * @list: a new list to add all removed entries
249d1b39d41SJosh Poimboeuf  * @head: a list with entries
250d1b39d41SJosh Poimboeuf  * @entry: an entry within head, could be the head itself
251d1b39d41SJosh Poimboeuf  *	and if so we won't cut the list
252d1b39d41SJosh Poimboeuf  *
253d1b39d41SJosh Poimboeuf  * This helper moves the initial part of @head, up to and
254d1b39d41SJosh Poimboeuf  * including @entry, from @head to @list. You should
255d1b39d41SJosh Poimboeuf  * pass on @entry an element you know is on @head. @list
256d1b39d41SJosh Poimboeuf  * should be an empty list or a list you do not care about
257d1b39d41SJosh Poimboeuf  * losing its data.
258d1b39d41SJosh Poimboeuf  *
259d1b39d41SJosh Poimboeuf  */
list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)260d1b39d41SJosh Poimboeuf static inline void list_cut_position(struct list_head *list,
261d1b39d41SJosh Poimboeuf 		struct list_head *head, struct list_head *entry)
262d1b39d41SJosh Poimboeuf {
263d1b39d41SJosh Poimboeuf 	if (list_empty(head))
264d1b39d41SJosh Poimboeuf 		return;
265d1b39d41SJosh Poimboeuf 	if (list_is_singular(head) &&
266d1b39d41SJosh Poimboeuf 		(head->next != entry && head != entry))
267d1b39d41SJosh Poimboeuf 		return;
268d1b39d41SJosh Poimboeuf 	if (entry == head)
269d1b39d41SJosh Poimboeuf 		INIT_LIST_HEAD(list);
270d1b39d41SJosh Poimboeuf 	else
271d1b39d41SJosh Poimboeuf 		__list_cut_position(list, head, entry);
272d1b39d41SJosh Poimboeuf }
273d1b39d41SJosh Poimboeuf 
__list_splice(const struct list_head * list,struct list_head * prev,struct list_head * next)274d1b39d41SJosh Poimboeuf static inline void __list_splice(const struct list_head *list,
275d1b39d41SJosh Poimboeuf 				 struct list_head *prev,
276d1b39d41SJosh Poimboeuf 				 struct list_head *next)
277d1b39d41SJosh Poimboeuf {
278d1b39d41SJosh Poimboeuf 	struct list_head *first = list->next;
279d1b39d41SJosh Poimboeuf 	struct list_head *last = list->prev;
280d1b39d41SJosh Poimboeuf 
281d1b39d41SJosh Poimboeuf 	first->prev = prev;
282d1b39d41SJosh Poimboeuf 	prev->next = first;
283d1b39d41SJosh Poimboeuf 
284d1b39d41SJosh Poimboeuf 	last->next = next;
285d1b39d41SJosh Poimboeuf 	next->prev = last;
286d1b39d41SJosh Poimboeuf }
287d1b39d41SJosh Poimboeuf 
288d1b39d41SJosh Poimboeuf /**
289d1b39d41SJosh Poimboeuf  * list_splice - join two lists, this is designed for stacks
290d1b39d41SJosh Poimboeuf  * @list: the new list to add.
291d1b39d41SJosh Poimboeuf  * @head: the place to add it in the first list.
292d1b39d41SJosh Poimboeuf  */
list_splice(const struct list_head * list,struct list_head * head)293d1b39d41SJosh Poimboeuf static inline void list_splice(const struct list_head *list,
294d1b39d41SJosh Poimboeuf 				struct list_head *head)
295d1b39d41SJosh Poimboeuf {
296d1b39d41SJosh Poimboeuf 	if (!list_empty(list))
297d1b39d41SJosh Poimboeuf 		__list_splice(list, head, head->next);
298d1b39d41SJosh Poimboeuf }
299d1b39d41SJosh Poimboeuf 
300d1b39d41SJosh Poimboeuf /**
301d1b39d41SJosh Poimboeuf  * list_splice_tail - join two lists, each list being a queue
302d1b39d41SJosh Poimboeuf  * @list: the new list to add.
303d1b39d41SJosh Poimboeuf  * @head: the place to add it in the first list.
304d1b39d41SJosh Poimboeuf  */
list_splice_tail(struct list_head * list,struct list_head * head)305d1b39d41SJosh Poimboeuf static inline void list_splice_tail(struct list_head *list,
306d1b39d41SJosh Poimboeuf 				struct list_head *head)
307d1b39d41SJosh Poimboeuf {
308d1b39d41SJosh Poimboeuf 	if (!list_empty(list))
309d1b39d41SJosh Poimboeuf 		__list_splice(list, head->prev, head);
310d1b39d41SJosh Poimboeuf }
311d1b39d41SJosh Poimboeuf 
312d1b39d41SJosh Poimboeuf /**
313d1b39d41SJosh Poimboeuf  * list_splice_init - join two lists and reinitialise the emptied list.
314d1b39d41SJosh Poimboeuf  * @list: the new list to add.
315d1b39d41SJosh Poimboeuf  * @head: the place to add it in the first list.
316d1b39d41SJosh Poimboeuf  *
317d1b39d41SJosh Poimboeuf  * The list at @list is reinitialised
318d1b39d41SJosh Poimboeuf  */
list_splice_init(struct list_head * list,struct list_head * head)319d1b39d41SJosh Poimboeuf static inline void list_splice_init(struct list_head *list,
320d1b39d41SJosh Poimboeuf 				    struct list_head *head)
321d1b39d41SJosh Poimboeuf {
322d1b39d41SJosh Poimboeuf 	if (!list_empty(list)) {
323d1b39d41SJosh Poimboeuf 		__list_splice(list, head, head->next);
324d1b39d41SJosh Poimboeuf 		INIT_LIST_HEAD(list);
325d1b39d41SJosh Poimboeuf 	}
326d1b39d41SJosh Poimboeuf }
327d1b39d41SJosh Poimboeuf 
328d1b39d41SJosh Poimboeuf /**
329d1b39d41SJosh Poimboeuf  * list_splice_tail_init - join two lists and reinitialise the emptied list
330d1b39d41SJosh Poimboeuf  * @list: the new list to add.
331d1b39d41SJosh Poimboeuf  * @head: the place to add it in the first list.
332d1b39d41SJosh Poimboeuf  *
333d1b39d41SJosh Poimboeuf  * Each of the lists is a queue.
334d1b39d41SJosh Poimboeuf  * The list at @list is reinitialised
335d1b39d41SJosh Poimboeuf  */
list_splice_tail_init(struct list_head * list,struct list_head * head)336d1b39d41SJosh Poimboeuf static inline void list_splice_tail_init(struct list_head *list,
337d1b39d41SJosh Poimboeuf 					 struct list_head *head)
338d1b39d41SJosh Poimboeuf {
339d1b39d41SJosh Poimboeuf 	if (!list_empty(list)) {
340d1b39d41SJosh Poimboeuf 		__list_splice(list, head->prev, head);
341d1b39d41SJosh Poimboeuf 		INIT_LIST_HEAD(list);
342d1b39d41SJosh Poimboeuf 	}
343d1b39d41SJosh Poimboeuf }
344d1b39d41SJosh Poimboeuf 
345d1b39d41SJosh Poimboeuf /**
346d1b39d41SJosh Poimboeuf  * list_entry - get the struct for this entry
347d1b39d41SJosh Poimboeuf  * @ptr:	the &struct list_head pointer.
348d1b39d41SJosh Poimboeuf  * @type:	the type of the struct this is embedded in.
349d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
350d1b39d41SJosh Poimboeuf  */
351d1b39d41SJosh Poimboeuf #define list_entry(ptr, type, member) \
352d1b39d41SJosh Poimboeuf 	container_of(ptr, type, member)
353d1b39d41SJosh Poimboeuf 
354d1b39d41SJosh Poimboeuf /**
355d1b39d41SJosh Poimboeuf  * list_first_entry - get the first element from a list
356d1b39d41SJosh Poimboeuf  * @ptr:	the list head to take the element from.
357d1b39d41SJosh Poimboeuf  * @type:	the type of the struct this is embedded in.
358d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
359d1b39d41SJosh Poimboeuf  *
360d1b39d41SJosh Poimboeuf  * Note, that list is expected to be not empty.
361d1b39d41SJosh Poimboeuf  */
362d1b39d41SJosh Poimboeuf #define list_first_entry(ptr, type, member) \
363d1b39d41SJosh Poimboeuf 	list_entry((ptr)->next, type, member)
364d1b39d41SJosh Poimboeuf 
365d1b39d41SJosh Poimboeuf /**
366d1b39d41SJosh Poimboeuf  * list_last_entry - get the last element from a list
367d1b39d41SJosh Poimboeuf  * @ptr:	the list head to take the element from.
368d1b39d41SJosh Poimboeuf  * @type:	the type of the struct this is embedded in.
369d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
370d1b39d41SJosh Poimboeuf  *
371d1b39d41SJosh Poimboeuf  * Note, that list is expected to be not empty.
372d1b39d41SJosh Poimboeuf  */
373d1b39d41SJosh Poimboeuf #define list_last_entry(ptr, type, member) \
374d1b39d41SJosh Poimboeuf 	list_entry((ptr)->prev, type, member)
375d1b39d41SJosh Poimboeuf 
376d1b39d41SJosh Poimboeuf /**
377d1b39d41SJosh Poimboeuf  * list_first_entry_or_null - get the first element from a list
378d1b39d41SJosh Poimboeuf  * @ptr:	the list head to take the element from.
379d1b39d41SJosh Poimboeuf  * @type:	the type of the struct this is embedded in.
380d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
381d1b39d41SJosh Poimboeuf  *
382d1b39d41SJosh Poimboeuf  * Note that if the list is empty, it returns NULL.
383d1b39d41SJosh Poimboeuf  */
384d1b39d41SJosh Poimboeuf #define list_first_entry_or_null(ptr, type, member) \
385d1b39d41SJosh Poimboeuf 	(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
386d1b39d41SJosh Poimboeuf 
387d1b39d41SJosh Poimboeuf /**
388*e432947eSYang Jihong  * list_last_entry_or_null - get the last element from a list
389*e432947eSYang Jihong  * @ptr:       the list head to take the element from.
390*e432947eSYang Jihong  * @type:      the type of the struct this is embedded in.
391*e432947eSYang Jihong  * @member:    the name of the list_head within the struct.
392*e432947eSYang Jihong  *
393*e432947eSYang Jihong  * Note that if the list is empty, it returns NULL.
394*e432947eSYang Jihong  */
395*e432947eSYang Jihong #define list_last_entry_or_null(ptr, type, member) \
396*e432947eSYang Jihong 	(!list_empty(ptr) ? list_last_entry(ptr, type, member) : NULL)
397*e432947eSYang Jihong 
398*e432947eSYang Jihong /**
399d1b39d41SJosh Poimboeuf  * list_next_entry - get the next element in list
400d1b39d41SJosh Poimboeuf  * @pos:	the type * to cursor
401d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
402d1b39d41SJosh Poimboeuf  */
403d1b39d41SJosh Poimboeuf #define list_next_entry(pos, member) \
404d1b39d41SJosh Poimboeuf 	list_entry((pos)->member.next, typeof(*(pos)), member)
405d1b39d41SJosh Poimboeuf 
406d1b39d41SJosh Poimboeuf /**
407d1b39d41SJosh Poimboeuf  * list_prev_entry - get the prev element in list
408d1b39d41SJosh Poimboeuf  * @pos:	the type * to cursor
409d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
410d1b39d41SJosh Poimboeuf  */
411d1b39d41SJosh Poimboeuf #define list_prev_entry(pos, member) \
412d1b39d41SJosh Poimboeuf 	list_entry((pos)->member.prev, typeof(*(pos)), member)
413d1b39d41SJosh Poimboeuf 
414d1b39d41SJosh Poimboeuf /**
415d1b39d41SJosh Poimboeuf  * list_for_each	-	iterate over a list
416d1b39d41SJosh Poimboeuf  * @pos:	the &struct list_head to use as a loop cursor.
417d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
418d1b39d41SJosh Poimboeuf  */
419d1b39d41SJosh Poimboeuf #define list_for_each(pos, head) \
420d1b39d41SJosh Poimboeuf 	for (pos = (head)->next; pos != (head); pos = pos->next)
421d1b39d41SJosh Poimboeuf 
422d1b39d41SJosh Poimboeuf /**
423d1b39d41SJosh Poimboeuf  * list_for_each_prev	-	iterate over a list backwards
424d1b39d41SJosh Poimboeuf  * @pos:	the &struct list_head to use as a loop cursor.
425d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
426d1b39d41SJosh Poimboeuf  */
427d1b39d41SJosh Poimboeuf #define list_for_each_prev(pos, head) \
428d1b39d41SJosh Poimboeuf 	for (pos = (head)->prev; pos != (head); pos = pos->prev)
429d1b39d41SJosh Poimboeuf 
430d1b39d41SJosh Poimboeuf /**
431d1b39d41SJosh Poimboeuf  * list_for_each_safe - iterate over a list safe against removal of list entry
432d1b39d41SJosh Poimboeuf  * @pos:	the &struct list_head to use as a loop cursor.
433d1b39d41SJosh Poimboeuf  * @n:		another &struct list_head to use as temporary storage
434d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
435d1b39d41SJosh Poimboeuf  */
436d1b39d41SJosh Poimboeuf #define list_for_each_safe(pos, n, head) \
437d1b39d41SJosh Poimboeuf 	for (pos = (head)->next, n = pos->next; pos != (head); \
438d1b39d41SJosh Poimboeuf 		pos = n, n = pos->next)
439d1b39d41SJosh Poimboeuf 
440d1b39d41SJosh Poimboeuf /**
441d1b39d41SJosh Poimboeuf  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
442d1b39d41SJosh Poimboeuf  * @pos:	the &struct list_head to use as a loop cursor.
443d1b39d41SJosh Poimboeuf  * @n:		another &struct list_head to use as temporary storage
444d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
445d1b39d41SJosh Poimboeuf  */
446d1b39d41SJosh Poimboeuf #define list_for_each_prev_safe(pos, n, head) \
447d1b39d41SJosh Poimboeuf 	for (pos = (head)->prev, n = pos->prev; \
448d1b39d41SJosh Poimboeuf 	     pos != (head); \
449d1b39d41SJosh Poimboeuf 	     pos = n, n = pos->prev)
450d1b39d41SJosh Poimboeuf 
451d1b39d41SJosh Poimboeuf /**
452d1b39d41SJosh Poimboeuf  * list_for_each_entry	-	iterate over list of given type
453d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
454d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
455d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
456d1b39d41SJosh Poimboeuf  */
457d1b39d41SJosh Poimboeuf #define list_for_each_entry(pos, head, member)				\
458d1b39d41SJosh Poimboeuf 	for (pos = list_first_entry(head, typeof(*pos), member);	\
459d1b39d41SJosh Poimboeuf 	     &pos->member != (head);					\
460d1b39d41SJosh Poimboeuf 	     pos = list_next_entry(pos, member))
461d1b39d41SJosh Poimboeuf 
462d1b39d41SJosh Poimboeuf /**
463d1b39d41SJosh Poimboeuf  * list_for_each_entry_reverse - iterate backwards over list of given type.
464d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
465d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
466d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
467d1b39d41SJosh Poimboeuf  */
468d1b39d41SJosh Poimboeuf #define list_for_each_entry_reverse(pos, head, member)			\
469d1b39d41SJosh Poimboeuf 	for (pos = list_last_entry(head, typeof(*pos), member);		\
470d1b39d41SJosh Poimboeuf 	     &pos->member != (head); 					\
471d1b39d41SJosh Poimboeuf 	     pos = list_prev_entry(pos, member))
472d1b39d41SJosh Poimboeuf 
473d1b39d41SJosh Poimboeuf /**
474d1b39d41SJosh Poimboeuf  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
475d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a start point
476d1b39d41SJosh Poimboeuf  * @head:	the head of the list
477d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
478d1b39d41SJosh Poimboeuf  *
479d1b39d41SJosh Poimboeuf  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
480d1b39d41SJosh Poimboeuf  */
481d1b39d41SJosh Poimboeuf #define list_prepare_entry(pos, head, member) \
482d1b39d41SJosh Poimboeuf 	((pos) ? : list_entry(head, typeof(*pos), member))
483d1b39d41SJosh Poimboeuf 
484d1b39d41SJosh Poimboeuf /**
485d1b39d41SJosh Poimboeuf  * list_for_each_entry_continue - continue iteration over list of given type
486d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
487d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
488d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
489d1b39d41SJosh Poimboeuf  *
490d1b39d41SJosh Poimboeuf  * Continue to iterate over list of given type, continuing after
491d1b39d41SJosh Poimboeuf  * the current position.
492d1b39d41SJosh Poimboeuf  */
493d1b39d41SJosh Poimboeuf #define list_for_each_entry_continue(pos, head, member) 		\
494d1b39d41SJosh Poimboeuf 	for (pos = list_next_entry(pos, member);			\
495d1b39d41SJosh Poimboeuf 	     &pos->member != (head);					\
496d1b39d41SJosh Poimboeuf 	     pos = list_next_entry(pos, member))
497d1b39d41SJosh Poimboeuf 
498d1b39d41SJosh Poimboeuf /**
499d1b39d41SJosh Poimboeuf  * list_for_each_entry_continue_reverse - iterate backwards from the given point
500d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
501d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
502d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
503d1b39d41SJosh Poimboeuf  *
504d1b39d41SJosh Poimboeuf  * Start to iterate over list of given type backwards, continuing after
505d1b39d41SJosh Poimboeuf  * the current position.
506d1b39d41SJosh Poimboeuf  */
507d1b39d41SJosh Poimboeuf #define list_for_each_entry_continue_reverse(pos, head, member)		\
508d1b39d41SJosh Poimboeuf 	for (pos = list_prev_entry(pos, member);			\
509d1b39d41SJosh Poimboeuf 	     &pos->member != (head);					\
510d1b39d41SJosh Poimboeuf 	     pos = list_prev_entry(pos, member))
511d1b39d41SJosh Poimboeuf 
512d1b39d41SJosh Poimboeuf /**
513d1b39d41SJosh Poimboeuf  * list_for_each_entry_from - iterate over list of given type from the current point
514d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
515d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
516d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
517d1b39d41SJosh Poimboeuf  *
518d1b39d41SJosh Poimboeuf  * Iterate over list of given type, continuing from current position.
519d1b39d41SJosh Poimboeuf  */
520d1b39d41SJosh Poimboeuf #define list_for_each_entry_from(pos, head, member) 			\
521d1b39d41SJosh Poimboeuf 	for (; &pos->member != (head);					\
522d1b39d41SJosh Poimboeuf 	     pos = list_next_entry(pos, member))
523d1b39d41SJosh Poimboeuf 
524d1b39d41SJosh Poimboeuf /**
525d1b39d41SJosh Poimboeuf  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
526d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
527d1b39d41SJosh Poimboeuf  * @n:		another type * to use as temporary storage
528d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
529d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
530d1b39d41SJosh Poimboeuf  */
531d1b39d41SJosh Poimboeuf #define list_for_each_entry_safe(pos, n, head, member)			\
532d1b39d41SJosh Poimboeuf 	for (pos = list_first_entry(head, typeof(*pos), member),	\
533d1b39d41SJosh Poimboeuf 		n = list_next_entry(pos, member);			\
534d1b39d41SJosh Poimboeuf 	     &pos->member != (head); 					\
535d1b39d41SJosh Poimboeuf 	     pos = n, n = list_next_entry(n, member))
536d1b39d41SJosh Poimboeuf 
537d1b39d41SJosh Poimboeuf /**
538d1b39d41SJosh Poimboeuf  * list_for_each_entry_safe_continue - continue list iteration safe against removal
539d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
540d1b39d41SJosh Poimboeuf  * @n:		another type * to use as temporary storage
541d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
542d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
543d1b39d41SJosh Poimboeuf  *
544d1b39d41SJosh Poimboeuf  * Iterate over list of given type, continuing after current point,
545d1b39d41SJosh Poimboeuf  * safe against removal of list entry.
546d1b39d41SJosh Poimboeuf  */
547d1b39d41SJosh Poimboeuf #define list_for_each_entry_safe_continue(pos, n, head, member) 		\
548d1b39d41SJosh Poimboeuf 	for (pos = list_next_entry(pos, member), 				\
549d1b39d41SJosh Poimboeuf 		n = list_next_entry(pos, member);				\
550d1b39d41SJosh Poimboeuf 	     &pos->member != (head);						\
551d1b39d41SJosh Poimboeuf 	     pos = n, n = list_next_entry(n, member))
552d1b39d41SJosh Poimboeuf 
553d1b39d41SJosh Poimboeuf /**
554d1b39d41SJosh Poimboeuf  * list_for_each_entry_safe_from - iterate over list from current point safe against removal
555d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
556d1b39d41SJosh Poimboeuf  * @n:		another type * to use as temporary storage
557d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
558d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
559d1b39d41SJosh Poimboeuf  *
560d1b39d41SJosh Poimboeuf  * Iterate over list of given type from current point, safe against
561d1b39d41SJosh Poimboeuf  * removal of list entry.
562d1b39d41SJosh Poimboeuf  */
563d1b39d41SJosh Poimboeuf #define list_for_each_entry_safe_from(pos, n, head, member) 			\
564d1b39d41SJosh Poimboeuf 	for (n = list_next_entry(pos, member);					\
565d1b39d41SJosh Poimboeuf 	     &pos->member != (head);						\
566d1b39d41SJosh Poimboeuf 	     pos = n, n = list_next_entry(n, member))
567d1b39d41SJosh Poimboeuf 
568d1b39d41SJosh Poimboeuf /**
569d1b39d41SJosh Poimboeuf  * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
570d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
571d1b39d41SJosh Poimboeuf  * @n:		another type * to use as temporary storage
572d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
573d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
574d1b39d41SJosh Poimboeuf  *
575d1b39d41SJosh Poimboeuf  * Iterate backwards over list of given type, safe against removal
576d1b39d41SJosh Poimboeuf  * of list entry.
577d1b39d41SJosh Poimboeuf  */
578d1b39d41SJosh Poimboeuf #define list_for_each_entry_safe_reverse(pos, n, head, member)		\
579d1b39d41SJosh Poimboeuf 	for (pos = list_last_entry(head, typeof(*pos), member),		\
580d1b39d41SJosh Poimboeuf 		n = list_prev_entry(pos, member);			\
581d1b39d41SJosh Poimboeuf 	     &pos->member != (head); 					\
582d1b39d41SJosh Poimboeuf 	     pos = n, n = list_prev_entry(n, member))
583d1b39d41SJosh Poimboeuf 
584d1b39d41SJosh Poimboeuf /**
585d1b39d41SJosh Poimboeuf  * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
586d1b39d41SJosh Poimboeuf  * @pos:	the loop cursor used in the list_for_each_entry_safe loop
587d1b39d41SJosh Poimboeuf  * @n:		temporary storage used in list_for_each_entry_safe
588d1b39d41SJosh Poimboeuf  * @member:	the name of the list_head within the struct.
589d1b39d41SJosh Poimboeuf  *
590d1b39d41SJosh Poimboeuf  * list_safe_reset_next is not safe to use in general if the list may be
591d1b39d41SJosh Poimboeuf  * modified concurrently (eg. the lock is dropped in the loop body). An
592d1b39d41SJosh Poimboeuf  * exception to this is if the cursor element (pos) is pinned in the list,
593d1b39d41SJosh Poimboeuf  * and list_safe_reset_next is called after re-taking the lock and before
594d1b39d41SJosh Poimboeuf  * completing the current iteration of the loop body.
595d1b39d41SJosh Poimboeuf  */
596d1b39d41SJosh Poimboeuf #define list_safe_reset_next(pos, n, member)				\
597d1b39d41SJosh Poimboeuf 	n = list_next_entry(pos, member)
598d1b39d41SJosh Poimboeuf 
599d1b39d41SJosh Poimboeuf /*
600d1b39d41SJosh Poimboeuf  * Double linked lists with a single pointer list head.
601d1b39d41SJosh Poimboeuf  * Mostly useful for hash tables where the two pointer list head is
602d1b39d41SJosh Poimboeuf  * too wasteful.
603d1b39d41SJosh Poimboeuf  * You lose the ability to access the tail in O(1).
604d1b39d41SJosh Poimboeuf  */
605d1b39d41SJosh Poimboeuf 
606d1b39d41SJosh Poimboeuf #define HLIST_HEAD_INIT { .first = NULL }
607d1b39d41SJosh Poimboeuf #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
608d1b39d41SJosh Poimboeuf #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
INIT_HLIST_NODE(struct hlist_node * h)609d1b39d41SJosh Poimboeuf static inline void INIT_HLIST_NODE(struct hlist_node *h)
610d1b39d41SJosh Poimboeuf {
611d1b39d41SJosh Poimboeuf 	h->next = NULL;
612d1b39d41SJosh Poimboeuf 	h->pprev = NULL;
613d1b39d41SJosh Poimboeuf }
614d1b39d41SJosh Poimboeuf 
hlist_unhashed(const struct hlist_node * h)615d1b39d41SJosh Poimboeuf static inline int hlist_unhashed(const struct hlist_node *h)
616d1b39d41SJosh Poimboeuf {
617d1b39d41SJosh Poimboeuf 	return !h->pprev;
618d1b39d41SJosh Poimboeuf }
619d1b39d41SJosh Poimboeuf 
hlist_empty(const struct hlist_head * h)620d1b39d41SJosh Poimboeuf static inline int hlist_empty(const struct hlist_head *h)
621d1b39d41SJosh Poimboeuf {
622d1b39d41SJosh Poimboeuf 	return !h->first;
623d1b39d41SJosh Poimboeuf }
624d1b39d41SJosh Poimboeuf 
__hlist_del(struct hlist_node * n)625d1b39d41SJosh Poimboeuf static inline void __hlist_del(struct hlist_node *n)
626d1b39d41SJosh Poimboeuf {
627d1b39d41SJosh Poimboeuf 	struct hlist_node *next = n->next;
628d1b39d41SJosh Poimboeuf 	struct hlist_node **pprev = n->pprev;
629d1b39d41SJosh Poimboeuf 
630d1b39d41SJosh Poimboeuf 	WRITE_ONCE(*pprev, next);
631d1b39d41SJosh Poimboeuf 	if (next)
632d1b39d41SJosh Poimboeuf 		next->pprev = pprev;
633d1b39d41SJosh Poimboeuf }
634d1b39d41SJosh Poimboeuf 
hlist_del(struct hlist_node * n)635d1b39d41SJosh Poimboeuf static inline void hlist_del(struct hlist_node *n)
636d1b39d41SJosh Poimboeuf {
637d1b39d41SJosh Poimboeuf 	__hlist_del(n);
638d1b39d41SJosh Poimboeuf 	n->next = LIST_POISON1;
639d1b39d41SJosh Poimboeuf 	n->pprev = LIST_POISON2;
640d1b39d41SJosh Poimboeuf }
641d1b39d41SJosh Poimboeuf 
hlist_del_init(struct hlist_node * n)642d1b39d41SJosh Poimboeuf static inline void hlist_del_init(struct hlist_node *n)
643d1b39d41SJosh Poimboeuf {
644d1b39d41SJosh Poimboeuf 	if (!hlist_unhashed(n)) {
645d1b39d41SJosh Poimboeuf 		__hlist_del(n);
646d1b39d41SJosh Poimboeuf 		INIT_HLIST_NODE(n);
647d1b39d41SJosh Poimboeuf 	}
648d1b39d41SJosh Poimboeuf }
649d1b39d41SJosh Poimboeuf 
hlist_add_head(struct hlist_node * n,struct hlist_head * h)650d1b39d41SJosh Poimboeuf static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
651d1b39d41SJosh Poimboeuf {
652d1b39d41SJosh Poimboeuf 	struct hlist_node *first = h->first;
653d1b39d41SJosh Poimboeuf 	n->next = first;
654d1b39d41SJosh Poimboeuf 	if (first)
655d1b39d41SJosh Poimboeuf 		first->pprev = &n->next;
656d1b39d41SJosh Poimboeuf 	h->first = n;
657d1b39d41SJosh Poimboeuf 	n->pprev = &h->first;
658d1b39d41SJosh Poimboeuf }
659d1b39d41SJosh Poimboeuf 
660d1b39d41SJosh Poimboeuf /* next must be != NULL */
hlist_add_before(struct hlist_node * n,struct hlist_node * next)661d1b39d41SJosh Poimboeuf static inline void hlist_add_before(struct hlist_node *n,
662d1b39d41SJosh Poimboeuf 					struct hlist_node *next)
663d1b39d41SJosh Poimboeuf {
664d1b39d41SJosh Poimboeuf 	n->pprev = next->pprev;
665d1b39d41SJosh Poimboeuf 	n->next = next;
666d1b39d41SJosh Poimboeuf 	next->pprev = &n->next;
667d1b39d41SJosh Poimboeuf 	*(n->pprev) = n;
668d1b39d41SJosh Poimboeuf }
669d1b39d41SJosh Poimboeuf 
hlist_add_behind(struct hlist_node * n,struct hlist_node * prev)670d1b39d41SJosh Poimboeuf static inline void hlist_add_behind(struct hlist_node *n,
671d1b39d41SJosh Poimboeuf 				    struct hlist_node *prev)
672d1b39d41SJosh Poimboeuf {
673d1b39d41SJosh Poimboeuf 	n->next = prev->next;
674d1b39d41SJosh Poimboeuf 	prev->next = n;
675d1b39d41SJosh Poimboeuf 	n->pprev = &prev->next;
676d1b39d41SJosh Poimboeuf 
677d1b39d41SJosh Poimboeuf 	if (n->next)
678d1b39d41SJosh Poimboeuf 		n->next->pprev  = &n->next;
679d1b39d41SJosh Poimboeuf }
680d1b39d41SJosh Poimboeuf 
681d1b39d41SJosh Poimboeuf /* after that we'll appear to be on some hlist and hlist_del will work */
hlist_add_fake(struct hlist_node * n)682d1b39d41SJosh Poimboeuf static inline void hlist_add_fake(struct hlist_node *n)
683d1b39d41SJosh Poimboeuf {
684d1b39d41SJosh Poimboeuf 	n->pprev = &n->next;
685d1b39d41SJosh Poimboeuf }
686d1b39d41SJosh Poimboeuf 
hlist_fake(struct hlist_node * h)687d1b39d41SJosh Poimboeuf static inline bool hlist_fake(struct hlist_node *h)
688d1b39d41SJosh Poimboeuf {
689d1b39d41SJosh Poimboeuf 	return h->pprev == &h->next;
690d1b39d41SJosh Poimboeuf }
691d1b39d41SJosh Poimboeuf 
692d1b39d41SJosh Poimboeuf /*
693d1b39d41SJosh Poimboeuf  * Move a list from one list head to another. Fixup the pprev
694d1b39d41SJosh Poimboeuf  * reference of the first entry if it exists.
695d1b39d41SJosh Poimboeuf  */
hlist_move_list(struct hlist_head * old,struct hlist_head * new)696d1b39d41SJosh Poimboeuf static inline void hlist_move_list(struct hlist_head *old,
697d1b39d41SJosh Poimboeuf 				   struct hlist_head *new)
698d1b39d41SJosh Poimboeuf {
699d1b39d41SJosh Poimboeuf 	new->first = old->first;
700d1b39d41SJosh Poimboeuf 	if (new->first)
701d1b39d41SJosh Poimboeuf 		new->first->pprev = &new->first;
702d1b39d41SJosh Poimboeuf 	old->first = NULL;
703d1b39d41SJosh Poimboeuf }
704d1b39d41SJosh Poimboeuf 
705d1b39d41SJosh Poimboeuf #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
706d1b39d41SJosh Poimboeuf 
707d1b39d41SJosh Poimboeuf #define hlist_for_each(pos, head) \
708d1b39d41SJosh Poimboeuf 	for (pos = (head)->first; pos ; pos = pos->next)
709d1b39d41SJosh Poimboeuf 
710d1b39d41SJosh Poimboeuf #define hlist_for_each_safe(pos, n, head) \
711d1b39d41SJosh Poimboeuf 	for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
712d1b39d41SJosh Poimboeuf 	     pos = n)
713d1b39d41SJosh Poimboeuf 
714d1b39d41SJosh Poimboeuf #define hlist_entry_safe(ptr, type, member) \
715d1b39d41SJosh Poimboeuf 	({ typeof(ptr) ____ptr = (ptr); \
716d1b39d41SJosh Poimboeuf 	   ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
717d1b39d41SJosh Poimboeuf 	})
718d1b39d41SJosh Poimboeuf 
719d1b39d41SJosh Poimboeuf /**
720d1b39d41SJosh Poimboeuf  * hlist_for_each_entry	- iterate over list of given type
721d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
722d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
723d1b39d41SJosh Poimboeuf  * @member:	the name of the hlist_node within the struct.
724d1b39d41SJosh Poimboeuf  */
725d1b39d41SJosh Poimboeuf #define hlist_for_each_entry(pos, head, member)				\
726d1b39d41SJosh Poimboeuf 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
727d1b39d41SJosh Poimboeuf 	     pos;							\
728d1b39d41SJosh Poimboeuf 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
729d1b39d41SJosh Poimboeuf 
730d1b39d41SJosh Poimboeuf /**
731d1b39d41SJosh Poimboeuf  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
732d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
733d1b39d41SJosh Poimboeuf  * @member:	the name of the hlist_node within the struct.
734d1b39d41SJosh Poimboeuf  */
735d1b39d41SJosh Poimboeuf #define hlist_for_each_entry_continue(pos, member)			\
736d1b39d41SJosh Poimboeuf 	for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
737d1b39d41SJosh Poimboeuf 	     pos;							\
738d1b39d41SJosh Poimboeuf 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
739d1b39d41SJosh Poimboeuf 
740d1b39d41SJosh Poimboeuf /**
741d1b39d41SJosh Poimboeuf  * hlist_for_each_entry_from - iterate over a hlist continuing from current point
742d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
743d1b39d41SJosh Poimboeuf  * @member:	the name of the hlist_node within the struct.
744d1b39d41SJosh Poimboeuf  */
745d1b39d41SJosh Poimboeuf #define hlist_for_each_entry_from(pos, member)				\
746d1b39d41SJosh Poimboeuf 	for (; pos;							\
747d1b39d41SJosh Poimboeuf 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
748d1b39d41SJosh Poimboeuf 
749d1b39d41SJosh Poimboeuf /**
750d1b39d41SJosh Poimboeuf  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
751d1b39d41SJosh Poimboeuf  * @pos:	the type * to use as a loop cursor.
752d1b39d41SJosh Poimboeuf  * @n:		another &struct hlist_node to use as temporary storage
753d1b39d41SJosh Poimboeuf  * @head:	the head for your list.
754d1b39d41SJosh Poimboeuf  * @member:	the name of the hlist_node within the struct.
755d1b39d41SJosh Poimboeuf  */
756d1b39d41SJosh Poimboeuf #define hlist_for_each_entry_safe(pos, n, head, member) 		\
757d1b39d41SJosh Poimboeuf 	for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
758d1b39d41SJosh Poimboeuf 	     pos && ({ n = pos->member.next; 1; });			\
759d1b39d41SJosh Poimboeuf 	     pos = hlist_entry_safe(n, typeof(*pos), member))
760d1b39d41SJosh Poimboeuf 
7614fc62a89SWang Nan /**
7624fc62a89SWang Nan  * list_del_range - deletes range of entries from list.
7634fc62a89SWang Nan  * @begin: first element in the range to delete from the list.
7644fc62a89SWang Nan  * @end: last element in the range to delete from the list.
7654fc62a89SWang Nan  * Note: list_empty on the range of entries does not return true after this,
7664fc62a89SWang Nan  * the entries is in an undefined state.
7674fc62a89SWang Nan  */
list_del_range(struct list_head * begin,struct list_head * end)7684fc62a89SWang Nan static inline void list_del_range(struct list_head *begin,
7694fc62a89SWang Nan 				  struct list_head *end)
7704fc62a89SWang Nan {
7714fc62a89SWang Nan 	begin->prev->next = end->next;
7724fc62a89SWang Nan 	end->next->prev = begin->prev;
7734fc62a89SWang Nan }
7744fc62a89SWang Nan 
7754fc62a89SWang Nan /**
7764fc62a89SWang Nan  * list_for_each_from	-	iterate over a list from one of its nodes
7774fc62a89SWang Nan  * @pos:  the &struct list_head to use as a loop cursor, from where to start
7784fc62a89SWang Nan  * @head: the head for your list.
7794fc62a89SWang Nan  */
7804fc62a89SWang Nan #define list_for_each_from(pos, head) \
7814fc62a89SWang Nan 	for (; pos != (head); pos = pos->next)
782d1b39d41SJosh Poimboeuf 
783d1b39d41SJosh Poimboeuf #endif /* __TOOLS_LINUX_LIST_H */
784