blob: 9a5f8a71810c55f231565422f8912bea5bbe2d7b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3
Chris Metcalfde5d9bf2010-07-02 13:41:14 -04004#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/stddef.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -07006#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Simple doubly linked list implementation.
11 *
12 * Some of the internal functions ("__xxx") are useful when
13 * manipulating whole lists rather than single entries, as
14 * sometimes we already know the next/prev entries and we can
15 * generate better code by using them directly rather than
16 * using the generic single-entry routines.
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define LIST_HEAD_INIT(name) { &(name), &(name) }
20
21#define LIST_HEAD(name) \
22 struct list_head name = LIST_HEAD_INIT(name)
23
Zach Brown490d6ab2006-02-03 03:03:56 -080024static inline void INIT_LIST_HEAD(struct list_head *list)
25{
26 list->next = list;
27 list->prev = list;
28}
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * Insert a new entry between two known consecutive entries.
32 *
33 * This is only for internal list manipulation where we know
34 * the prev/next entries already!
35 */
Dave Jones199a9af2006-09-29 01:59:00 -070036#ifndef CONFIG_DEBUG_LIST
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static inline void __list_add(struct list_head *new,
38 struct list_head *prev,
39 struct list_head *next)
40{
41 next->prev = new;
42 new->next = next;
43 new->prev = prev;
44 prev->next = new;
45}
Dave Jones199a9af2006-09-29 01:59:00 -070046#else
47extern void __list_add(struct list_head *new,
48 struct list_head *prev,
49 struct list_head *next);
50#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/**
53 * list_add - add a new entry
54 * @new: new entry to be added
55 * @head: list head to add it after
56 *
57 * Insert a new entry after the specified head.
58 * This is good for implementing stacks.
59 */
60static inline void list_add(struct list_head *new, struct list_head *head)
61{
62 __list_add(new, head, head->next);
63}
Dave Jones199a9af2006-09-29 01:59:00 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * 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.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080095 * Note: list_empty() on entry does not return true after this, the entry is
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * in an undefined state.
97 */
Dave Jones199a9af2006-09-29 01:59:00 -070098#ifndef CONFIG_DEBUG_LIST
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static inline void list_del(struct list_head *entry)
100{
101 __list_del(entry->prev, entry->next);
102 entry->next = LIST_POISON1;
103 entry->prev = LIST_POISON2;
104}
Dave Jones199a9af2006-09-29 01:59:00 -0700105#else
106extern void list_del(struct list_head *entry);
107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/**
Oleg Nesterov54e73772006-06-23 02:05:54 -0700110 * list_replace - replace old entry by new one
111 * @old : the element to be replaced
112 * @new : the new element to insert
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800113 *
114 * If @old was empty, it will be overwritten.
Oleg Nesterov54e73772006-06-23 02:05:54 -0700115 */
116static inline void list_replace(struct list_head *old,
117 struct list_head *new)
118{
119 new->next = old->next;
120 new->next->prev = new;
121 new->prev = old->prev;
122 new->prev->next = new;
123}
124
125static inline void list_replace_init(struct list_head *old,
126 struct list_head *new)
127{
128 list_replace(old, new);
129 INIT_LIST_HEAD(old);
130}
131
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800132/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * list_del_init - deletes entry from list and reinitialize it.
134 * @entry: the element to delete from the list.
135 */
136static inline void list_del_init(struct list_head *entry)
137{
138 __list_del(entry->prev, entry->next);
139 INIT_LIST_HEAD(entry);
140}
141
142/**
143 * list_move - delete from one list and add as another's head
144 * @list: the entry to move
145 * @head: the head that will precede our entry
146 */
147static inline void list_move(struct list_head *list, struct list_head *head)
148{
Daniel Walker78db2ad2007-05-12 16:28:35 -0700149 __list_del(list->prev, list->next);
150 list_add(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153/**
154 * list_move_tail - delete from one list and add as another's tail
155 * @list: the entry to move
156 * @head: the head that will follow our entry
157 */
158static inline void list_move_tail(struct list_head *list,
159 struct list_head *head)
160{
Daniel Walker78db2ad2007-05-12 16:28:35 -0700161 __list_del(list->prev, list->next);
162 list_add_tail(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165/**
Shailabh Nagare8f4d972006-07-14 00:24:35 -0700166 * list_is_last - tests whether @list is the last entry in list @head
167 * @list: the entry to test
168 * @head: the head of the list
169 */
170static inline int list_is_last(const struct list_head *list,
171 const struct list_head *head)
172{
173 return list->next == head;
174}
175
176/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * list_empty - tests whether a list is empty
178 * @head: the list to test.
179 */
180static inline int list_empty(const struct list_head *head)
181{
182 return head->next == head;
183}
184
185/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700186 * list_empty_careful - tests whether a list is empty and not being modified
187 * @head: the list to test
188 *
189 * Description:
190 * tests whether a list is empty _and_ checks that no other CPU might be
191 * in the process of modifying either member (next or prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
193 * NOTE: using list_empty_careful() without synchronization
194 * can only be safe if the only activity that can happen
195 * to the list entry is list_del_init(). Eg. it cannot be used
196 * if another CPU could re-list_add() it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
198static inline int list_empty_careful(const struct list_head *head)
199{
200 struct list_head *next = head->next;
201 return (next == head) && (next == head->prev);
202}
203
Masami Hiramatsu99602572008-04-28 02:14:27 -0700204/**
Frederic Weisbecker5908cdc2010-01-09 20:53:14 +0100205 * list_rotate_left - rotate the list to the left
206 * @head: the head of the list
207 */
208static inline void list_rotate_left(struct list_head *head)
209{
210 struct list_head *first;
211
212 if (!list_empty(head)) {
213 first = head->next;
214 list_move_tail(first, head);
215 }
216}
217
218/**
Masami Hiramatsu99602572008-04-28 02:14:27 -0700219 * list_is_singular - tests whether a list has just one entry.
220 * @head: the list to test.
221 */
222static inline int list_is_singular(const struct list_head *head)
223{
224 return !list_empty(head) && (head->next == head->prev);
225}
226
Luis R. Rodriguez00e8a4d2008-08-06 13:28:54 -0700227static inline void __list_cut_position(struct list_head *list,
228 struct list_head *head, struct list_head *entry)
229{
230 struct list_head *new_first = entry->next;
231 list->next = head->next;
232 list->next->prev = list;
233 list->prev = entry;
234 entry->next = list;
235 head->next = new_first;
236 new_first->prev = head;
237}
238
239/**
240 * list_cut_position - cut a list into two
241 * @list: a new list to add all removed entries
242 * @head: a list with entries
243 * @entry: an entry within head, could be the head itself
244 * and if so we won't cut the list
245 *
246 * This helper moves the initial part of @head, up to and
247 * including @entry, from @head to @list. You should
248 * pass on @entry an element you know is on @head. @list
249 * should be an empty list or a list you do not care about
250 * losing its data.
251 *
252 */
253static inline void list_cut_position(struct list_head *list,
254 struct list_head *head, struct list_head *entry)
255{
256 if (list_empty(head))
257 return;
258 if (list_is_singular(head) &&
259 (head->next != entry && head != entry))
260 return;
261 if (entry == head)
262 INIT_LIST_HEAD(list);
263 else
264 __list_cut_position(list, head, entry);
265}
266
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700267static inline void __list_splice(const struct list_head *list,
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700268 struct list_head *prev,
269 struct list_head *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct list_head *first = list->next;
272 struct list_head *last = list->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700274 first->prev = prev;
275 prev->next = first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700277 last->next = next;
278 next->prev = last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/**
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700282 * list_splice - join two lists, this is designed for stacks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * @list: the new list to add.
284 * @head: the place to add it in the first list.
285 */
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700286static inline void list_splice(const struct list_head *list,
287 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 if (!list_empty(list))
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700290 __list_splice(list, head, head->next);
291}
292
293/**
294 * list_splice_tail - join two lists, each list being a queue
295 * @list: the new list to add.
296 * @head: the place to add it in the first list.
297 */
298static inline void list_splice_tail(struct list_head *list,
299 struct list_head *head)
300{
301 if (!list_empty(list))
302 __list_splice(list, head->prev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/**
306 * list_splice_init - join two lists and reinitialise the emptied list.
307 * @list: the new list to add.
308 * @head: the place to add it in the first list.
309 *
310 * The list at @list is reinitialised
311 */
312static inline void list_splice_init(struct list_head *list,
313 struct list_head *head)
314{
315 if (!list_empty(list)) {
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700316 __list_splice(list, head, head->next);
317 INIT_LIST_HEAD(list);
318 }
319}
320
321/**
Randy Dunlap6724cce2008-08-08 13:56:20 -0700322 * list_splice_tail_init - join two lists and reinitialise the emptied list
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700323 * @list: the new list to add.
324 * @head: the place to add it in the first list.
325 *
Randy Dunlap6724cce2008-08-08 13:56:20 -0700326 * Each of the lists is a queue.
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700327 * The list at @list is reinitialised
328 */
329static inline void list_splice_tail_init(struct list_head *list,
330 struct list_head *head)
331{
332 if (!list_empty(list)) {
333 __list_splice(list, head->prev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 INIT_LIST_HEAD(list);
335 }
336}
337
338/**
339 * list_entry - get the struct for this entry
340 * @ptr: the &struct list_head pointer.
341 * @type: the type of the struct this is embedded in.
342 * @member: the name of the list_struct within the struct.
343 */
344#define list_entry(ptr, type, member) \
345 container_of(ptr, type, member)
346
347/**
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700348 * list_first_entry - get the first element from a list
349 * @ptr: the list head to take the element from.
350 * @type: the type of the struct this is embedded in.
351 * @member: the name of the list_struct within the struct.
352 *
353 * Note, that list is expected to be not empty.
354 */
355#define list_first_entry(ptr, type, member) \
356 list_entry((ptr)->next, type, member)
357
358/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700360 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * @head: the head for your list.
362 */
363#define list_for_each(pos, head) \
364 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
365 pos = pos->next)
366
367/**
368 * __list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700369 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * @head: the head for your list.
371 *
372 * This variant differs from list_for_each() in that it's the
373 * simplest possible list iteration code, no prefetching is done.
374 * Use this for code that knows the list to be very short (empty
375 * or 1 entry) most of the time.
376 */
377#define __list_for_each(pos, head) \
378 for (pos = (head)->next; pos != (head); pos = pos->next)
379
380/**
381 * list_for_each_prev - iterate over a list backwards
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700382 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 * @head: the head for your list.
384 */
385#define list_for_each_prev(pos, head) \
386 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
387 pos = pos->prev)
388
389/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700390 * list_for_each_safe - iterate over a list safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700391 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * @n: another &struct list_head to use as temporary storage
393 * @head: the head for your list.
394 */
395#define list_for_each_safe(pos, n, head) \
396 for (pos = (head)->next, n = pos->next; pos != (head); \
397 pos = n, n = pos->next)
398
399/**
Randy Dunlap8f731f72007-10-18 23:39:28 -0700400 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
Denis V. Lunev37c42522007-10-16 23:29:53 -0700401 * @pos: the &struct list_head to use as a loop cursor.
402 * @n: another &struct list_head to use as temporary storage
403 * @head: the head for your list.
404 */
405#define list_for_each_prev_safe(pos, n, head) \
406 for (pos = (head)->prev, n = pos->prev; \
407 prefetch(pos->prev), pos != (head); \
408 pos = n, n = pos->prev)
409
410/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * list_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700412 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * @head: the head for your list.
414 * @member: the name of the list_struct within the struct.
415 */
416#define list_for_each_entry(pos, head, member) \
417 for (pos = list_entry((head)->next, typeof(*pos), member); \
418 prefetch(pos->member.next), &pos->member != (head); \
419 pos = list_entry(pos->member.next, typeof(*pos), member))
420
421/**
422 * list_for_each_entry_reverse - iterate backwards over list of given type.
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700423 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * @head: the head for your list.
425 * @member: the name of the list_struct within the struct.
426 */
427#define list_for_each_entry_reverse(pos, head, member) \
428 for (pos = list_entry((head)->prev, typeof(*pos), member); \
429 prefetch(pos->member.prev), &pos->member != (head); \
430 pos = list_entry(pos->member.prev, typeof(*pos), member))
431
432/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800433 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * @pos: the type * to use as a start point
435 * @head: the head of the list
436 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700437 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800438 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
440#define list_prepare_entry(pos, head, member) \
441 ((pos) ? : list_entry(head, typeof(*pos), member))
442
443/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700444 * list_for_each_entry_continue - continue iteration over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700445 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * @head: the head for your list.
447 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700448 *
449 * Continue to iterate over list of given type, continuing after
450 * the current position.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
452#define list_for_each_entry_continue(pos, head, member) \
453 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
454 prefetch(pos->member.next), &pos->member != (head); \
455 pos = list_entry(pos->member.next, typeof(*pos), member))
456
457/**
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700458 * list_for_each_entry_continue_reverse - iterate backwards from the given point
459 * @pos: the type * to use as a loop cursor.
460 * @head: the head for your list.
461 * @member: the name of the list_struct within the struct.
462 *
463 * Start to iterate over list of given type backwards, continuing after
464 * the current position.
465 */
466#define list_for_each_entry_continue_reverse(pos, head, member) \
467 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
468 prefetch(pos->member.prev), &pos->member != (head); \
469 pos = list_entry(pos->member.prev, typeof(*pos), member))
470
471/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700472 * list_for_each_entry_from - iterate over list of given type from the current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700473 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800474 * @head: the head for your list.
475 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700476 *
477 * Iterate over list of given type, continuing from current position.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800478 */
479#define list_for_each_entry_from(pos, head, member) \
480 for (; prefetch(pos->member.next), &pos->member != (head); \
481 pos = list_entry(pos->member.next, typeof(*pos), member))
482
483/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700485 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * @n: another type * to use as temporary storage
487 * @head: the head for your list.
488 * @member: the name of the list_struct within the struct.
489 */
490#define list_for_each_entry_safe(pos, n, head, member) \
491 for (pos = list_entry((head)->next, typeof(*pos), member), \
492 n = list_entry(pos->member.next, typeof(*pos), member); \
493 &pos->member != (head); \
494 pos = n, n = list_entry(n->member.next, typeof(*n), member))
495
496/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800497 * list_for_each_entry_safe_continue - continue list iteration safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700498 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700499 * @n: another type * to use as temporary storage
500 * @head: the head for your list.
501 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700502 *
503 * Iterate over list of given type, continuing after current point,
504 * safe against removal of list entry.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700505 */
506#define list_for_each_entry_safe_continue(pos, n, head, member) \
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300507 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
508 n = list_entry(pos->member.next, typeof(*pos), member); \
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700509 &pos->member != (head); \
510 pos = n, n = list_entry(n->member.next, typeof(*n), member))
511
512/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800513 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700514 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800515 * @n: another type * to use as temporary storage
516 * @head: the head for your list.
517 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700518 *
519 * Iterate over list of given type from current point, safe against
520 * removal of list entry.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800521 */
522#define list_for_each_entry_safe_from(pos, n, head, member) \
523 for (n = list_entry(pos->member.next, typeof(*pos), member); \
524 &pos->member != (head); \
525 pos = n, n = list_entry(n->member.next, typeof(*n), member))
526
527/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800528 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700529 * @pos: the type * to use as a loop cursor.
David Howells0ad42352006-01-09 20:51:31 -0800530 * @n: another type * to use as temporary storage
531 * @head: the head for your list.
532 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700533 *
534 * Iterate backwards over list of given type, safe against removal
535 * of list entry.
David Howells0ad42352006-01-09 20:51:31 -0800536 */
537#define list_for_each_entry_safe_reverse(pos, n, head, member) \
538 for (pos = list_entry((head)->prev, typeof(*pos), member), \
539 n = list_entry(pos->member.prev, typeof(*pos), member); \
540 &pos->member != (head); \
541 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
542
npiggin@suse.de57439f82010-06-24 13:02:14 +1000543/**
544 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
545 * @pos: the loop cursor used in the list_for_each_entry_safe loop
546 * @n: temporary storage used in list_for_each_entry_safe
547 * @member: the name of the list_struct within the struct.
548 *
549 * list_safe_reset_next is not safe to use in general if the list may be
550 * modified concurrently (eg. the lock is dropped in the loop body). An
551 * exception to this is if the cursor element (pos) is pinned in the list,
552 * and list_safe_reset_next is called after re-taking the lock and before
553 * completing the current iteration of the loop body.
554 */
555#define list_safe_reset_next(pos, n, member) \
556 n = list_entry(pos->member.next, typeof(*pos), member)
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
559 * Double linked lists with a single pointer list head.
560 * Mostly useful for hash tables where the two pointer list head is
561 * too wasteful.
562 * You lose the ability to access the tail in O(1).
563 */
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565#define HLIST_HEAD_INIT { .first = NULL }
566#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
567#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
Zach Brown490d6ab2006-02-03 03:03:56 -0800568static inline void INIT_HLIST_NODE(struct hlist_node *h)
569{
570 h->next = NULL;
571 h->pprev = NULL;
572}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574static inline int hlist_unhashed(const struct hlist_node *h)
575{
576 return !h->pprev;
577}
578
579static inline int hlist_empty(const struct hlist_head *h)
580{
581 return !h->first;
582}
583
584static inline void __hlist_del(struct hlist_node *n)
585{
586 struct hlist_node *next = n->next;
587 struct hlist_node **pprev = n->pprev;
588 *pprev = next;
589 if (next)
590 next->pprev = pprev;
591}
592
593static inline void hlist_del(struct hlist_node *n)
594{
595 __hlist_del(n);
596 n->next = LIST_POISON1;
597 n->pprev = LIST_POISON2;
598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600static inline void hlist_del_init(struct hlist_node *n)
601{
Akinobu Mitada753be2006-04-28 15:21:23 -0700602 if (!hlist_unhashed(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 __hlist_del(n);
604 INIT_HLIST_NODE(n);
605 }
606}
607
608static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
609{
610 struct hlist_node *first = h->first;
611 n->next = first;
612 if (first)
613 first->pprev = &n->next;
614 h->first = n;
615 n->pprev = &h->first;
616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/* next must be != NULL */
619static inline void hlist_add_before(struct hlist_node *n,
620 struct hlist_node *next)
621{
622 n->pprev = next->pprev;
623 n->next = next;
624 next->pprev = &n->next;
625 *(n->pprev) = n;
626}
627
628static inline void hlist_add_after(struct hlist_node *n,
629 struct hlist_node *next)
630{
631 next->next = n->next;
632 n->next = next;
633 next->pprev = &n->next;
634
635 if(next->next)
636 next->next->pprev = &next->next;
637}
638
Al Viro756acc22010-10-23 15:23:40 -0400639/* after that we'll appear to be on some hlist and hlist_del will work */
640static inline void hlist_add_fake(struct hlist_node *n)
641{
642 n->pprev = &n->next;
643}
644
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200645/*
646 * Move a list from one list head to another. Fixup the pprev
647 * reference of the first entry if it exists.
648 */
649static inline void hlist_move_list(struct hlist_head *old,
650 struct hlist_head *new)
651{
652 new->first = old->first;
653 if (new->first)
654 new->first->pprev = &new->first;
655 old->first = NULL;
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
659
660#define hlist_for_each(pos, head) \
661 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
662 pos = pos->next)
663
664#define hlist_for_each_safe(pos, n, head) \
665 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
666 pos = n)
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/**
669 * hlist_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700670 * @tpos: the type * to use as a loop cursor.
671 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * @head: the head for your list.
673 * @member: the name of the hlist_node within the struct.
674 */
675#define hlist_for_each_entry(tpos, pos, head, member) \
676 for (pos = (head)->first; \
677 pos && ({ prefetch(pos->next); 1;}) && \
678 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
679 pos = pos->next)
680
681/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700682 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700683 * @tpos: the type * to use as a loop cursor.
684 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 * @member: the name of the hlist_node within the struct.
686 */
687#define hlist_for_each_entry_continue(tpos, pos, member) \
688 for (pos = (pos)->next; \
689 pos && ({ prefetch(pos->next); 1;}) && \
690 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
691 pos = pos->next)
692
693/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700694 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700695 * @tpos: the type * to use as a loop cursor.
696 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * @member: the name of the hlist_node within the struct.
698 */
699#define hlist_for_each_entry_from(tpos, pos, member) \
700 for (; pos && ({ prefetch(pos->next); 1;}) && \
701 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
702 pos = pos->next)
703
704/**
705 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700706 * @tpos: the type * to use as a loop cursor.
707 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * @n: another &struct hlist_node to use as temporary storage
709 * @head: the head for your list.
710 * @member: the name of the hlist_node within the struct.
711 */
712#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
713 for (pos = (head)->first; \
714 pos && ({ n = pos->next; 1; }) && \
715 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
716 pos = n)
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718#endif