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