Handle streams separately in tree_add_track()
[cmus.git] / list.h
blobbeaac346b0bc5ca26418810d324937a70ea1030d
1 /* Stolen from Linux 2.6.7 */
2 #ifndef _LINUX_LIST_H
3 #define _LINUX_LIST_H
5 #include <stdlib.h>
7 static inline void prefetch(const void *x)
12 * These are non-NULL pointers that will result in page faults
13 * under normal circumstances, used to verify that nobody uses
14 * non-initialized list entries.
16 #define LIST_POISON1 ((void *) 0x00100100)
17 #define LIST_POISON2 ((void *) 0x00200200)
20 * Simple doubly linked list implementation.
22 * Some of the internal functions ("__xxx") are useful when
23 * manipulating whole lists rather than single entries, as
24 * sometimes we already know the next/prev entries and we can
25 * generate better code by using them directly rather than
26 * using the generic single-entry routines.
29 struct list_head {
30 struct list_head *next, *prev;
33 #define LIST_HEAD_INIT(name) { &(name), &(name) }
35 #define LIST_HEAD(name) \
36 struct list_head name = LIST_HEAD_INIT(name)
38 static inline void list_init(struct list_head *head)
40 head->next = head;
41 head->prev = head;
45 * Insert a new entry between two known consecutive entries.
47 * This is only for internal list manipulation where we know
48 * the prev/next entries already!
50 static inline void __list_add(struct list_head *new,
51 struct list_head *prev,
52 struct list_head *next)
54 next->prev = new;
55 new->next = next;
56 new->prev = prev;
57 prev->next = new;
60 /**
61 * list_add - add a new entry
62 * @new: new entry to be added
63 * @head: list head to add it after
65 * Insert a new entry after the specified head.
66 * This is good for implementing stacks.
68 static inline void list_add(struct list_head *new, struct list_head *head)
70 __list_add(new, head, head->next);
73 /**
74 * list_add_tail - add a new entry
75 * @new: new entry to be added
76 * @head: list head to add it before
78 * Insert a new entry before the specified head.
79 * This is useful for implementing queues.
81 static inline void list_add_tail(struct list_head *new, struct list_head *head)
83 __list_add(new, head->prev, head);
87 * Delete a list entry by making the prev/next entries
88 * point to each other.
90 * This is only for internal list manipulation where we know
91 * the prev/next entries already!
93 static inline void __list_del(struct list_head *prev, struct list_head *next)
95 next->prev = prev;
96 prev->next = next;
99 /**
100 * list_del - deletes entry from list.
101 * @entry: the element to delete from the list.
102 * Note: list_empty on entry does not return true after this, the entry is
103 * in an undefined state.
105 static inline void list_del(struct list_head *entry)
107 __list_del(entry->prev, entry->next);
108 entry->next = LIST_POISON1;
109 entry->prev = LIST_POISON2;
113 * list_del_init - deletes entry from list and reinitialize it.
114 * @entry: the element to delete from the list.
116 static inline void list_del_init(struct list_head *entry)
118 __list_del(entry->prev, entry->next);
119 list_init(entry);
123 * list_move - delete from one list and add as another's head
124 * @list: the entry to move
125 * @head: the head that will precede our entry
127 static inline void list_move(struct list_head *list, struct list_head *head)
129 __list_del(list->prev, list->next);
130 list_add(list, head);
134 * list_move_tail - delete from one list and add as another's tail
135 * @list: the entry to move
136 * @head: the head that will follow our entry
138 static inline void list_move_tail(struct list_head *list,
139 struct list_head *head)
141 __list_del(list->prev, list->next);
142 list_add_tail(list, head);
146 * list_empty - tests whether a list is empty
147 * @head: the list to test.
149 static inline int list_empty(const struct list_head *head)
151 return head->next == head;
154 static inline void __list_splice(struct list_head *list,
155 struct list_head *head)
157 struct list_head *first = list->next;
158 struct list_head *last = list->prev;
159 struct list_head *at = head->next;
161 first->prev = head;
162 head->next = first;
164 last->next = at;
165 at->prev = last;
169 * list_splice - join two lists
170 * @list: the new list to add.
171 * @head: the place to add it in the first list.
173 static inline void list_splice(struct list_head *list, struct list_head *head)
175 if (!list_empty(list))
176 __list_splice(list, head);
180 * list_splice_init - join two lists and reinitialise the emptied list.
181 * @list: the new list to add.
182 * @head: the place to add it in the first list.
184 * The list at @list is reinitialised
186 static inline void list_splice_init(struct list_head *list,
187 struct list_head *head)
189 if (!list_empty(list)) {
190 __list_splice(list, head);
191 list_init(list);
195 #undef offsetof
196 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
199 * container_of - cast a member of a structure out to the containing structure
201 * @ptr: the pointer to the member.
202 * @type: the type of the container struct this is embedded in.
203 * @member: the name of the member within the struct.
206 #define container_of(ptr, type, member) ({ \
207 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
208 (type *)( (char *)__mptr - offsetof(type,member) );})
211 * list_entry - get the struct for this entry
212 * @ptr: the &struct list_head pointer.
213 * @type: the type of the struct this is embedded in.
214 * @member: the name of the list_struct within the struct.
216 #define list_entry(ptr, type, member) \
217 container_of(ptr, type, member)
220 * list_for_each - iterate over a list
221 * @pos: the &struct list_head to use as a loop counter.
222 * @head: the head for your list.
224 #define list_for_each(pos, head) \
225 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
226 pos = pos->next, prefetch(pos->next))
229 * __list_for_each - iterate over a list
230 * @pos: the &struct list_head to use as a loop counter.
231 * @head: the head for your list.
233 * This variant differs from list_for_each() in that it's the
234 * simplest possible list iteration code, no prefetching is done.
235 * Use this for code that knows the list to be very short (empty
236 * or 1 entry) most of the time.
238 #define __list_for_each(pos, head) \
239 for (pos = (head)->next; pos != (head); pos = pos->next)
242 * list_for_each_prev - iterate over a list backwards
243 * @pos: the &struct list_head to use as a loop counter.
244 * @head: the head for your list.
246 #define list_for_each_prev(pos, head) \
247 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
248 pos = pos->prev, prefetch(pos->prev))
251 * list_for_each_safe - iterate over a list safe against removal of list entry
252 * @pos: the &struct list_head to use as a loop counter.
253 * @n: another &struct list_head to use as temporary storage
254 * @head: the head for your list.
256 #define list_for_each_safe(pos, n, head) \
257 for (pos = (head)->next, n = pos->next; pos != (head); \
258 pos = n, n = pos->next)
261 * list_for_each_entry - iterate over list of given type
262 * @pos: the type * to use as a loop counter.
263 * @head: the head for your list.
264 * @member: the name of the list_struct within the struct.
266 #define list_for_each_entry(pos, head, member) \
267 for (pos = list_entry((head)->next, __typeof__(*pos), member), \
268 prefetch(pos->member.next); \
269 &pos->member != (head); \
270 pos = list_entry(pos->member.next, __typeof__(*pos), member), \
271 prefetch(pos->member.next))
274 * list_for_each_entry_reverse - iterate backwards over list of given type.
275 * @pos: the type * to use as a loop counter.
276 * @head: the head for your list.
277 * @member: the name of the list_struct within the struct.
279 #define list_for_each_entry_reverse(pos, head, member) \
280 for (pos = list_entry((head)->prev, __typeof__(*pos), member), \
281 prefetch(pos->member.prev); \
282 &pos->member != (head); \
283 pos = list_entry(pos->member.prev, __typeof__(*pos), member), \
284 prefetch(pos->member.prev))
287 * list_prepare_entry - prepare a pos entry for use as a start point in
288 * list_for_each_entry_continue
289 * @pos: the type * to use as a start point
290 * @head: the head of the list
291 * @member: the name of the list_struct within the struct.
293 #define list_prepare_entry(pos, head, member) \
294 ((pos) ? : list_entry(head, __typeof__(*pos), member))
297 * list_for_each_entry_continue - iterate over list of given type
298 * continuing after existing point
299 * @pos: the type * to use as a loop counter.
300 * @head: the head for your list.
301 * @member: the name of the list_struct within the struct.
303 #define list_for_each_entry_continue(pos, head, member) \
304 for (pos = list_entry(pos->member.next, __typeof__(*pos), member), \
305 prefetch(pos->member.next); \
306 &pos->member != (head); \
307 pos = list_entry(pos->member.next, __typeof__(*pos), member), \
308 prefetch(pos->member.next))
311 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
312 * @pos: the type * to use as a loop counter.
313 * @n: another type * to use as temporary storage
314 * @head: the head for your list.
315 * @member: the name of the list_struct within the struct.
317 #define list_for_each_entry_safe(pos, n, head, member) \
318 for (pos = list_entry((head)->next, __typeof__(*pos), member), \
319 n = list_entry(pos->member.next, __typeof__(*pos), member); \
320 &pos->member != (head); \
321 pos = n, n = list_entry(n->member.next, __typeof__(*n), member))
323 #endif