Major revamping (ver 2.0)
[blktrace.git] / btt / list.h
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3
4 /*
5  * These are non-NULL pointers that will result in page faults
6  * under normal circumstances, used to verify that nobody uses
7  * non-initialized list entries.
8  */
9 #define LIST_POISON1  ((void *) 0x00100100)
10 #define LIST_POISON2  ((void *) 0x00200200)
11
12 struct list_head {
13         struct list_head *next, *prev;
14 };
15
16 #define LIST_HEAD_INIT(name) { &(name), &(name) }
17
18 #define LIST_HEAD(name) \
19         struct list_head name = LIST_HEAD_INIT(name)
20
21 static inline void INIT_LIST_HEAD(struct list_head *list)
22 {
23         list->next = list;
24         list->prev = list;
25 }
26
27 /*
28  * Insert a new entry between two known consecutive entries.
29  *
30  * This is only for internal list manipulation where we know
31  * the prev/next entries already!
32  */
33 static inline void __list_add(struct list_head *new,
34                               struct list_head *prev,
35                               struct list_head *next)
36 {
37         next->prev = new;
38         new->next = next;
39         new->prev = prev;
40         prev->next = new;
41 }
42
43 /**
44  * list_add - add a new entry
45  * @new: new entry to be added
46  * @head: list head to add it after
47  *
48  * Insert a new entry after the specified head.
49  * This is good for implementing stacks.
50  */
51 static inline void list_add(struct list_head *new, struct list_head *head)
52 {
53         __list_add(new, head, head->next);
54 }
55
56 /**
57  * list_add_tail - add a new entry
58  * @new: new entry to be added
59  * @head: list head to add it before
60  *
61  * Insert a new entry before the specified head.
62  * This is useful for implementing queues.
63  */
64 static inline void list_add_tail(struct list_head *new, struct list_head *head)
65 {
66         __list_add(new, head->prev, head);
67 }
68
69 /*
70  * Delete a list entry by making the prev/next entries
71  * point to each other.
72  *
73  * This is only for internal list manipulation where we know
74  * the prev/next entries already!
75  */
76 static inline void __list_del(struct list_head * prev, struct list_head * next)
77 {
78         next->prev = prev;
79         prev->next = next;
80 }
81
82 /**
83  * list_del - deletes entry from list.
84  * @entry: the element to delete from the list.
85  * Note: list_empty on entry does not return true after this, the entry is
86  * in an undefined state.
87  */
88 static inline void list_del(struct list_head *entry)
89 {
90         __list_del(entry->prev, entry->next);
91         entry->next = LIST_POISON1;
92         entry->prev = LIST_POISON2;
93 }
94
95 /**
96  * __list_for_each      -       iterate over a list
97  * @pos:        the &struct list_head to use as a loop counter.
98  * @head:       the head for your list.
99  *
100  * This variant differs from list_for_each() in that it's the
101  * simplest possible list iteration code, no prefetching is done.
102  * Use this for code that knows the list to be very short (empty
103  * or 1 entry) most of the time.
104  */
105 #define __list_for_each(pos, head) \
106         for (pos = (head)->next; pos != (head); pos = pos->next)
107
108 /**
109  * list_for_each_safe   -       iterate over a list safe against removal of list entry
110  * @pos:        the &struct list_head to use as a loop counter.
111  * @n:          another &struct list_head to use as temporary storage
112  * @head:       the head for your list.
113  */
114 #define list_for_each_safe(pos, n, head) \
115         for (pos = (head)->next, n = pos->next; pos != (head); \
116                 pos = n, n = pos->next)
117
118 /**
119  * list_entry - get the struct for this entry
120  * @ptr:        the &struct list_head pointer.
121  * @type:       the type of the struct this is embedded in.
122  * @member:     the name of the list_struct within the struct.
123  */
124 #define list_entry(ptr, type, member) \
125         container_of(ptr, type, member)
126
127 static inline int list_len(struct list_head *head_p)
128 {
129         struct list_head *p;
130         int n = 0;
131
132         __list_for_each(p, head_p) {
133                 n++;
134         }
135
136         return n;
137 }
138
139 /**
140  * list_empty - tests whether a list is empty
141  * @head: the list to test.
142  */
143 static inline int list_empty(const struct list_head *head)
144 {
145         return head->next == head;
146 }
147
148 /**
149  * list_first - Returns first entry on list, or NULL if empty
150  * @head: the list
151  */
152 static inline struct list_head *list_first(const struct list_head *head)
153 {
154         return list_empty(head) ? NULL : head->next;
155 }
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  */
162 static inline void list_move_tail(struct list_head *list,
163                                   struct list_head *head)
164 {
165         __list_del(list->prev, list->next);
166         list_add_tail(list, head);
167 }
168
169 #endif