csum data struct changes
[platform/upstream/btrfs-progs.git] / list.h
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3
4 #define LIST_POISON1  ((void *) 0x00100100)
5 #define LIST_POISON2  ((void *) 0x00200200)
6
7 /*
8  * Simple doubly linked list implementation.
9  *
10  * Some of the internal functions ("__xxx") are useful when
11  * manipulating whole lists rather than single entries, as
12  * sometimes we already know the next/prev entries and we can
13  * generate better code by using them directly rather than
14  * using the generic single-entry routines.
15  */
16
17 struct list_head {
18         struct list_head *next, *prev;
19 };
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 static inline void INIT_LIST_HEAD(struct list_head *list)
27 {
28         list->next = list;
29         list->prev = list;
30 }
31
32 /*
33  * Insert a new entry between two known consecutive entries.
34  *
35  * This is only for internal list manipulation where we know
36  * the prev/next entries already!
37  */
38 #ifndef CONFIG_DEBUG_LIST
39 static inline void __list_add(struct list_head *new,
40                               struct list_head *prev,
41                               struct list_head *next)
42 {
43         next->prev = new;
44         new->next = next;
45         new->prev = prev;
46         prev->next = new;
47 }
48 #else
49 extern void __list_add(struct list_head *new,
50                               struct list_head *prev,
51                               struct list_head *next);
52 #endif
53
54 /**
55  * list_add - add a new entry
56  * @new: new entry to be added
57  * @head: list head to add it after
58  *
59  * Insert a new entry after the specified head.
60  * This is good for implementing stacks.
61  */
62 #ifndef CONFIG_DEBUG_LIST
63 static inline void list_add(struct list_head *new, struct list_head *head)
64 {
65         __list_add(new, head, head->next);
66 }
67 #else
68 extern void list_add(struct list_head *new, struct list_head *head);
69 #endif
70
71
72 /**
73  * list_add_tail - add a new entry
74  * @new: new entry to be added
75  * @head: list head to add it before
76  *
77  * Insert a new entry before the specified head.
78  * This is useful for implementing queues.
79  */
80 static inline void list_add_tail(struct list_head *new, struct list_head *head)
81 {
82         __list_add(new, head->prev, head);
83 }
84
85 /*
86  * Delete a list entry by making the prev/next entries
87  * point to each other.
88  *
89  * This is only for internal list manipulation where we know
90  * the prev/next entries already!
91  */
92 static inline void __list_del(struct list_head * prev, struct list_head * next)
93 {
94         next->prev = prev;
95         prev->next = next;
96 }
97
98 /**
99  * list_del - deletes entry from list.
100  * @entry: the element to delete from the list.
101  * Note: list_empty on entry does not return true after this, the entry is
102  * in an undefined state.
103  */
104 #ifndef CONFIG_DEBUG_LIST
105 static inline void list_del(struct list_head *entry)
106 {
107         __list_del(entry->prev, entry->next);
108         entry->next = LIST_POISON1;
109         entry->prev = LIST_POISON2;
110 }
111 #else
112 extern void list_del(struct list_head *entry);
113 #endif
114
115 /**
116  * list_replace - replace old entry by new one
117  * @old : the element to be replaced
118  * @new : the new element to insert
119  * Note: if 'old' was empty, it will be overwritten.
120  */
121 static inline void list_replace(struct list_head *old,
122                                 struct list_head *new)
123 {
124         new->next = old->next;
125         new->next->prev = new;
126         new->prev = old->prev;
127         new->prev->next = new;
128 }
129
130 static inline void list_replace_init(struct list_head *old,
131                                         struct list_head *new)
132 {
133         list_replace(old, new);
134         INIT_LIST_HEAD(old);
135 }
136 /**
137  * list_del_init - deletes entry from list and reinitialize it.
138  * @entry: the element to delete from the list.
139  */
140 static inline void list_del_init(struct list_head *entry)
141 {
142         __list_del(entry->prev, entry->next);
143         INIT_LIST_HEAD(entry);
144 }
145
146 /**
147  * list_move - delete from one list and add as another's head
148  * @list: the entry to move
149  * @head: the head that will precede our entry
150  */
151 static inline void list_move(struct list_head *list, struct list_head *head)
152 {
153         __list_del(list->prev, list->next);
154         list_add(list, head);
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 /**
170  * list_is_last - tests whether @list is the last entry in list @head
171  * @list: the entry to test
172  * @head: the head of the list
173  */
174 static inline int list_is_last(const struct list_head *list,
175                                 const struct list_head *head)
176 {
177         return list->next == head;
178 }
179
180 /**
181  * list_empty - tests whether a list is empty
182  * @head: the list to test.
183  */
184 static inline int list_empty(const struct list_head *head)
185 {
186         return head->next == head;
187 }
188
189 /**
190  * list_empty_careful - tests whether a list is empty and not being modified
191  * @head: the list to test
192  *
193  * Description:
194  * tests whether a list is empty _and_ checks that no other CPU might be
195  * in the process of modifying either member (next or prev)
196  *
197  * NOTE: using list_empty_careful() without synchronization
198  * can only be safe if the only activity that can happen
199  * to the list entry is list_del_init(). Eg. it cannot be used
200  * if another CPU could re-list_add() it.
201  */
202 static inline int list_empty_careful(const struct list_head *head)
203 {
204         struct list_head *next = head->next;
205         return (next == head) && (next == head->prev);
206 }
207
208 static inline void __list_splice(struct list_head *list,
209                                  struct list_head *head)
210 {
211         struct list_head *first = list->next;
212         struct list_head *last = list->prev;
213         struct list_head *at = head->next;
214
215         first->prev = head;
216         head->next = first;
217
218         last->next = at;
219         at->prev = last;
220 }
221
222 /**
223  * list_splice - join two lists
224  * @list: the new list to add.
225  * @head: the place to add it in the first list.
226  */
227 static inline void list_splice(struct list_head *list, struct list_head *head)
228 {
229         if (!list_empty(list))
230                 __list_splice(list, head);
231 }
232
233 /**
234  * list_splice_init - join two lists and reinitialise the emptied list.
235  * @list: the new list to add.
236  * @head: the place to add it in the first list.
237  *
238  * The list at @list is reinitialised
239  */
240 static inline void list_splice_init(struct list_head *list,
241                                     struct list_head *head)
242 {
243         if (!list_empty(list)) {
244                 __list_splice(list, head);
245                 INIT_LIST_HEAD(list);
246         }
247 }
248
249 /**
250  * list_entry - get the struct for this entry
251  * @ptr:        the &struct list_head pointer.
252  * @type:       the type of the struct this is embedded in.
253  * @member:     the name of the list_struct within the struct.
254  */
255 #define list_entry(ptr, type, member) \
256         container_of(ptr, type, member)
257
258 /**
259  * list_for_each        -       iterate over a list
260  * @pos:        the &struct list_head to use as a loop cursor.
261  * @head:       the head for your list.
262  */
263 #define list_for_each(pos, head) \
264         for (pos = (head)->next; prefetch(pos->next), pos != (head); \
265                 pos = pos->next)
266
267 /**
268  * __list_for_each      -       iterate over a list
269  * @pos:        the &struct list_head to use as a loop cursor.
270  * @head:       the head for your list.
271  *
272  * This variant differs from list_for_each() in that it's the
273  * simplest possible list iteration code, no prefetching is done.
274  * Use this for code that knows the list to be very short (empty
275  * or 1 entry) most of the time.
276  */
277 #define __list_for_each(pos, head) \
278         for (pos = (head)->next; pos != (head); pos = pos->next)
279
280 /**
281  * list_for_each_prev   -       iterate over a list backwards
282  * @pos:        the &struct list_head to use as a loop cursor.
283  * @head:       the head for your list.
284  */
285 #define list_for_each_prev(pos, head) \
286         for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
287                 pos = pos->prev)
288
289 /**
290  * list_for_each_safe - iterate over a list safe against removal of list entry
291  * @pos:        the &struct list_head to use as a loop cursor.
292  * @n:          another &struct list_head to use as temporary storage
293  * @head:       the head for your list.
294  */
295 #define list_for_each_safe(pos, n, head) \
296         for (pos = (head)->next, n = pos->next; pos != (head); \
297                 pos = n, n = pos->next)
298
299 /**
300  * list_for_each_entry  -       iterate over list of given type
301  * @pos:        the type * to use as a loop cursor.
302  * @head:       the head for your list.
303  * @member:     the name of the list_struct within the struct.
304  */
305 #define list_for_each_entry(pos, head, member)                          \
306         for (pos = list_entry((head)->next, typeof(*pos), member);      \
307              prefetch(pos->member.next), &pos->member != (head);        \
308              pos = list_entry(pos->member.next, typeof(*pos), member))
309
310 /**
311  * list_for_each_entry_reverse - iterate backwards over list of given type.
312  * @pos:        the type * to use as a loop cursor.
313  * @head:       the head for your list.
314  * @member:     the name of the list_struct within the struct.
315  */
316 #define list_for_each_entry_reverse(pos, head, member)                  \
317         for (pos = list_entry((head)->prev, typeof(*pos), member);      \
318              prefetch(pos->member.prev), &pos->member != (head);        \
319              pos = list_entry(pos->member.prev, typeof(*pos), member))
320
321 /**
322  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
323  * @pos:        the type * to use as a start point
324  * @head:       the head of the list
325  * @member:     the name of the list_struct within the struct.
326  *
327  * Prepares a pos entry for use as a start point in list_for_each_entry_continue.
328  */
329 #define list_prepare_entry(pos, head, member) \
330         ((pos) ? : list_entry(head, typeof(*pos), member))
331
332 /**
333  * list_for_each_entry_continue - continue iteration over list of given type
334  * @pos:        the type * to use as a loop cursor.
335  * @head:       the head for your list.
336  * @member:     the name of the list_struct within the struct.
337  *
338  * Continue to iterate over list of given type, continuing after
339  * the current position.
340  */
341 #define list_for_each_entry_continue(pos, head, member)                 \
342         for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
343              prefetch(pos->member.next), &pos->member != (head);        \
344              pos = list_entry(pos->member.next, typeof(*pos), member))
345
346 /**
347  * list_for_each_entry_from - iterate over list of given type from the current point
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  * Iterate over list of given type, continuing from current position.
353  */
354 #define list_for_each_entry_from(pos, head, member)                     \
355         for (; prefetch(pos->member.next), &pos->member != (head);      \
356              pos = list_entry(pos->member.next, typeof(*pos), member))
357
358 /**
359  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
360  * @pos:        the type * to use as a loop cursor.
361  * @n:          another type * to use as temporary storage
362  * @head:       the head for your list.
363  * @member:     the name of the list_struct within the struct.
364  */
365 #define list_for_each_entry_safe(pos, n, head, member)                  \
366         for (pos = list_entry((head)->next, typeof(*pos), member),      \
367                 n = list_entry(pos->member.next, typeof(*pos), member); \
368              &pos->member != (head);                                    \
369              pos = n, n = list_entry(n->member.next, typeof(*n), member))
370
371 /**
372  * list_for_each_entry_safe_continue
373  * @pos:        the type * to use as a loop cursor.
374  * @n:          another type * to use as temporary storage
375  * @head:       the head for your list.
376  * @member:     the name of the list_struct within the struct.
377  *
378  * Iterate over list of given type, continuing after current point,
379  * safe against removal of list entry.
380  */
381 #define list_for_each_entry_safe_continue(pos, n, head, member)                 \
382         for (pos = list_entry(pos->member.next, typeof(*pos), member),          \
383                 n = list_entry(pos->member.next, typeof(*pos), member);         \
384              &pos->member != (head);                                            \
385              pos = n, n = list_entry(n->member.next, typeof(*n), member))
386
387 /**
388  * list_for_each_entry_safe_from
389  * @pos:        the type * to use as a loop cursor.
390  * @n:          another type * to use as temporary storage
391  * @head:       the head for your list.
392  * @member:     the name of the list_struct within the struct.
393  *
394  * Iterate over list of given type from current point, safe against
395  * removal of list entry.
396  */
397 #define list_for_each_entry_safe_from(pos, n, head, member)                     \
398         for (n = list_entry(pos->member.next, typeof(*pos), member);            \
399              &pos->member != (head);                                            \
400              pos = n, n = list_entry(n->member.next, typeof(*n), member))
401
402 /**
403  * list_for_each_entry_safe_reverse
404  * @pos:        the type * to use as a loop cursor.
405  * @n:          another type * to use as temporary storage
406  * @head:       the head for your list.
407  * @member:     the name of the list_struct within the struct.
408  *
409  * Iterate backwards over list of given type, safe against removal
410  * of list entry.
411  */
412 #define list_for_each_entry_safe_reverse(pos, n, head, member)          \
413         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
414                 n = list_entry(pos->member.prev, typeof(*pos), member); \
415              &pos->member != (head);                                    \
416              pos = n, n = list_entry(n->member.prev, typeof(*n), member))
417
418 #endif