1 /* SPDX-License-Identifier: GPL-2.0-or-later */
7 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
10 #define container_of(ptr, type, member) ({ \
11 const typeof(((type *)0)->member) * __mptr = (ptr); \
12 (type *)((char *)__mptr - offsetof(type, member)); \
16 * These are non-NULL pointers that will result in page faults
17 * under normal circumstances, used to verify that nobody uses
18 * non-initialized list entries.
20 #define LIST_POISON1 ((void *) 0x00100100)
21 #define LIST_POISON2 ((void *) 0x00200200)
24 * Simple doubly linked list implementation.
26 * Some of the internal functions ("__xxx") are useful when
27 * manipulating whole lists rather than single entries, as
28 * sometimes we already know the next/prev entries and we can
29 * generate better code by using them directly rather than
30 * using the generic single-entry routines.
33 struct list_head *next, *prev;
36 #define LIST_HEAD_INIT(name) { &(name), &(name) }
38 #define LIST_HEAD(name) \
39 struct list_head name = LIST_HEAD_INIT(name)
41 #define INIT_LIST_HEAD(ptr) do { \
42 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
46 * Insert a new entry between two known consecutive entries.
48 * This is only for internal list manipulation where we know
49 * the prev/next entries already!
51 static inline void __list_add(struct list_head *new,
52 struct list_head *prev,
53 struct list_head *next)
62 * list_add - add a new entry
63 * @new: new entry to be added
64 * @head: list head to add it after
66 * Insert a new entry after the specified head.
67 * This is good for implementing stacks.
69 static inline void list_add(struct list_head *new, struct list_head *head)
71 __list_add(new, head, head->next);
75 * list_add_tail - add a new entry
76 * @new: new entry to be added
77 * @head: list head to add it before
79 * Insert a new entry before the specified head.
80 * This is useful for implementing queues.
82 static inline void list_add_tail(struct list_head *new, struct list_head *head)
84 __list_add(new, head->prev, head);
89 * Delete a list entry by making the prev/next entries
90 * point to each other.
92 * This is only for internal list manipulation where we know
93 * the prev/next entries already!
95 static inline void __list_del(struct list_head *prev, struct list_head *next)
102 * list_del - deletes entry from list.
103 * @entry: the element to delete from the list.
104 * Note: list_empty on entry does not return true after this, the entry is
105 * in an undefined state.
107 static inline void list_del(struct list_head *entry)
109 __list_del(entry->prev, entry->next);
110 entry->next = LIST_POISON1;
111 entry->prev = LIST_POISON2;
117 * list_del_init - deletes entry from list and reinitialize it.
118 * @entry: the element to delete from the list.
120 static inline void list_del_init(struct list_head *entry)
122 __list_del(entry->prev, entry->next);
123 INIT_LIST_HEAD(entry);
127 * list_move - delete from one list and add as another's head
128 * @list: the entry to move
129 * @head: the head that will precede our entry
131 static inline void list_move(struct list_head *list, struct list_head *head)
133 __list_del(list->prev, list->next);
134 list_add(list, head);
138 * list_move_tail - delete from one list and add as another's tail
139 * @list: the entry to move
140 * @head: the head that will follow our entry
142 static inline void list_move_tail(struct list_head *list,
143 struct list_head *head)
145 __list_del(list->prev, list->next);
146 list_add_tail(list, head);
150 * list_empty - tests whether a list is empty
151 * @head: the list to test.
153 static inline int list_empty(const struct list_head *head)
155 return head->next == head;
158 static inline void __list_splice(struct list_head *list,
159 struct list_head *head)
161 struct list_head *first = list->next;
162 struct list_head *last = list->prev;
163 struct list_head *at = head->next;
173 * list_splice - join two lists
174 * @list: the new list to add.
175 * @head: the place to add it in the first list.
177 static inline void list_splice(struct list_head *list, struct list_head *head)
179 if (!list_empty(list))
180 __list_splice(list, head);
184 * list_splice_init - join two lists and reinitialise the emptied list.
185 * @list: the new list to add.
186 * @head: the place to add it in the first list.
188 * The list at @list is reinitialised
190 static inline void list_splice_init(struct list_head *list,
191 struct list_head *head)
193 if (!list_empty(list)) {
194 __list_splice(list, head);
195 INIT_LIST_HEAD(list);
200 * list_entry - get the struct for this entry
201 * @ptr: the &struct list_head pointer.
202 * @type: the type of the struct this is embedded in.
203 * @member: the name of the list_struct within the struct.
205 #define list_entry(ptr, type, member) \
206 container_of(ptr, type, member)
209 * list_for_each - iterate over a list
210 * @pos: the &struct list_head to use as a loop counter.
211 * @head: the head for your list.
214 #define list_for_each(pos, head) \
215 for (pos = (head)->next; pos != (head); \
219 * __list_for_each - iterate over a list
220 * @pos: the &struct list_head to use as a loop counter.
221 * @head: the head for your list.
223 * This variant differs from list_for_each() in that it's the
224 * simplest possible list iteration code, no prefetching is done.
225 * Use this for code that knows the list to be very short (empty
226 * or 1 entry) most of the time.
228 #define __list_for_each(pos, head) \
229 for (pos = (head)->next; pos != (head); pos = pos->next)
232 * list_for_each_prev - iterate over a list backwards
233 * @pos: the &struct list_head to use as a loop counter.
234 * @head: the head for your list.
236 #define list_for_each_prev(pos, head) \
237 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
241 * list_for_each_safe - iterate over a list safe against removal of list entry
242 * @pos: the &struct list_head to use as a loop counter.
243 * @n: another &struct list_head to use as temporary storage
244 * @head: the head for your list.
246 #define list_for_each_safe(pos, n, head) \
247 for (pos = (head)->next, n = pos->next; pos != (head); \
248 pos = n, n = pos->next)
251 * list_for_each_entry - iterate over list of given type
252 * @pos: the type * to use as a loop counter.
253 * @head: the head for your list.
254 * @member: the name of the list_struct within the struct.
256 #define list_for_each_entry(pos, head, member) \
257 for (pos = list_entry((head)->next, typeof(*pos), member); \
258 &pos->member != (head); \
259 pos = list_entry(pos->member.next, typeof(*pos), member))
262 * list_for_each_entry_reverse - iterate backwards over list of given type.
263 * @pos: the type * to use as a loop counter.
264 * @head: the head for your list.
265 * @member: the name of the list_struct within the struct.
267 #define list_for_each_entry_reverse(pos, head, member) \
268 for (pos = list_entry((head)->prev, typeof(*pos), member); \
269 &pos->member != (head); \
270 pos = list_entry(pos->member.prev, typeof(*pos), member))
273 * list_prepare_entry - prepare a pos entry for use as a start point in
274 * list_for_each_entry_continue
275 * @pos: the type * to use as a start point
276 * @head: the head of the list
277 * @member: the name of the list_struct within the struct.
279 #define list_prepare_entry(pos, head, member) \
280 ((pos) ? : list_entry(head, typeof(*pos), member))
283 * list_for_each_entry_continue - iterate over list of given type
284 * continuing after existing point
285 * @pos: the type * to use as a loop counter.
286 * @head: the head for your list.
287 * @member: the name of the list_struct within the struct.
289 #define list_for_each_entry_continue(pos, head, member) \
290 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
291 &pos->member != (head); \
292 pos = list_entry(pos->member.next, typeof(*pos), member))
295 * list_for_each_entry_safe - iterate over list of given type safe against
296 * removal of list entry
297 * @pos: the type * to use as a loop counter.
298 * @n: another type * to use as temporary storage
299 * @head: the head for your list.
300 * @member: the name of the list_struct within the struct.
302 #define list_for_each_entry_safe(pos, n, head, member) \
303 for (pos = list_entry((head)->next, typeof(*pos), member), \
304 n = list_entry(pos->member.next, typeof(*pos), member); \
305 &pos->member != (head); \
306 pos = n, n = list_entry(n->member.next, typeof(*n), member))
309 * list_for_each_entry_safe_continue - iterate over list of given type
310 * continuing after existing point safe against removal of list entry
311 * @pos: the type * to use as a loop counter.
312 * @n: another type * to use as temporary storage
313 * @head: the head for your list.
314 * @member: the name of the list_struct within the struct.
316 #define list_for_each_entry_safe_continue(pos, n, head, member) \
317 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
318 n = list_entry(pos->member.next, typeof(*pos), member); \
319 &pos->member != (head); \
320 pos = n, n = list_entry(n->member.next, typeof(*n), member))
323 * list_for_each_entry_safe_reverse - iterate backwards over list of given
324 * type safe against removal of list entry
325 * @pos: the type * to use as a loop counter.
326 * @n: another type * to use as temporary storage
327 * @head: the head for your list.
328 * @member: the name of the list_struct within the struct.
330 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
331 for (pos = list_entry((head)->prev, typeof(*pos), member), \
332 n = list_entry(pos->member.prev, typeof(*pos), member); \
333 &pos->member != (head); \
334 pos = n, n = list_entry(n->member.prev, typeof(*n), member))