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