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