3 * Shamelessly adapted from the linux kernel.
9 #include "firm_config.h"
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.
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 #define INIT_LIST_HEAD(ptr) do { \
31 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
34 #define _list_offsetof(type,member) \
35 ((char *) &(((type *) 0)->member) - (char *) 0)
37 #define _list_container_of(ptr, type, member) \
38 ((type *) ((char *) (ptr) - _list_offsetof(type, member)))
41 * Insert a new entry between two known consecutive entries.
43 * This is only for internal list manipulation where we know
44 * the prev/next entries already!
46 static INLINE void __list_add(struct list_head *new_node,
47 struct list_head *prev,
48 struct list_head *next)
50 next->prev = new_node;
51 new_node->next = next;
52 new_node->prev = prev;
53 prev->next = new_node;
57 * list_add - add a new entry
58 * @new: new entry to be added
59 * @head: list head to add it after
61 * Insert a new entry after the specified head.
62 * This is good for implementing stacks.
64 static INLINE void list_add(struct list_head *new_node, struct list_head *head)
66 __list_add(new_node, head, head->next);
70 * list_add_tail - add a new entry
71 * @new: new entry to be added
72 * @head: list head to add it before
74 * Insert a new entry before the specified head.
75 * This is useful for implementing queues.
77 static INLINE void list_add_tail(struct list_head *new_node, struct list_head *head)
79 __list_add(new_node, head->prev, head);
83 * Delete a list entry by making the prev/next entries
84 * point to each other.
86 * This is only for internal list manipulation where we know
87 * the prev/next entries already!
89 static INLINE void __list_del(struct list_head * prev, struct list_head * next)
96 * list_del - deletes entry from list.
97 * @entry: the element to delete from the list.
98 * Note: list_empty on entry does not return true after this, the entry is
99 * in an undefined state.
101 static INLINE void list_del(struct list_head *entry)
103 __list_del(entry->prev, entry->next);
110 * list_del_init - deletes entry from list and reinitialize it.
111 * @entry: the element to delete from the list.
113 static INLINE void list_del_init(struct list_head *entry)
115 __list_del(entry->prev, entry->next);
116 INIT_LIST_HEAD(entry);
120 * list_move - delete from one list and add as another's head
121 * @list: the entry to move
122 * @head: the head that will precede our entry
124 static INLINE void list_move(struct list_head *list, struct list_head *head)
126 __list_del(list->prev, list->next);
127 list_add(list, head);
131 * list_move_tail - delete from one list and add as another's tail
132 * @list: the entry to move
133 * @head: the head that will follow our entry
135 static INLINE void list_move_tail(struct list_head *list,
136 struct list_head *head)
138 __list_del(list->prev, list->next);
139 list_add_tail(list, head);
143 * list_empty - tests whether a list is empty
144 * @head: the list to test.
146 static INLINE int list_empty(const struct list_head *head)
148 return head->next == head;
151 static INLINE void __list_splice(struct list_head *list,
152 struct list_head *head)
154 struct list_head *first = list->next;
155 struct list_head *last = list->prev;
156 struct list_head *at = head->next;
166 * list_splice - join two lists
167 * @list: the new list to add.
168 * @head: the place to add it in the first list.
170 static INLINE void list_splice(struct list_head *list, struct list_head *head)
172 if (!list_empty(list))
173 __list_splice(list, head);
177 * list_splice_init - join two lists and reinitialise the emptied list.
178 * @list: the new list to add.
179 * @head: the place to add it in the first list.
181 * The list at @list is reinitialised
183 static INLINE void list_splice_init(struct list_head *list,
184 struct list_head *head)
186 if (!list_empty(list)) {
187 __list_splice(list, head);
188 INIT_LIST_HEAD(list);
193 * list_entry - get the struct for this entry
194 * @ptr: the &struct list_head pointer.
195 * @type: the type of the struct this is embedded in.
196 * @member: the name of the list_struct within the struct.
198 #define list_entry(ptr, type, member) \
199 _list_container_of(ptr, type, member)
202 * list_for_each - iterate over a list
203 * @pos: the &struct list_head to use as a loop counter.
204 * @head: the head for your list.
206 #define list_for_each(pos, head) \
207 for (pos = (head)->next, (pos->next); pos != (head); \
208 pos = pos->next, (pos->next))
211 * __list_for_each - iterate over a list
212 * @pos: the &struct list_head to use as a loop counter.
213 * @head: the head for your list.
215 * This variant differs from list_for_each() in that it's the
216 * simplest possible list iteration code, no ing is done.
217 * Use this for code that knows the list to be very short (empty
218 * or 1 entry) most of the time.
220 #define __list_for_each(pos, head) \
221 for (pos = (head)->next; pos != (head); pos = pos->next)
224 * list_for_each_prev - iterate over a list backwards
225 * @pos: the &struct list_head to use as a loop counter.
226 * @head: the head for your list.
228 #define list_for_each_prev(pos, head) \
229 for (pos = (head)->prev, (pos->prev); pos != (head); \
230 pos = pos->prev, (pos->prev))
233 * list_for_each_safe - iterate over a list safe against removal of list entry
234 * @pos: the &struct list_head to use as a loop counter.
235 * @n: another &struct list_head to use as temporary storage
236 * @head: the head for your list.
238 #define list_for_each_safe(pos, n, head) \
239 for (pos = (head)->next, n = pos->next; pos != (head); \
240 pos = n, n = pos->next)
243 * list_for_each_entry - iterate over list of given type
244 * @pos: the type * to use as a loop counter.
245 * @head: the head for your list.
246 * @member: the name of the list_struct within the struct.
248 #define list_for_each_entry(type, pos, head, member) \
249 for (pos = list_entry((head)->next, type, member); \
250 &pos->member != (head); \
251 pos = list_entry(pos->member.next, type, member))
254 * list_for_each_entry_reverse - iterate backwards over list of given type.
255 * @pos: the type * to use as a loop counter.
256 * @head: the head for your list.
257 * @member: the name of the list_struct within the struct.
259 #define list_for_each_entry_reverse(type, pos, head, member) \
260 for (pos = list_entry((head)->prev, type, member); \
261 &pos->member != (head); \
262 pos = list_entry(pos->member.prev, type, member))
266 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
267 * @pos: the type * to use as a loop counter.
268 * @n: another type * to use as temporary storage
269 * @head: the head for your list.
270 * @member: the name of the list_struct within the struct.
272 #define list_for_each_entry_safe(type, pos, n, head, member) \
273 for (pos = list_entry((head)->next, type, member), \
274 n = list_entry(pos->member.next, type, member); \
275 &pos->member != (head); \
276 pos = n, n = list_entry(n->member.next, type, member))