blob: 139ec41d9c2ebd7e74d0c53e04433dd996a3f233 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/stddef.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -07005#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/prefetch.h>
7#include <asm/system.h>
8
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
19struct list_head {
20 struct list_head *next, *prev;
21};
22
23#define LIST_HEAD_INIT(name) { &(name), &(name) }
24
25#define LIST_HEAD(name) \
26 struct list_head name = LIST_HEAD_INIT(name)
27
Zach Brown490d6ab2006-02-03 03:03:56 -080028static inline void INIT_LIST_HEAD(struct list_head *list)
29{
30 list->next = list;
31 list->prev = list;
32}
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/*
35 * Insert a new entry between two known consecutive entries.
36 *
37 * This is only for internal list manipulation where we know
38 * the prev/next entries already!
39 */
Dave Jones199a9af2006-09-29 01:59:00 -070040#ifndef CONFIG_DEBUG_LIST
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static inline void __list_add(struct list_head *new,
42 struct list_head *prev,
43 struct list_head *next)
44{
45 next->prev = new;
46 new->next = next;
47 new->prev = prev;
48 prev->next = new;
49}
Dave Jones199a9af2006-09-29 01:59:00 -070050#else
51extern void __list_add(struct list_head *new,
52 struct list_head *prev,
53 struct list_head *next);
54#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/**
57 * list_add - add a new entry
58 * @new: new entry to be added
59 * @head: list head to add it after
60 *
61 * Insert a new entry after the specified head.
62 * This is good for implementing stacks.
63 */
Dave Jones199a9af2006-09-29 01:59:00 -070064#ifndef CONFIG_DEBUG_LIST
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static inline void list_add(struct list_head *new, struct list_head *head)
66{
67 __list_add(new, head, head->next);
68}
Dave Jones199a9af2006-09-29 01:59:00 -070069#else
70extern void list_add(struct list_head *new, struct list_head *head);
71#endif
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/**
75 * list_add_tail - add a new entry
76 * @new: new entry to be added
77 * @head: list head to add it before
78 *
79 * Insert a new entry before the specified head.
80 * This is useful for implementing queues.
81 */
82static inline void list_add_tail(struct list_head *new, struct list_head *head)
83{
84 __list_add(new, head->prev, head);
85}
86
87/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * Delete a list entry by making the prev/next entries
89 * point to each other.
90 *
91 * This is only for internal list manipulation where we know
92 * the prev/next entries already!
93 */
94static inline void __list_del(struct list_head * prev, struct list_head * next)
95{
96 next->prev = prev;
97 prev->next = next;
98}
99
100/**
101 * list_del - deletes entry from list.
102 * @entry: the element to delete from the list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800103 * Note: list_empty() on entry does not return true after this, the entry is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * in an undefined state.
105 */
Dave Jones199a9af2006-09-29 01:59:00 -0700106#ifndef CONFIG_DEBUG_LIST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static inline void list_del(struct list_head *entry)
108{
109 __list_del(entry->prev, entry->next);
110 entry->next = LIST_POISON1;
111 entry->prev = LIST_POISON2;
112}
Dave Jones199a9af2006-09-29 01:59:00 -0700113#else
114extern void list_del(struct list_head *entry);
115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/**
Oleg Nesterov54e73772006-06-23 02:05:54 -0700118 * list_replace - replace old entry by new one
119 * @old : the element to be replaced
120 * @new : the new element to insert
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800121 *
122 * If @old was empty, it will be overwritten.
Oleg Nesterov54e73772006-06-23 02:05:54 -0700123 */
124static inline void list_replace(struct list_head *old,
125 struct list_head *new)
126{
127 new->next = old->next;
128 new->next->prev = new;
129 new->prev = old->prev;
130 new->prev->next = new;
131}
132
133static inline void list_replace_init(struct list_head *old,
134 struct list_head *new)
135{
136 list_replace(old, new);
137 INIT_LIST_HEAD(old);
138}
139
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800140/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * list_del_init - deletes entry from list and reinitialize it.
142 * @entry: the element to delete from the list.
143 */
144static inline void list_del_init(struct list_head *entry)
145{
146 __list_del(entry->prev, entry->next);
147 INIT_LIST_HEAD(entry);
148}
149
150/**
151 * list_move - delete from one list and add as another's head
152 * @list: the entry to move
153 * @head: the head that will precede our entry
154 */
155static inline void list_move(struct list_head *list, struct list_head *head)
156{
Daniel Walker78db2ad2007-05-12 16:28:35 -0700157 __list_del(list->prev, list->next);
158 list_add(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/**
162 * list_move_tail - delete from one list and add as another's tail
163 * @list: the entry to move
164 * @head: the head that will follow our entry
165 */
166static inline void list_move_tail(struct list_head *list,
167 struct list_head *head)
168{
Daniel Walker78db2ad2007-05-12 16:28:35 -0700169 __list_del(list->prev, list->next);
170 list_add_tail(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/**
Shailabh Nagare8f4d972006-07-14 00:24:35 -0700174 * list_is_last - tests whether @list is the last entry in list @head
175 * @list: the entry to test
176 * @head: the head of the list
177 */
178static inline int list_is_last(const struct list_head *list,
179 const struct list_head *head)
180{
181 return list->next == head;
182}
183
184/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * list_empty - tests whether a list is empty
186 * @head: the list to test.
187 */
188static inline int list_empty(const struct list_head *head)
189{
190 return head->next == head;
191}
192
193/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700194 * list_empty_careful - tests whether a list is empty and not being modified
195 * @head: the list to test
196 *
197 * Description:
198 * tests whether a list is empty _and_ checks that no other CPU might be
199 * in the process of modifying either member (next or prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
201 * NOTE: using list_empty_careful() without synchronization
202 * can only be safe if the only activity that can happen
203 * to the list entry is list_del_init(). Eg. it cannot be used
204 * if another CPU could re-list_add() it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
206static inline int list_empty_careful(const struct list_head *head)
207{
208 struct list_head *next = head->next;
209 return (next == head) && (next == head->prev);
210}
211
Masami Hiramatsu99602572008-04-28 02:14:27 -0700212/**
213 * list_is_singular - tests whether a list has just one entry.
214 * @head: the list to test.
215 */
216static inline int list_is_singular(const struct list_head *head)
217{
218 return !list_empty(head) && (head->next == head->prev);
219}
220
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700221static inline void __list_splice(const struct list_head *list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct list_head *head)
223{
224 struct list_head *first = list->next;
225 struct list_head *last = list->prev;
226 struct list_head *at = head->next;
227
228 first->prev = head;
229 head->next = first;
230
231 last->next = at;
232 at->prev = last;
233}
234
235/**
236 * list_splice - join two lists
237 * @list: the new list to add.
238 * @head: the place to add it in the first list.
239 */
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700240static inline void list_splice(const struct list_head *list,
241 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 if (!list_empty(list))
244 __list_splice(list, head);
245}
246
247/**
248 * list_splice_init - join two lists and reinitialise the emptied list.
249 * @list: the new list to add.
250 * @head: the place to add it in the first list.
251 *
252 * The list at @list is reinitialised
253 */
254static inline void list_splice_init(struct list_head *list,
255 struct list_head *head)
256{
257 if (!list_empty(list)) {
258 __list_splice(list, head);
259 INIT_LIST_HEAD(list);
260 }
261}
262
263/**
264 * list_entry - get the struct for this entry
265 * @ptr: the &struct list_head pointer.
266 * @type: the type of the struct this is embedded in.
267 * @member: the name of the list_struct within the struct.
268 */
269#define list_entry(ptr, type, member) \
270 container_of(ptr, type, member)
271
272/**
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700273 * list_first_entry - get the first element from a list
274 * @ptr: the list head to take the element from.
275 * @type: the type of the struct this is embedded in.
276 * @member: the name of the list_struct within the struct.
277 *
278 * Note, that list is expected to be not empty.
279 */
280#define list_first_entry(ptr, type, member) \
281 list_entry((ptr)->next, type, member)
282
283/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700285 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * @head: the head for your list.
287 */
288#define list_for_each(pos, head) \
289 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
290 pos = pos->next)
291
292/**
293 * __list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700294 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * @head: the head for your list.
296 *
297 * This variant differs from list_for_each() in that it's the
298 * simplest possible list iteration code, no prefetching is done.
299 * Use this for code that knows the list to be very short (empty
300 * or 1 entry) most of the time.
301 */
302#define __list_for_each(pos, head) \
303 for (pos = (head)->next; pos != (head); pos = pos->next)
304
305/**
306 * list_for_each_prev - iterate over a list backwards
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700307 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * @head: the head for your list.
309 */
310#define list_for_each_prev(pos, head) \
311 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
312 pos = pos->prev)
313
314/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700315 * list_for_each_safe - iterate over a list safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700316 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * @n: another &struct list_head to use as temporary storage
318 * @head: the head for your list.
319 */
320#define list_for_each_safe(pos, n, head) \
321 for (pos = (head)->next, n = pos->next; pos != (head); \
322 pos = n, n = pos->next)
323
324/**
Randy Dunlap8f731f72007-10-18 23:39:28 -0700325 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
Denis V. Lunev37c42522007-10-16 23:29:53 -0700326 * @pos: the &struct list_head to use as a loop cursor.
327 * @n: another &struct list_head to use as temporary storage
328 * @head: the head for your list.
329 */
330#define list_for_each_prev_safe(pos, n, head) \
331 for (pos = (head)->prev, n = pos->prev; \
332 prefetch(pos->prev), pos != (head); \
333 pos = n, n = pos->prev)
334
335/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * list_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700337 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * @head: the head for your list.
339 * @member: the name of the list_struct within the struct.
340 */
341#define list_for_each_entry(pos, head, member) \
342 for (pos = list_entry((head)->next, typeof(*pos), member); \
343 prefetch(pos->member.next), &pos->member != (head); \
344 pos = list_entry(pos->member.next, typeof(*pos), member))
345
346/**
347 * list_for_each_entry_reverse - iterate backwards over list of given type.
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700348 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * @head: the head for your list.
350 * @member: the name of the list_struct within the struct.
351 */
352#define list_for_each_entry_reverse(pos, head, member) \
353 for (pos = list_entry((head)->prev, typeof(*pos), member); \
354 prefetch(pos->member.prev), &pos->member != (head); \
355 pos = list_entry(pos->member.prev, typeof(*pos), member))
356
357/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800358 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * @pos: the type * to use as a start point
360 * @head: the head of the list
361 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700362 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800363 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365#define list_prepare_entry(pos, head, member) \
366 ((pos) ? : list_entry(head, typeof(*pos), member))
367
368/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700369 * list_for_each_entry_continue - continue iteration over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700370 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * @head: the head for your list.
372 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700373 *
374 * Continue to iterate over list of given type, continuing after
375 * the current position.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
377#define list_for_each_entry_continue(pos, head, member) \
378 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
379 prefetch(pos->member.next), &pos->member != (head); \
380 pos = list_entry(pos->member.next, typeof(*pos), member))
381
382/**
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700383 * list_for_each_entry_continue_reverse - iterate backwards from the given point
384 * @pos: the type * to use as a loop cursor.
385 * @head: the head for your list.
386 * @member: the name of the list_struct within the struct.
387 *
388 * Start to iterate over list of given type backwards, continuing after
389 * the current position.
390 */
391#define list_for_each_entry_continue_reverse(pos, head, member) \
392 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
393 prefetch(pos->member.prev), &pos->member != (head); \
394 pos = list_entry(pos->member.prev, typeof(*pos), member))
395
396/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700397 * list_for_each_entry_from - iterate over list of given type from the current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700398 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800399 * @head: the head for your list.
400 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700401 *
402 * Iterate over list of given type, continuing from current position.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800403 */
404#define list_for_each_entry_from(pos, head, member) \
405 for (; prefetch(pos->member.next), &pos->member != (head); \
406 pos = list_entry(pos->member.next, typeof(*pos), member))
407
408/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700410 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * @n: another type * to use as temporary storage
412 * @head: the head for your list.
413 * @member: the name of the list_struct within the struct.
414 */
415#define list_for_each_entry_safe(pos, n, head, member) \
416 for (pos = list_entry((head)->next, typeof(*pos), member), \
417 n = list_entry(pos->member.next, typeof(*pos), member); \
418 &pos->member != (head); \
419 pos = n, n = list_entry(n->member.next, typeof(*n), member))
420
421/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700422 * list_for_each_entry_safe_continue
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700423 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700424 * @n: another type * to use as temporary storage
425 * @head: the head for your list.
426 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700427 *
428 * Iterate over list of given type, continuing after current point,
429 * safe against removal of list entry.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700430 */
431#define list_for_each_entry_safe_continue(pos, n, head, member) \
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300432 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
433 n = list_entry(pos->member.next, typeof(*pos), member); \
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700434 &pos->member != (head); \
435 pos = n, n = list_entry(n->member.next, typeof(*n), member))
436
437/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700438 * list_for_each_entry_safe_from
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700439 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800440 * @n: another type * to use as temporary storage
441 * @head: the head for your list.
442 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700443 *
444 * Iterate over list of given type from current point, safe against
445 * removal of list entry.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800446 */
447#define list_for_each_entry_safe_from(pos, n, head, member) \
448 for (n = list_entry(pos->member.next, typeof(*pos), member); \
449 &pos->member != (head); \
450 pos = n, n = list_entry(n->member.next, typeof(*n), member))
451
452/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700453 * list_for_each_entry_safe_reverse
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700454 * @pos: the type * to use as a loop cursor.
David Howells0ad42352006-01-09 20:51:31 -0800455 * @n: another type * to use as temporary storage
456 * @head: the head for your list.
457 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700458 *
459 * Iterate backwards over list of given type, safe against removal
460 * of list entry.
David Howells0ad42352006-01-09 20:51:31 -0800461 */
462#define list_for_each_entry_safe_reverse(pos, n, head, member) \
463 for (pos = list_entry((head)->prev, typeof(*pos), member), \
464 n = list_entry(pos->member.prev, typeof(*pos), member); \
465 &pos->member != (head); \
466 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
469 * Double linked lists with a single pointer list head.
470 * Mostly useful for hash tables where the two pointer list head is
471 * too wasteful.
472 * You lose the ability to access the tail in O(1).
473 */
474
475struct hlist_head {
476 struct hlist_node *first;
477};
478
479struct hlist_node {
480 struct hlist_node *next, **pprev;
481};
482
483#define HLIST_HEAD_INIT { .first = NULL }
484#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
485#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
Zach Brown490d6ab2006-02-03 03:03:56 -0800486static inline void INIT_HLIST_NODE(struct hlist_node *h)
487{
488 h->next = NULL;
489 h->pprev = NULL;
490}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492static inline int hlist_unhashed(const struct hlist_node *h)
493{
494 return !h->pprev;
495}
496
497static inline int hlist_empty(const struct hlist_head *h)
498{
499 return !h->first;
500}
501
502static inline void __hlist_del(struct hlist_node *n)
503{
504 struct hlist_node *next = n->next;
505 struct hlist_node **pprev = n->pprev;
506 *pprev = next;
507 if (next)
508 next->pprev = pprev;
509}
510
511static inline void hlist_del(struct hlist_node *n)
512{
513 __hlist_del(n);
514 n->next = LIST_POISON1;
515 n->pprev = LIST_POISON2;
516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518static inline void hlist_del_init(struct hlist_node *n)
519{
Akinobu Mitada753be2006-04-28 15:21:23 -0700520 if (!hlist_unhashed(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 __hlist_del(n);
522 INIT_HLIST_NODE(n);
523 }
524}
525
526static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
527{
528 struct hlist_node *first = h->first;
529 n->next = first;
530 if (first)
531 first->pprev = &n->next;
532 h->first = n;
533 n->pprev = &h->first;
534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/* next must be != NULL */
537static inline void hlist_add_before(struct hlist_node *n,
538 struct hlist_node *next)
539{
540 n->pprev = next->pprev;
541 n->next = next;
542 next->pprev = &n->next;
543 *(n->pprev) = n;
544}
545
546static inline void hlist_add_after(struct hlist_node *n,
547 struct hlist_node *next)
548{
549 next->next = n->next;
550 n->next = next;
551 next->pprev = &n->next;
552
553 if(next->next)
554 next->next->pprev = &next->next;
555}
556
557#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
558
559#define hlist_for_each(pos, head) \
560 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
561 pos = pos->next)
562
563#define hlist_for_each_safe(pos, n, head) \
564 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
565 pos = n)
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/**
568 * hlist_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700569 * @tpos: the type * to use as a loop cursor.
570 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * @head: the head for your list.
572 * @member: the name of the hlist_node within the struct.
573 */
574#define hlist_for_each_entry(tpos, pos, head, member) \
575 for (pos = (head)->first; \
576 pos && ({ prefetch(pos->next); 1;}) && \
577 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
578 pos = pos->next)
579
580/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700581 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700582 * @tpos: the type * to use as a loop cursor.
583 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * @member: the name of the hlist_node within the struct.
585 */
586#define hlist_for_each_entry_continue(tpos, pos, member) \
587 for (pos = (pos)->next; \
588 pos && ({ prefetch(pos->next); 1;}) && \
589 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
590 pos = pos->next)
591
592/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700593 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700594 * @tpos: the type * to use as a loop cursor.
595 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * @member: the name of the hlist_node within the struct.
597 */
598#define hlist_for_each_entry_from(tpos, pos, member) \
599 for (; pos && ({ prefetch(pos->next); 1;}) && \
600 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
601 pos = pos->next)
602
603/**
604 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700605 * @tpos: the type * to use as a loop cursor.
606 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * @n: another &struct hlist_node to use as temporary storage
608 * @head: the head for your list.
609 * @member: the name of the hlist_node within the struct.
610 */
611#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
612 for (pos = (head)->first; \
613 pos && ({ n = pos->next; 1; }) && \
614 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
615 pos = n)
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#endif