4 * @brief Doubly linked lists.
7 * Simple doubly linked list implementation.
9 * Some of the internal functions ("__xxx") are useful when
10 * manipulating whole lists rather than single entries, as
11 * sometimes we already know the next/prev entries and we can
12 * generate better code by using them directly rather than
13 * using the generic single-entry routines.
15 #ifndef FIRM_ADT_LIST_H
16 #define FIRM_ADT_LIST_H
18 #include "firm_config.h"
21 struct list_head *next, *prev;
24 #define LIST_HEAD_INIT(name) { &(name), &(name) }
26 #define LIST_HEAD(name) \
27 struct list_head name = LIST_HEAD_INIT(name)
29 #define INIT_LIST_HEAD(ptr) do { \
30 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
33 #define _list_offsetof(type,member) \
34 ((char *) &(((type *) 0)->member) - (char *) 0)
36 #define _list_container_of(ptr, type, member) \
37 ((type *) ((char *) (ptr) - _list_offsetof(type, member)))
40 * Insert a new entry between two known consecutive entries.
42 * This is only for internal list manipulation where we know
43 * the prev/next entries already!
45 static INLINE void __list_add(struct list_head *new_node,
46 struct list_head *prev,
47 struct list_head *next)
49 next->prev = new_node;
50 new_node->next = next;
51 new_node->prev = prev;
52 prev->next = new_node;
56 * list_add - add a new entry
57 * @param new_node new entry to be added
58 * @param head list head to add it after
60 * Insert a new entry after the specified head.
61 * This is good for implementing stacks.
63 static INLINE void list_add(struct list_head *new_node, struct list_head *head)
65 __list_add(new_node, head, head->next);
69 * list_add_tail - add a new entry
70 * @param new_node new entry to be added
71 * @param head list head to add it before
73 * Insert a new entry before the specified head.
74 * This is useful for implementing queues.
76 static INLINE void list_add_tail(struct list_head *new_node, struct list_head *head)
78 __list_add(new_node, head->prev, head);
82 * Delete a list entry by making the prev/next entries
83 * point to each other.
85 * This is only for internal list manipulation where we know
86 * the prev/next entries already!
88 static INLINE void __list_del(struct list_head * prev, struct list_head * next)
95 * list_del - deletes entry from list.
96 * @param entry the element to delete from the list.
99 * list_empty on entry does not return true after this, the entry is
100 * in an undefined state.
102 static INLINE void list_del(struct list_head *entry)
104 __list_del(entry->prev, entry->next);
111 * list_del_init - deletes entry from list and reinitialize it.
112 * @param entry the element to delete from the list.
114 static INLINE void list_del_init(struct list_head *entry)
116 __list_del(entry->prev, entry->next);
117 INIT_LIST_HEAD(entry);
121 * list_move - delete from one list and add as another's head
122 * @param list the entry to move
123 * @param head the head that will precede our entry
125 static INLINE void list_move(struct list_head *list, struct list_head *head)
127 __list_del(list->prev, list->next);
128 list_add(list, head);
132 * list_move_tail - delete from one list and add as another's tail
133 * @param list the entry to move
134 * @param head the head that will follow our entry
136 static INLINE void list_move_tail(struct list_head *list,
137 struct list_head *head)
139 __list_del(list->prev, list->next);
140 list_add_tail(list, head);
144 * list_empty - tests whether a list is empty
145 * @param head the list to test.
147 static INLINE int list_empty(const struct list_head *head)
149 return head->next == head;
152 static INLINE void __list_splice(struct list_head *list,
153 struct list_head *head)
155 struct list_head *first = list->next;
156 struct list_head *last = list->prev;
157 struct list_head *at = head->next;
167 * list_splice - join two lists
168 * @param list the new list to add.
169 * @param head the place to add it in the first list.
171 static INLINE void list_splice(struct list_head *list, struct list_head *head)
173 if (!list_empty(list))
174 __list_splice(list, head);
178 * list_splice_init - join two lists and reinitialize the emptied list.
179 * @param list the new list to add.
180 * @param head the place to add it in the first list.
182 * The list at list is reinitialized
184 static INLINE void list_splice_init(struct list_head *list,
185 struct list_head *head)
187 if (!list_empty(list)) {
188 __list_splice(list, head);
189 INIT_LIST_HEAD(list);
194 * list_entry - get the struct for this entry
195 * @param ptr the &struct list_head pointer.
196 * @param type the type of the struct this is embedded in.
197 * @param member the name of the list_struct within the struct.
199 #define list_entry(ptr, type, member) \
200 _list_container_of(ptr, type, member)
203 * list_for_each - iterate over a list
204 * @param pos the &struct list_head to use as a loop counter.
205 * @param head the head for your list.
207 #define list_for_each(pos, head) \
208 for (pos = (head)->next; pos != (head); pos = pos->next)
211 * __list_for_each - iterate over a list
212 * @param pos the &struct list_head to use as a loop counter.
213 * @param 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 * @param pos the &struct list_head to use as a loop counter.
226 * @param head the head for your list.
228 #define list_for_each_prev(pos, head) \
229 for (pos = (head)->prev; pos != (head); pos = pos->prev)
232 * list_for_each_safe - iterate over a list safe against removal of list entry
233 * @param pos the &struct list_head to use as a loop counter.
234 * @param n another &struct list_head to use as temporary storage
235 * @param head the head for your list.
237 #define list_for_each_safe(pos, n, head) \
238 for (pos = (head)->next, n = pos->next; pos != (head); \
239 pos = n, n = pos->next)
242 * list_for_each_entry - iterate over list of given type
243 * @param type the type of the struct where the listhead is embedded in
244 * @param pos the type * to use as a loop counter.
245 * @param head the head for your list.
246 * @param 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 * @param type the type of the struct where the listhead is embedded in
256 * @param pos the type * to use as a loop counter.
257 * @param head the head for your list.
258 * @param member the name of the list_struct within the struct.
260 #define list_for_each_entry_reverse(type, pos, head, member) \
261 for (pos = list_entry((head)->prev, type, member); \
262 &pos->member != (head); \
263 pos = list_entry(pos->member.prev, type, member))
267 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
268 * @param type the type of the struct where the listhead is embedded in
269 * @param pos the type * to use as a loop counter.
270 * @param n another type * to use as temporary storage
271 * @param head the head for your list.
272 * @param member the name of the list_struct within the struct.
274 #define list_for_each_entry_safe(type, pos, n, head, member) \
275 for (pos = list_entry((head)->next, type, member), \
276 n = list_entry(pos->member.next, type, member); \
277 &pos->member != (head); \
278 pos = n, n = list_entry(n->member.next, type, member))