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