blob: 0f9d939b05f295fb6baf7d3efdae92b04ece2312 [file] [log] [blame]
Wolfgang Denk47f57792005-08-08 01:03:24 +02001#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +02004#include <linux/stddef.h>
5#include <linux/poison.h>
6
Wolfgang Denk47f57792005-08-08 01:03:24 +02007#ifndef ARCH_HAS_PREFETCH
8#define ARCH_HAS_PREFETCH
9static inline void prefetch(const void *x) {;}
10#endif
11
12/*
13 * Simple doubly linked list implementation.
14 *
15 * Some of the internal functions ("__xxx") are useful when
16 * manipulating whole lists rather than single entries, as
17 * sometimes we already know the next/prev entries and we can
18 * generate better code by using them directly rather than
19 * using the generic single-entry routines.
20 */
21
22struct list_head {
23 struct list_head *next, *prev;
24};
25
26#define LIST_HEAD_INIT(name) { &(name), &(name) }
27
28#define LIST_HEAD(name) \
29 struct list_head name = LIST_HEAD_INIT(name)
30
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +020031static inline void INIT_LIST_HEAD(struct list_head *list)
32{
33 list->next = list;
34 list->prev = list;
35}
Wolfgang Denk47f57792005-08-08 01:03:24 +020036
37/*
Wolfgang Denkeb95c852005-08-10 15:14:32 +020038 * Insert a new entry between two known consecutive entries.
Wolfgang Denk47f57792005-08-08 01:03:24 +020039 *
40 * This is only for internal list manipulation where we know
41 * the prev/next entries already!
42 */
43static inline void __list_add(struct list_head *new,
44 struct list_head *prev,
45 struct list_head *next)
46{
47 next->prev = new;
48 new->next = next;
49 new->prev = prev;
50 prev->next = new;
51}
52
53/**
54 * list_add - add a new entry
55 * @new: new entry to be added
56 * @head: list head to add it after
57 *
58 * Insert a new entry after the specified head.
59 * This is good for implementing stacks.
60 */
61static inline void list_add(struct list_head *new, struct list_head *head)
62{
63 __list_add(new, head, head->next);
64}
65
66/**
67 * list_add_tail - add a new entry
68 * @new: new entry to be added
69 * @head: list head to add it before
70 *
71 * Insert a new entry before the specified head.
72 * This is useful for implementing queues.
73 */
74static inline void list_add_tail(struct list_head *new, struct list_head *head)
75{
76 __list_add(new, head->prev, head);
77}
78
79/*
80 * Delete a list entry by making the prev/next entries
81 * point to each other.
82 *
83 * This is only for internal list manipulation where we know
84 * the prev/next entries already!
85 */
86static inline void __list_del(struct list_head *prev, struct list_head *next)
87{
88 next->prev = prev;
89 prev->next = next;
90}
91
92/**
93 * list_del - deletes entry from list.
94 * @entry: the element to delete from the list.
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +020095 * Note: list_empty() on entry does not return true after this, the entry is
96 * in an undefined state.
Wolfgang Denk47f57792005-08-08 01:03:24 +020097 */
98static inline void list_del(struct list_head *entry)
99{
100 __list_del(entry->prev, entry->next);
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200101 entry->next = LIST_POISON1;
102 entry->prev = LIST_POISON2;
103}
104
105/**
106 * list_replace - replace old entry by new one
107 * @old : the element to be replaced
108 * @new : the new element to insert
109 *
110 * If @old was empty, it will be overwritten.
111 */
112static inline void list_replace(struct list_head *old,
113 struct list_head *new)
114{
115 new->next = old->next;
116 new->next->prev = new;
117 new->prev = old->prev;
118 new->prev->next = new;
119}
120
121static inline void list_replace_init(struct list_head *old,
122 struct list_head *new)
123{
124 list_replace(old, new);
125 INIT_LIST_HEAD(old);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200126}
127
128/**
129 * list_del_init - deletes entry from list and reinitialize it.
130 * @entry: the element to delete from the list.
131 */
132static inline void list_del_init(struct list_head *entry)
133{
134 __list_del(entry->prev, entry->next);
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200135 INIT_LIST_HEAD(entry);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200136}
137
138/**
139 * list_move - delete from one list and add as another's head
140 * @list: the entry to move
141 * @head: the head that will precede our entry
142 */
143static inline void list_move(struct list_head *list, struct list_head *head)
144{
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200145 __list_del(list->prev, list->next);
146 list_add(list, head);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200147}
148
149/**
150 * list_move_tail - delete from one list and add as another's tail
151 * @list: the entry to move
152 * @head: the head that will follow our entry
153 */
154static inline void list_move_tail(struct list_head *list,
155 struct list_head *head)
156{
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200157 __list_del(list->prev, list->next);
158 list_add_tail(list, head);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200159}
160
161/**
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200162 * list_is_last - tests whether @list is the last entry in list @head
163 * @list: the entry to test
164 * @head: the head of the list
165 */
166static inline int list_is_last(const struct list_head *list,
167 const struct list_head *head)
168{
169 return list->next == head;
170}
171
172/**
Wolfgang Denk47f57792005-08-08 01:03:24 +0200173 * list_empty - tests whether a list is empty
174 * @head: the list to test.
175 */
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200176static inline int list_empty(const struct list_head *head)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200177{
178 return head->next == head;
179}
180
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200181/**
182 * list_empty_careful - tests whether a list is empty and not being modified
183 * @head: the list to test
184 *
185 * Description:
186 * tests whether a list is empty _and_ checks that no other CPU might be
187 * in the process of modifying either member (next or prev)
188 *
189 * NOTE: using list_empty_careful() without synchronization
190 * can only be safe if the only activity that can happen
191 * to the list entry is list_del_init(). Eg. it cannot be used
192 * if another CPU could re-list_add() it.
193 */
194static inline int list_empty_careful(const struct list_head *head)
195{
196 struct list_head *next = head->next;
197 return (next == head) && (next == head->prev);
198}
199
200/**
201 * list_is_singular - tests whether a list has just one entry.
202 * @head: the list to test.
203 */
204static inline int list_is_singular(const struct list_head *head)
205{
206 return !list_empty(head) && (head->next == head->prev);
207}
208
209static inline void __list_cut_position(struct list_head *list,
210 struct list_head *head, struct list_head *entry)
211{
212 struct list_head *new_first = entry->next;
213 list->next = head->next;
214 list->next->prev = list;
215 list->prev = entry;
216 entry->next = list;
217 head->next = new_first;
218 new_first->prev = head;
219}
220
221/**
222 * list_cut_position - cut a list into two
223 * @list: a new list to add all removed entries
224 * @head: a list with entries
225 * @entry: an entry within head, could be the head itself
226 * and if so we won't cut the list
227 *
228 * This helper moves the initial part of @head, up to and
229 * including @entry, from @head to @list. You should
230 * pass on @entry an element you know is on @head. @list
231 * should be an empty list or a list you do not care about
232 * losing its data.
233 *
234 */
235static inline void list_cut_position(struct list_head *list,
236 struct list_head *head, struct list_head *entry)
237{
238 if (list_empty(head))
239 return;
240 if (list_is_singular(head) &&
241 (head->next != entry && head != entry))
242 return;
243 if (entry == head)
244 INIT_LIST_HEAD(list);
245 else
246 __list_cut_position(list, head, entry);
247}
248
249static inline void __list_splice(const struct list_head *list,
250 struct list_head *prev,
251 struct list_head *next)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200252{
253 struct list_head *first = list->next;
254 struct list_head *last = list->prev;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200255
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200256 first->prev = prev;
257 prev->next = first;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200258
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200259 last->next = next;
260 next->prev = last;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200261}
262
263/**
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200264 * list_splice - join two lists, this is designed for stacks
Wolfgang Denk47f57792005-08-08 01:03:24 +0200265 * @list: the new list to add.
266 * @head: the place to add it in the first list.
267 */
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200268static inline void list_splice(const struct list_head *list,
269 struct list_head *head)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200270{
271 if (!list_empty(list))
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200272 __list_splice(list, head, head->next);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200273}
274
275/**
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200276 * list_splice_tail - join two lists, each list being a queue
277 * @list: the new list to add.
278 * @head: the place to add it in the first list.
279 */
280static inline void list_splice_tail(struct list_head *list,
281 struct list_head *head)
282{
283 if (!list_empty(list))
284 __list_splice(list, head->prev, head);
285}
286
287/**
Wolfgang Denk47f57792005-08-08 01:03:24 +0200288 * list_splice_init - join two lists and reinitialise the emptied list.
289 * @list: the new list to add.
290 * @head: the place to add it in the first list.
291 *
292 * The list at @list is reinitialised
293 */
294static inline void list_splice_init(struct list_head *list,
295 struct list_head *head)
296{
297 if (!list_empty(list)) {
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200298 __list_splice(list, head, head->next);
299 INIT_LIST_HEAD(list);
300 }
301}
302
303/**
304 * list_splice_tail_init - join two lists and reinitialise the emptied list
305 * @list: the new list to add.
306 * @head: the place to add it in the first list.
307 *
308 * Each of the lists is a queue.
309 * The list at @list is reinitialised
310 */
311static inline void list_splice_tail_init(struct list_head *list,
312 struct list_head *head)
313{
314 if (!list_empty(list)) {
315 __list_splice(list, head->prev, head);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200316 INIT_LIST_HEAD(list);
317 }
318}
319
320/**
321 * list_entry - get the struct for this entry
322 * @ptr: the &struct list_head pointer.
323 * @type: the type of the struct this is embedded in.
324 * @member: the name of the list_struct within the struct.
325 */
326#define list_entry(ptr, type, member) \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200327 container_of(ptr, type, member)
328
329/**
330 * list_first_entry - get the first element from a list
331 * @ptr: the list head to take the element from.
332 * @type: the type of the struct this is embedded in.
333 * @member: the name of the list_struct within the struct.
334 *
335 * Note, that list is expected to be not empty.
336 */
337#define list_first_entry(ptr, type, member) \
338 list_entry((ptr)->next, type, member)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200339
340/**
Simon Glass7ab0c2d2016-10-05 20:42:12 -0600341 * list_last_entry - get the last element from a list
342 * @ptr: the list head to take the element from.
343 * @type: the type of the struct this is embedded in.
344 * @member: the name of the list_struct within the struct.
345 *
346 * Note, that list is expected to be not empty.
347 */
348#define list_last_entry(ptr, type, member) \
349 list_entry((ptr)->prev, type, member)
350
351/**
Vignesh Raghavendra0e3ec362019-10-01 17:26:29 +0530352 * list_first_entry_or_null - get the first element from a list
353 * @ptr: the list head to take the element from.
354 * @type: the type of the struct this is embedded in.
355 * @member: the name of the list_head within the struct.
356 *
357 * Note that if the list is empty, it returns NULL.
358 */
359#define list_first_entry_or_null(ptr, type, member) ({ \
360 struct list_head *head__ = (ptr); \
361 struct list_head *pos__ = READ_ONCE(head__->next); \
362 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
363})
364
365/**
Wolfgang Denk47f57792005-08-08 01:03:24 +0200366 * list_for_each - iterate over a list
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200367 * @pos: the &struct list_head to use as a loop cursor.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200368 * @head: the head for your list.
369 */
370#define list_for_each(pos, head) \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200371 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200372 pos = pos->next)
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200373
374/**
375 * __list_for_each - iterate over a list
376 * @pos: the &struct list_head to use as a loop cursor.
377 * @head: the head for your list.
378 *
379 * This variant differs from list_for_each() in that it's the
380 * simplest possible list iteration code, no prefetching is done.
381 * Use this for code that knows the list to be very short (empty
382 * or 1 entry) most of the time.
383 */
384#define __list_for_each(pos, head) \
385 for (pos = (head)->next; pos != (head); pos = pos->next)
386
Wolfgang Denk47f57792005-08-08 01:03:24 +0200387/**
388 * list_for_each_prev - iterate over a list backwards
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200389 * @pos: the &struct list_head to use as a loop cursor.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200390 * @head: the head for your list.
391 */
392#define list_for_each_prev(pos, head) \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200393 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200394 pos = pos->prev)
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200395
Wolfgang Denk47f57792005-08-08 01:03:24 +0200396/**
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200397 * list_for_each_safe - iterate over a list safe against removal of list entry
398 * @pos: the &struct list_head to use as a loop cursor.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200399 * @n: another &struct list_head to use as temporary storage
400 * @head: the head for your list.
401 */
402#define list_for_each_safe(pos, n, head) \
403 for (pos = (head)->next, n = pos->next; pos != (head); \
404 pos = n, n = pos->next)
405
406/**
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200407 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
408 * @pos: the &struct list_head to use as a loop cursor.
409 * @n: another &struct list_head to use as temporary storage
410 * @head: the head for your list.
411 */
412#define list_for_each_prev_safe(pos, n, head) \
413 for (pos = (head)->prev, n = pos->prev; \
414 prefetch(pos->prev), pos != (head); \
415 pos = n, n = pos->prev)
416
417/**
Wolfgang Denk47f57792005-08-08 01:03:24 +0200418 * list_for_each_entry - iterate over list of given type
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200419 * @pos: the type * to use as a loop cursor.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200420 * @head: the head for your list.
421 * @member: the name of the list_struct within the struct.
422 */
423#define list_for_each_entry(pos, head, member) \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200424 for (pos = list_entry((head)->next, typeof(*pos), member); \
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200425 prefetch(pos->member.next), &pos->member != (head); \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200426 pos = list_entry(pos->member.next, typeof(*pos), member))
427
428/**
429 * list_for_each_entry_reverse - iterate backwards over list of given type.
430 * @pos: the type * to use as a loop cursor.
431 * @head: the head for your list.
432 * @member: the name of the list_struct within the struct.
433 */
434#define list_for_each_entry_reverse(pos, head, member) \
435 for (pos = list_entry((head)->prev, typeof(*pos), member); \
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200436 prefetch(pos->member.prev), &pos->member != (head); \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200437 pos = list_entry(pos->member.prev, typeof(*pos), member))
438
439/**
440 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
441 * @pos: the type * to use as a start point
442 * @head: the head of the list
443 * @member: the name of the list_struct within the struct.
444 *
445 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
446 */
447#define list_prepare_entry(pos, head, member) \
448 ((pos) ? : list_entry(head, typeof(*pos), member))
Wolfgang Denk47f57792005-08-08 01:03:24 +0200449
450/**
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200451 * list_for_each_entry_continue - continue iteration over list of given type
452 * @pos: the type * to use as a loop cursor.
453 * @head: the head for your list.
454 * @member: the name of the list_struct within the struct.
455 *
456 * Continue to iterate over list of given type, continuing after
457 * the current position.
458 */
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200459#define list_for_each_entry_continue(pos, head, member) \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200460 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
461 prefetch(pos->member.next), &pos->member != (head); \
462 pos = list_entry(pos->member.next, typeof(*pos), member))
463
464/**
465 * list_for_each_entry_continue_reverse - iterate backwards from the given point
466 * @pos: the type * to use as a loop cursor.
467 * @head: the head for your list.
468 * @member: the name of the list_struct within the struct.
469 *
470 * Start to iterate over list of given type backwards, continuing after
471 * the current position.
472 */
473#define list_for_each_entry_continue_reverse(pos, head, member) \
474 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
475 prefetch(pos->member.prev), &pos->member != (head); \
476 pos = list_entry(pos->member.prev, typeof(*pos), member))
477
478/**
479 * list_for_each_entry_from - iterate over list of given type from the current point
480 * @pos: the type * to use as a loop cursor.
481 * @head: the head for your list.
482 * @member: the name of the list_struct within the struct.
483 *
484 * Iterate over list of given type, continuing from current position.
485 */
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200486#define list_for_each_entry_from(pos, head, member) \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200487 for (; prefetch(pos->member.next), &pos->member != (head); \
488 pos = list_entry(pos->member.next, typeof(*pos), member))
489
490/**
Wolfgang Denk47f57792005-08-08 01:03:24 +0200491 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200492 * @pos: the type * to use as a loop cursor.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200493 * @n: another type * to use as temporary storage
494 * @head: the head for your list.
495 * @member: the name of the list_struct within the struct.
496 */
497#define list_for_each_entry_safe(pos, n, head, member) \
498 for (pos = list_entry((head)->next, typeof(*pos), member), \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200499 n = list_entry(pos->member.next, typeof(*pos), member); \
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200500 &pos->member != (head); \
Wolfgang Denk47f57792005-08-08 01:03:24 +0200501 pos = n, n = list_entry(n->member.next, typeof(*n), member))
502
503/**
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200504 * list_for_each_entry_safe_continue
505 * @pos: the type * to use as a loop cursor.
506 * @n: another type * to use as temporary storage
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200507 * @head: the head for your list.
508 * @member: the name of the list_struct within the struct.
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200509 *
510 * Iterate over list of given type, continuing after current point,
511 * safe against removal of list entry.
512 */
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200513#define list_for_each_entry_safe_continue(pos, n, head, member) \
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200514 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200515 n = list_entry(pos->member.next, typeof(*pos), member); \
516 &pos->member != (head); \
517 pos = n, n = list_entry(n->member.next, typeof(*n), member))
518
519/**
520 * list_for_each_entry_safe_from
521 * @pos: the type * to use as a loop cursor.
522 * @n: another type * to use as temporary storage
523 * @head: the head for your list.
524 * @member: the name of the list_struct within the struct.
525 *
526 * Iterate over list of given type from current point, safe against
527 * removal of list entry.
528 */
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200529#define list_for_each_entry_safe_from(pos, n, head, member) \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200530 for (n = list_entry(pos->member.next, typeof(*pos), member); \
531 &pos->member != (head); \
532 pos = n, n = list_entry(n->member.next, typeof(*n), member))
533
534/**
535 * list_for_each_entry_safe_reverse
536 * @pos: the type * to use as a loop cursor.
537 * @n: another type * to use as temporary storage
538 * @head: the head for your list.
539 * @member: the name of the list_struct within the struct.
540 *
541 * Iterate backwards over list of given type, safe against removal
542 * of list entry.
543 */
544#define list_for_each_entry_safe_reverse(pos, n, head, member) \
545 for (pos = list_entry((head)->prev, typeof(*pos), member), \
546 n = list_entry(pos->member.prev, typeof(*pos), member); \
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200547 &pos->member != (head); \
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200548 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
549
Sughosh Ganu7ac57772024-07-30 16:41:28 +0530550/**
551 * list_count_nodes - count nodes in the list
552 * @head: the head for your list.
553 */
554static inline size_t list_count_nodes(struct list_head *head)
555{
556 struct list_head *pos;
557 size_t count = 0;
558
559 list_for_each(pos, head)
560 count++;
561
562 return count;
563}
564
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200565/*
566 * Double linked lists with a single pointer list head.
567 * Mostly useful for hash tables where the two pointer list head is
568 * too wasteful.
569 * You lose the ability to access the tail in O(1).
570 */
571
572struct hlist_head {
573 struct hlist_node *first;
574};
575
576struct hlist_node {
577 struct hlist_node *next, **pprev;
578};
579
580#define HLIST_HEAD_INIT { .first = NULL }
581#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
582#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
583static inline void INIT_HLIST_NODE(struct hlist_node *h)
584{
585 h->next = NULL;
586 h->pprev = NULL;
587}
588
589static inline int hlist_unhashed(const struct hlist_node *h)
590{
591 return !h->pprev;
592}
593
594static inline int hlist_empty(const struct hlist_head *h)
595{
596 return !h->first;
597}
598
599static inline void __hlist_del(struct hlist_node *n)
600{
601 struct hlist_node *next = n->next;
602 struct hlist_node **pprev = n->pprev;
603 *pprev = next;
604 if (next)
605 next->pprev = pprev;
606}
607
608static inline void hlist_del(struct hlist_node *n)
609{
610 __hlist_del(n);
611 n->next = LIST_POISON1;
612 n->pprev = LIST_POISON2;
613}
614
615static inline void hlist_del_init(struct hlist_node *n)
616{
617 if (!hlist_unhashed(n)) {
618 __hlist_del(n);
619 INIT_HLIST_NODE(n);
620 }
621}
622
623static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
624{
625 struct hlist_node *first = h->first;
626 n->next = first;
627 if (first)
628 first->pprev = &n->next;
629 h->first = n;
630 n->pprev = &h->first;
631}
632
633/* next must be != NULL */
634static inline void hlist_add_before(struct hlist_node *n,
635 struct hlist_node *next)
636{
637 n->pprev = next->pprev;
638 n->next = next;
639 next->pprev = &n->next;
640 *(n->pprev) = n;
641}
642
643static inline void hlist_add_after(struct hlist_node *n,
644 struct hlist_node *next)
645{
646 next->next = n->next;
647 n->next = next;
648 next->pprev = &n->next;
649
650 if(next->next)
651 next->next->pprev = &next->next;
652}
653
654#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
655
656#define hlist_for_each(pos, head) \
657 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
658 pos = pos->next)
659
660#define hlist_for_each_safe(pos, n, head) \
661 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
662 pos = n)
663
Rasmus Villemoes73bec9d2022-10-28 13:50:52 +0200664#define hlist_entry_safe(ptr, type, member) \
665 ({ typeof(ptr) ____ptr = (ptr); \
666 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
667 })
668
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200669/**
670 * hlist_for_each_entry - iterate over list of given type
Rasmus Villemoes73bec9d2022-10-28 13:50:52 +0200671 * @pos: the type * to use as a loop cursor.
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200672 * @head: the head for your list.
673 * @member: the name of the hlist_node within the struct.
674 */
Rasmus Villemoes73bec9d2022-10-28 13:50:52 +0200675#define hlist_for_each_entry(pos, head, member) \
676 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
677 pos; \
678 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200679
680/**
681 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
Rasmus Villemoes73bec9d2022-10-28 13:50:52 +0200682 * @pos: the type * to use as a loop cursor.
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200683 * @member: the name of the hlist_node within the struct.
684 */
Rasmus Villemoes73bec9d2022-10-28 13:50:52 +0200685#define hlist_for_each_entry_continue(pos, member) \
686 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
687 pos; \
688 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200689
690/**
691 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
Rasmus Villemoes73bec9d2022-10-28 13:50:52 +0200692 * @pos: the type * to use as a loop cursor.
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200693 * @member: the name of the hlist_node within the struct.
694 */
Rasmus Villemoes73bec9d2022-10-28 13:50:52 +0200695#define hlist_for_each_entry_from(pos, member) \
696 for (; pos; \
697 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200698
699/**
700 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Rasmus Villemoes73bec9d2022-10-28 13:50:52 +0200701 * @pos: the type * to use as a loop cursor.
702 * @n: a &struct hlist_node to use as temporary storage
Jean-Christophe PLAGNIOL-VILLARD8dd070b2008-08-31 04:24:51 +0200703 * @head: the head for your list.
704 * @member: the name of the hlist_node within the struct.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200705 */
Rasmus Villemoes73bec9d2022-10-28 13:50:52 +0200706#define hlist_for_each_entry_safe(pos, n, head, member) \
707 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
708 pos && ({ n = pos->member.next; 1; }); \
709 pos = hlist_entry_safe(n, typeof(*pos), member))
Wolfgang Denk47f57792005-08-08 01:03:24 +0200710
711#endif