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 typedef struct list_head list_head;
23 struct list_head *next, *prev;
26 #define LIST_HEAD_INIT(name) { &(name), &(name) }
28 #define LIST_HEAD(name) \
29 struct list_head name = LIST_HEAD_INIT(name)
31 #define INIT_LIST_HEAD(ptr) do { \
32 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
35 #define _list_offsetof(type,member) \
36 ((char *) &(((type *) 0)->member) - (char *) 0)
38 #define _list_container_of(ptr, type, member) \
39 ((type *) ((char *) (ptr) - _list_offsetof(type, member)))
42 * Insert a new entry between two known consecutive entries.
44 * This is only for internal list manipulation where we know
45 * the prev/next entries already!
47 static INLINE void __list_add(struct list_head *new_node,
48 struct list_head *prev,
49 struct list_head *next)
51 next->prev = new_node;
52 new_node->next = next;
53 new_node->prev = prev;
54 prev->next = new_node;
58 * list_add - add a new entry
59 * @param new_node new entry to be added
60 * @param head list head to add it after
62 * Insert a new entry after the specified head.
63 * This is good for implementing stacks.
65 static INLINE void list_add(struct list_head *new_node, struct list_head *head)
67 __list_add(new_node, head, head->next);
71 * list_add_tail - add a new entry
72 * @param new_node new entry to be added
73 * @param head list head to add it before
75 * Insert a new entry before the specified head.
76 * This is useful for implementing queues.
78 static INLINE void list_add_tail(struct list_head *new_node, struct list_head *head)
80 __list_add(new_node, head->prev, head);
84 * Delete a list entry by making the prev/next entries
85 * point to each other.
87 * This is only for internal list manipulation where we know
88 * the prev/next entries already!
90 static INLINE void __list_del(struct list_head * prev, struct list_head * next)
97 * list_del - deletes entry from list.
98 * @param entry the element to delete from the list.
101 * list_empty on entry does not return true after this, the entry is
102 * in an undefined state.
104 static INLINE void list_del(struct list_head *entry)
106 __list_del(entry->prev, entry->next);
113 * list_del_init - deletes entry from list and reinitialize it.
114 * @param 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 INIT_LIST_HEAD(entry);
123 * list_move - delete from one list and add as another's head
124 * @param list the entry to move
125 * @param 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 * @param list the entry to move
136 * @param 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 * @param 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;
169 * list_splice - join two lists
170 * @param list the new list to add.
171 * @param 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 reinitialize the emptied list.
181 * @param list the new list to add.
182 * @param head the place to add it in the first list.
184 * The list at list is reinitialized
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 INIT_LIST_HEAD(list);
196 * list_entry - get the struct for this entry
197 * @param ptr the &struct list_head pointer.
198 * @param type the type of the struct this is embedded in.
199 * @param member the name of the list_struct within the struct.
201 #define list_entry(ptr, type, member) \
202 _list_container_of(ptr, type, member)
205 * list_for_each - iterate over a list
206 * @param pos the &struct list_head to use as a loop counter.
207 * @param head the head for your list.
209 #define list_for_each(pos, head) \
210 for (pos = (head)->next; pos != (head); pos = pos->next)
213 * __list_for_each - iterate over a list
214 * @param pos the &struct list_head to use as a loop counter.
215 * @param head the head for your list.
217 * This variant differs from list_for_each() in that it's the
218 * simplest possible list iteration code, no ing is done.
219 * Use this for code that knows the list to be very short (empty
220 * or 1 entry) most of the time.
222 #define __list_for_each(pos, head) \
223 for (pos = (head)->next; pos != (head); pos = pos->next)
226 * list_for_each_prev - iterate over a list backwards
227 * @param pos the &struct list_head to use as a loop counter.
228 * @param head the head for your list.
230 #define list_for_each_prev(pos, head) \
231 for (pos = (head)->prev; pos != (head); pos = pos->prev)
234 * list_for_each_safe - iterate over a list safe against removal of list entry
235 * @param pos the &struct list_head to use as a loop counter.
236 * @param n another &struct list_head to use as temporary storage
237 * @param head the head for your list.
239 #define list_for_each_safe(pos, n, head) \
240 for (pos = (head)->next, n = pos->next; pos != (head); \
241 pos = n, n = pos->next)
244 * list_for_each_entry - iterate over list of given type
245 * @param type the type of the struct where the listhead is embedded in
246 * @param pos the type * to use as a loop counter.
247 * @param head the head for your list.
248 * @param member the name of the list_struct within the struct.
250 #define list_for_each_entry(type, pos, head, member) \
251 for (pos = list_entry((head)->next, type, member); \
252 &pos->member != (head); \
253 pos = list_entry(pos->member.next, type, member))
256 * list_for_each_entry_reverse - iterate backwards over list of given type.
257 * @param type the type of the struct where the listhead is embedded in
258 * @param pos the type * to use as a loop counter.
259 * @param head the head for your list.
260 * @param member the name of the list_struct within the struct.
262 #define list_for_each_entry_reverse(type, pos, head, member) \
263 for (pos = list_entry((head)->prev, type, member); \
264 &pos->member != (head); \
265 pos = list_entry(pos->member.prev, type, member))
269 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
270 * @param type the type of the struct where the listhead is embedded in
271 * @param pos the type * to use as a loop counter.
272 * @param n another type * to use as temporary storage
273 * @param head the head for your list.
274 * @param member the name of the list_struct within the struct.
276 #define list_for_each_entry_safe(type, pos, n, head, member) \
277 for (pos = list_entry((head)->next, type, member), \
278 n = list_entry(pos->member.next, type, member); \
279 &pos->member != (head); \
280 pos = n, n = list_entry(n->member.next, type, member))