1fbaf242cSMasahiro Yamada /* SPDX-License-Identifier: GPL-2.0 */
2fbaf242cSMasahiro Yamada #ifndef LIST_H
3fbaf242cSMasahiro Yamada #define LIST_H
4fbaf242cSMasahiro Yamada
5fbaf242cSMasahiro Yamada #include <stddef.h>
6fbaf242cSMasahiro Yamada
7fbaf242cSMasahiro Yamada #include "list_types.h"
8fbaf242cSMasahiro Yamada
9fbaf242cSMasahiro Yamada /* Are two types/vars the same type (ignoring qualifiers)? */
10fbaf242cSMasahiro Yamada #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
11fbaf242cSMasahiro Yamada
12fbaf242cSMasahiro Yamada /**
13fbaf242cSMasahiro Yamada * container_of - cast a member of a structure out to the containing structure
14fbaf242cSMasahiro Yamada * @ptr: the pointer to the member.
15fbaf242cSMasahiro Yamada * @type: the type of the container struct this is embedded in.
16fbaf242cSMasahiro Yamada * @member: the name of the member within the struct.
17fbaf242cSMasahiro Yamada *
18fbaf242cSMasahiro Yamada */
19fbaf242cSMasahiro Yamada #define container_of(ptr, type, member) ({ \
20fbaf242cSMasahiro Yamada void *__mptr = (void *)(ptr); \
21fbaf242cSMasahiro Yamada _Static_assert(__same_type(*(ptr), ((type *)0)->member) || \
22fbaf242cSMasahiro Yamada __same_type(*(ptr), void), \
23fbaf242cSMasahiro Yamada "pointer type mismatch in container_of()"); \
24fbaf242cSMasahiro Yamada ((type *)(__mptr - offsetof(type, member))); })
25fbaf242cSMasahiro Yamada
26fbaf242cSMasahiro Yamada #define LIST_POISON1 ((void *) 0x100)
27fbaf242cSMasahiro Yamada #define LIST_POISON2 ((void *) 0x122)
28fbaf242cSMasahiro Yamada
29fbaf242cSMasahiro Yamada /*
30fbaf242cSMasahiro Yamada * Circular doubly linked list implementation.
31fbaf242cSMasahiro Yamada *
32fbaf242cSMasahiro Yamada * Some of the internal functions ("__xxx") are useful when
33fbaf242cSMasahiro Yamada * manipulating whole lists rather than single entries, as
34fbaf242cSMasahiro Yamada * sometimes we already know the next/prev entries and we can
35fbaf242cSMasahiro Yamada * generate better code by using them directly rather than
36fbaf242cSMasahiro Yamada * using the generic single-entry routines.
37fbaf242cSMasahiro Yamada */
38fbaf242cSMasahiro Yamada
39fbaf242cSMasahiro Yamada #define LIST_HEAD_INIT(name) { &(name), &(name) }
40fbaf242cSMasahiro Yamada
41fbaf242cSMasahiro Yamada #define LIST_HEAD(name) \
42fbaf242cSMasahiro Yamada struct list_head name = LIST_HEAD_INIT(name)
43fbaf242cSMasahiro Yamada
44fbaf242cSMasahiro Yamada /**
45fbaf242cSMasahiro Yamada * INIT_LIST_HEAD - Initialize a list_head structure
46fbaf242cSMasahiro Yamada * @list: list_head structure to be initialized.
47fbaf242cSMasahiro Yamada *
48fbaf242cSMasahiro Yamada * Initializes the list_head to point to itself. If it is a list header,
49fbaf242cSMasahiro Yamada * the result is an empty list.
50fbaf242cSMasahiro Yamada */
INIT_LIST_HEAD(struct list_head * list)51fbaf242cSMasahiro Yamada static inline void INIT_LIST_HEAD(struct list_head *list)
52fbaf242cSMasahiro Yamada {
53fbaf242cSMasahiro Yamada list->next = list;
54fbaf242cSMasahiro Yamada list->prev = list;
55fbaf242cSMasahiro Yamada }
56fbaf242cSMasahiro Yamada
57fbaf242cSMasahiro Yamada /*
58fbaf242cSMasahiro Yamada * Insert a new entry between two known consecutive entries.
59fbaf242cSMasahiro Yamada *
60fbaf242cSMasahiro Yamada * This is only for internal list manipulation where we know
61fbaf242cSMasahiro Yamada * the prev/next entries already!
62fbaf242cSMasahiro Yamada */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)63fbaf242cSMasahiro Yamada static inline void __list_add(struct list_head *new,
64fbaf242cSMasahiro Yamada struct list_head *prev,
65fbaf242cSMasahiro Yamada struct list_head *next)
66fbaf242cSMasahiro Yamada {
67fbaf242cSMasahiro Yamada next->prev = new;
68fbaf242cSMasahiro Yamada new->next = next;
69fbaf242cSMasahiro Yamada new->prev = prev;
70fbaf242cSMasahiro Yamada prev->next = new;
71fbaf242cSMasahiro Yamada }
72fbaf242cSMasahiro Yamada
73fbaf242cSMasahiro Yamada /**
74fbaf242cSMasahiro Yamada * list_add - add a new entry
75fbaf242cSMasahiro Yamada * @new: new entry to be added
76fbaf242cSMasahiro Yamada * @head: list head to add it after
77fbaf242cSMasahiro Yamada *
78fbaf242cSMasahiro Yamada * Insert a new entry after the specified head.
79fbaf242cSMasahiro Yamada * This is good for implementing stacks.
80fbaf242cSMasahiro Yamada */
list_add(struct list_head * new,struct list_head * head)81fbaf242cSMasahiro Yamada static inline void list_add(struct list_head *new, struct list_head *head)
82fbaf242cSMasahiro Yamada {
83fbaf242cSMasahiro Yamada __list_add(new, head, head->next);
84fbaf242cSMasahiro Yamada }
85fbaf242cSMasahiro Yamada
86fbaf242cSMasahiro Yamada /**
87fbaf242cSMasahiro Yamada * list_add_tail - add a new entry
88fbaf242cSMasahiro Yamada * @new: new entry to be added
89fbaf242cSMasahiro Yamada * @head: list head to add it before
90fbaf242cSMasahiro Yamada *
91fbaf242cSMasahiro Yamada * Insert a new entry before the specified head.
92fbaf242cSMasahiro Yamada * This is useful for implementing queues.
93fbaf242cSMasahiro Yamada */
list_add_tail(struct list_head * new,struct list_head * head)94fbaf242cSMasahiro Yamada static inline void list_add_tail(struct list_head *new, struct list_head *head)
95fbaf242cSMasahiro Yamada {
96fbaf242cSMasahiro Yamada __list_add(new, head->prev, head);
97fbaf242cSMasahiro Yamada }
98fbaf242cSMasahiro Yamada
99fbaf242cSMasahiro Yamada /*
100fbaf242cSMasahiro Yamada * Delete a list entry by making the prev/next entries
101fbaf242cSMasahiro Yamada * point to each other.
102fbaf242cSMasahiro Yamada *
103fbaf242cSMasahiro Yamada * This is only for internal list manipulation where we know
104fbaf242cSMasahiro Yamada * the prev/next entries already!
105fbaf242cSMasahiro Yamada */
__list_del(struct list_head * prev,struct list_head * next)106fbaf242cSMasahiro Yamada static inline void __list_del(struct list_head *prev, struct list_head *next)
107fbaf242cSMasahiro Yamada {
108fbaf242cSMasahiro Yamada next->prev = prev;
109fbaf242cSMasahiro Yamada prev->next = next;
110fbaf242cSMasahiro Yamada }
111fbaf242cSMasahiro Yamada
__list_del_entry(struct list_head * entry)112fbaf242cSMasahiro Yamada static inline void __list_del_entry(struct list_head *entry)
113fbaf242cSMasahiro Yamada {
114fbaf242cSMasahiro Yamada __list_del(entry->prev, entry->next);
115fbaf242cSMasahiro Yamada }
116fbaf242cSMasahiro Yamada
117fbaf242cSMasahiro Yamada /**
118fbaf242cSMasahiro Yamada * list_del - deletes entry from list.
119fbaf242cSMasahiro Yamada * @entry: the element to delete from the list.
120fbaf242cSMasahiro Yamada * Note: list_empty() on entry does not return true after this, the entry is
121fbaf242cSMasahiro Yamada * in an undefined state.
122fbaf242cSMasahiro Yamada */
list_del(struct list_head * entry)123fbaf242cSMasahiro Yamada static inline void list_del(struct list_head *entry)
124fbaf242cSMasahiro Yamada {
125fbaf242cSMasahiro Yamada __list_del_entry(entry);
126fbaf242cSMasahiro Yamada entry->next = LIST_POISON1;
127fbaf242cSMasahiro Yamada entry->prev = LIST_POISON2;
128fbaf242cSMasahiro Yamada }
129fbaf242cSMasahiro Yamada
130fbaf242cSMasahiro Yamada /**
131*c14a3046SSami Tolvanen * list_replace - replace old entry by new one
132*c14a3046SSami Tolvanen * @old : the element to be replaced
133*c14a3046SSami Tolvanen * @new : the new element to insert
134*c14a3046SSami Tolvanen *
135*c14a3046SSami Tolvanen * If @old was empty, it will be overwritten.
136*c14a3046SSami Tolvanen */
list_replace(struct list_head * old,struct list_head * new)137*c14a3046SSami Tolvanen static inline void list_replace(struct list_head *old,
138*c14a3046SSami Tolvanen struct list_head *new)
139*c14a3046SSami Tolvanen {
140*c14a3046SSami Tolvanen new->next = old->next;
141*c14a3046SSami Tolvanen new->next->prev = new;
142*c14a3046SSami Tolvanen new->prev = old->prev;
143*c14a3046SSami Tolvanen new->prev->next = new;
144*c14a3046SSami Tolvanen }
145*c14a3046SSami Tolvanen
146*c14a3046SSami Tolvanen /**
147*c14a3046SSami Tolvanen * list_replace_init - replace old entry by new one and initialize the old one
148*c14a3046SSami Tolvanen * @old : the element to be replaced
149*c14a3046SSami Tolvanen * @new : the new element to insert
150*c14a3046SSami Tolvanen *
151*c14a3046SSami Tolvanen * If @old was empty, it will be overwritten.
152*c14a3046SSami Tolvanen */
list_replace_init(struct list_head * old,struct list_head * new)153*c14a3046SSami Tolvanen static inline void list_replace_init(struct list_head *old,
154*c14a3046SSami Tolvanen struct list_head *new)
155*c14a3046SSami Tolvanen {
156*c14a3046SSami Tolvanen list_replace(old, new);
157*c14a3046SSami Tolvanen INIT_LIST_HEAD(old);
158*c14a3046SSami Tolvanen }
159*c14a3046SSami Tolvanen
160*c14a3046SSami Tolvanen /**
161fbaf242cSMasahiro Yamada * list_move - delete from one list and add as another's head
162fbaf242cSMasahiro Yamada * @list: the entry to move
163fbaf242cSMasahiro Yamada * @head: the head that will precede our entry
164fbaf242cSMasahiro Yamada */
list_move(struct list_head * list,struct list_head * head)165fbaf242cSMasahiro Yamada static inline void list_move(struct list_head *list, struct list_head *head)
166fbaf242cSMasahiro Yamada {
167fbaf242cSMasahiro Yamada __list_del_entry(list);
168fbaf242cSMasahiro Yamada list_add(list, head);
169fbaf242cSMasahiro Yamada }
170fbaf242cSMasahiro Yamada
171fbaf242cSMasahiro Yamada /**
172fbaf242cSMasahiro Yamada * list_move_tail - delete from one list and add as another's tail
173fbaf242cSMasahiro Yamada * @list: the entry to move
174fbaf242cSMasahiro Yamada * @head: the head that will follow our entry
175fbaf242cSMasahiro Yamada */
list_move_tail(struct list_head * list,struct list_head * head)176fbaf242cSMasahiro Yamada static inline void list_move_tail(struct list_head *list,
177fbaf242cSMasahiro Yamada struct list_head *head)
178fbaf242cSMasahiro Yamada {
179fbaf242cSMasahiro Yamada __list_del_entry(list);
180fbaf242cSMasahiro Yamada list_add_tail(list, head);
181fbaf242cSMasahiro Yamada }
182fbaf242cSMasahiro Yamada
183fbaf242cSMasahiro Yamada /**
184*c14a3046SSami Tolvanen * list_is_first -- tests whether @list is the first entry in list @head
185*c14a3046SSami Tolvanen * @list: the entry to test
186*c14a3046SSami Tolvanen * @head: the head of the list
187*c14a3046SSami Tolvanen */
list_is_first(const struct list_head * list,const struct list_head * head)188*c14a3046SSami Tolvanen static inline int list_is_first(const struct list_head *list, const struct list_head *head)
189*c14a3046SSami Tolvanen {
190*c14a3046SSami Tolvanen return list->prev == head;
191*c14a3046SSami Tolvanen }
192*c14a3046SSami Tolvanen
193*c14a3046SSami Tolvanen /**
194*c14a3046SSami Tolvanen * list_is_last - tests whether @list is the last entry in list @head
195*c14a3046SSami Tolvanen * @list: the entry to test
196*c14a3046SSami Tolvanen * @head: the head of the list
197*c14a3046SSami Tolvanen */
list_is_last(const struct list_head * list,const struct list_head * head)198*c14a3046SSami Tolvanen static inline int list_is_last(const struct list_head *list, const struct list_head *head)
199*c14a3046SSami Tolvanen {
200*c14a3046SSami Tolvanen return list->next == head;
201*c14a3046SSami Tolvanen }
202*c14a3046SSami Tolvanen
203*c14a3046SSami Tolvanen /**
204fbaf242cSMasahiro Yamada * list_is_head - tests whether @list is the list @head
205fbaf242cSMasahiro Yamada * @list: the entry to test
206fbaf242cSMasahiro Yamada * @head: the head of the list
207fbaf242cSMasahiro Yamada */
list_is_head(const struct list_head * list,const struct list_head * head)208fbaf242cSMasahiro Yamada static inline int list_is_head(const struct list_head *list, const struct list_head *head)
209fbaf242cSMasahiro Yamada {
210fbaf242cSMasahiro Yamada return list == head;
211fbaf242cSMasahiro Yamada }
212fbaf242cSMasahiro Yamada
213fbaf242cSMasahiro Yamada /**
214fbaf242cSMasahiro Yamada * list_empty - tests whether a list is empty
215fbaf242cSMasahiro Yamada * @head: the list to test.
216fbaf242cSMasahiro Yamada */
list_empty(const struct list_head * head)217fbaf242cSMasahiro Yamada static inline int list_empty(const struct list_head *head)
218fbaf242cSMasahiro Yamada {
219fbaf242cSMasahiro Yamada return head->next == head;
220fbaf242cSMasahiro Yamada }
221fbaf242cSMasahiro Yamada
222fbaf242cSMasahiro Yamada /**
223fbaf242cSMasahiro Yamada * list_entry - get the struct for this entry
224fbaf242cSMasahiro Yamada * @ptr: the &struct list_head pointer.
225fbaf242cSMasahiro Yamada * @type: the type of the struct this is embedded in.
226fbaf242cSMasahiro Yamada * @member: the name of the list_head within the struct.
227fbaf242cSMasahiro Yamada */
228fbaf242cSMasahiro Yamada #define list_entry(ptr, type, member) \
229fbaf242cSMasahiro Yamada container_of(ptr, type, member)
230fbaf242cSMasahiro Yamada
231fbaf242cSMasahiro Yamada /**
232fbaf242cSMasahiro Yamada * list_first_entry - get the first element from a list
233fbaf242cSMasahiro Yamada * @ptr: the list head to take the element from.
234fbaf242cSMasahiro Yamada * @type: the type of the struct this is embedded in.
235fbaf242cSMasahiro Yamada * @member: the name of the list_head within the struct.
236fbaf242cSMasahiro Yamada *
237fbaf242cSMasahiro Yamada * Note, that list is expected to be not empty.
238fbaf242cSMasahiro Yamada */
239fbaf242cSMasahiro Yamada #define list_first_entry(ptr, type, member) \
240fbaf242cSMasahiro Yamada list_entry((ptr)->next, type, member)
241fbaf242cSMasahiro Yamada
242fbaf242cSMasahiro Yamada /**
243fbaf242cSMasahiro Yamada * list_last_entry - get the last element from a list
244fbaf242cSMasahiro Yamada * @ptr: the list head to take the element from.
245fbaf242cSMasahiro Yamada * @type: the type of the struct this is embedded in.
246fbaf242cSMasahiro Yamada * @member: the name of the list_head within the struct.
247fbaf242cSMasahiro Yamada *
248fbaf242cSMasahiro Yamada * Note, that list is expected to be not empty.
249fbaf242cSMasahiro Yamada */
250fbaf242cSMasahiro Yamada #define list_last_entry(ptr, type, member) \
251fbaf242cSMasahiro Yamada list_entry((ptr)->prev, type, member)
252fbaf242cSMasahiro Yamada
253fbaf242cSMasahiro Yamada /**
254fbaf242cSMasahiro Yamada * list_next_entry - get the next element in list
255fbaf242cSMasahiro Yamada * @pos: the type * to cursor
256fbaf242cSMasahiro Yamada * @member: the name of the list_head within the struct.
257fbaf242cSMasahiro Yamada */
258fbaf242cSMasahiro Yamada #define list_next_entry(pos, member) \
259fbaf242cSMasahiro Yamada list_entry((pos)->member.next, typeof(*(pos)), member)
260fbaf242cSMasahiro Yamada
261fbaf242cSMasahiro Yamada /**
262fbaf242cSMasahiro Yamada * list_prev_entry - get the prev element in list
263fbaf242cSMasahiro Yamada * @pos: the type * to cursor
264fbaf242cSMasahiro Yamada * @member: the name of the list_head within the struct.
265fbaf242cSMasahiro Yamada */
266fbaf242cSMasahiro Yamada #define list_prev_entry(pos, member) \
267fbaf242cSMasahiro Yamada list_entry((pos)->member.prev, typeof(*(pos)), member)
268fbaf242cSMasahiro Yamada
269fbaf242cSMasahiro Yamada /**
270fbaf242cSMasahiro Yamada * list_entry_is_head - test if the entry points to the head of the list
271fbaf242cSMasahiro Yamada * @pos: the type * to cursor
272fbaf242cSMasahiro Yamada * @head: the head for your list.
273fbaf242cSMasahiro Yamada * @member: the name of the list_head within the struct.
274fbaf242cSMasahiro Yamada */
275fbaf242cSMasahiro Yamada #define list_entry_is_head(pos, head, member) \
276fbaf242cSMasahiro Yamada (&pos->member == (head))
277fbaf242cSMasahiro Yamada
278fbaf242cSMasahiro Yamada /**
279fbaf242cSMasahiro Yamada * list_for_each_entry - iterate over list of given type
280fbaf242cSMasahiro Yamada * @pos: the type * to use as a loop cursor.
281fbaf242cSMasahiro Yamada * @head: the head for your list.
282fbaf242cSMasahiro Yamada * @member: the name of the list_head within the struct.
283fbaf242cSMasahiro Yamada */
284fbaf242cSMasahiro Yamada #define list_for_each_entry(pos, head, member) \
285fbaf242cSMasahiro Yamada for (pos = list_first_entry(head, typeof(*pos), member); \
286fbaf242cSMasahiro Yamada !list_entry_is_head(pos, head, member); \
287fbaf242cSMasahiro Yamada pos = list_next_entry(pos, member))
288fbaf242cSMasahiro Yamada
289fbaf242cSMasahiro Yamada /**
290fbaf242cSMasahiro Yamada * list_for_each_entry_reverse - iterate backwards over list of given type.
291fbaf242cSMasahiro Yamada * @pos: the type * to use as a loop cursor.
292fbaf242cSMasahiro Yamada * @head: the head for your list.
293fbaf242cSMasahiro Yamada * @member: the name of the list_head within the struct.
294fbaf242cSMasahiro Yamada */
295fbaf242cSMasahiro Yamada #define list_for_each_entry_reverse(pos, head, member) \
296fbaf242cSMasahiro Yamada for (pos = list_last_entry(head, typeof(*pos), member); \
297fbaf242cSMasahiro Yamada !list_entry_is_head(pos, head, member); \
298fbaf242cSMasahiro Yamada pos = list_prev_entry(pos, member))
299fbaf242cSMasahiro Yamada
300fbaf242cSMasahiro Yamada /**
301fbaf242cSMasahiro Yamada * list_for_each_entry_safe - iterate over list of given type. Safe against removal of list entry
302fbaf242cSMasahiro Yamada * @pos: the type * to use as a loop cursor.
303fbaf242cSMasahiro Yamada * @n: another type * to use as temporary storage
304fbaf242cSMasahiro Yamada * @head: the head for your list.
305fbaf242cSMasahiro Yamada * @member: the name of the list_head within the struct.
306fbaf242cSMasahiro Yamada */
307fbaf242cSMasahiro Yamada #define list_for_each_entry_safe(pos, n, head, member) \
308fbaf242cSMasahiro Yamada for (pos = list_first_entry(head, typeof(*pos), member), \
309fbaf242cSMasahiro Yamada n = list_next_entry(pos, member); \
310fbaf242cSMasahiro Yamada !list_entry_is_head(pos, head, member); \
311fbaf242cSMasahiro Yamada pos = n, n = list_next_entry(n, member))
312fbaf242cSMasahiro Yamada
313fbaf242cSMasahiro Yamada /*
314fbaf242cSMasahiro Yamada * Double linked lists with a single pointer list head.
315fbaf242cSMasahiro Yamada * Mostly useful for hash tables where the two pointer list head is
316fbaf242cSMasahiro Yamada * too wasteful.
317fbaf242cSMasahiro Yamada * You lose the ability to access the tail in O(1).
318fbaf242cSMasahiro Yamada */
319fbaf242cSMasahiro Yamada
320fbaf242cSMasahiro Yamada #define HLIST_HEAD_INIT { .first = NULL }
32116ff3f60SMasahiro Yamada #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
INIT_HLIST_NODE(struct hlist_node * h)32216ff3f60SMasahiro Yamada static inline void INIT_HLIST_NODE(struct hlist_node *h)
32316ff3f60SMasahiro Yamada {
32416ff3f60SMasahiro Yamada h->next = NULL;
32516ff3f60SMasahiro Yamada h->pprev = NULL;
32616ff3f60SMasahiro Yamada }
32716ff3f60SMasahiro Yamada
32816ff3f60SMasahiro Yamada /**
32916ff3f60SMasahiro Yamada * hlist_unhashed - Has node been removed from list and reinitialized?
33016ff3f60SMasahiro Yamada * @h: Node to be checked
33116ff3f60SMasahiro Yamada *
33216ff3f60SMasahiro Yamada * Not that not all removal functions will leave a node in unhashed
33316ff3f60SMasahiro Yamada * state. For example, hlist_nulls_del_init_rcu() does leave the
33416ff3f60SMasahiro Yamada * node in unhashed state, but hlist_nulls_del() does not.
33516ff3f60SMasahiro Yamada */
hlist_unhashed(const struct hlist_node * h)33616ff3f60SMasahiro Yamada static inline int hlist_unhashed(const struct hlist_node *h)
33716ff3f60SMasahiro Yamada {
33816ff3f60SMasahiro Yamada return !h->pprev;
33916ff3f60SMasahiro Yamada }
34016ff3f60SMasahiro Yamada
__hlist_del(struct hlist_node * n)34116ff3f60SMasahiro Yamada static inline void __hlist_del(struct hlist_node *n)
34216ff3f60SMasahiro Yamada {
34316ff3f60SMasahiro Yamada struct hlist_node *next = n->next;
34416ff3f60SMasahiro Yamada struct hlist_node **pprev = n->pprev;
34516ff3f60SMasahiro Yamada
34616ff3f60SMasahiro Yamada *pprev = next;
34716ff3f60SMasahiro Yamada if (next)
34816ff3f60SMasahiro Yamada next->pprev = pprev;
34916ff3f60SMasahiro Yamada }
35016ff3f60SMasahiro Yamada
35116ff3f60SMasahiro Yamada /**
35216ff3f60SMasahiro Yamada * hlist_del - Delete the specified hlist_node from its list
35316ff3f60SMasahiro Yamada * @n: Node to delete.
35416ff3f60SMasahiro Yamada *
35516ff3f60SMasahiro Yamada * Note that this function leaves the node in hashed state. Use
35616ff3f60SMasahiro Yamada * hlist_del_init() or similar instead to unhash @n.
35716ff3f60SMasahiro Yamada */
hlist_del(struct hlist_node * n)35816ff3f60SMasahiro Yamada static inline void hlist_del(struct hlist_node *n)
35916ff3f60SMasahiro Yamada {
36016ff3f60SMasahiro Yamada __hlist_del(n);
36116ff3f60SMasahiro Yamada n->next = LIST_POISON1;
36216ff3f60SMasahiro Yamada n->pprev = LIST_POISON2;
36316ff3f60SMasahiro Yamada }
36416ff3f60SMasahiro Yamada
36516ff3f60SMasahiro Yamada /**
36616ff3f60SMasahiro Yamada * hlist_del_init - Delete the specified hlist_node from its list and initialize
36716ff3f60SMasahiro Yamada * @n: Node to delete.
36816ff3f60SMasahiro Yamada *
36916ff3f60SMasahiro Yamada * Note that this function leaves the node in unhashed state.
37016ff3f60SMasahiro Yamada */
hlist_del_init(struct hlist_node * n)37116ff3f60SMasahiro Yamada static inline void hlist_del_init(struct hlist_node *n)
37216ff3f60SMasahiro Yamada {
37316ff3f60SMasahiro Yamada if (!hlist_unhashed(n)) {
37416ff3f60SMasahiro Yamada __hlist_del(n);
37516ff3f60SMasahiro Yamada INIT_HLIST_NODE(n);
37616ff3f60SMasahiro Yamada }
37716ff3f60SMasahiro Yamada }
378fbaf242cSMasahiro Yamada
379fbaf242cSMasahiro Yamada /**
380fbaf242cSMasahiro Yamada * hlist_add_head - add a new entry at the beginning of the hlist
381fbaf242cSMasahiro Yamada * @n: new entry to be added
382fbaf242cSMasahiro Yamada * @h: hlist head to add it after
383fbaf242cSMasahiro Yamada *
384fbaf242cSMasahiro Yamada * Insert a new entry after the specified head.
385fbaf242cSMasahiro Yamada * This is good for implementing stacks.
386fbaf242cSMasahiro Yamada */
hlist_add_head(struct hlist_node * n,struct hlist_head * h)387fbaf242cSMasahiro Yamada static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
388fbaf242cSMasahiro Yamada {
389fbaf242cSMasahiro Yamada struct hlist_node *first = h->first;
390fbaf242cSMasahiro Yamada
391fbaf242cSMasahiro Yamada n->next = first;
392fbaf242cSMasahiro Yamada if (first)
393fbaf242cSMasahiro Yamada first->pprev = &n->next;
394fbaf242cSMasahiro Yamada h->first = n;
395fbaf242cSMasahiro Yamada n->pprev = &h->first;
396fbaf242cSMasahiro Yamada }
397fbaf242cSMasahiro Yamada
398fbaf242cSMasahiro Yamada #define hlist_entry(ptr, type, member) container_of(ptr, type, member)
399fbaf242cSMasahiro Yamada
400fbaf242cSMasahiro Yamada #define hlist_entry_safe(ptr, type, member) \
401fbaf242cSMasahiro Yamada ({ typeof(ptr) ____ptr = (ptr); \
402fbaf242cSMasahiro Yamada ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
403fbaf242cSMasahiro Yamada })
404fbaf242cSMasahiro Yamada
405fbaf242cSMasahiro Yamada /**
406fbaf242cSMasahiro Yamada * hlist_for_each_entry - iterate over list of given type
407fbaf242cSMasahiro Yamada * @pos: the type * to use as a loop cursor.
408fbaf242cSMasahiro Yamada * @head: the head for your list.
409fbaf242cSMasahiro Yamada * @member: the name of the hlist_node within the struct.
410fbaf242cSMasahiro Yamada */
411fbaf242cSMasahiro Yamada #define hlist_for_each_entry(pos, head, member) \
412fbaf242cSMasahiro Yamada for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
413fbaf242cSMasahiro Yamada pos; \
414fbaf242cSMasahiro Yamada pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
415fbaf242cSMasahiro Yamada
41616ff3f60SMasahiro Yamada /**
41716ff3f60SMasahiro Yamada * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
41816ff3f60SMasahiro Yamada * @pos: the type * to use as a loop cursor.
41916ff3f60SMasahiro Yamada * @n: a &struct hlist_node to use as temporary storage
42016ff3f60SMasahiro Yamada * @head: the head for your list.
42116ff3f60SMasahiro Yamada * @member: the name of the hlist_node within the struct.
42216ff3f60SMasahiro Yamada */
42316ff3f60SMasahiro Yamada #define hlist_for_each_entry_safe(pos, n, head, member) \
42416ff3f60SMasahiro Yamada for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
42516ff3f60SMasahiro Yamada pos && ({ n = pos->member.next; 1; }); \
42616ff3f60SMasahiro Yamada pos = hlist_entry_safe(n, typeof(*pos), member))
42716ff3f60SMasahiro Yamada
428fbaf242cSMasahiro Yamada #endif /* LIST_H */
429