blob: bc43e8a0d7f6346478b00353e2f977a01f5dbd56 [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>
8#include <asm/system.h>
9
10/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Simple doubly linked list implementation.
12 *
13 * Some of the internal functions ("__xxx") are useful when
14 * manipulating whole lists rather than single entries, as
15 * sometimes we already know the next/prev entries and we can
16 * generate better code by using them directly rather than
17 * using the generic single-entry routines.
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define LIST_HEAD_INIT(name) { &(name), &(name) }
21
22#define LIST_HEAD(name) \
23 struct list_head name = LIST_HEAD_INIT(name)
24
Zach Brown490d6ab2006-02-03 03:03:56 -080025static inline void INIT_LIST_HEAD(struct list_head *list)
26{
27 list->next = list;
28 list->prev = list;
29}
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * Insert a new entry between two known consecutive entries.
33 *
34 * This is only for internal list manipulation where we know
35 * the prev/next entries already!
36 */
Dave Jones199a9af2006-09-29 01:59:00 -070037#ifndef CONFIG_DEBUG_LIST
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static inline void __list_add(struct list_head *new,
39 struct list_head *prev,
40 struct list_head *next)
41{
42 next->prev = new;
43 new->next = next;
44 new->prev = prev;
45 prev->next = new;
46}
Dave Jones199a9af2006-09-29 01:59:00 -070047#else
48extern void __list_add(struct list_head *new,
49 struct list_head *prev,
50 struct list_head *next);
51#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
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}
Dave Jones199a9af2006-09-29 01:59:00 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/**
68 * list_add_tail - add a new entry
69 * @new: new entry to be added
70 * @head: list head to add it before
71 *
72 * Insert a new entry before the specified head.
73 * This is useful for implementing queues.
74 */
75static inline void list_add_tail(struct list_head *new, struct list_head *head)
76{
77 __list_add(new, head->prev, head);
78}
79
80/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * Delete a list entry by making the prev/next entries
82 * point to each other.
83 *
84 * This is only for internal list manipulation where we know
85 * the prev/next entries already!
86 */
87static inline void __list_del(struct list_head * prev, struct list_head * next)
88{
89 next->prev = prev;
90 prev->next = next;
91}
92
93/**
94 * list_del - deletes entry from list.
95 * @entry: the element to delete from the list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080096 * Note: list_empty() on entry does not return true after this, the entry is
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * in an undefined state.
98 */
Dave Jones199a9af2006-09-29 01:59:00 -070099#ifndef CONFIG_DEBUG_LIST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static inline void list_del(struct list_head *entry)
101{
102 __list_del(entry->prev, entry->next);
103 entry->next = LIST_POISON1;
104 entry->prev = LIST_POISON2;
105}
Dave Jones199a9af2006-09-29 01:59:00 -0700106#else
107extern void list_del(struct list_head *entry);
108#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/**
Oleg Nesterov54e73772006-06-23 02:05:54 -0700111 * list_replace - replace old entry by new one
112 * @old : the element to be replaced
113 * @new : the new element to insert
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800114 *
115 * If @old was empty, it will be overwritten.
Oleg Nesterov54e73772006-06-23 02:05:54 -0700116 */
117static inline void list_replace(struct list_head *old,
118 struct list_head *new)
119{
120 new->next = old->next;
121 new->next->prev = new;
122 new->prev = old->prev;
123 new->prev->next = new;
124}
125
126static inline void list_replace_init(struct list_head *old,
127 struct list_head *new)
128{
129 list_replace(old, new);
130 INIT_LIST_HEAD(old);
131}
132
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800133/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * list_del_init - deletes entry from list and reinitialize it.
135 * @entry: the element to delete from the list.
136 */
137static inline void list_del_init(struct list_head *entry)
138{
139 __list_del(entry->prev, entry->next);
140 INIT_LIST_HEAD(entry);
141}
142
143/**
144 * list_move - delete from one list and add as another's head
145 * @list: the entry to move
146 * @head: the head that will precede our entry
147 */
148static inline void list_move(struct list_head *list, struct list_head *head)
149{
Daniel Walker78db2ad2007-05-12 16:28:35 -0700150 __list_del(list->prev, list->next);
151 list_add(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154/**
155 * list_move_tail - delete from one list and add as another's tail
156 * @list: the entry to move
157 * @head: the head that will follow our entry
158 */
159static inline void list_move_tail(struct list_head *list,
160 struct list_head *head)
161{
Daniel Walker78db2ad2007-05-12 16:28:35 -0700162 __list_del(list->prev, list->next);
163 list_add_tail(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166/**
Shailabh Nagare8f4d972006-07-14 00:24:35 -0700167 * list_is_last - tests whether @list is the last entry in list @head
168 * @list: the entry to test
169 * @head: the head of the list
170 */
171static inline int list_is_last(const struct list_head *list,
172 const struct list_head *head)
173{
174 return list->next == head;
175}
176
177/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * list_empty - tests whether a list is empty
179 * @head: the list to test.
180 */
181static inline int list_empty(const struct list_head *head)
182{
183 return head->next == head;
184}
185
186/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700187 * list_empty_careful - tests whether a list is empty and not being modified
188 * @head: the list to test
189 *
190 * Description:
191 * tests whether a list is empty _and_ checks that no other CPU might be
192 * in the process of modifying either member (next or prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
194 * NOTE: using list_empty_careful() without synchronization
195 * can only be safe if the only activity that can happen
196 * to the list entry is list_del_init(). Eg. it cannot be used
197 * if another CPU could re-list_add() it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
199static inline int list_empty_careful(const struct list_head *head)
200{
201 struct list_head *next = head->next;
202 return (next == head) && (next == head->prev);
203}
204
Masami Hiramatsu99602572008-04-28 02:14:27 -0700205/**
Frederic Weisbecker5908cdc2010-01-09 20:53:14 +0100206 * list_rotate_left - rotate the list to the left
207 * @head: the head of the list
208 */
209static inline void list_rotate_left(struct list_head *head)
210{
211 struct list_head *first;
212
213 if (!list_empty(head)) {
214 first = head->next;
215 list_move_tail(first, head);
216 }
217}
218
219/**
Masami Hiramatsu99602572008-04-28 02:14:27 -0700220 * list_is_singular - tests whether a list has just one entry.
221 * @head: the list to test.
222 */
223static inline int list_is_singular(const struct list_head *head)
224{
225 return !list_empty(head) && (head->next == head->prev);
226}
227
Luis R. Rodriguez00e8a4d2008-08-06 13:28:54 -0700228static inline void __list_cut_position(struct list_head *list,
229 struct list_head *head, struct list_head *entry)
230{
231 struct list_head *new_first = entry->next;
232 list->next = head->next;
233 list->next->prev = list;
234 list->prev = entry;
235 entry->next = list;
236 head->next = new_first;
237 new_first->prev = head;
238}
239
240/**
241 * list_cut_position - cut a list into two
242 * @list: a new list to add all removed entries
243 * @head: a list with entries
244 * @entry: an entry within head, could be the head itself
245 * and if so we won't cut the list
246 *
247 * This helper moves the initial part of @head, up to and
248 * including @entry, from @head to @list. You should
249 * pass on @entry an element you know is on @head. @list
250 * should be an empty list or a list you do not care about
251 * losing its data.
252 *
253 */
254static inline void list_cut_position(struct list_head *list,
255 struct list_head *head, struct list_head *entry)
256{
257 if (list_empty(head))
258 return;
259 if (list_is_singular(head) &&
260 (head->next != entry && head != entry))
261 return;
262 if (entry == head)
263 INIT_LIST_HEAD(list);
264 else
265 __list_cut_position(list, head, entry);
266}
267
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700268static inline void __list_splice(const struct list_head *list,
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700269 struct list_head *prev,
270 struct list_head *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 struct list_head *first = list->next;
273 struct list_head *last = list->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700275 first->prev = prev;
276 prev->next = first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700278 last->next = next;
279 next->prev = last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/**
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700283 * list_splice - join two lists, this is designed for stacks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * @list: the new list to add.
285 * @head: the place to add it in the first list.
286 */
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700287static inline void list_splice(const struct list_head *list,
288 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 if (!list_empty(list))
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700291 __list_splice(list, head, head->next);
292}
293
294/**
295 * list_splice_tail - join two lists, each list being a queue
296 * @list: the new list to add.
297 * @head: the place to add it in the first list.
298 */
299static inline void list_splice_tail(struct list_head *list,
300 struct list_head *head)
301{
302 if (!list_empty(list))
303 __list_splice(list, head->prev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/**
307 * list_splice_init - join two lists and reinitialise the emptied list.
308 * @list: the new list to add.
309 * @head: the place to add it in the first list.
310 *
311 * The list at @list is reinitialised
312 */
313static inline void list_splice_init(struct list_head *list,
314 struct list_head *head)
315{
316 if (!list_empty(list)) {
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700317 __list_splice(list, head, head->next);
318 INIT_LIST_HEAD(list);
319 }
320}
321
322/**
Randy Dunlap6724cce2008-08-08 13:56:20 -0700323 * list_splice_tail_init - join two lists and reinitialise the emptied list
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700324 * @list: the new list to add.
325 * @head: the place to add it in the first list.
326 *
Randy Dunlap6724cce2008-08-08 13:56:20 -0700327 * Each of the lists is a queue.
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700328 * The list at @list is reinitialised
329 */
330static inline void list_splice_tail_init(struct list_head *list,
331 struct list_head *head)
332{
333 if (!list_empty(list)) {
334 __list_splice(list, head->prev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 INIT_LIST_HEAD(list);
336 }
337}
338
339/**
340 * list_entry - get the struct for this entry
341 * @ptr: the &struct list_head pointer.
342 * @type: the type of the struct this is embedded in.
343 * @member: the name of the list_struct within the struct.
344 */
345#define list_entry(ptr, type, member) \
346 container_of(ptr, type, member)
347
348/**
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700349 * list_first_entry - get the first element from a list
350 * @ptr: the list head to take the element from.
351 * @type: the type of the struct this is embedded in.
352 * @member: the name of the list_struct within the struct.
353 *
354 * Note, that list is expected to be not empty.
355 */
356#define list_first_entry(ptr, type, member) \
357 list_entry((ptr)->next, type, member)
358
359/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700361 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * @head: the head for your list.
363 */
364#define list_for_each(pos, head) \
365 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
366 pos = pos->next)
367
368/**
369 * __list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700370 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * @head: the head for your list.
372 *
373 * This variant differs from list_for_each() in that it's the
374 * simplest possible list iteration code, no prefetching is done.
375 * Use this for code that knows the list to be very short (empty
376 * or 1 entry) most of the time.
377 */
378#define __list_for_each(pos, head) \
379 for (pos = (head)->next; pos != (head); pos = pos->next)
380
381/**
382 * list_for_each_prev - iterate over a list backwards
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700383 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * @head: the head for your list.
385 */
386#define list_for_each_prev(pos, head) \
387 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
388 pos = pos->prev)
389
390/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700391 * list_for_each_safe - iterate over a list safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700392 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * @n: another &struct list_head to use as temporary storage
394 * @head: the head for your list.
395 */
396#define list_for_each_safe(pos, n, head) \
397 for (pos = (head)->next, n = pos->next; pos != (head); \
398 pos = n, n = pos->next)
399
400/**
Randy Dunlap8f731f72007-10-18 23:39:28 -0700401 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
Denis V. Lunev37c42522007-10-16 23:29:53 -0700402 * @pos: the &struct list_head to use as a loop cursor.
403 * @n: another &struct list_head to use as temporary storage
404 * @head: the head for your list.
405 */
406#define list_for_each_prev_safe(pos, n, head) \
407 for (pos = (head)->prev, n = pos->prev; \
408 prefetch(pos->prev), pos != (head); \
409 pos = n, n = pos->prev)
410
411/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * list_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700413 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * @head: the head for your list.
415 * @member: the name of the list_struct within the struct.
416 */
417#define list_for_each_entry(pos, head, member) \
418 for (pos = list_entry((head)->next, typeof(*pos), member); \
419 prefetch(pos->member.next), &pos->member != (head); \
420 pos = list_entry(pos->member.next, typeof(*pos), member))
421
422/**
423 * list_for_each_entry_reverse - iterate backwards over list of given type.
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700424 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * @head: the head for your list.
426 * @member: the name of the list_struct within the struct.
427 */
428#define list_for_each_entry_reverse(pos, head, member) \
429 for (pos = list_entry((head)->prev, typeof(*pos), member); \
430 prefetch(pos->member.prev), &pos->member != (head); \
431 pos = list_entry(pos->member.prev, typeof(*pos), member))
432
433/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800434 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * @pos: the type * to use as a start point
436 * @head: the head of the list
437 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700438 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800439 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
441#define list_prepare_entry(pos, head, member) \
442 ((pos) ? : list_entry(head, typeof(*pos), member))
443
444/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700445 * list_for_each_entry_continue - continue iteration over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700446 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * @head: the head for your list.
448 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700449 *
450 * Continue to iterate over list of given type, continuing after
451 * the current position.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
453#define list_for_each_entry_continue(pos, head, member) \
454 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
455 prefetch(pos->member.next), &pos->member != (head); \
456 pos = list_entry(pos->member.next, typeof(*pos), member))
457
458/**
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700459 * list_for_each_entry_continue_reverse - iterate backwards from the given point
460 * @pos: the type * to use as a loop cursor.
461 * @head: the head for your list.
462 * @member: the name of the list_struct within the struct.
463 *
464 * Start to iterate over list of given type backwards, continuing after
465 * the current position.
466 */
467#define list_for_each_entry_continue_reverse(pos, head, member) \
468 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
469 prefetch(pos->member.prev), &pos->member != (head); \
470 pos = list_entry(pos->member.prev, typeof(*pos), member))
471
472/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700473 * list_for_each_entry_from - iterate over list of given type from the current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700474 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800475 * @head: the head for your list.
476 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700477 *
478 * Iterate over list of given type, continuing from current position.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800479 */
480#define list_for_each_entry_from(pos, head, member) \
481 for (; prefetch(pos->member.next), &pos->member != (head); \
482 pos = list_entry(pos->member.next, typeof(*pos), member))
483
484/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700486 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * @n: another type * to use as temporary storage
488 * @head: the head for your list.
489 * @member: the name of the list_struct within the struct.
490 */
491#define list_for_each_entry_safe(pos, n, head, member) \
492 for (pos = list_entry((head)->next, typeof(*pos), member), \
493 n = list_entry(pos->member.next, typeof(*pos), member); \
494 &pos->member != (head); \
495 pos = n, n = list_entry(n->member.next, typeof(*n), member))
496
497/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800498 * list_for_each_entry_safe_continue - continue list iteration safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700499 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700500 * @n: another type * to use as temporary storage
501 * @head: the head for your list.
502 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700503 *
504 * Iterate over list of given type, continuing after current point,
505 * safe against removal of list entry.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700506 */
507#define list_for_each_entry_safe_continue(pos, n, head, member) \
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300508 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
509 n = list_entry(pos->member.next, typeof(*pos), member); \
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700510 &pos->member != (head); \
511 pos = n, n = list_entry(n->member.next, typeof(*n), member))
512
513/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800514 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700515 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800516 * @n: another type * to use as temporary storage
517 * @head: the head for your list.
518 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700519 *
520 * Iterate over list of given type from current point, safe against
521 * removal of list entry.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800522 */
523#define list_for_each_entry_safe_from(pos, n, head, member) \
524 for (n = list_entry(pos->member.next, typeof(*pos), member); \
525 &pos->member != (head); \
526 pos = n, n = list_entry(n->member.next, typeof(*n), member))
527
528/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800529 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700530 * @pos: the type * to use as a loop cursor.
David Howells0ad42352006-01-09 20:51:31 -0800531 * @n: another type * to use as temporary storage
532 * @head: the head for your list.
533 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700534 *
535 * Iterate backwards over list of given type, safe against removal
536 * of list entry.
David Howells0ad42352006-01-09 20:51:31 -0800537 */
538#define list_for_each_entry_safe_reverse(pos, n, head, member) \
539 for (pos = list_entry((head)->prev, typeof(*pos), member), \
540 n = list_entry(pos->member.prev, typeof(*pos), member); \
541 &pos->member != (head); \
542 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/*
545 * Double linked lists with a single pointer list head.
546 * Mostly useful for hash tables where the two pointer list head is
547 * too wasteful.
548 * You lose the ability to access the tail in O(1).
549 */
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#define HLIST_HEAD_INIT { .first = NULL }
552#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
553#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
Zach Brown490d6ab2006-02-03 03:03:56 -0800554static inline void INIT_HLIST_NODE(struct hlist_node *h)
555{
556 h->next = NULL;
557 h->pprev = NULL;
558}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560static inline int hlist_unhashed(const struct hlist_node *h)
561{
562 return !h->pprev;
563}
564
565static inline int hlist_empty(const struct hlist_head *h)
566{
567 return !h->first;
568}
569
570static inline void __hlist_del(struct hlist_node *n)
571{
572 struct hlist_node *next = n->next;
573 struct hlist_node **pprev = n->pprev;
574 *pprev = next;
575 if (next)
576 next->pprev = pprev;
577}
578
579static inline void hlist_del(struct hlist_node *n)
580{
581 __hlist_del(n);
582 n->next = LIST_POISON1;
583 n->pprev = LIST_POISON2;
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static inline void hlist_del_init(struct hlist_node *n)
587{
Akinobu Mitada753be2006-04-28 15:21:23 -0700588 if (!hlist_unhashed(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 __hlist_del(n);
590 INIT_HLIST_NODE(n);
591 }
592}
593
594static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
595{
596 struct hlist_node *first = h->first;
597 n->next = first;
598 if (first)
599 first->pprev = &n->next;
600 h->first = n;
601 n->pprev = &h->first;
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/* next must be != NULL */
605static inline void hlist_add_before(struct hlist_node *n,
606 struct hlist_node *next)
607{
608 n->pprev = next->pprev;
609 n->next = next;
610 next->pprev = &n->next;
611 *(n->pprev) = n;
612}
613
614static inline void hlist_add_after(struct hlist_node *n,
615 struct hlist_node *next)
616{
617 next->next = n->next;
618 n->next = next;
619 next->pprev = &n->next;
620
621 if(next->next)
622 next->next->pprev = &next->next;
623}
624
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200625/*
626 * Move a list from one list head to another. Fixup the pprev
627 * reference of the first entry if it exists.
628 */
629static inline void hlist_move_list(struct hlist_head *old,
630 struct hlist_head *new)
631{
632 new->first = old->first;
633 if (new->first)
634 new->first->pprev = &new->first;
635 old->first = NULL;
636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
639
640#define hlist_for_each(pos, head) \
641 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
642 pos = pos->next)
643
644#define hlist_for_each_safe(pos, n, head) \
645 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
646 pos = n)
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648/**
649 * hlist_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700650 * @tpos: the type * to use as a loop cursor.
651 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * @head: the head for your list.
653 * @member: the name of the hlist_node within the struct.
654 */
655#define hlist_for_each_entry(tpos, pos, head, member) \
656 for (pos = (head)->first; \
657 pos && ({ prefetch(pos->next); 1;}) && \
658 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
659 pos = pos->next)
660
661/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700662 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700663 * @tpos: the type * to use as a loop cursor.
664 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * @member: the name of the hlist_node within the struct.
666 */
667#define hlist_for_each_entry_continue(tpos, pos, member) \
668 for (pos = (pos)->next; \
669 pos && ({ prefetch(pos->next); 1;}) && \
670 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
671 pos = pos->next)
672
673/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700674 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700675 * @tpos: the type * to use as a loop cursor.
676 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * @member: the name of the hlist_node within the struct.
678 */
679#define hlist_for_each_entry_from(tpos, pos, member) \
680 for (; pos && ({ prefetch(pos->next); 1;}) && \
681 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
682 pos = pos->next)
683
684/**
685 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700686 * @tpos: the type * to use as a loop cursor.
687 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * @n: another &struct hlist_node to use as temporary storage
689 * @head: the head for your list.
690 * @member: the name of the hlist_node within the struct.
691 */
692#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
693 for (pos = (head)->first; \
694 pos && ({ n = pos->next; 1; }) && \
695 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
696 pos = n)
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#endif