blob: 3a54266a1e85c3fc60e603c8cff6368961296608 [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 Torvalds3c18d4d2011-02-18 11:32:28 -080099static inline void __list_del_entry(struct list_head *entry)
100{
101 __list_del(entry->prev, entry->next);
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static inline void list_del(struct list_head *entry)
105{
106 __list_del(entry->prev, entry->next);
107 entry->next = LIST_POISON1;
108 entry->prev = LIST_POISON2;
109}
Dave Jones199a9af2006-09-29 01:59:00 -0700110#else
Linus Torvalds3c18d4d2011-02-18 11:32:28 -0800111extern void __list_del_entry(struct list_head *entry);
Dave Jones199a9af2006-09-29 01:59:00 -0700112extern void list_del(struct list_head *entry);
113#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/**
Oleg Nesterov54e73772006-06-23 02:05:54 -0700116 * list_replace - replace old entry by new one
117 * @old : the element to be replaced
118 * @new : the new element to insert
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800119 *
120 * If @old was empty, it will be overwritten.
Oleg Nesterov54e73772006-06-23 02:05:54 -0700121 */
122static inline void list_replace(struct list_head *old,
123 struct list_head *new)
124{
125 new->next = old->next;
126 new->next->prev = new;
127 new->prev = old->prev;
128 new->prev->next = new;
129}
130
131static inline void list_replace_init(struct list_head *old,
132 struct list_head *new)
133{
134 list_replace(old, new);
135 INIT_LIST_HEAD(old);
136}
137
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800138/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * list_del_init - deletes entry from list and reinitialize it.
140 * @entry: the element to delete from the list.
141 */
142static inline void list_del_init(struct list_head *entry)
143{
Linus Torvalds3c18d4d2011-02-18 11:32:28 -0800144 __list_del_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 INIT_LIST_HEAD(entry);
146}
147
148/**
149 * list_move - delete from one list and add as another's head
150 * @list: the entry to move
151 * @head: the head that will precede our entry
152 */
153static inline void list_move(struct list_head *list, struct list_head *head)
154{
Linus Torvalds3c18d4d2011-02-18 11:32:28 -0800155 __list_del_entry(list);
Daniel Walker78db2ad2007-05-12 16:28:35 -0700156 list_add(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159/**
160 * list_move_tail - delete from one list and add as another's tail
161 * @list: the entry to move
162 * @head: the head that will follow our entry
163 */
164static inline void list_move_tail(struct list_head *list,
165 struct list_head *head)
166{
Linus Torvalds3c18d4d2011-02-18 11:32:28 -0800167 __list_del_entry(list);
Daniel Walker78db2ad2007-05-12 16:28:35 -0700168 list_add_tail(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/**
Shailabh Nagare8f4d972006-07-14 00:24:35 -0700172 * list_is_last - tests whether @list is the last entry in list @head
173 * @list: the entry to test
174 * @head: the head of the list
175 */
176static inline int list_is_last(const struct list_head *list,
177 const struct list_head *head)
178{
179 return list->next == head;
180}
181
182/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * list_empty - tests whether a list is empty
184 * @head: the list to test.
185 */
186static inline int list_empty(const struct list_head *head)
187{
188 return head->next == head;
189}
190
191/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700192 * list_empty_careful - tests whether a list is empty and not being modified
193 * @head: the list to test
194 *
195 * Description:
196 * tests whether a list is empty _and_ checks that no other CPU might be
197 * in the process of modifying either member (next or prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * NOTE: using list_empty_careful() without synchronization
200 * can only be safe if the only activity that can happen
201 * to the list entry is list_del_init(). Eg. it cannot be used
202 * if another CPU could re-list_add() it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
204static inline int list_empty_careful(const struct list_head *head)
205{
206 struct list_head *next = head->next;
207 return (next == head) && (next == head->prev);
208}
209
Masami Hiramatsu99602572008-04-28 02:14:27 -0700210/**
Frederic Weisbecker5908cdc2010-01-09 20:53:14 +0100211 * list_rotate_left - rotate the list to the left
212 * @head: the head of the list
213 */
214static inline void list_rotate_left(struct list_head *head)
215{
216 struct list_head *first;
217
218 if (!list_empty(head)) {
219 first = head->next;
220 list_move_tail(first, head);
221 }
222}
223
224/**
Masami Hiramatsu99602572008-04-28 02:14:27 -0700225 * list_is_singular - tests whether a list has just one entry.
226 * @head: the list to test.
227 */
228static inline int list_is_singular(const struct list_head *head)
229{
230 return !list_empty(head) && (head->next == head->prev);
231}
232
Luis R. Rodriguez00e8a4d2008-08-06 13:28:54 -0700233static inline void __list_cut_position(struct list_head *list,
234 struct list_head *head, struct list_head *entry)
235{
236 struct list_head *new_first = entry->next;
237 list->next = head->next;
238 list->next->prev = list;
239 list->prev = entry;
240 entry->next = list;
241 head->next = new_first;
242 new_first->prev = head;
243}
244
245/**
246 * list_cut_position - cut a list into two
247 * @list: a new list to add all removed entries
248 * @head: a list with entries
249 * @entry: an entry within head, could be the head itself
250 * and if so we won't cut the list
251 *
252 * This helper moves the initial part of @head, up to and
253 * including @entry, from @head to @list. You should
254 * pass on @entry an element you know is on @head. @list
255 * should be an empty list or a list you do not care about
256 * losing its data.
257 *
258 */
259static inline void list_cut_position(struct list_head *list,
260 struct list_head *head, struct list_head *entry)
261{
262 if (list_empty(head))
263 return;
264 if (list_is_singular(head) &&
265 (head->next != entry && head != entry))
266 return;
267 if (entry == head)
268 INIT_LIST_HEAD(list);
269 else
270 __list_cut_position(list, head, entry);
271}
272
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700273static inline void __list_splice(const struct list_head *list,
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700274 struct list_head *prev,
275 struct list_head *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct list_head *first = list->next;
278 struct list_head *last = list->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700280 first->prev = prev;
281 prev->next = first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700283 last->next = next;
284 next->prev = last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/**
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700288 * list_splice - join two lists, this is designed for stacks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * @list: the new list to add.
290 * @head: the place to add it in the first list.
291 */
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700292static inline void list_splice(const struct list_head *list,
293 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 if (!list_empty(list))
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700296 __list_splice(list, head, head->next);
297}
298
299/**
300 * list_splice_tail - join two lists, each list being a queue
301 * @list: the new list to add.
302 * @head: the place to add it in the first list.
303 */
304static inline void list_splice_tail(struct list_head *list,
305 struct list_head *head)
306{
307 if (!list_empty(list))
308 __list_splice(list, head->prev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/**
312 * list_splice_init - join two lists and reinitialise the emptied list.
313 * @list: the new list to add.
314 * @head: the place to add it in the first list.
315 *
316 * The list at @list is reinitialised
317 */
318static inline void list_splice_init(struct list_head *list,
319 struct list_head *head)
320{
321 if (!list_empty(list)) {
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700322 __list_splice(list, head, head->next);
323 INIT_LIST_HEAD(list);
324 }
325}
326
327/**
Randy Dunlap6724cce2008-08-08 13:56:20 -0700328 * list_splice_tail_init - join two lists and reinitialise the emptied list
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700329 * @list: the new list to add.
330 * @head: the place to add it in the first list.
331 *
Randy Dunlap6724cce2008-08-08 13:56:20 -0700332 * Each of the lists is a queue.
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700333 * The list at @list is reinitialised
334 */
335static inline void list_splice_tail_init(struct list_head *list,
336 struct list_head *head)
337{
338 if (!list_empty(list)) {
339 __list_splice(list, head->prev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 INIT_LIST_HEAD(list);
341 }
342}
343
344/**
345 * list_entry - get the struct for this entry
346 * @ptr: the &struct list_head pointer.
347 * @type: the type of the struct this is embedded in.
348 * @member: the name of the list_struct within the struct.
349 */
350#define list_entry(ptr, type, member) \
351 container_of(ptr, type, member)
352
353/**
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700354 * list_first_entry - get the first element from a list
355 * @ptr: the list head to take the element from.
356 * @type: the type of the struct this is embedded in.
357 * @member: the name of the list_struct within the struct.
358 *
359 * Note, that list is expected to be not empty.
360 */
361#define list_first_entry(ptr, type, member) \
362 list_entry((ptr)->next, type, member)
363
364/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700366 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * @head: the head for your list.
368 */
369#define list_for_each(pos, head) \
370 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
371 pos = pos->next)
372
373/**
374 * __list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700375 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * @head: the head for your list.
377 *
378 * This variant differs from list_for_each() in that it's the
379 * simplest possible list iteration code, no prefetching is done.
380 * Use this for code that knows the list to be very short (empty
381 * or 1 entry) most of the time.
382 */
383#define __list_for_each(pos, head) \
384 for (pos = (head)->next; pos != (head); pos = pos->next)
385
386/**
387 * list_for_each_prev - iterate over a list backwards
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700388 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * @head: the head for your list.
390 */
391#define list_for_each_prev(pos, head) \
392 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
393 pos = pos->prev)
394
395/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700396 * list_for_each_safe - iterate over a list safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700397 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * @n: another &struct list_head to use as temporary storage
399 * @head: the head for your list.
400 */
401#define list_for_each_safe(pos, n, head) \
402 for (pos = (head)->next, n = pos->next; pos != (head); \
403 pos = n, n = pos->next)
404
405/**
Randy Dunlap8f731f72007-10-18 23:39:28 -0700406 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
Denis V. Lunev37c42522007-10-16 23:29:53 -0700407 * @pos: the &struct list_head to use as a loop cursor.
408 * @n: another &struct list_head to use as temporary storage
409 * @head: the head for your list.
410 */
411#define list_for_each_prev_safe(pos, n, head) \
412 for (pos = (head)->prev, n = pos->prev; \
413 prefetch(pos->prev), pos != (head); \
414 pos = n, n = pos->prev)
415
416/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * list_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700418 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * @head: the head for your list.
420 * @member: the name of the list_struct within the struct.
421 */
422#define list_for_each_entry(pos, head, member) \
423 for (pos = list_entry((head)->next, typeof(*pos), member); \
424 prefetch(pos->member.next), &pos->member != (head); \
425 pos = list_entry(pos->member.next, typeof(*pos), member))
426
427/**
428 * list_for_each_entry_reverse - iterate backwards over list of given type.
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700429 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * @head: the head for your list.
431 * @member: the name of the list_struct within the struct.
432 */
433#define list_for_each_entry_reverse(pos, head, member) \
434 for (pos = list_entry((head)->prev, typeof(*pos), member); \
435 prefetch(pos->member.prev), &pos->member != (head); \
436 pos = list_entry(pos->member.prev, typeof(*pos), member))
437
438/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800439 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * @pos: the type * to use as a start point
441 * @head: the head of the list
442 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700443 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800444 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
446#define list_prepare_entry(pos, head, member) \
447 ((pos) ? : list_entry(head, typeof(*pos), member))
448
449/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700450 * list_for_each_entry_continue - continue iteration over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700451 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * @head: the head for your list.
453 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700454 *
455 * Continue to iterate over list of given type, continuing after
456 * the current position.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
458#define list_for_each_entry_continue(pos, head, member) \
459 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
460 prefetch(pos->member.next), &pos->member != (head); \
461 pos = list_entry(pos->member.next, typeof(*pos), member))
462
463/**
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700464 * list_for_each_entry_continue_reverse - iterate backwards from the given point
465 * @pos: the type * to use as a loop cursor.
466 * @head: the head for your list.
467 * @member: the name of the list_struct within the struct.
468 *
469 * Start to iterate over list of given type backwards, continuing after
470 * the current position.
471 */
472#define list_for_each_entry_continue_reverse(pos, head, member) \
473 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
474 prefetch(pos->member.prev), &pos->member != (head); \
475 pos = list_entry(pos->member.prev, typeof(*pos), member))
476
477/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700478 * list_for_each_entry_from - iterate over list of given type from the current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700479 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800480 * @head: the head for your list.
481 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700482 *
483 * Iterate over list of given type, continuing from current position.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800484 */
485#define list_for_each_entry_from(pos, head, member) \
486 for (; prefetch(pos->member.next), &pos->member != (head); \
487 pos = list_entry(pos->member.next, typeof(*pos), member))
488
489/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700491 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * @n: another type * to use as temporary storage
493 * @head: the head for your list.
494 * @member: the name of the list_struct within the struct.
495 */
496#define list_for_each_entry_safe(pos, n, head, member) \
497 for (pos = list_entry((head)->next, typeof(*pos), member), \
498 n = list_entry(pos->member.next, typeof(*pos), member); \
499 &pos->member != (head); \
500 pos = n, n = list_entry(n->member.next, typeof(*n), member))
501
502/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800503 * list_for_each_entry_safe_continue - continue list iteration safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700504 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700505 * @n: another type * to use as temporary storage
506 * @head: the head for your list.
507 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700508 *
509 * Iterate over list of given type, continuing after current point,
510 * safe against removal of list entry.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700511 */
512#define list_for_each_entry_safe_continue(pos, n, head, member) \
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300513 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
514 n = list_entry(pos->member.next, typeof(*pos), member); \
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700515 &pos->member != (head); \
516 pos = n, n = list_entry(n->member.next, typeof(*n), member))
517
518/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800519 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700520 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800521 * @n: another type * to use as temporary storage
522 * @head: the head for your list.
523 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700524 *
525 * Iterate over list of given type from current point, safe against
526 * removal of list entry.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800527 */
528#define list_for_each_entry_safe_from(pos, n, head, member) \
529 for (n = list_entry(pos->member.next, typeof(*pos), member); \
530 &pos->member != (head); \
531 pos = n, n = list_entry(n->member.next, typeof(*n), member))
532
533/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800534 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700535 * @pos: the type * to use as a loop cursor.
David Howells0ad42352006-01-09 20:51:31 -0800536 * @n: another type * to use as temporary storage
537 * @head: the head for your list.
538 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700539 *
540 * Iterate backwards over list of given type, safe against removal
541 * of list entry.
David Howells0ad42352006-01-09 20:51:31 -0800542 */
543#define list_for_each_entry_safe_reverse(pos, n, head, member) \
544 for (pos = list_entry((head)->prev, typeof(*pos), member), \
545 n = list_entry(pos->member.prev, typeof(*pos), member); \
546 &pos->member != (head); \
547 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
548
npiggin@suse.de57439f82010-06-24 13:02:14 +1000549/**
550 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
551 * @pos: the loop cursor used in the list_for_each_entry_safe loop
552 * @n: temporary storage used in list_for_each_entry_safe
553 * @member: the name of the list_struct within the struct.
554 *
555 * list_safe_reset_next is not safe to use in general if the list may be
556 * modified concurrently (eg. the lock is dropped in the loop body). An
557 * exception to this is if the cursor element (pos) is pinned in the list,
558 * and list_safe_reset_next is called after re-taking the lock and before
559 * completing the current iteration of the loop body.
560 */
561#define list_safe_reset_next(pos, n, member) \
562 n = list_entry(pos->member.next, typeof(*pos), member)
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
565 * Double linked lists with a single pointer list head.
566 * Mostly useful for hash tables where the two pointer list head is
567 * too wasteful.
568 * You lose the ability to access the tail in O(1).
569 */
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571#define HLIST_HEAD_INIT { .first = NULL }
572#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
573#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
Zach Brown490d6ab2006-02-03 03:03:56 -0800574static inline void INIT_HLIST_NODE(struct hlist_node *h)
575{
576 h->next = NULL;
577 h->pprev = NULL;
578}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580static inline int hlist_unhashed(const struct hlist_node *h)
581{
582 return !h->pprev;
583}
584
585static inline int hlist_empty(const struct hlist_head *h)
586{
587 return !h->first;
588}
589
590static inline void __hlist_del(struct hlist_node *n)
591{
592 struct hlist_node *next = n->next;
593 struct hlist_node **pprev = n->pprev;
594 *pprev = next;
595 if (next)
596 next->pprev = pprev;
597}
598
599static inline void hlist_del(struct hlist_node *n)
600{
601 __hlist_del(n);
602 n->next = LIST_POISON1;
603 n->pprev = LIST_POISON2;
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606static inline void hlist_del_init(struct hlist_node *n)
607{
Akinobu Mitada753be2006-04-28 15:21:23 -0700608 if (!hlist_unhashed(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 __hlist_del(n);
610 INIT_HLIST_NODE(n);
611 }
612}
613
614static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
615{
616 struct hlist_node *first = h->first;
617 n->next = first;
618 if (first)
619 first->pprev = &n->next;
620 h->first = n;
621 n->pprev = &h->first;
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/* next must be != NULL */
625static inline void hlist_add_before(struct hlist_node *n,
626 struct hlist_node *next)
627{
628 n->pprev = next->pprev;
629 n->next = next;
630 next->pprev = &n->next;
631 *(n->pprev) = n;
632}
633
634static inline void hlist_add_after(struct hlist_node *n,
635 struct hlist_node *next)
636{
637 next->next = n->next;
638 n->next = next;
639 next->pprev = &n->next;
640
641 if(next->next)
642 next->next->pprev = &next->next;
643}
644
Al Viro756acc22010-10-23 15:23:40 -0400645/* after that we'll appear to be on some hlist and hlist_del will work */
646static inline void hlist_add_fake(struct hlist_node *n)
647{
648 n->pprev = &n->next;
649}
650
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200651/*
652 * Move a list from one list head to another. Fixup the pprev
653 * reference of the first entry if it exists.
654 */
655static inline void hlist_move_list(struct hlist_head *old,
656 struct hlist_head *new)
657{
658 new->first = old->first;
659 if (new->first)
660 new->first->pprev = &new->first;
661 old->first = NULL;
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
665
666#define hlist_for_each(pos, head) \
667 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
668 pos = pos->next)
669
670#define hlist_for_each_safe(pos, n, head) \
671 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
672 pos = n)
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674/**
675 * hlist_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700676 * @tpos: the type * to use as a loop cursor.
677 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * @head: the head for your list.
679 * @member: the name of the hlist_node within the struct.
680 */
681#define hlist_for_each_entry(tpos, pos, head, member) \
682 for (pos = (head)->first; \
683 pos && ({ prefetch(pos->next); 1;}) && \
684 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
685 pos = pos->next)
686
687/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700688 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700689 * @tpos: the type * to use as a loop cursor.
690 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * @member: the name of the hlist_node within the struct.
692 */
693#define hlist_for_each_entry_continue(tpos, pos, member) \
694 for (pos = (pos)->next; \
695 pos && ({ prefetch(pos->next); 1;}) && \
696 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
697 pos = pos->next)
698
699/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700700 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700701 * @tpos: the type * to use as a loop cursor.
702 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * @member: the name of the hlist_node within the struct.
704 */
705#define hlist_for_each_entry_from(tpos, pos, member) \
706 for (; pos && ({ prefetch(pos->next); 1;}) && \
707 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
708 pos = pos->next)
709
710/**
711 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700712 * @tpos: the type * to use as a loop cursor.
713 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 * @n: another &struct hlist_node to use as temporary storage
715 * @head: the head for your list.
716 * @member: the name of the hlist_node within the struct.
717 */
718#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
719 for (pos = (head)->first; \
720 pos && ({ n = pos->next; 1; }) && \
721 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
722 pos = n)
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724#endif