blob: a886f27a11817c3303826d1f13ce5cf0255a38e7 [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 */
64static inline void list_add(struct list_head *new, struct list_head *head)
65{
66 __list_add(new, head, head->next);
67}
Dave Jones199a9af2006-09-29 01:59:00 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/**
71 * list_add_tail - add a new entry
72 * @new: new entry to be added
73 * @head: list head to add it before
74 *
75 * Insert a new entry before the specified head.
76 * This is useful for implementing queues.
77 */
78static inline void list_add_tail(struct list_head *new, struct list_head *head)
79{
80 __list_add(new, head->prev, head);
81}
82
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Delete a list entry by making the prev/next entries
85 * point to each other.
86 *
87 * This is only for internal list manipulation where we know
88 * the prev/next entries already!
89 */
90static inline void __list_del(struct list_head * prev, struct list_head * next)
91{
92 next->prev = prev;
93 prev->next = next;
94}
95
96/**
97 * list_del - deletes entry from list.
98 * @entry: the element to delete from the list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080099 * Note: list_empty() on entry does not return true after this, the entry is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * in an undefined state.
101 */
Dave Jones199a9af2006-09-29 01:59:00 -0700102#ifndef CONFIG_DEBUG_LIST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static inline void list_del(struct list_head *entry)
104{
105 __list_del(entry->prev, entry->next);
106 entry->next = LIST_POISON1;
107 entry->prev = LIST_POISON2;
108}
Dave Jones199a9af2006-09-29 01:59:00 -0700109#else
110extern void list_del(struct list_head *entry);
111#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/**
Oleg Nesterov54e73772006-06-23 02:05:54 -0700114 * list_replace - replace old entry by new one
115 * @old : the element to be replaced
116 * @new : the new element to insert
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800117 *
118 * If @old was empty, it will be overwritten.
Oleg Nesterov54e73772006-06-23 02:05:54 -0700119 */
120static inline void list_replace(struct list_head *old,
121 struct list_head *new)
122{
123 new->next = old->next;
124 new->next->prev = new;
125 new->prev = old->prev;
126 new->prev->next = new;
127}
128
129static inline void list_replace_init(struct list_head *old,
130 struct list_head *new)
131{
132 list_replace(old, new);
133 INIT_LIST_HEAD(old);
134}
135
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800136/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * list_del_init - deletes entry from list and reinitialize it.
138 * @entry: the element to delete from the list.
139 */
140static inline void list_del_init(struct list_head *entry)
141{
142 __list_del(entry->prev, entry->next);
143 INIT_LIST_HEAD(entry);
144}
145
146/**
147 * list_move - delete from one list and add as another's head
148 * @list: the entry to move
149 * @head: the head that will precede our entry
150 */
151static inline void list_move(struct list_head *list, struct list_head *head)
152{
Daniel Walker78db2ad2007-05-12 16:28:35 -0700153 __list_del(list->prev, list->next);
154 list_add(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/**
158 * list_move_tail - delete from one list and add as another's tail
159 * @list: the entry to move
160 * @head: the head that will follow our entry
161 */
162static inline void list_move_tail(struct list_head *list,
163 struct list_head *head)
164{
Daniel Walker78db2ad2007-05-12 16:28:35 -0700165 __list_del(list->prev, list->next);
166 list_add_tail(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169/**
Shailabh Nagare8f4d972006-07-14 00:24:35 -0700170 * list_is_last - tests whether @list is the last entry in list @head
171 * @list: the entry to test
172 * @head: the head of the list
173 */
174static inline int list_is_last(const struct list_head *list,
175 const struct list_head *head)
176{
177 return list->next == head;
178}
179
180/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * list_empty - tests whether a list is empty
182 * @head: the list to test.
183 */
184static inline int list_empty(const struct list_head *head)
185{
186 return head->next == head;
187}
188
189/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700190 * list_empty_careful - tests whether a list is empty and not being modified
191 * @head: the list to test
192 *
193 * Description:
194 * tests whether a list is empty _and_ checks that no other CPU might be
195 * in the process of modifying either member (next or prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
197 * NOTE: using list_empty_careful() without synchronization
198 * can only be safe if the only activity that can happen
199 * to the list entry is list_del_init(). Eg. it cannot be used
200 * if another CPU could re-list_add() it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
202static inline int list_empty_careful(const struct list_head *head)
203{
204 struct list_head *next = head->next;
205 return (next == head) && (next == head->prev);
206}
207
Masami Hiramatsu99602572008-04-28 02:14:27 -0700208/**
209 * list_is_singular - tests whether a list has just one entry.
210 * @head: the list to test.
211 */
212static inline int list_is_singular(const struct list_head *head)
213{
214 return !list_empty(head) && (head->next == head->prev);
215}
216
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700217static inline void __list_splice(const struct list_head *list,
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700218 struct list_head *prev,
219 struct list_head *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 struct list_head *first = list->next;
222 struct list_head *last = list->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700224 first->prev = prev;
225 prev->next = first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700227 last->next = next;
228 next->prev = last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231/**
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700232 * list_splice - join two lists, this is designed for stacks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * @list: the new list to add.
234 * @head: the place to add it in the first list.
235 */
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700236static inline void list_splice(const struct list_head *list,
237 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 if (!list_empty(list))
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700240 __list_splice(list, head, head->next);
241}
242
243/**
244 * list_splice_tail - join two lists, each list being a queue
245 * @list: the new list to add.
246 * @head: the place to add it in the first list.
247 */
248static inline void list_splice_tail(struct list_head *list,
249 struct list_head *head)
250{
251 if (!list_empty(list))
252 __list_splice(list, head->prev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * list_splice_init - join two lists and reinitialise the emptied list.
257 * @list: the new list to add.
258 * @head: the place to add it in the first list.
259 *
260 * The list at @list is reinitialised
261 */
262static inline void list_splice_init(struct list_head *list,
263 struct list_head *head)
264{
265 if (!list_empty(list)) {
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700266 __list_splice(list, head, head->next);
267 INIT_LIST_HEAD(list);
268 }
269}
270
271/**
272 * list_splice_tail_init - join two lists, each list being a queue, and
273 * reinitialise the emptied list.
274 * @list: the new list to add.
275 * @head: the place to add it in the first list.
276 *
277 * The list at @list is reinitialised
278 */
279static inline void list_splice_tail_init(struct list_head *list,
280 struct list_head *head)
281{
282 if (!list_empty(list)) {
283 __list_splice(list, head->prev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 INIT_LIST_HEAD(list);
285 }
286}
287
288/**
289 * list_entry - get the struct for this entry
290 * @ptr: the &struct list_head pointer.
291 * @type: the type of the struct this is embedded in.
292 * @member: the name of the list_struct within the struct.
293 */
294#define list_entry(ptr, type, member) \
295 container_of(ptr, type, member)
296
297/**
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700298 * list_first_entry - get the first element from a list
299 * @ptr: the list head to take the element from.
300 * @type: the type of the struct this is embedded in.
301 * @member: the name of the list_struct within the struct.
302 *
303 * Note, that list is expected to be not empty.
304 */
305#define list_first_entry(ptr, type, member) \
306 list_entry((ptr)->next, type, member)
307
308/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700310 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * @head: the head for your list.
312 */
313#define list_for_each(pos, head) \
314 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
315 pos = pos->next)
316
317/**
318 * __list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700319 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * @head: the head for your list.
321 *
322 * This variant differs from list_for_each() in that it's the
323 * simplest possible list iteration code, no prefetching is done.
324 * Use this for code that knows the list to be very short (empty
325 * or 1 entry) most of the time.
326 */
327#define __list_for_each(pos, head) \
328 for (pos = (head)->next; pos != (head); pos = pos->next)
329
330/**
331 * list_for_each_prev - iterate over a list backwards
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700332 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * @head: the head for your list.
334 */
335#define list_for_each_prev(pos, head) \
336 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
337 pos = pos->prev)
338
339/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700340 * list_for_each_safe - iterate over a list safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700341 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 * @n: another &struct list_head to use as temporary storage
343 * @head: the head for your list.
344 */
345#define list_for_each_safe(pos, n, head) \
346 for (pos = (head)->next, n = pos->next; pos != (head); \
347 pos = n, n = pos->next)
348
349/**
Randy Dunlap8f731f72007-10-18 23:39:28 -0700350 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
Denis V. Lunev37c42522007-10-16 23:29:53 -0700351 * @pos: the &struct list_head to use as a loop cursor.
352 * @n: another &struct list_head to use as temporary storage
353 * @head: the head for your list.
354 */
355#define list_for_each_prev_safe(pos, n, head) \
356 for (pos = (head)->prev, n = pos->prev; \
357 prefetch(pos->prev), pos != (head); \
358 pos = n, n = pos->prev)
359
360/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * list_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700362 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * @head: the head for your list.
364 * @member: the name of the list_struct within the struct.
365 */
366#define list_for_each_entry(pos, head, member) \
367 for (pos = list_entry((head)->next, typeof(*pos), member); \
368 prefetch(pos->member.next), &pos->member != (head); \
369 pos = list_entry(pos->member.next, typeof(*pos), member))
370
371/**
372 * list_for_each_entry_reverse - iterate backwards over list of given type.
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700373 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * @head: the head for your list.
375 * @member: the name of the list_struct within the struct.
376 */
377#define list_for_each_entry_reverse(pos, head, member) \
378 for (pos = list_entry((head)->prev, typeof(*pos), member); \
379 prefetch(pos->member.prev), &pos->member != (head); \
380 pos = list_entry(pos->member.prev, typeof(*pos), member))
381
382/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800383 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * @pos: the type * to use as a start point
385 * @head: the head of the list
386 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700387 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800388 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
390#define list_prepare_entry(pos, head, member) \
391 ((pos) ? : list_entry(head, typeof(*pos), member))
392
393/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700394 * list_for_each_entry_continue - continue iteration over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700395 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * @head: the head for your list.
397 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700398 *
399 * Continue to iterate over list of given type, continuing after
400 * the current position.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
402#define list_for_each_entry_continue(pos, head, member) \
403 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
404 prefetch(pos->member.next), &pos->member != (head); \
405 pos = list_entry(pos->member.next, typeof(*pos), member))
406
407/**
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700408 * list_for_each_entry_continue_reverse - iterate backwards from the given point
409 * @pos: the type * to use as a loop cursor.
410 * @head: the head for your list.
411 * @member: the name of the list_struct within the struct.
412 *
413 * Start to iterate over list of given type backwards, continuing after
414 * the current position.
415 */
416#define list_for_each_entry_continue_reverse(pos, head, member) \
417 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
418 prefetch(pos->member.prev), &pos->member != (head); \
419 pos = list_entry(pos->member.prev, typeof(*pos), member))
420
421/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700422 * list_for_each_entry_from - iterate over list of given type from the current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700423 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800424 * @head: the head for your list.
425 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700426 *
427 * Iterate over list of given type, continuing from current position.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800428 */
429#define list_for_each_entry_from(pos, head, member) \
430 for (; prefetch(pos->member.next), &pos->member != (head); \
431 pos = list_entry(pos->member.next, typeof(*pos), member))
432
433/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700435 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * @n: another type * to use as temporary storage
437 * @head: the head for your list.
438 * @member: the name of the list_struct within the struct.
439 */
440#define list_for_each_entry_safe(pos, n, head, member) \
441 for (pos = list_entry((head)->next, typeof(*pos), member), \
442 n = list_entry(pos->member.next, typeof(*pos), member); \
443 &pos->member != (head); \
444 pos = n, n = list_entry(n->member.next, typeof(*n), member))
445
446/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700447 * list_for_each_entry_safe_continue
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700448 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700449 * @n: another type * to use as temporary storage
450 * @head: the head for your list.
451 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700452 *
453 * Iterate over list of given type, continuing after current point,
454 * safe against removal of list entry.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700455 */
456#define list_for_each_entry_safe_continue(pos, n, head, member) \
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300457 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
458 n = list_entry(pos->member.next, typeof(*pos), member); \
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700459 &pos->member != (head); \
460 pos = n, n = list_entry(n->member.next, typeof(*n), member))
461
462/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700463 * list_for_each_entry_safe_from
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700464 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800465 * @n: another type * to use as temporary storage
466 * @head: the head for your list.
467 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700468 *
469 * Iterate over list of given type from current point, safe against
470 * removal of list entry.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800471 */
472#define list_for_each_entry_safe_from(pos, n, head, member) \
473 for (n = list_entry(pos->member.next, typeof(*pos), member); \
474 &pos->member != (head); \
475 pos = n, n = list_entry(n->member.next, typeof(*n), member))
476
477/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700478 * list_for_each_entry_safe_reverse
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700479 * @pos: the type * to use as a loop cursor.
David Howells0ad42352006-01-09 20:51:31 -0800480 * @n: another type * to use as temporary storage
481 * @head: the head for your list.
482 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700483 *
484 * Iterate backwards over list of given type, safe against removal
485 * of list entry.
David Howells0ad42352006-01-09 20:51:31 -0800486 */
487#define list_for_each_entry_safe_reverse(pos, n, head, member) \
488 for (pos = list_entry((head)->prev, typeof(*pos), member), \
489 n = list_entry(pos->member.prev, typeof(*pos), member); \
490 &pos->member != (head); \
491 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/*
494 * Double linked lists with a single pointer list head.
495 * Mostly useful for hash tables where the two pointer list head is
496 * too wasteful.
497 * You lose the ability to access the tail in O(1).
498 */
499
500struct hlist_head {
501 struct hlist_node *first;
502};
503
504struct hlist_node {
505 struct hlist_node *next, **pprev;
506};
507
508#define HLIST_HEAD_INIT { .first = NULL }
509#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
510#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
Zach Brown490d6ab2006-02-03 03:03:56 -0800511static inline void INIT_HLIST_NODE(struct hlist_node *h)
512{
513 h->next = NULL;
514 h->pprev = NULL;
515}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517static inline int hlist_unhashed(const struct hlist_node *h)
518{
519 return !h->pprev;
520}
521
522static inline int hlist_empty(const struct hlist_head *h)
523{
524 return !h->first;
525}
526
527static inline void __hlist_del(struct hlist_node *n)
528{
529 struct hlist_node *next = n->next;
530 struct hlist_node **pprev = n->pprev;
531 *pprev = next;
532 if (next)
533 next->pprev = pprev;
534}
535
536static inline void hlist_del(struct hlist_node *n)
537{
538 __hlist_del(n);
539 n->next = LIST_POISON1;
540 n->pprev = LIST_POISON2;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static inline void hlist_del_init(struct hlist_node *n)
544{
Akinobu Mitada753be2006-04-28 15:21:23 -0700545 if (!hlist_unhashed(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 __hlist_del(n);
547 INIT_HLIST_NODE(n);
548 }
549}
550
551static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
552{
553 struct hlist_node *first = h->first;
554 n->next = first;
555 if (first)
556 first->pprev = &n->next;
557 h->first = n;
558 n->pprev = &h->first;
559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/* next must be != NULL */
562static inline void hlist_add_before(struct hlist_node *n,
563 struct hlist_node *next)
564{
565 n->pprev = next->pprev;
566 n->next = next;
567 next->pprev = &n->next;
568 *(n->pprev) = n;
569}
570
571static inline void hlist_add_after(struct hlist_node *n,
572 struct hlist_node *next)
573{
574 next->next = n->next;
575 n->next = next;
576 next->pprev = &n->next;
577
578 if(next->next)
579 next->next->pprev = &next->next;
580}
581
582#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
583
584#define hlist_for_each(pos, head) \
585 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
586 pos = pos->next)
587
588#define hlist_for_each_safe(pos, n, head) \
589 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
590 pos = n)
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/**
593 * hlist_for_each_entry - iterate over list of given type
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 * @head: the head for your list.
597 * @member: the name of the hlist_node within the struct.
598 */
599#define hlist_for_each_entry(tpos, pos, head, member) \
600 for (pos = (head)->first; \
601 pos && ({ prefetch(pos->next); 1;}) && \
602 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
603 pos = pos->next)
604
605/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700606 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700607 * @tpos: the type * to use as a loop cursor.
608 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * @member: the name of the hlist_node within the struct.
610 */
611#define hlist_for_each_entry_continue(tpos, pos, member) \
612 for (pos = (pos)->next; \
613 pos && ({ prefetch(pos->next); 1;}) && \
614 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
615 pos = pos->next)
616
617/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700618 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700619 * @tpos: the type * to use as a loop cursor.
620 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 * @member: the name of the hlist_node within the struct.
622 */
623#define hlist_for_each_entry_from(tpos, pos, member) \
624 for (; pos && ({ prefetch(pos->next); 1;}) && \
625 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
626 pos = pos->next)
627
628/**
629 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700630 * @tpos: the type * to use as a loop cursor.
631 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * @n: another &struct hlist_node to use as temporary storage
633 * @head: the head for your list.
634 * @member: the name of the hlist_node within the struct.
635 */
636#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
637 for (pos = (head)->first; \
638 pos && ({ n = pos->next; 1; }) && \
639 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
640 pos = n)
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642#endif