xref: /rk3399_rockchip-uboot/include/linux/list.h (revision ef0255fc75f28655f9681422079287d68a14dbaa)
1700a0c64SWolfgang Denk #ifndef _LINUX_LIST_H
2700a0c64SWolfgang Denk #define _LINUX_LIST_H
3700a0c64SWolfgang Denk 
4*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #include <linux/stddef.h>
5*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #include <linux/poison.h>
6*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
7700a0c64SWolfgang Denk #ifndef ARCH_HAS_PREFETCH
8700a0c64SWolfgang Denk #define ARCH_HAS_PREFETCH
9700a0c64SWolfgang Denk static inline void prefetch(const void *x) {;}
10700a0c64SWolfgang Denk #endif
11700a0c64SWolfgang Denk 
12700a0c64SWolfgang Denk /*
13700a0c64SWolfgang Denk  * Simple doubly linked list implementation.
14700a0c64SWolfgang Denk  *
15700a0c64SWolfgang Denk  * Some of the internal functions ("__xxx") are useful when
16700a0c64SWolfgang Denk  * manipulating whole lists rather than single entries, as
17700a0c64SWolfgang Denk  * sometimes we already know the next/prev entries and we can
18700a0c64SWolfgang Denk  * generate better code by using them directly rather than
19700a0c64SWolfgang Denk  * using the generic single-entry routines.
20700a0c64SWolfgang Denk  */
21700a0c64SWolfgang Denk 
22700a0c64SWolfgang Denk struct list_head {
23700a0c64SWolfgang Denk 	struct list_head *next, *prev;
24700a0c64SWolfgang Denk };
25700a0c64SWolfgang Denk 
26700a0c64SWolfgang Denk #define LIST_HEAD_INIT(name) { &(name), &(name) }
27700a0c64SWolfgang Denk 
28700a0c64SWolfgang Denk #define LIST_HEAD(name) \
29700a0c64SWolfgang Denk 	struct list_head name = LIST_HEAD_INIT(name)
30700a0c64SWolfgang Denk 
31*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void INIT_LIST_HEAD(struct list_head *list)
32*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
33*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	list->next = list;
34*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	list->prev = list;
35*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
36700a0c64SWolfgang Denk 
37700a0c64SWolfgang Denk /*
38700a0c64SWolfgang Denk  * Insert a new entry between two known consecutive entries.
39700a0c64SWolfgang Denk  *
40700a0c64SWolfgang Denk  * This is only for internal list manipulation where we know
41700a0c64SWolfgang Denk  * the prev/next entries already!
42700a0c64SWolfgang Denk  */
43700a0c64SWolfgang Denk static inline void __list_add(struct list_head *new,
44700a0c64SWolfgang Denk 			      struct list_head *prev,
45700a0c64SWolfgang Denk 			      struct list_head *next)
46700a0c64SWolfgang Denk {
47700a0c64SWolfgang Denk 	next->prev = new;
48700a0c64SWolfgang Denk 	new->next = next;
49700a0c64SWolfgang Denk 	new->prev = prev;
50700a0c64SWolfgang Denk 	prev->next = new;
51700a0c64SWolfgang Denk }
52700a0c64SWolfgang Denk 
53700a0c64SWolfgang Denk /**
54700a0c64SWolfgang Denk  * list_add - add a new entry
55700a0c64SWolfgang Denk  * @new: new entry to be added
56700a0c64SWolfgang Denk  * @head: list head to add it after
57700a0c64SWolfgang Denk  *
58700a0c64SWolfgang Denk  * Insert a new entry after the specified head.
59700a0c64SWolfgang Denk  * This is good for implementing stacks.
60700a0c64SWolfgang Denk  */
61700a0c64SWolfgang Denk static inline void list_add(struct list_head *new, struct list_head *head)
62700a0c64SWolfgang Denk {
63700a0c64SWolfgang Denk 	__list_add(new, head, head->next);
64700a0c64SWolfgang Denk }
65700a0c64SWolfgang Denk 
66700a0c64SWolfgang Denk /**
67700a0c64SWolfgang Denk  * list_add_tail - add a new entry
68700a0c64SWolfgang Denk  * @new: new entry to be added
69700a0c64SWolfgang Denk  * @head: list head to add it before
70700a0c64SWolfgang Denk  *
71700a0c64SWolfgang Denk  * Insert a new entry before the specified head.
72700a0c64SWolfgang Denk  * This is useful for implementing queues.
73700a0c64SWolfgang Denk  */
74700a0c64SWolfgang Denk static inline void list_add_tail(struct list_head *new, struct list_head *head)
75700a0c64SWolfgang Denk {
76700a0c64SWolfgang Denk 	__list_add(new, head->prev, head);
77700a0c64SWolfgang Denk }
78700a0c64SWolfgang Denk 
79700a0c64SWolfgang Denk /*
80700a0c64SWolfgang Denk  * Delete a list entry by making the prev/next entries
81700a0c64SWolfgang Denk  * point to each other.
82700a0c64SWolfgang Denk  *
83700a0c64SWolfgang Denk  * This is only for internal list manipulation where we know
84700a0c64SWolfgang Denk  * the prev/next entries already!
85700a0c64SWolfgang Denk  */
86700a0c64SWolfgang Denk static inline void __list_del(struct list_head *prev, struct list_head *next)
87700a0c64SWolfgang Denk {
88700a0c64SWolfgang Denk 	next->prev = prev;
89700a0c64SWolfgang Denk 	prev->next = next;
90700a0c64SWolfgang Denk }
91700a0c64SWolfgang Denk 
92700a0c64SWolfgang Denk /**
93700a0c64SWolfgang Denk  * list_del - deletes entry from list.
94700a0c64SWolfgang Denk  * @entry: the element to delete from the list.
95*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Note: list_empty() on entry does not return true after this, the entry is
96*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * in an undefined state.
97700a0c64SWolfgang Denk  */
98700a0c64SWolfgang Denk static inline void list_del(struct list_head *entry)
99700a0c64SWolfgang Denk {
100700a0c64SWolfgang Denk 	__list_del(entry->prev, entry->next);
101*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	entry->next = LIST_POISON1;
102*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	entry->prev = LIST_POISON2;
103*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
104*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
105*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
106*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_replace - replace old entry by new one
107*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @old : the element to be replaced
108*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @new : the new element to insert
109*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
110*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * If @old was empty, it will be overwritten.
111*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
112*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_replace(struct list_head *old,
113*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 				struct list_head *new)
114*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
115*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	new->next = old->next;
116*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	new->next->prev = new;
117*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	new->prev = old->prev;
118*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	new->prev->next = new;
119*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
120*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
121*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_replace_init(struct list_head *old,
122*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 					struct list_head *new)
123*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
124*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	list_replace(old, new);
125*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	INIT_LIST_HEAD(old);
126700a0c64SWolfgang Denk }
127700a0c64SWolfgang Denk 
128700a0c64SWolfgang Denk /**
129700a0c64SWolfgang Denk  * list_del_init - deletes entry from list and reinitialize it.
130700a0c64SWolfgang Denk  * @entry: the element to delete from the list.
131700a0c64SWolfgang Denk  */
132700a0c64SWolfgang Denk static inline void list_del_init(struct list_head *entry)
133700a0c64SWolfgang Denk {
134700a0c64SWolfgang Denk 	__list_del(entry->prev, entry->next);
135700a0c64SWolfgang Denk 	INIT_LIST_HEAD(entry);
136700a0c64SWolfgang Denk }
137700a0c64SWolfgang Denk 
138700a0c64SWolfgang Denk /**
139700a0c64SWolfgang Denk  * list_move - delete from one list and add as another's head
140700a0c64SWolfgang Denk  * @list: the entry to move
141700a0c64SWolfgang Denk  * @head: the head that will precede our entry
142700a0c64SWolfgang Denk  */
143700a0c64SWolfgang Denk static inline void list_move(struct list_head *list, struct list_head *head)
144700a0c64SWolfgang Denk {
145700a0c64SWolfgang Denk 	__list_del(list->prev, list->next);
146700a0c64SWolfgang Denk 	list_add(list, head);
147700a0c64SWolfgang Denk }
148700a0c64SWolfgang Denk 
149700a0c64SWolfgang Denk /**
150700a0c64SWolfgang Denk  * list_move_tail - delete from one list and add as another's tail
151700a0c64SWolfgang Denk  * @list: the entry to move
152700a0c64SWolfgang Denk  * @head: the head that will follow our entry
153700a0c64SWolfgang Denk  */
154700a0c64SWolfgang Denk static inline void list_move_tail(struct list_head *list,
155700a0c64SWolfgang Denk 				  struct list_head *head)
156700a0c64SWolfgang Denk {
157700a0c64SWolfgang Denk 	__list_del(list->prev, list->next);
158700a0c64SWolfgang Denk 	list_add_tail(list, head);
159700a0c64SWolfgang Denk }
160700a0c64SWolfgang Denk 
161700a0c64SWolfgang Denk /**
162*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_is_last - tests whether @list is the last entry in list @head
163*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @list: the entry to test
164*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head: the head of the list
165*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
166*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int list_is_last(const struct list_head *list,
167*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 				const struct list_head *head)
168*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
169*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	return list->next == head;
170*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
171*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
172*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
173700a0c64SWolfgang Denk  * list_empty - tests whether a list is empty
174700a0c64SWolfgang Denk  * @head: the list to test.
175700a0c64SWolfgang Denk  */
176*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int list_empty(const struct list_head *head)
177700a0c64SWolfgang Denk {
178700a0c64SWolfgang Denk 	return head->next == head;
179700a0c64SWolfgang Denk }
180700a0c64SWolfgang Denk 
181*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
182*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_empty_careful - tests whether a list is empty and not being modified
183*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head: the list to test
184*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
185*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Description:
186*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * tests whether a list is empty _and_ checks that no other CPU might be
187*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * in the process of modifying either member (next or prev)
188*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
189*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * NOTE: using list_empty_careful() without synchronization
190*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * can only be safe if the only activity that can happen
191*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * to the list entry is list_del_init(). Eg. it cannot be used
192*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * if another CPU could re-list_add() it.
193*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
194*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int list_empty_careful(const struct list_head *head)
195700a0c64SWolfgang Denk {
196*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	struct list_head *next = head->next;
197*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	return (next == head) && (next == head->prev);
198700a0c64SWolfgang Denk }
199700a0c64SWolfgang Denk 
200700a0c64SWolfgang Denk /**
201*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_is_singular - tests whether a list has just one entry.
202*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head: the list to test.
203*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
204*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int list_is_singular(const struct list_head *head)
205*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
206*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	return !list_empty(head) && (head->next == head->prev);
207*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
208*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
209*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void __list_cut_position(struct list_head *list,
210*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		struct list_head *head, struct list_head *entry)
211*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
212*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	struct list_head *new_first = entry->next;
213*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	list->next = head->next;
214*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	list->next->prev = list;
215*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	list->prev = entry;
216*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	entry->next = list;
217*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	head->next = new_first;
218*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	new_first->prev = head;
219*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
220*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
221*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
222*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_cut_position - cut a list into two
223*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @list: a new list to add all removed entries
224*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head: a list with entries
225*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @entry: an entry within head, could be the head itself
226*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *	and if so we won't cut the list
227*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
228*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * This helper moves the initial part of @head, up to and
229*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * including @entry, from @head to @list. You should
230*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * pass on @entry an element you know is on @head. @list
231*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * should be an empty list or a list you do not care about
232*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * losing its data.
233*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
234*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
235*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_cut_position(struct list_head *list,
236*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		struct list_head *head, struct list_head *entry)
237*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
238*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	if (list_empty(head))
239*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		return;
240*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	if (list_is_singular(head) &&
241*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		(head->next != entry && head != entry))
242*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		return;
243*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	if (entry == head)
244*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		INIT_LIST_HEAD(list);
245*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	else
246*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		__list_cut_position(list, head, entry);
247*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
248*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
249*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void __list_splice(const struct list_head *list,
250*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 				 struct list_head *prev,
251*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 				 struct list_head *next)
252*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
253*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	struct list_head *first = list->next;
254*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	struct list_head *last = list->prev;
255*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
256*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	first->prev = prev;
257*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	prev->next = first;
258*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
259*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	last->next = next;
260*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	next->prev = last;
261*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
262*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
263*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
264*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_splice - join two lists, this is designed for stacks
265700a0c64SWolfgang Denk  * @list: the new list to add.
266700a0c64SWolfgang Denk  * @head: the place to add it in the first list.
267700a0c64SWolfgang Denk  */
268*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_splice(const struct list_head *list,
269*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 				struct list_head *head)
270700a0c64SWolfgang Denk {
271700a0c64SWolfgang Denk 	if (!list_empty(list))
272*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		__list_splice(list, head, head->next);
273*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
274*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
275*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
276*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_splice_tail - join two lists, each list being a queue
277*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @list: the new list to add.
278*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head: the place to add it in the first list.
279*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
280*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_splice_tail(struct list_head *list,
281*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 				struct list_head *head)
282*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
283*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	if (!list_empty(list))
284*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		__list_splice(list, head->prev, head);
285700a0c64SWolfgang Denk }
286700a0c64SWolfgang Denk 
287700a0c64SWolfgang Denk /**
288700a0c64SWolfgang Denk  * list_splice_init - join two lists and reinitialise the emptied list.
289700a0c64SWolfgang Denk  * @list: the new list to add.
290700a0c64SWolfgang Denk  * @head: the place to add it in the first list.
291700a0c64SWolfgang Denk  *
292700a0c64SWolfgang Denk  * The list at @list is reinitialised
293700a0c64SWolfgang Denk  */
294700a0c64SWolfgang Denk static inline void list_splice_init(struct list_head *list,
295700a0c64SWolfgang Denk 				    struct list_head *head)
296700a0c64SWolfgang Denk {
297700a0c64SWolfgang Denk 	if (!list_empty(list)) {
298*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		__list_splice(list, head, head->next);
299*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		INIT_LIST_HEAD(list);
300*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	}
301*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
302*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
303*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
304*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_splice_tail_init - join two lists and reinitialise the emptied list
305*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @list: the new list to add.
306*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head: the place to add it in the first list.
307*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
308*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Each of the lists is a queue.
309*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * The list at @list is reinitialised
310*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
311*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_splice_tail_init(struct list_head *list,
312*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 					 struct list_head *head)
313*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
314*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	if (!list_empty(list)) {
315*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		__list_splice(list, head->prev, head);
316700a0c64SWolfgang Denk 		INIT_LIST_HEAD(list);
317700a0c64SWolfgang Denk 	}
318700a0c64SWolfgang Denk }
319700a0c64SWolfgang Denk 
320700a0c64SWolfgang Denk /**
321700a0c64SWolfgang Denk  * list_entry - get the struct for this entry
322700a0c64SWolfgang Denk  * @ptr:	the &struct list_head pointer.
323700a0c64SWolfgang Denk  * @type:	the type of the struct this is embedded in.
324700a0c64SWolfgang Denk  * @member:	the name of the list_struct within the struct.
325700a0c64SWolfgang Denk  */
326700a0c64SWolfgang Denk #define list_entry(ptr, type, member) \
327*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	container_of(ptr, type, member)
328*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
329*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
330*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_first_entry - get the first element from a list
331*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @ptr:	the list head to take the element from.
332*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @type:	the type of the struct this is embedded in.
333*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the list_struct within the struct.
334*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
335*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Note, that list is expected to be not empty.
336*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
337*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_first_entry(ptr, type, member) \
338*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	list_entry((ptr)->next, type, member)
339700a0c64SWolfgang Denk 
340700a0c64SWolfgang Denk /**
341700a0c64SWolfgang Denk  * list_for_each	-	iterate over a list
342*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the &struct list_head to use as a loop cursor.
343700a0c64SWolfgang Denk  * @head:	the head for your list.
344700a0c64SWolfgang Denk  */
345700a0c64SWolfgang Denk #define list_for_each(pos, head) \
346*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = (head)->next; prefetch(pos->next), pos != (head); \
347*ef0255fcSJean-Christophe PLAGNIOL-VILLARD         	pos = pos->next)
348*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
349*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
350*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * __list_for_each	-	iterate over a list
351*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the &struct list_head to use as a loop cursor.
352*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head:	the head for your list.
353*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
354*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * This variant differs from list_for_each() in that it's the
355*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * simplest possible list iteration code, no prefetching is done.
356*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Use this for code that knows the list to be very short (empty
357*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * or 1 entry) most of the time.
358*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
359*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define __list_for_each(pos, head) \
360*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = (head)->next; pos != (head); pos = pos->next)
361*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
362700a0c64SWolfgang Denk /**
363700a0c64SWolfgang Denk  * list_for_each_prev	-	iterate over a list backwards
364*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the &struct list_head to use as a loop cursor.
365700a0c64SWolfgang Denk  * @head:	the head for your list.
366700a0c64SWolfgang Denk  */
367700a0c64SWolfgang Denk #define list_for_each_prev(pos, head) \
368*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
369*ef0255fcSJean-Christophe PLAGNIOL-VILLARD         	pos = pos->prev)
370700a0c64SWolfgang Denk 
371700a0c64SWolfgang Denk /**
372700a0c64SWolfgang Denk  * list_for_each_safe - iterate over a list safe against removal of list entry
373*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the &struct list_head to use as a loop cursor.
374700a0c64SWolfgang Denk  * @n:		another &struct list_head to use as temporary storage
375700a0c64SWolfgang Denk  * @head:	the head for your list.
376700a0c64SWolfgang Denk  */
377700a0c64SWolfgang Denk #define list_for_each_safe(pos, n, head) \
378700a0c64SWolfgang Denk 	for (pos = (head)->next, n = pos->next; pos != (head); \
379700a0c64SWolfgang Denk 		pos = n, n = pos->next)
380700a0c64SWolfgang Denk 
381700a0c64SWolfgang Denk /**
382*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
383*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the &struct list_head to use as a loop cursor.
384*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @n:		another &struct list_head to use as temporary storage
385*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head:	the head for your list.
386*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
387*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_prev_safe(pos, n, head) \
388*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = (head)->prev, n = pos->prev; \
389*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     prefetch(pos->prev), pos != (head); \
390*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = n, n = pos->prev)
391*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
392*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
393700a0c64SWolfgang Denk  * list_for_each_entry	-	iterate over list of given type
394*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the type * to use as a loop cursor.
395700a0c64SWolfgang Denk  * @head:	the head for your list.
396700a0c64SWolfgang Denk  * @member:	the name of the list_struct within the struct.
397700a0c64SWolfgang Denk  */
398700a0c64SWolfgang Denk #define list_for_each_entry(pos, head, member)				\
399*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
400*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     prefetch(pos->member.next), &pos->member != (head); 	\
401*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = list_entry(pos->member.next, typeof(*pos), member))
402*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
403*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
404*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_for_each_entry_reverse - iterate backwards over list of given type.
405*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the type * to use as a loop cursor.
406*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head:	the head for your list.
407*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the list_struct within the struct.
408*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
409*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_reverse(pos, head, member)			\
410*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
411*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     prefetch(pos->member.prev), &pos->member != (head); 	\
412*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = list_entry(pos->member.prev, typeof(*pos), member))
413*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
414*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
415*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
416*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the type * to use as a start point
417*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head:	the head of the list
418*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the list_struct within the struct.
419*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
420*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
421*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
422*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_prepare_entry(pos, head, member) \
423*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	((pos) ? : list_entry(head, typeof(*pos), member))
424*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
425*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
426*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_for_each_entry_continue - continue iteration over list of given type
427*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the type * to use as a loop cursor.
428*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head:	the head for your list.
429*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the list_struct within the struct.
430*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
431*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Continue to iterate over list of given type, continuing after
432*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * the current position.
433*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
434*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_continue(pos, head, member) 		\
435*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
436*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     prefetch(pos->member.next), &pos->member != (head);	\
437*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = list_entry(pos->member.next, typeof(*pos), member))
438*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
439*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
440*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_for_each_entry_continue_reverse - iterate backwards from the given point
441*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the type * to use as a loop cursor.
442*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head:	the head for your list.
443*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the list_struct within the struct.
444*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
445*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Start to iterate over list of given type backwards, continuing after
446*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * the current position.
447*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
448*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_continue_reverse(pos, head, member)		\
449*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = list_entry(pos->member.prev, typeof(*pos), member);	\
450*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     prefetch(pos->member.prev), &pos->member != (head);	\
451*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = list_entry(pos->member.prev, typeof(*pos), member))
452*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
453*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
454*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_for_each_entry_from - iterate over list of given type from the current point
455*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the type * to use as a loop cursor.
456*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head:	the head for your list.
457*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the list_struct within the struct.
458*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
459*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Iterate over list of given type, continuing from current position.
460*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
461*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_from(pos, head, member) 			\
462*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (; prefetch(pos->member.next), &pos->member != (head);	\
463*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = list_entry(pos->member.next, typeof(*pos), member))
464700a0c64SWolfgang Denk 
465700a0c64SWolfgang Denk /**
466700a0c64SWolfgang Denk  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
467*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the type * to use as a loop cursor.
468700a0c64SWolfgang Denk  * @n:		another type * to use as temporary storage
469700a0c64SWolfgang Denk  * @head:	the head for your list.
470700a0c64SWolfgang Denk  * @member:	the name of the list_struct within the struct.
471700a0c64SWolfgang Denk  */
472700a0c64SWolfgang Denk #define list_for_each_entry_safe(pos, n, head, member)			\
473700a0c64SWolfgang Denk 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
474700a0c64SWolfgang Denk 		n = list_entry(pos->member.next, typeof(*pos), member);	\
475700a0c64SWolfgang Denk 	     &pos->member != (head); 					\
476700a0c64SWolfgang Denk 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
477700a0c64SWolfgang Denk 
478700a0c64SWolfgang Denk /**
479*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_for_each_entry_safe_continue
480*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the type * to use as a loop cursor.
481*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @n:		another type * to use as temporary storage
482700a0c64SWolfgang Denk  * @head:	the head for your list.
483700a0c64SWolfgang Denk  * @member:	the name of the list_struct within the struct.
484*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
485*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Iterate over list of given type, continuing after current point,
486*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * safe against removal of list entry.
487700a0c64SWolfgang Denk  */
488*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_safe_continue(pos, n, head, member) 		\
489700a0c64SWolfgang Denk 	for (pos = list_entry(pos->member.next, typeof(*pos), member), 		\
490*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		n = list_entry(pos->member.next, typeof(*pos), member);		\
491700a0c64SWolfgang Denk 	     &pos->member != (head);						\
492*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
493*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
494*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
495*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_for_each_entry_safe_from
496*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the type * to use as a loop cursor.
497*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @n:		another type * to use as temporary storage
498*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head:	the head for your list.
499*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the list_struct within the struct.
500*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
501*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Iterate over list of given type from current point, safe against
502*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * removal of list entry.
503*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
504*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_safe_from(pos, n, head, member) 			\
505*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (n = list_entry(pos->member.next, typeof(*pos), member);		\
506*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     &pos->member != (head);						\
507*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
508*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
509*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
510*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * list_for_each_entry_safe_reverse
511*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the type * to use as a loop cursor.
512*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @n:		another type * to use as temporary storage
513*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head:	the head for your list.
514*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the list_struct within the struct.
515*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  *
516*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Iterate backwards over list of given type, safe against removal
517*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * of list entry.
518*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
519*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_safe_reverse(pos, n, head, member)		\
520*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
521*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		n = list_entry(pos->member.prev, typeof(*pos), member);	\
522*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     &pos->member != (head); 					\
523*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = n, n = list_entry(n->member.prev, typeof(*n), member))
524*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
525*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /*
526*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Double linked lists with a single pointer list head.
527*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * Mostly useful for hash tables where the two pointer list head is
528*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * too wasteful.
529*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * You lose the ability to access the tail in O(1).
530*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
531*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
532*ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct hlist_head {
533*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	struct hlist_node *first;
534*ef0255fcSJean-Christophe PLAGNIOL-VILLARD };
535*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
536*ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct hlist_node {
537*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	struct hlist_node *next, **pprev;
538*ef0255fcSJean-Christophe PLAGNIOL-VILLARD };
539*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
540*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define HLIST_HEAD_INIT { .first = NULL }
541*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
542*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
543*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void INIT_HLIST_NODE(struct hlist_node *h)
544*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
545*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	h->next = NULL;
546*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	h->pprev = NULL;
547*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
548*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
549*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int hlist_unhashed(const struct hlist_node *h)
550*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
551*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	return !h->pprev;
552*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
553*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
554*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int hlist_empty(const struct hlist_head *h)
555*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
556*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	return !h->first;
557*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
558*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
559*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void __hlist_del(struct hlist_node *n)
560*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
561*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	struct hlist_node *next = n->next;
562*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	struct hlist_node **pprev = n->pprev;
563*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	*pprev = next;
564*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	if (next)
565*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		next->pprev = pprev;
566*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
567*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
568*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void hlist_del(struct hlist_node *n)
569*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
570*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	__hlist_del(n);
571*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	n->next = LIST_POISON1;
572*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	n->pprev = LIST_POISON2;
573*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
574*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
575*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void hlist_del_init(struct hlist_node *n)
576*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
577*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	if (!hlist_unhashed(n)) {
578*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		__hlist_del(n);
579*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		INIT_HLIST_NODE(n);
580*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	}
581*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
582*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
583*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
584*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
585*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	struct hlist_node *first = h->first;
586*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	n->next = first;
587*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	if (first)
588*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		first->pprev = &n->next;
589*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	h->first = n;
590*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	n->pprev = &h->first;
591*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
592*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
593*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /* next must be != NULL */
594*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void hlist_add_before(struct hlist_node *n,
595*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 					struct hlist_node *next)
596*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
597*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	n->pprev = next->pprev;
598*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	n->next = next;
599*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	next->pprev = &n->next;
600*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	*(n->pprev) = n;
601*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
602*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
603*ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void hlist_add_after(struct hlist_node *n,
604*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 					struct hlist_node *next)
605*ef0255fcSJean-Christophe PLAGNIOL-VILLARD {
606*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	next->next = n->next;
607*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	n->next = next;
608*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	next->pprev = &n->next;
609*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
610*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	if(next->next)
611*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		next->next->pprev  = &next->next;
612*ef0255fcSJean-Christophe PLAGNIOL-VILLARD }
613*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
614*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
615*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
616*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each(pos, head) \
617*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
618*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = pos->next)
619*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
620*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each_safe(pos, n, head) \
621*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
622*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = n)
623*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
624*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
625*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * hlist_for_each_entry	- iterate over list of given type
626*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @tpos:	the type * to use as a loop cursor.
627*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the &struct hlist_node to use as a loop cursor.
628*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head:	the head for your list.
629*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the hlist_node within the struct.
630*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
631*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each_entry(tpos, pos, head, member)			 \
632*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = (head)->first;					 \
633*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos && ({ prefetch(pos->next); 1;}) &&			 \
634*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
635*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = pos->next)
636*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
637*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
638*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
639*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @tpos:	the type * to use as a loop cursor.
640*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the &struct hlist_node to use as a loop cursor.
641*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the hlist_node within the struct.
642*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
643*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each_entry_continue(tpos, pos, member)		 \
644*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = (pos)->next;						 \
645*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos && ({ prefetch(pos->next); 1;}) &&			 \
646*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
647*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = pos->next)
648*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
649*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
650*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * hlist_for_each_entry_from - iterate over a hlist continuing from current point
651*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @tpos:	the type * to use as a loop cursor.
652*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the &struct hlist_node to use as a loop cursor.
653*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the hlist_node within the struct.
654*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
655*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each_entry_from(tpos, pos, member)			 \
656*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (; pos && ({ prefetch(pos->next); 1;}) &&			 \
657*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
658*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = pos->next)
659*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 
660*ef0255fcSJean-Christophe PLAGNIOL-VILLARD /**
661*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
662*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @tpos:	the type * to use as a loop cursor.
663*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @pos:	the &struct hlist_node to use as a loop cursor.
664*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @n:		another &struct hlist_node to use as temporary storage
665*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @head:	the head for your list.
666*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  * @member:	the name of the hlist_node within the struct.
667*ef0255fcSJean-Christophe PLAGNIOL-VILLARD  */
668*ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each_entry_safe(tpos, pos, n, head, member) 		 \
669*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	for (pos = (head)->first;					 \
670*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos && ({ n = pos->next; 1; }) && 				 \
671*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
672*ef0255fcSJean-Christophe PLAGNIOL-VILLARD 	     pos = n)
673700a0c64SWolfgang Denk 
674700a0c64SWolfgang Denk #endif
675