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