Whamcloud - gitweb
- 2.6 fixes landed
[fs/lustre-release.git] / lustre / portals / include / portals / list.h
1 #ifndef _LINUX_LIST_H
2 /*
3  * Simple doubly linked list implementation.
4  *
5  * Some of the internal functions ("__xxx") are useful when
6  * manipulating whole lists rather than single entries, as
7  * sometimes we already know the next/prev entries and we can
8  * generate better code by using them directly rather than
9  * using the generic single-entry routines.
10  */
11
12 #define prefetch(a) ((void)a)
13
14 struct list_head {
15         struct list_head *next, *prev;
16 };
17
18 typedef struct list_head list_t;
19
20 #define LIST_HEAD_INIT(name) { &(name), &(name) }
21
22 #define LIST_HEAD(name) \
23         struct list_head name = LIST_HEAD_INIT(name)
24
25 #define INIT_LIST_HEAD(ptr) do { \
26         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
27 } while (0)
28
29 /*
30  * Insert a new entry between two known consecutive entries.
31  *
32  * This is only for internal list manipulation where we know
33  * the prev/next entries already!
34  */
35 static inline void __list_add(struct list_head * new,
36                               struct list_head * prev,
37                               struct list_head * next)
38 {
39         next->prev = new;
40         new->next = next;
41         new->prev = prev;
42         prev->next = new;
43 }
44
45 /**
46  * list_add - add a new entry
47  * @new: new entry to be added
48  * @head: list head to add it after
49  *
50  * Insert a new entry after the specified head.
51  * This is good for implementing stacks.
52  */
53 static inline void list_add(struct list_head *new, struct list_head *head)
54 {
55         __list_add(new, head, head->next);
56 }
57
58 /**
59  * list_add_tail - add a new entry
60  * @new: new entry to be added
61  * @head: list head to add it before
62  *
63  * Insert a new entry before the specified head.
64  * This is useful for implementing queues.
65  */
66 static inline void list_add_tail(struct list_head *new, struct list_head *head)
67 {
68         __list_add(new, head->prev, head);
69 }
70
71 /*
72  * Delete a list entry by making the prev/next entries
73  * point to each other.
74  *
75  * This is only for internal list manipulation where we know
76  * the prev/next entries already!
77  */
78 static inline void __list_del(struct list_head * prev, struct list_head * next)
79 {
80         next->prev = prev;
81         prev->next = next;
82 }
83
84 /**
85  * list_del - deletes entry from list.
86  * @entry: the element to delete from the list.
87  * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
88  */
89 static inline void list_del(struct list_head *entry)
90 {
91         __list_del(entry->prev, entry->next);
92 }
93
94 /**
95  * list_del_init - deletes entry from list and reinitialize it.
96  * @entry: the element to delete from the list.
97  */
98 static inline void list_del_init(struct list_head *entry)
99 {
100         __list_del(entry->prev, entry->next);
101         INIT_LIST_HEAD(entry);
102 }
103 #endif
104
105 #ifndef list_for_each_entry
106 /**
107  * list_move - delete from one list and add as another's head
108  * @list: the entry to move
109  * @head: the head that will precede our entry
110  */
111 static inline void list_move(struct list_head *list, struct list_head *head)
112 {
113         __list_del(list->prev, list->next);
114         list_add(list, head);
115 }
116
117 /**
118  * list_move_tail - delete from one list and add as another's tail
119  * @list: the entry to move
120  * @head: the head that will follow our entry
121  */
122 static inline void list_move_tail(struct list_head *list,
123                                   struct list_head *head)
124 {
125         __list_del(list->prev, list->next);
126         list_add_tail(list, head);
127 }
128 #endif
129
130 #ifndef _LINUX_LIST_H
131 #define _LINUX_LIST_H
132 /**
133  * list_empty - tests whether a list is empty
134  * @head: the list to test.
135  */
136 static inline int list_empty(struct list_head *head)
137 {
138         return head->next == head;
139 }
140
141 static inline void __list_splice(struct list_head *list,
142                                  struct list_head *head)
143 {
144         struct list_head *first = list->next;
145         struct list_head *last = list->prev;
146         struct list_head *at = head->next;
147
148         first->prev = head;
149         head->next = first;
150
151         last->next = at;
152         at->prev = last;
153 }
154
155 /**
156  * list_splice - join two lists
157  * @list: the new list to add.
158  * @head: the place to add it in the first list.
159  */
160 static inline void list_splice(struct list_head *list, struct list_head *head)
161 {
162         if (!list_empty(list))
163                 __list_splice(list, head);
164 }
165
166 /**
167  * list_splice_init - join two lists and reinitialise the emptied list.
168  * @list: the new list to add.
169  * @head: the place to add it in the first list.
170  *
171  * The list at @list is reinitialised
172  */
173 static inline void list_splice_init(struct list_head *list,
174                                     struct list_head *head)
175 {
176         if (!list_empty(list)) {
177                 __list_splice(list, head);
178                 INIT_LIST_HEAD(list);
179         }
180 }
181
182 /**
183  * list_entry - get the struct for this entry
184  * @ptr:        the &struct list_head pointer.
185  * @type:       the type of the struct this is embedded in.
186  * @member:     the name of the list_struct within the struct.
187  */
188 #define list_entry(ptr, type, member) \
189         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
190
191 /**
192  * list_for_each        -       iterate over a list
193  * @pos:        the &struct list_head to use as a loop counter.
194  * @head:       the head for your list.
195  */
196 #define list_for_each(pos, head) \
197         for (pos = (head)->next, prefetch(pos->next); pos != (head); \
198                 pos = pos->next, prefetch(pos->next))
199
200 /**
201  * list_for_each_prev   -       iterate over a list in reverse order
202  * @pos:        the &struct list_head to use as a loop counter.
203  * @head:       the head for your list.
204  */
205 #define list_for_each_prev(pos, head) \
206         for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
207                 pos = pos->prev, prefetch(pos->prev))
208
209 /**
210  * list_for_each_safe   -       iterate over a list safe against removal of list entry
211  * @pos:        the &struct list_head to use as a loop counter.
212  * @n:          another &struct list_head to use as temporary storage
213  * @head:       the head for your list.
214  */
215 #define list_for_each_safe(pos, n, head) \
216         for (pos = (head)->next, n = pos->next; pos != (head); \
217                 pos = n, n = pos->next)
218
219 #endif
220
221 #ifndef list_for_each_entry
222 /**
223  * list_for_each_entry  -       iterate over list of given type
224  * @pos:        the type * to use as a loop counter.
225  * @head:       the head for your list.
226  * @member:     the name of the list_struct within the struct.
227  */
228 #define list_for_each_entry(pos, head, member)                          \
229         for (pos = list_entry((head)->next, typeof(*pos), member),      \
230                      prefetch(pos->member.next);                        \
231              &pos->member != (head);                                    \
232              pos = list_entry(pos->member.next, typeof(*pos), member),  \
233              prefetch(pos->member.next))
234 #endif
235
236 #ifndef list_for_each_entry_safe
237 /**
238  * list_for_each_entry_safe  -       iterate over list of given type safe against removal of list entry
239  * @pos:        the type * to use as a loop counter.
240  * @n:          another type * to use as temporary storage
241  * @head:       the head for your list.
242  * @member:     the name of the list_struct within the struct.
243  */
244 #define list_for_each_entry_safe(pos, n, head, member)                  \
245         for (pos = list_entry((head)->next, typeof(*pos), member),      \
246                 n = list_entry(pos->member.next, typeof(*pos), member); \
247              &pos->member != (head);                                    \
248              pos = n, n = list_entry(n->member.next, typeof(*n), member))
249 #endif