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