doxify list.h
[paraslash.git] / list.h
1 /*
2  * Copied from the Linux kernel source tree, version 2.6.13.
3  *
4  * Licensed under the GPL v2 as per the whole kernel source tree.
5  *
6  */
7
8 #include <stddef.h> /* offsetof */
9
10 #define container_of(ptr, type, member) ({                      \
11         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
12         (type *)( (char *)__mptr - offsetof(type,member) );})
13
14 /*
15  * These are non-NULL pointers that will result in page faults
16  * under normal circumstances, used to verify that nobody uses
17  * non-initialized list entries.
18  */
19 #define LIST_POISON1  ((void *) 0x00100100)
20 #define LIST_POISON2  ((void *) 0x00200200)
21
22 /**
23  * Simple doubly linked list implementation.
24  *
25  * Some of the internal functions ("__xxx") are useful when
26  * manipulating whole lists rather than single entries, as
27  * sometimes we already know the next/prev entries and we can
28  * generate better code by using them directly rather than
29  * using the generic single-entry routines.
30  */
31 struct list_head {
32         struct list_head *next, *prev;
33 };
34
35 #define INIT_LIST_HEAD(ptr) do { \
36         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
37 } while (0)
38
39 /*
40  * Insert a new entry between two known consecutive entries.
41  *
42  * This is only for internal list manipulation where we know
43  * the prev/next entries already!
44  */
45 static inline void __list_add(struct list_head *new,
46                               struct list_head *prev,
47                               struct list_head *next)
48 {
49         next->prev = new;
50         new->next = next;
51         new->prev = prev;
52         prev->next = new;
53 }
54
55 /**
56  * add a new entry
57  *
58  * \param new new entry to be added
59  * \param head list head to add it after
60  *
61  * Insert a new entry after the specified head.
62  * This is good for implementing stacks.
63  */
64 static inline void para_list_add(struct list_head *new, struct list_head *head)
65 {
66         __list_add(new, head, head->next);
67 }
68
69 /**
70  * add a new entry
71  *
72  * \param new new entry to be added
73  * \param head list head to add it before
74  *
75  * Insert a new entry before the specified head.
76  * This is useful for implementing queues.
77  */
78 static inline void list_add_tail(struct list_head *new, struct list_head *head)
79 {
80         __list_add(new, head->prev, head);
81 }
82
83 /*
84  * Delete a list entry by making the prev/next entries
85  * point to each other.
86  *
87  * This is only for internal list manipulation where we know
88  * the prev/next entries already!
89  */
90 static inline void __list_del(struct list_head * prev, struct list_head * next)
91 {
92         next->prev = prev;
93         prev->next = next;
94 }
95
96 /**
97  * Delete entry from list.
98  *
99  * \param entry the element to delete from the list.
100  *
101  * Note: list_empty on entry does not return true after this, the entry is
102  * in an undefined state.
103  */
104 static inline void list_del(struct list_head *entry)
105 {
106         __list_del(entry->prev, entry->next);
107         entry->next = LIST_POISON1;
108         entry->prev = LIST_POISON2;
109 }
110
111 /**
112  * delete from one list and add as another's head
113  *
114  * \param list: the entry to move
115  * \param head: the head that will precede our entry
116  */
117 static inline void list_move(struct list_head *list, struct list_head *head)
118 {
119         __list_del(list->prev, list->next);
120         para_list_add(list, head);
121 }
122
123 /**
124  * test whether a list is empty
125  *
126  * \param head the list to test.
127  */
128 static inline int list_empty(const struct list_head *head)
129 {
130         return head->next == head;
131 }
132
133 /**
134  * get the struct for this entry
135  *
136  * \param ptr the &struct list_head pointer.
137  * \param type the type of the struct this is embedded in.
138  * \param member the name of the list_struct within the struct.
139  */
140 #define list_entry(ptr, type, member) \
141         container_of(ptr, type, member)
142
143 /**
144  * iterate over a list safe against removal of list entry
145  *
146  * \param pos the &struct list_head to use as a loop counter.
147  * \param n another &struct list_head to use as temporary storage
148  * \param head the head for your list.
149  */
150 #define list_for_each_safe(pos, n, head) \
151         for (pos = (head)->next, n = pos->next; pos != (head); \
152                 pos = n, n = pos->next)
153
154 /**
155  * iterate over list of given type
156  *
157  * \param pos the type * to use as a loop counter.
158  * \param head the head for your list.
159  * \param member the name of the list_struct within the struct.
160  */
161 #define list_for_each_entry(pos, head, member)                          \
162         for (pos = list_entry((head)->next, typeof(*pos), member);      \
163              &pos->member != (head);    \
164              pos = list_entry(pos->member.next, typeof(*pos), member))
165
166 /**
167  * iterate over list of given type safe against removal of list entry
168  *
169  * \param pos the type * to use as a loop counter.
170  * \param n another type * to use as temporary storage
171  * \param head the head for your list.
172  * \param member the name of the list_struct within the struct.
173  */
174 #define list_for_each_entry_safe(pos, n, head, member)                  \
175         for (pos = list_entry((head)->next, typeof(*pos), member),      \
176                 n = list_entry(pos->member.next, typeof(*pos), member); \
177              &pos->member != (head);                                    \
178              pos = n, n = list_entry(n->member.next, typeof(*n), member))
179 /**
180  * iterate backwards over list of given type safe against removal of list entry
181  * \param pos the type * to use as a loop counter.
182  * \param n another type * to use as temporary storage
183  * \param head the head for your list.
184  * \param member the name of the list_struct within the struct.
185  */
186 #define list_for_each_entry_safe_reverse(pos, n, head, member)          \
187         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
188                 n = list_entry(pos->member.prev, typeof(*pos), member); \
189              &pos->member != (head);                                    \
190              pos = n, n = list_entry(n->member.prev, typeof(*n), member))