9d6ec715ccaf50182e60ff6e3611d25bfaaf95c7
[libfirm] / include / libfirm / adt / list.h
1 #ifndef FIRM_ADT_LIST_H
2 #define FIRM_ADT_LIST_H
3
4 #include <stdlib.h>
5
6 #include "../begin.h"
7
8 /**
9  * @ingroup adt
10  * @defgroup lists Linked Lists
11  * @brief   Doubly linked lists.
12  *
13  * Simple doubly linked list implementation.
14  *
15  * Some of the internal functions ("__xxx") are useful when
16  * manipulating whole lists rather than single entries, as
17  * sometimes we already know the next/prev entries and we can
18  * generate better code by using them directly rather than
19  * using the generic single-entry routines.
20  * @{
21  */
22
23 /**
24  * List Header.
25  * Put this into all list elements and at the place where you want to keep
26  * references to the list. */
27 typedef struct list_head list_head;
28
29 /** Static initializer for a list header */
30 #define LIST_HEAD_INIT(name) { &(name), &(name) }
31
32 /** Defines a (static) list reference */
33 #define LIST_HEAD(name) \
34         struct list_head name = LIST_HEAD_INIT(name)
35
36 /** Initializes a list header */
37 #define INIT_LIST_HEAD(ptr) do { \
38         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
39 } while (0)
40
41 /** @cond PRIVATE */
42 struct list_head {
43         struct list_head *next, *prev;
44 };
45
46 #define _list_offsetof(type,member) \
47   ((char *) &(((type *) 0)->member) - (char *) 0)
48
49 #define _list_container_of(ptr, type, member) \
50         ((type *) ((char *) (ptr) - _list_offsetof(type, member)))
51 /** @endcond  */
52
53 /**
54  * Inserts a new entry between two known consecutive entries.
55  *
56  * This is only for internal list manipulation where we know
57  * the prev/next entries already!
58  */
59 static inline void __list_add(struct list_head *new_node,
60                               struct list_head *prev,
61                               struct list_head *next)
62 {
63         next->prev = new_node;
64         new_node->next = next;
65         new_node->prev = prev;
66         prev->next = new_node;
67 }
68
69 /**
70  * Adds a new entry
71  * @param new_node   new entry to be added
72  * @param head       list head to add it after
73  *
74  * Insert a new entry after the specified head.
75  * This is good for implementing stacks.
76  */
77 static inline void list_add(struct list_head *new_node, struct list_head *head)
78 {
79         __list_add(new_node, head, head->next);
80 }
81
82 /**
83  * Adds a new entry
84  * @param new_node   new entry to be added
85  * @param head       list head to add it before
86  *
87  * Insert a new entry before the specified head.
88  * This is useful for implementing queues.
89  */
90 static inline void list_add_tail(struct list_head *new_node, struct list_head *head)
91 {
92         __list_add(new_node, head->prev, head);
93 }
94
95 /**
96  * Deletes a list entry by making the prev/next entries
97  * point to each other.
98  *
99  * This is only for internal list manipulation where we know
100  * the prev/next entries already!
101  */
102 static inline void __list_del(struct list_head * prev, struct list_head * next)
103 {
104         next->prev = prev;
105         prev->next = next;
106 }
107
108 /**
109  * Deletes entry from list.
110  * @param entry  the element to delete from the list.
111  *
112  * @note
113  *   list_empty on entry does not return true after this, the entry is
114  *   in an undefined state.
115  */
116 static inline void list_del(struct list_head *entry)
117 {
118         __list_del(entry->prev, entry->next);
119         entry->next = NULL;
120         entry->prev = NULL;
121 }
122
123
124 /**
125  * Deletes entry from list and reinitialize it.
126  * @param entry   the element to delete from the list.
127  */
128 static inline void list_del_init(struct list_head *entry)
129 {
130         __list_del(entry->prev, entry->next);
131         INIT_LIST_HEAD(entry);
132 }
133
134 /**
135  * Deletes from one list and add as another's head
136  * @param list   the entry to move
137  * @param head   the head that will precede our entry
138  */
139 static inline void list_move(struct list_head *list, struct list_head *head)
140 {
141         __list_del(list->prev, list->next);
142         list_add(list, head);
143 }
144
145 /**
146  * Deletes from one list and add as another's tail
147  * @param list   the entry to move
148  * @param head   the head that will follow our entry
149  */
150 static inline void list_move_tail(struct list_head *list,
151                                   struct list_head *head)
152 {
153         __list_del(list->prev, list->next);
154         list_add_tail(list, head);
155 }
156
157 /**
158  * Tests whether a list is empty
159  * @param head   the list to test.
160  */
161 static inline int list_empty(const struct list_head *head)
162 {
163         return head->next == head;
164 }
165
166 /**
167  * Join two nonempty lists.
168  *
169  * @note Use list_splice() if @p list is possibly empty.
170  * @param list   the new list to add.
171  * @param head   the place to add it in the first list.
172  */
173 static inline void __list_splice(struct list_head *list,
174                                  struct list_head *head)
175 {
176         struct list_head *first = list->next;
177         struct list_head *last = list->prev;
178         struct list_head *at = head->next;
179
180         first->prev = head;
181         head->next = first;
182
183         last->next = at;
184         at->prev = last;
185 }
186
187 /**
188  * list_splice - join two lists
189  * @param list   the new list to add.
190  * @param head   the place to add it in the first list.
191  */
192 static inline void list_splice(struct list_head *list, struct list_head *head)
193 {
194         if (!list_empty(list))
195                 __list_splice(list, head);
196 }
197
198 /**
199  * list_splice_init - join two lists and reinitialize the emptied list.
200  * @param list   the new list to add.
201  * @param head   the place to add it in the first list.
202  *
203  * The list at list is reinitialized
204  */
205 static inline void list_splice_init(struct list_head *list,
206                                     struct list_head *head)
207 {
208         if (!list_empty(list)) {
209                 __list_splice(list, head);
210                 INIT_LIST_HEAD(list);
211         }
212 }
213
214 /**
215  * list_entry - get the struct for this entry
216  * @param ptr     the &struct list_head pointer.
217  * @param type    the type of the struct this is embedded in.
218  * @param member  the name of the list_struct within the struct.
219  */
220 #define list_entry(ptr, type, member) \
221         _list_container_of(ptr, type, member)
222
223 /**
224  * list_for_each - iterate over a list
225  * @param pos   the &struct list_head to use as a loop counter.
226  * @param head  the head for your list.
227  */
228 #define list_for_each(pos, head) \
229         for (pos = (head)->next; pos != (head); pos = pos->next)
230
231 /**
232  * list_for_each_prev - iterate over a list backwards
233  * @param pos   the &struct list_head to use as a loop counter.
234  * @param head  the head for your list.
235  */
236 #define list_for_each_prev(pos, head) \
237         for (pos = (head)->prev; pos != (head); pos = pos->prev)
238
239 /**
240  * list_for_each_safe - iterate over a list safe against removal of list entry
241  * @param pos   the &struct list_head to use as a loop counter.
242  * @param n     another &struct list_head to use as temporary storage
243  * @param head  the head for your list.
244  */
245 #define list_for_each_safe(pos, n, head) \
246         for (pos = (head)->next, n = pos->next; pos != (head); \
247                 pos = n, n = pos->next)
248
249 /**
250  * list_for_each_entry - iterate over list of given type
251  * @param type    the type of the struct where the listhead is embedded in
252  * @param pos     the type * to use as a loop counter.
253  * @param head    the head for your list.
254  * @param member  the name of the list_struct within the struct.
255  */
256 #define list_for_each_entry(type, pos, head, member)    \
257         for (pos = list_entry((head)->next, type, member);  \
258              &pos->member != (head);                        \
259              pos = list_entry(pos->member.next, type, member))
260
261 /**
262  * list_for_each_entry_reverse - iterate backwards over list of given type.
263  * @param type    the type of the struct where the listhead is embedded in
264  * @param pos     the type * to use as a loop counter.
265  * @param head    the head for your list.
266  * @param member  the name of the list_struct within the struct.
267  */
268 #define list_for_each_entry_reverse(type, pos, head, member) \
269         for (pos = list_entry((head)->prev, type, member);       \
270              &pos->member != (head);                             \
271              pos = list_entry(pos->member.prev, type, member))
272
273
274 /**
275  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
276  * @param type    the type of the struct where the listhead is embedded in
277  * @param pos     the type * to use as a loop counter.
278  * @param n       another type * to use as temporary storage
279  * @param head    the head for your list.
280  * @param member  the name of the list_struct within the struct.
281  */
282 #define list_for_each_entry_safe(type, pos, n, head, member) \
283         for (pos = list_entry((head)->next, type, member),       \
284                 n = list_entry(pos->member.next, type, member);      \
285              &pos->member != (head);                             \
286              pos = n, n = list_entry(n->member.next, type, member))
287
288 /** @} */
289
290 #include "../end.h"
291
292 #endif