[ARM] S3C maintainer updates merge branch maintainers-updates into s3c-fixes
[linux-2.6] / tools / perf / util / list.h
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3 /*
4   Copyright (C) Cast of dozens, comes from the Linux kernel
5
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of version 2 of the GNU General Public License as
8   published by the Free Software Foundation.
9 */
10
11 #include <stddef.h>
12
13 /*
14  * These are non-NULL pointers that will result in page faults
15  * under normal circumstances, used to verify that nobody uses
16  * non-initialized list entries.
17  */
18 #define LIST_POISON1 ((void *)0x00100100)
19 #define LIST_POISON2 ((void *)0x00200200)
20
21 /**
22  * container_of - cast a member of a structure out to the containing structure
23  * @ptr:        the pointer to the member.
24  * @type:       the type of the container struct this is embedded in.
25  * @member:     the name of the member within the struct.
26  *
27  */
28 #define container_of(ptr, type, member) ({                      \
29         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
30         (type *)( (char *)__mptr - offsetof(type,member) );})
31
32 /*
33  * Simple doubly linked list implementation.
34  *
35  * Some of the internal functions ("__xxx") are useful when
36  * manipulating whole lists rather than single entries, as
37  * sometimes we already know the next/prev entries and we can
38  * generate better code by using them directly rather than
39  * using the generic single-entry routines.
40  */
41
42 struct list_head {
43         struct list_head *next, *prev;
44 };
45
46 #define LIST_HEAD_INIT(name) { &(name), &(name) }
47
48 #define LIST_HEAD(name) \
49         struct list_head name = LIST_HEAD_INIT(name)
50
51 static inline void INIT_LIST_HEAD(struct list_head *list)
52 {
53         list->next = list;
54         list->prev = list;
55 }
56
57 /*
58  * Insert a new entry between two known consecutive entries.
59  *
60  * This is only for internal list manipulation where we know
61  * the prev/next entries already!
62  */
63 static inline void __list_add(struct list_head *new,
64                               struct list_head *prev,
65                               struct list_head *next)
66 {
67         next->prev = new;
68         new->next = next;
69         new->prev = prev;
70         prev->next = new;
71 }
72
73 /**
74  * list_add - add a new entry
75  * @new: new entry to be added
76  * @head: list head to add it after
77  *
78  * Insert a new entry after the specified head.
79  * This is good for implementing stacks.
80  */
81 static inline void list_add(struct list_head *new, struct list_head *head)
82 {
83         __list_add(new, head, head->next);
84 }
85
86 /**
87  * list_add_tail - add a new entry
88  * @new: new entry to be added
89  * @head: list head to add it before
90  *
91  * Insert a new entry before the specified head.
92  * This is useful for implementing queues.
93  */
94 static inline void list_add_tail(struct list_head *new, struct list_head *head)
95 {
96         __list_add(new, head->prev, head);
97 }
98
99 /*
100  * Delete a list entry by making the prev/next entries
101  * point to each other.
102  *
103  * This is only for internal list manipulation where we know
104  * the prev/next entries already!
105  */
106 static inline void __list_del(struct list_head * prev, struct list_head * next)
107 {
108         next->prev = prev;
109         prev->next = next;
110 }
111
112 /**
113  * list_del - deletes entry from list.
114  * @entry: the element to delete from the list.
115  * Note: list_empty on entry does not return true after this, the entry is
116  * in an undefined state.
117  */
118 static inline void list_del(struct list_head *entry)
119 {
120         __list_del(entry->prev, entry->next);
121         entry->next = LIST_POISON1;
122         entry->prev = LIST_POISON2;
123 }
124
125 /**
126  * list_del_range - deletes range of entries from list.
127  * @beging: first element in the range to delete from the list.
128  * @beging: first element in the range to delete from the list.
129  * Note: list_empty on the range of entries does not return true after this,
130  * the entries is in an undefined state.
131  */
132 static inline void list_del_range(struct list_head *begin,
133                                   struct list_head *end)
134 {
135         begin->prev->next = end->next;
136         end->next->prev = begin->prev;
137 }
138
139 /**
140  * list_replace - replace old entry by new one
141  * @old : the element to be replaced
142  * @new : the new element to insert
143  * Note: if 'old' was empty, it will be overwritten.
144  */
145 static inline void list_replace(struct list_head *old,
146                                 struct list_head *new)
147 {
148         new->next = old->next;
149         new->next->prev = new;
150         new->prev = old->prev;
151         new->prev->next = new;
152 }
153
154 static inline void list_replace_init(struct list_head *old,
155                                         struct list_head *new)
156 {
157         list_replace(old, new);
158         INIT_LIST_HEAD(old);
159 }
160
161 /**
162  * list_del_init - deletes entry from list and reinitialize it.
163  * @entry: the element to delete from the list.
164  */
165 static inline void list_del_init(struct list_head *entry)
166 {
167         __list_del(entry->prev, entry->next);
168         INIT_LIST_HEAD(entry);
169 }
170
171 /**
172  * list_move - delete from one list and add as another's head
173  * @list: the entry to move
174  * @head: the head that will precede our entry
175  */
176 static inline void list_move(struct list_head *list, struct list_head *head)
177 {
178         __list_del(list->prev, list->next);
179         list_add(list, head);
180 }
181
182 /**
183  * list_move_tail - delete from one list and add as another's tail
184  * @list: the entry to move
185  * @head: the head that will follow our entry
186  */
187 static inline void list_move_tail(struct list_head *list,
188                                   struct list_head *head)
189 {
190         __list_del(list->prev, list->next);
191         list_add_tail(list, head);
192 }
193
194 /**
195  * list_is_last - tests whether @list is the last entry in list @head
196  * @list: the entry to test
197  * @head: the head of the list
198  */
199 static inline int list_is_last(const struct list_head *list,
200                                 const struct list_head *head)
201 {
202         return list->next == head;
203 }
204
205 /**
206  * list_empty - tests whether a list is empty
207  * @head: the list to test.
208  */
209 static inline int list_empty(const struct list_head *head)
210 {
211         return head->next == head;
212 }
213
214 /**
215  * list_empty_careful - tests whether a list is empty and not being modified
216  * @head: the list to test
217  *
218  * Description:
219  * tests whether a list is empty _and_ checks that no other CPU might be
220  * in the process of modifying either member (next or prev)
221  *
222  * NOTE: using list_empty_careful() without synchronization
223  * can only be safe if the only activity that can happen
224  * to the list entry is list_del_init(). Eg. it cannot be used
225  * if another CPU could re-list_add() it.
226  */
227 static inline int list_empty_careful(const struct list_head *head)
228 {
229         struct list_head *next = head->next;
230         return (next == head) && (next == head->prev);
231 }
232
233 static inline void __list_splice(struct list_head *list,
234                                  struct list_head *head)
235 {
236         struct list_head *first = list->next;
237         struct list_head *last = list->prev;
238         struct list_head *at = head->next;
239
240         first->prev = head;
241         head->next = first;
242
243         last->next = at;
244         at->prev = last;
245 }
246
247 /**
248  * list_splice - join two lists
249  * @list: the new list to add.
250  * @head: the place to add it in the first list.
251  */
252 static inline void list_splice(struct list_head *list, struct list_head *head)
253 {
254         if (!list_empty(list))
255                 __list_splice(list, head);
256 }
257
258 /**
259  * list_splice_init - join two lists and reinitialise the emptied list.
260  * @list: the new list to add.
261  * @head: the place to add it in the first list.
262  *
263  * The list at @list is reinitialised
264  */
265 static inline void list_splice_init(struct list_head *list,
266                                     struct list_head *head)
267 {
268         if (!list_empty(list)) {
269                 __list_splice(list, head);
270                 INIT_LIST_HEAD(list);
271         }
272 }
273
274 /**
275  * list_entry - get the struct for this entry
276  * @ptr:        the &struct list_head pointer.
277  * @type:       the type of the struct this is embedded in.
278  * @member:     the name of the list_struct within the struct.
279  */
280 #define list_entry(ptr, type, member) \
281         container_of(ptr, type, member)
282
283 /**
284  * list_first_entry - get the first element from a list
285  * @ptr:       the list head to take the element from.
286  * @type:      the type of the struct this is embedded in.
287  * @member:    the name of the list_struct within the struct.
288  *
289  * Note, that list is expected to be not empty.
290  */
291 #define list_first_entry(ptr, type, member) \
292         list_entry((ptr)->next, type, member)
293
294 /**
295  * list_for_each        -       iterate over a list
296  * @pos:        the &struct list_head to use as a loop cursor.
297  * @head:       the head for your list.
298  */
299 #define list_for_each(pos, head) \
300         for (pos = (head)->next; pos != (head); \
301                 pos = pos->next)
302
303 /**
304  * __list_for_each      -       iterate over a list
305  * @pos:        the &struct list_head to use as a loop cursor.
306  * @head:       the head for your list.
307  *
308  * This variant differs from list_for_each() in that it's the
309  * simplest possible list iteration code, no prefetching is done.
310  * Use this for code that knows the list to be very short (empty
311  * or 1 entry) most of the time.
312  */
313 #define __list_for_each(pos, head) \
314         for (pos = (head)->next; pos != (head); pos = pos->next)
315
316 /**
317  * list_for_each_prev   -       iterate over a list backwards
318  * @pos:        the &struct list_head to use as a loop cursor.
319  * @head:       the head for your list.
320  */
321 #define list_for_each_prev(pos, head) \
322         for (pos = (head)->prev; pos != (head); \
323                 pos = pos->prev)
324
325 /**
326  * list_for_each_safe - iterate over a list safe against removal of list entry
327  * @pos:        the &struct list_head to use as a loop cursor.
328  * @n:          another &struct list_head to use as temporary storage
329  * @head:       the head for your list.
330  */
331 #define list_for_each_safe(pos, n, head) \
332         for (pos = (head)->next, n = pos->next; pos != (head); \
333                 pos = n, n = pos->next)
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              &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              &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              &pos->member != (head);    \
380              pos = list_entry(pos->member.next, typeof(*pos), member))
381
382 /**
383  * list_for_each_entry_from - iterate over list of given type from the current 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  * Iterate over list of given type, continuing from current position.
389  */
390 #define list_for_each_entry_from(pos, head, member)                     \
391         for (; &pos->member != (head);  \
392              pos = list_entry(pos->member.next, typeof(*pos), member))
393
394 /**
395  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
396  * @pos:        the type * to use as a loop cursor.
397  * @n:          another type * to use as temporary storage
398  * @head:       the head for your list.
399  * @member:     the name of the list_struct within the struct.
400  */
401 #define list_for_each_entry_safe(pos, n, head, member)                  \
402         for (pos = list_entry((head)->next, typeof(*pos), member),      \
403                 n = list_entry(pos->member.next, typeof(*pos), member); \
404              &pos->member != (head);                                    \
405              pos = n, n = list_entry(n->member.next, typeof(*n), member))
406
407 /**
408  * list_for_each_entry_safe_continue
409  * @pos:        the type * to use as a loop cursor.
410  * @n:          another type * to use as temporary storage
411  * @head:       the head for your list.
412  * @member:     the name of the list_struct within the struct.
413  *
414  * Iterate over list of given type, continuing after current point,
415  * safe against removal of list entry.
416  */
417 #define list_for_each_entry_safe_continue(pos, n, head, member)                 \
418         for (pos = list_entry(pos->member.next, typeof(*pos), member),          \
419                 n = list_entry(pos->member.next, typeof(*pos), member);         \
420              &pos->member != (head);                                            \
421              pos = n, n = list_entry(n->member.next, typeof(*n), member))
422
423 /**
424  * list_for_each_entry_safe_from
425  * @pos:        the type * to use as a loop cursor.
426  * @n:          another type * to use as temporary storage
427  * @head:       the head for your list.
428  * @member:     the name of the list_struct within the struct.
429  *
430  * Iterate over list of given type from current point, safe against
431  * removal of list entry.
432  */
433 #define list_for_each_entry_safe_from(pos, n, head, member)                     \
434         for (n = list_entry(pos->member.next, typeof(*pos), member);            \
435              &pos->member != (head);                                            \
436              pos = n, n = list_entry(n->member.next, typeof(*n), member))
437
438 /**
439  * list_for_each_entry_safe_reverse
440  * @pos:        the type * to use as a loop cursor.
441  * @n:          another type * to use as temporary storage
442  * @head:       the head for your list.
443  * @member:     the name of the list_struct within the struct.
444  *
445  * Iterate backwards over list of given type, safe against removal
446  * of list entry.
447  */
448 #define list_for_each_entry_safe_reverse(pos, n, head, member)          \
449         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
450                 n = list_entry(pos->member.prev, typeof(*pos), member); \
451              &pos->member != (head);                                    \
452              pos = n, n = list_entry(n->member.prev, typeof(*n), member))
453
454 /*
455  * Double linked lists with a single pointer list head.
456  * Mostly useful for hash tables where the two pointer list head is
457  * too wasteful.
458  * You lose the ability to access the tail in O(1).
459  */
460
461 struct hlist_head {
462         struct hlist_node *first;
463 };
464
465 struct hlist_node {
466         struct hlist_node *next, **pprev;
467 };
468
469 #define HLIST_HEAD_INIT { .first = NULL }
470 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
471 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
472 static inline void INIT_HLIST_NODE(struct hlist_node *h)
473 {
474         h->next = NULL;
475         h->pprev = NULL;
476 }
477
478 static inline int hlist_unhashed(const struct hlist_node *h)
479 {
480         return !h->pprev;
481 }
482
483 static inline int hlist_empty(const struct hlist_head *h)
484 {
485         return !h->first;
486 }
487
488 static inline void __hlist_del(struct hlist_node *n)
489 {
490         struct hlist_node *next = n->next;
491         struct hlist_node **pprev = n->pprev;
492         *pprev = next;
493         if (next)
494                 next->pprev = pprev;
495 }
496
497 static inline void hlist_del(struct hlist_node *n)
498 {
499         __hlist_del(n);
500         n->next = LIST_POISON1;
501         n->pprev = LIST_POISON2;
502 }
503
504 static inline void hlist_del_init(struct hlist_node *n)
505 {
506         if (!hlist_unhashed(n)) {
507                 __hlist_del(n);
508                 INIT_HLIST_NODE(n);
509         }
510 }
511
512 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
513 {
514         struct hlist_node *first = h->first;
515         n->next = first;
516         if (first)
517                 first->pprev = &n->next;
518         h->first = n;
519         n->pprev = &h->first;
520 }
521
522 /* next must be != NULL */
523 static inline void hlist_add_before(struct hlist_node *n,
524                                         struct hlist_node *next)
525 {
526         n->pprev = next->pprev;
527         n->next = next;
528         next->pprev = &n->next;
529         *(n->pprev) = n;
530 }
531
532 static inline void hlist_add_after(struct hlist_node *n,
533                                         struct hlist_node *next)
534 {
535         next->next = n->next;
536         n->next = next;
537         next->pprev = &n->next;
538
539         if(next->next)
540                 next->next->pprev  = &next->next;
541 }
542
543 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
544
545 #define hlist_for_each(pos, head) \
546         for (pos = (head)->first; pos; \
547              pos = pos->next)
548
549 #define hlist_for_each_safe(pos, n, head) \
550         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
551              pos = n)
552
553 /**
554  * hlist_for_each_entry - iterate over list of given type
555  * @tpos:       the type * to use as a loop cursor.
556  * @pos:        the &struct hlist_node to use as a loop cursor.
557  * @head:       the head for your list.
558  * @member:     the name of the hlist_node within the struct.
559  */
560 #define hlist_for_each_entry(tpos, pos, head, member)                    \
561         for (pos = (head)->first;                                        \
562              pos &&                      \
563                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
564              pos = pos->next)
565
566 /**
567  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
568  * @tpos:       the type * to use as a loop cursor.
569  * @pos:        the &struct hlist_node to use as a loop cursor.
570  * @member:     the name of the hlist_node within the struct.
571  */
572 #define hlist_for_each_entry_continue(tpos, pos, member)                 \
573         for (pos = (pos)->next;                                          \
574              pos &&                      \
575                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
576              pos = pos->next)
577
578 /**
579  * hlist_for_each_entry_from - iterate over a hlist continuing from current point
580  * @tpos:       the type * to use as a loop cursor.
581  * @pos:        the &struct hlist_node to use as a loop cursor.
582  * @member:     the name of the hlist_node within the struct.
583  */
584 #define hlist_for_each_entry_from(tpos, pos, member)                     \
585         for (; pos &&                    \
586                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
587              pos = pos->next)
588
589 /**
590  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
591  * @tpos:       the type * to use as a loop cursor.
592  * @pos:        the &struct hlist_node to use as a loop cursor.
593  * @n:          another &struct hlist_node to use as temporary storage
594  * @head:       the head for your list.
595  * @member:     the name of the hlist_node within the struct.
596  */
597 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
598         for (pos = (head)->first;                                        \
599              pos && ({ n = pos->next; 1; }) &&                           \
600                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
601              pos = n)
602
603 #endif