Merge branch 'core/rcu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / include / linux / list.h
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3
4 #include <linux/stddef.h>
5 #include <linux/poison.h>
6 #include <linux/prefetch.h>
7 #include <asm/system.h>
8
9 /*
10  * 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
19 struct 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
28 static inline void INIT_LIST_HEAD(struct list_head *list)
29 {
30         list->next = list;
31         list->prev = list;
32 }
33
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  */
40 #ifndef CONFIG_DEBUG_LIST
41 static 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 }
50 #else
51 extern void __list_add(struct list_head *new,
52                               struct list_head *prev,
53                               struct list_head *next);
54 #endif
55
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  */
64 #ifndef CONFIG_DEBUG_LIST
65 static inline void list_add(struct list_head *new, struct list_head *head)
66 {
67         __list_add(new, head, head->next);
68 }
69 #else
70 extern void list_add(struct list_head *new, struct list_head *head);
71 #endif
72
73
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  */
82 static inline void list_add_tail(struct list_head *new, struct list_head *head)
83 {
84         __list_add(new, head->prev, head);
85 }
86
87 /*
88  * 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  */
94 static 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.
103  * Note: list_empty() on entry does not return true after this, the entry is
104  * in an undefined state.
105  */
106 #ifndef CONFIG_DEBUG_LIST
107 static 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 }
113 #else
114 extern void list_del(struct list_head *entry);
115 #endif
116
117 /**
118  * list_replace - replace old entry by new one
119  * @old : the element to be replaced
120  * @new : the new element to insert
121  *
122  * If @old was empty, it will be overwritten.
123  */
124 static 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
133 static 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
140 /**
141  * list_del_init - deletes entry from list and reinitialize it.
142  * @entry: the element to delete from the list.
143  */
144 static 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  */
155 static inline void list_move(struct list_head *list, struct list_head *head)
156 {
157         __list_del(list->prev, list->next);
158         list_add(list, head);
159 }
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  */
166 static inline void list_move_tail(struct list_head *list,
167                                   struct list_head *head)
168 {
169         __list_del(list->prev, list->next);
170         list_add_tail(list, head);
171 }
172
173 /**
174  * 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  */
178 static 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 /**
185  * list_empty - tests whether a list is empty
186  * @head: the list to test.
187  */
188 static inline int list_empty(const struct list_head *head)
189 {
190         return head->next == head;
191 }
192
193 /**
194  * 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)
200  *
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.
205  */
206 static 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
212 /**
213  * list_is_singular - tests whether a list has just one entry.
214  * @head: the list to test.
215  */
216 static inline int list_is_singular(const struct list_head *head)
217 {
218         return !list_empty(head) && (head->next == head->prev);
219 }
220
221 static inline void __list_splice(const struct list_head *list,
222                                  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  */
240 static inline void list_splice(const struct list_head *list,
241                                 struct list_head *head)
242 {
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  */
254 static 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 /**
273  * 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 /**
284  * list_for_each        -       iterate over a list
285  * @pos:        the &struct list_head to use as a loop cursor.
286  * @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
294  * @pos:        the &struct list_head to use as a loop cursor.
295  * @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
307  * @pos:        the &struct list_head to use as a loop cursor.
308  * @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 /**
315  * list_for_each_safe - iterate over a list safe against removal of list entry
316  * @pos:        the &struct list_head to use as a loop cursor.
317  * @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 /**
325  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
326  * @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 /**
336  * list_for_each_entry  -       iterate over list of given type
337  * @pos:        the type * to use as a loop cursor.
338  * @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.
348  * @pos:        the type * to use as a loop cursor.
349  * @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 /**
358  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
359  * @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.
362  *
363  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
364  */
365 #define list_prepare_entry(pos, head, member) \
366         ((pos) ? : list_entry(head, typeof(*pos), member))
367
368 /**
369  * list_for_each_entry_continue - continue iteration over list of given type
370  * @pos:        the type * to use as a loop cursor.
371  * @head:       the head for your list.
372  * @member:     the name of the list_struct within the struct.
373  *
374  * Continue to iterate over list of given type, continuing after
375  * the current position.
376  */
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 /**
383  * 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 /**
397  * list_for_each_entry_from - iterate over list of given type from the current point
398  * @pos:        the type * to use as a loop cursor.
399  * @head:       the head for your list.
400  * @member:     the name of the list_struct within the struct.
401  *
402  * Iterate over list of given type, continuing from current position.
403  */
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 /**
409  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
410  * @pos:        the type * to use as a loop cursor.
411  * @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 /**
422  * list_for_each_entry_safe_continue
423  * @pos:        the type * to use as a loop cursor.
424  * @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.
427  *
428  * Iterate over list of given type, continuing after current point,
429  * safe against removal of list entry.
430  */
431 #define list_for_each_entry_safe_continue(pos, n, head, member)                 \
432         for (pos = list_entry(pos->member.next, typeof(*pos), member),          \
433                 n = list_entry(pos->member.next, typeof(*pos), member);         \
434              &pos->member != (head);                                            \
435              pos = n, n = list_entry(n->member.next, typeof(*n), member))
436
437 /**
438  * list_for_each_entry_safe_from
439  * @pos:        the type * to use as a loop cursor.
440  * @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.
443  *
444  * Iterate over list of given type from current point, safe against
445  * removal of list entry.
446  */
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 /**
453  * list_for_each_entry_safe_reverse
454  * @pos:        the type * to use as a loop cursor.
455  * @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.
458  *
459  * Iterate backwards over list of given type, safe against removal
460  * of list entry.
461  */
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
468 /*
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
475 struct hlist_head {
476         struct hlist_node *first;
477 };
478
479 struct 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)
486 static inline void INIT_HLIST_NODE(struct hlist_node *h)
487 {
488         h->next = NULL;
489         h->pprev = NULL;
490 }
491
492 static inline int hlist_unhashed(const struct hlist_node *h)
493 {
494         return !h->pprev;
495 }
496
497 static inline int hlist_empty(const struct hlist_head *h)
498 {
499         return !h->first;
500 }
501
502 static 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
511 static 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
518 static inline void hlist_del_init(struct hlist_node *n)
519 {
520         if (!hlist_unhashed(n)) {
521                 __hlist_del(n);
522                 INIT_HLIST_NODE(n);
523         }
524 }
525
526 static 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
536 /* next must be != NULL */
537 static 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
546 static 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
567 /**
568  * hlist_for_each_entry - iterate over list of given type
569  * @tpos:       the type * to use as a loop cursor.
570  * @pos:        the &struct hlist_node to use as a loop cursor.
571  * @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 /**
581  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
582  * @tpos:       the type * to use as a loop cursor.
583  * @pos:        the &struct hlist_node to use as a loop cursor.
584  * @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 /**
593  * hlist_for_each_entry_from - iterate over a hlist continuing from current point
594  * @tpos:       the type * to use as a loop cursor.
595  * @pos:        the &struct hlist_node to use as a loop cursor.
596  * @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
605  * @tpos:       the type * to use as a loop cursor.
606  * @pos:        the &struct hlist_node to use as a loop cursor.
607  * @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
617 #endif