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 * @param new_node new entry to be added
59 * @param 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 * @param new_node new entry to be added
72 * @param 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 * @param entry the element to delete from the list.
100 * list_empty on entry does not return true after this, the entry is
101 * in an undefined state.
103 static INLINE void list_del(struct list_head *entry)
105 __list_del(entry->prev, entry->next);
112 * list_del_init - deletes entry from list and reinitialize it.
113 * @param entry the element to delete from the list.
115 static INLINE void list_del_init(struct list_head *entry)
117 __list_del(entry->prev, entry->next);
118 INIT_LIST_HEAD(entry);
122 * list_move - delete from one list and add as another's head
123 * @param list the entry to move
124 * @param head the head that will precede our entry
126 static INLINE void list_move(struct list_head *list, struct list_head *head)
128 __list_del(list->prev, list->next);
129 list_add(list, head);
133 * list_move_tail - delete from one list and add as another's tail
134 * @param list the entry to move
135 * @param head the head that will follow our entry
137 static INLINE void list_move_tail(struct list_head *list,
138 struct list_head *head)
140 __list_del(list->prev, list->next);
141 list_add_tail(list, head);
145 * list_empty - tests whether a list is empty
146 * @param head the list to test.
148 static INLINE int list_empty(const struct list_head *head)
150 return head->next == head;
153 static INLINE void __list_splice(struct list_head *list,
154 struct list_head *head)
156 struct list_head *first = list->next;
157 struct list_head *last = list->prev;
158 struct list_head *at = head->next;
168 * list_splice - join two lists
169 * @param list the new list to add.
170 * @param head the place to add it in the first list.
172 static INLINE void list_splice(struct list_head *list, struct list_head *head)
174 if (!list_empty(list))
175 __list_splice(list, head);
179 * list_splice_init - join two lists and reinitialize the emptied list.
180 * @param list the new list to add.
181 * @param head the place to add it in the first list.
183 * The list at list is reinitialized
185 static INLINE void list_splice_init(struct list_head *list,
186 struct list_head *head)
188 if (!list_empty(list)) {
189 __list_splice(list, head);
190 INIT_LIST_HEAD(list);
195 * list_entry - get the struct for this entry
196 * @param ptr the &struct list_head pointer.
197 * @param type the type of the struct this is embedded in.
198 * @param member the name of the list_struct within the struct.
200 #define list_entry(ptr, type, member) \
201 _list_container_of(ptr, type, member)
204 * list_for_each - iterate over a list
205 * @param pos the &struct list_head to use as a loop counter.
206 * @param head the head for your list.
208 #define list_for_each(pos, head) \
209 for (pos = (head)->next; pos != (head); pos = pos->next)
212 * __list_for_each - iterate over a list
213 * @param pos the &struct list_head to use as a loop counter.
214 * @param head the head for your list.
216 * This variant differs from list_for_each() in that it's the
217 * simplest possible list iteration code, no ing is done.
218 * Use this for code that knows the list to be very short (empty
219 * or 1 entry) most of the time.
221 #define __list_for_each(pos, head) \
222 for (pos = (head)->next; pos != (head); pos = pos->next)
225 * list_for_each_prev - iterate over a list backwards
226 * @param pos the &struct list_head to use as a loop counter.
227 * @param head the head for your list.
229 #define list_for_each_prev(pos, head) \
230 for (pos = (head)->prev; pos != (head); pos = pos->prev)
233 * list_for_each_safe - iterate over a list safe against removal of list entry
234 * @param pos the &struct list_head to use as a loop counter.
235 * @param n another &struct list_head to use as temporary storage
236 * @param 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 * @param type the type of the struct where the listhead is embedded in
245 * @param pos the type * to use as a loop counter.
246 * @param head the head for your list.
247 * @param member the name of the list_struct within the struct.
249 #define list_for_each_entry(type, pos, head, member) \
250 for (pos = list_entry((head)->next, type, member); \
251 &pos->member != (head); \
252 pos = list_entry(pos->member.next, type, member))
255 * list_for_each_entry_reverse - iterate backwards over list of given type.
256 * @param type the type of the struct where the listhead is embedded in
257 * @param pos the type * to use as a loop counter.
258 * @param head the head for your list.
259 * @param member the name of the list_struct within the struct.
261 #define list_for_each_entry_reverse(type, pos, head, member) \
262 for (pos = list_entry((head)->prev, type, member); \
263 &pos->member != (head); \
264 pos = list_entry(pos->member.prev, type, member))
268 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
269 * @param type the type of the struct where the listhead is embedded in
270 * @param pos the type * to use as a loop counter.
271 * @param n another type * to use as temporary storage
272 * @param head the head for your list.
273 * @param member the name of the list_struct within the struct.
275 #define list_for_each_entry_safe(type, pos, n, head, member) \
276 for (pos = list_entry((head)->next, type, member), \
277 n = list_entry(pos->member.next, type, member); \
278 &pos->member != (head); \
279 pos = n, n = list_entry(n->member.next, type, member))