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