Import source
[hvf.git] / include / list.h
blob3202369d86015e63228d9b9cd9e2ded8fd59fd45
1 /*
2 * Based on list.h from Linux Kernel
3 */
5 #ifndef _LINUX_LIST_H
6 #define _LINUX_LIST_H
8 #define LIST_POISON1 ((void*) 0x8585858585858585)
9 #define LIST_POISON2 ((void*) 0x8686868686868686)
12 * Simple doubly linked list implementation.
14 * Some of the internal functions ("__xxx") are useful when
15 * manipulating whole lists rather than single entries, as
16 * sometimes we already know the next/prev entries and we can
17 * generate better code by using them directly rather than
18 * using the generic single-entry routines.
21 struct list_head {
22 struct list_head *next, *prev;
25 #define LIST_HEAD_INIT(name) { &(name), &(name) }
27 #define LIST_HEAD(name) \
28 struct list_head name = LIST_HEAD_INIT(name)
30 static inline void INIT_LIST_HEAD(struct list_head *list)
32 list->next = list;
33 list->prev = list;
37 * Insert a new entry between two known consecutive entries.
39 * This is only for internal list manipulation where we know
40 * the prev/next entries already!
42 static inline void __list_add(struct list_head *new,
43 struct list_head *prev,
44 struct list_head *next)
46 next->prev = new;
47 new->next = next;
48 new->prev = prev;
49 prev->next = new;
52 /**
53 * list_add - add a new entry
54 * @new: new entry to be added
55 * @head: list head to add it after
57 * Insert a new entry after the specified head.
58 * This is good for implementing stacks.
60 static inline void list_add(struct list_head *new, struct list_head *head)
62 __list_add(new, head, head->next);
66 /**
67 * list_add_tail - add a new entry
68 * @new: new entry to be added
69 * @head: list head to add it before
71 * Insert a new entry before the specified head.
72 * This is useful for implementing queues.
74 static inline void list_add_tail(struct list_head *new, struct list_head *head)
76 __list_add(new, head->prev, head);
80 * Delete a list entry by making the prev/next entries
81 * point to each other.
83 * This is only for internal list manipulation where we know
84 * the prev/next entries already!
86 static inline void __list_del(struct list_head * prev, struct list_head * next)
88 next->prev = prev;
89 prev->next = next;
92 /**
93 * list_del - deletes entry from list.
94 * @entry: the element to delete from the list.
95 * Note: list_empty() on entry does not return true after this, the entry is
96 * in an undefined state.
98 static inline void list_del(struct list_head *entry)
100 __list_del(entry->prev, entry->next);
101 entry->next = LIST_POISON1;
102 entry->prev = LIST_POISON2;
106 * list_replace - replace old entry by new one
107 * @old : the element to be replaced
108 * @new : the new element to insert
110 * If @old was empty, it will be overwritten.
112 static inline void list_replace(struct list_head *old,
113 struct list_head *new)
115 new->next = old->next;
116 new->next->prev = new;
117 new->prev = old->prev;
118 new->prev->next = new;
121 static inline void list_replace_init(struct list_head *old,
122 struct list_head *new)
124 list_replace(old, new);
125 INIT_LIST_HEAD(old);
129 * list_del_init - deletes entry from list and reinitialize it.
130 * @entry: the element to delete from the list.
132 static inline void list_del_init(struct list_head *entry)
134 __list_del(entry->prev, entry->next);
135 INIT_LIST_HEAD(entry);
139 * list_move - delete from one list and add as another's head
140 * @list: the entry to move
141 * @head: the head that will precede our entry
143 static inline void list_move(struct list_head *list, struct list_head *head)
145 __list_del(list->prev, list->next);
146 list_add(list, head);
150 * list_move_tail - delete from one list and add as another's tail
151 * @list: the entry to move
152 * @head: the head that will follow our entry
154 static inline void list_move_tail(struct list_head *list,
155 struct list_head *head)
157 __list_del(list->prev, list->next);
158 list_add_tail(list, head);
162 * list_is_last - tests whether @list is the last entry in list @head
163 * @list: the entry to test
164 * @head: the head of the list
166 static inline int list_is_last(const struct list_head *list,
167 const struct list_head *head)
169 return list->next == head;
173 * list_empty - tests whether a list is empty
174 * @head: the list to test.
176 static inline int list_empty(const struct list_head *head)
178 return head->next == head;
182 * list_empty_careful - tests whether a list is empty and not being modified
183 * @head: the list to test
185 * Description:
186 * tests whether a list is empty _and_ checks that no other CPU might be
187 * in the process of modifying either member (next or prev)
189 * NOTE: using list_empty_careful() without synchronization
190 * can only be safe if the only activity that can happen
191 * to the list entry is list_del_init(). Eg. it cannot be used
192 * if another CPU could re-list_add() it.
194 static inline int list_empty_careful(const struct list_head *head)
196 struct list_head *next = head->next;
197 return (next == head) && (next == head->prev);
200 static inline void __list_splice(struct list_head *list,
201 struct list_head *head)
203 struct list_head *first = list->next;
204 struct list_head *last = list->prev;
205 struct list_head *at = head->next;
207 first->prev = head;
208 head->next = first;
210 last->next = at;
211 at->prev = last;
215 * list_splice - join two lists
216 * @list: the new list to add.
217 * @head: the place to add it in the first list.
219 static inline void list_splice(struct list_head *list, struct list_head *head)
221 if (!list_empty(list))
222 __list_splice(list, head);
226 * list_splice_init - join two lists and reinitialise the emptied list.
227 * @list: the new list to add.
228 * @head: the place to add it in the first list.
230 * The list at @list is reinitialised
232 static inline void list_splice_init(struct list_head *list,
233 struct list_head *head)
235 if (!list_empty(list)) {
236 __list_splice(list, head);
237 INIT_LIST_HEAD(list);
242 * list_entry - get the struct for this entry
243 * @ptr: the &struct list_head pointer.
244 * @type: the type of the struct this is embedded in.
245 * @member: the name of the list_struct within the struct.
247 #define list_entry(ptr, type, member) \
248 container_of(ptr, type, member)
251 * list_first_entry - get the first element from a list
252 * @ptr: the list head to take the element from.
253 * @type: the type of the struct this is embedded in.
254 * @member: the name of the list_struct within the struct.
256 * Note, that list is expected to be not empty.
258 #define list_first_entry(ptr, type, member) \
259 list_entry((ptr)->next, type, member)
262 * list_for_each - iterate over a list
263 * @pos: the &struct list_head to use as a loop cursor.
264 * @head: the head for your list.
266 #define list_for_each(pos, head) \
267 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
268 pos = pos->next)
271 * __list_for_each - iterate over a list
272 * @pos: the &struct list_head to use as a loop cursor.
273 * @head: the head for your list.
275 * This variant differs from list_for_each() in that it's the
276 * simplest possible list iteration code, no prefetching is done.
277 * Use this for code that knows the list to be very short (empty
278 * or 1 entry) most of the time.
280 #define __list_for_each(pos, head) \
281 for (pos = (head)->next; pos != (head); pos = pos->next)
284 * list_for_each_prev - iterate over a list backwards
285 * @pos: the &struct list_head to use as a loop cursor.
286 * @head: the head for your list.
288 #define list_for_each_prev(pos, head) \
289 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
290 pos = pos->prev)
293 * list_for_each_safe - iterate over a list safe against removal of list entry
294 * @pos: the &struct list_head to use as a loop cursor.
295 * @n: another &struct list_head to use as temporary storage
296 * @head: the head for your list.
298 #define list_for_each_safe(pos, n, head) \
299 for (pos = (head)->next, n = pos->next; pos != (head); \
300 pos = n, n = pos->next)
303 * list_for_each_entry - iterate over list of given type
304 * @pos: the type * to use as a loop cursor.
305 * @head: the head for your list.
306 * @member: the name of the list_struct within the struct.
308 #define list_for_each_entry(pos, head, member) \
309 for (pos = list_entry((head)->next, typeof(*pos), member); \
310 prefetch(pos->member.next), &pos->member != (head); \
311 pos = list_entry(pos->member.next, typeof(*pos), member))
314 * list_for_each_entry_reverse - iterate backwards over list of given type.
315 * @pos: the type * to use as a loop cursor.
316 * @head: the head for your list.
317 * @member: the name of the list_struct within the struct.
319 #define list_for_each_entry_reverse(pos, head, member) \
320 for (pos = list_entry((head)->prev, typeof(*pos), member); \
321 prefetch(pos->member.prev), &pos->member != (head); \
322 pos = list_entry(pos->member.prev, typeof(*pos), member))
325 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
326 * @pos: the type * to use as a start point
327 * @head: the head of the list
328 * @member: the name of the list_struct within the struct.
330 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
332 #define list_prepare_entry(pos, head, member) \
333 ((pos) ? : list_entry(head, typeof(*pos), member))
336 * list_for_each_entry_continue - continue iteration over list of given type
337 * @pos: the type * to use as a loop cursor.
338 * @head: the head for your list.
339 * @member: the name of the list_struct within the struct.
341 * Continue to iterate over list of given type, continuing after
342 * the current position.
344 #define list_for_each_entry_continue(pos, head, member) \
345 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
346 prefetch(pos->member.next), &pos->member != (head); \
347 pos = list_entry(pos->member.next, typeof(*pos), member))
350 * list_for_each_entry_from - iterate over list of given type from the current point
351 * @pos: the type * to use as a loop cursor.
352 * @head: the head for your list.
353 * @member: the name of the list_struct within the struct.
355 * Iterate over list of given type, continuing from current position.
357 #define list_for_each_entry_from(pos, head, member) \
358 for (; prefetch(pos->member.next), &pos->member != (head); \
359 pos = list_entry(pos->member.next, typeof(*pos), member))
362 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
363 * @pos: the type * to use as a loop cursor.
364 * @n: another type * to use as temporary storage
365 * @head: the head for your list.
366 * @member: the name of the list_struct within the struct.
368 #define list_for_each_entry_safe(pos, n, head, member) \
369 for (pos = list_entry((head)->next, typeof(*pos), member), \
370 n = list_entry(pos->member.next, typeof(*pos), member); \
371 &pos->member != (head); \
372 pos = n, n = list_entry(n->member.next, typeof(*n), member))
375 * list_for_each_entry_safe_continue
376 * @pos: the type * to use as a loop cursor.
377 * @n: another type * to use as temporary storage
378 * @head: the head for your list.
379 * @member: the name of the list_struct within the struct.
381 * Iterate over list of given type, continuing after current point,
382 * safe against removal of list entry.
384 #define list_for_each_entry_safe_continue(pos, n, head, member) \
385 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
386 n = list_entry(pos->member.next, typeof(*pos), member); \
387 &pos->member != (head); \
388 pos = n, n = list_entry(n->member.next, typeof(*n), member))
391 * list_for_each_entry_safe_from
392 * @pos: the type * to use as a loop cursor.
393 * @n: another type * to use as temporary storage
394 * @head: the head for your list.
395 * @member: the name of the list_struct within the struct.
397 * Iterate over list of given type from current point, safe against
398 * removal of list entry.
400 #define list_for_each_entry_safe_from(pos, n, head, member) \
401 for (n = list_entry(pos->member.next, typeof(*pos), member); \
402 &pos->member != (head); \
403 pos = n, n = list_entry(n->member.next, typeof(*n), member))
406 * list_for_each_entry_safe_reverse
407 * @pos: the type * to use as a loop cursor.
408 * @n: another type * to use as temporary storage
409 * @head: the head for your list.
410 * @member: the name of the list_struct within the struct.
412 * Iterate backwards over list of given type, safe against removal
413 * of list entry.
415 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
416 for (pos = list_entry((head)->prev, typeof(*pos), member), \
417 n = list_entry(pos->member.prev, typeof(*pos), member); \
418 &pos->member != (head); \
419 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
421 #endif