2 * This file is part of libFirm.
3 * Copyright (C) 2012 Karlsruhe Institute of Technology.
5 #ifndef FIRM_ADT_LIST_H
6 #define FIRM_ADT_LIST_H
14 * @defgroup lists Linked Lists
15 * @brief Doubly linked lists.
17 * Simple doubly linked list implementation.
19 * Some of the internal functions ("__xxx") are useful when
20 * manipulating whole lists rather than single entries, as
21 * sometimes we already know the next/prev entries and we can
22 * generate better code by using them directly rather than
23 * using the generic single-entry routines.
29 * Put this into all list elements and at the place where you want to keep
30 * references to the list. */
31 typedef struct list_head list_head;
33 /** Static initializer for a list header */
34 #define LIST_HEAD_INIT(name) { &(name), &(name) }
36 /** Defines a (static) list reference */
37 #define LIST_HEAD(name) \
38 struct list_head name = LIST_HEAD_INIT(name)
40 /** Initializes a list header */
41 #define INIT_LIST_HEAD(ptr) do { \
42 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
47 struct list_head *next, *prev;
50 #define _list_offsetof(type,member) \
51 ((char *) &(((type *) 0)->member) - (char *) 0)
53 #define _list_container_of(ptr, type, member) \
54 ((type *) ((char *) (ptr) - _list_offsetof(type, member)))
58 * Inserts a new entry between two known consecutive entries.
60 * This is only for internal list manipulation where we know
61 * the prev/next entries already!
63 static inline void __list_add(struct list_head *new_node,
64 struct list_head *prev,
65 struct list_head *next)
67 next->prev = new_node;
68 new_node->next = next;
69 new_node->prev = prev;
70 prev->next = new_node;
75 * @param new_node new entry to be added
76 * @param head list head to add it after
78 * Insert a new entry after the specified head.
79 * This is good for implementing stacks.
81 static inline void list_add(struct list_head *new_node, struct list_head *head)
83 __list_add(new_node, head, head->next);
88 * @param new_node new entry to be added
89 * @param head list head to add it before
91 * Insert a new entry before the specified head.
92 * This is useful for implementing queues.
94 static inline void list_add_tail(struct list_head *new_node, struct list_head *head)
96 __list_add(new_node, head->prev, head);
100 * Deletes a list entry by making the prev/next entries
101 * point to each other.
103 * This is only for internal list manipulation where we know
104 * the prev/next entries already!
106 static inline void __list_del(struct list_head * prev, struct list_head * next)
113 * Deletes entry from list.
114 * @param entry the element to delete from the list.
117 * list_empty on entry does not return true after this, the entry is
118 * in an undefined state.
120 static inline void list_del(struct list_head *entry)
122 __list_del(entry->prev, entry->next);
129 * Deletes entry from list and reinitialize it.
130 * @param entry the element to delete from the list.
132 static inline void list_del_init(struct list_head *entry)
134 __list_del(entry->prev, entry->next);
135 INIT_LIST_HEAD(entry);
139 * Deletes from one list and add as another's head
140 * @param list the entry to move
141 * @param head the head that will precede our entry
143 static inline void list_move(struct list_head *list, struct list_head *head)
145 __list_del(list->prev, list->next);
146 list_add(list, head);
150 * Deletes from one list and add as another's tail
151 * @param list the entry to move
152 * @param head the head that will follow our entry
154 static inline void list_move_tail(struct list_head *list,
155 struct list_head *head)
157 __list_del(list->prev, list->next);
158 list_add_tail(list, head);
162 * Tests whether a list is empty
163 * @param head the list to test.
165 static inline int list_empty(const struct list_head *head)
167 return head->next == head;
171 * Join two nonempty lists.
173 * @note Use list_splice() if @p list is possibly empty.
174 * @param list the new list to add.
175 * @param head the place to add it in the first list.
177 static inline void __list_splice(struct list_head *list,
178 struct list_head *head)
180 struct list_head *first = list->next;
181 struct list_head *last = list->prev;
182 struct list_head *at = head->next;
192 * list_splice - join two lists
193 * @param list the new list to add.
194 * @param head the place to add it in the first list.
196 static inline void list_splice(struct list_head *list, struct list_head *head)
198 if (!list_empty(list))
199 __list_splice(list, head);
203 * list_splice_init - join two lists and reinitialize the emptied list.
204 * @param list the new list to add.
205 * @param head the place to add it in the first list.
207 * The list at list is reinitialized
209 static inline void list_splice_init(struct list_head *list,
210 struct list_head *head)
212 if (!list_empty(list)) {
213 __list_splice(list, head);
214 INIT_LIST_HEAD(list);
219 * list_entry - get the struct for this entry
220 * @param ptr the &struct list_head pointer.
221 * @param type the type of the struct this is embedded in.
222 * @param member the name of the list_struct within the struct.
224 #define list_entry(ptr, type, member) \
225 _list_container_of(ptr, type, member)
228 * list_for_each - iterate over a list
229 * @param pos the &struct list_head to use as a loop counter.
230 * @param head the head for your list.
232 #define list_for_each(pos, head) \
233 for (pos = (head)->next; pos != (head); pos = pos->next)
236 * list_for_each_prev - iterate over a list backwards
237 * @param pos the &struct list_head to use as a loop counter.
238 * @param head the head for your list.
240 #define list_for_each_prev(pos, head) \
241 for (pos = (head)->prev; pos != (head); pos = pos->prev)
244 * list_for_each_safe - iterate over a list safe against removal of list entry
245 * @param pos the &struct list_head to use as a loop counter.
246 * @param n another &struct list_head to use as temporary storage
247 * @param head the head for your list.
249 #define list_for_each_safe(pos, n, head) \
250 for (pos = (head)->next, n = pos->next; pos != (head); \
251 pos = n, n = pos->next)
254 * list_for_each_entry - iterate 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(type, pos, head, member) \
261 for (type *pos = list_entry((head)->next, type, member); \
262 &pos->member != (head); \
263 pos = list_entry(pos->member.next, type, member))
266 * list_for_each_entry_reverse - iterate backwards over list of given type.
267 * @param type the type of the struct where the listhead is embedded in
268 * @param pos the type * to use as a loop counter.
269 * @param head the head for your list.
270 * @param member the name of the list_struct within the struct.
272 #define list_for_each_entry_reverse(type, pos, head, member) \
273 for (type *pos = list_entry((head)->prev, type, member); \
274 &pos->member != (head); \
275 pos = list_entry(pos->member.prev, type, member))
279 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
280 * @param type the type of the struct where the listhead is embedded in
281 * @param pos the type * to use as a loop counter.
282 * @param n another type * to use as temporary storage
283 * @param head the head for your list.
284 * @param member the name of the list_struct within the struct.
286 #define list_for_each_entry_safe(type, pos, n, head, member) \
287 for (type *pos = list_entry((head)->next, type, member), \
288 *n = list_entry(pos->member.next, type, member); \
289 &pos->member != (head); \
290 pos = n, n = list_entry(n->member.next, type, member))