Some bugfixes
[libfirm] / ir / adt / list.h
1 /**
2  * Linked lists.
3  * Shamelessly adapted from the linux kernel.
4  */
5
6 #ifndef _FIRM_LIST_H
7 #define _FIRM_LIST_H
8
9 #include "firm_config.h"
10
11 /*
12  * Simple doubly linked list implementation.
13  *
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.
19  */
20
21 struct list_head {
22         struct list_head *next, *prev;
23 };
24
25 #define LIST_HEAD_INIT(name) { &(name), &(name) }
26
27 #define LIST_HEAD(name) \
28         struct list_head name = LIST_HEAD_INIT(name)
29
30 #define INIT_LIST_HEAD(ptr) do { \
31         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
32 } while (0)
33
34 #define _list_offsetof(type,member) \
35   ((char *) &(((type *) 0)->member) - (char *) 0)
36
37 #define _list_container_of(ptr, type, member) \
38         ((type *) ((char *) (ptr) - _list_offsetof(type, member)))
39
40 /*
41  * Insert a new entry between two known consecutive entries.
42  *
43  * This is only for internal list manipulation where we know
44  * the prev/next entries already!
45  */
46 static INLINE void __list_add(struct list_head *new_node,
47                               struct list_head *prev,
48                               struct list_head *next)
49 {
50         next->prev = new_node;
51         new_node->next = next;
52         new_node->prev = prev;
53         prev->next = new_node;
54 }
55
56 /**
57  * list_add - add a new entry
58  * @new: new entry to be added
59  * @head: list head to add it after
60  *
61  * Insert a new entry after the specified head.
62  * This is good for implementing stacks.
63  */
64 static INLINE void list_add(struct list_head *new_node, struct list_head *head)
65 {
66         __list_add(new_node, head, head->next);
67 }
68
69 /**
70  * list_add_tail - add a new entry
71  * @new: new entry to be added
72  * @head: list head to add it before
73  *
74  * Insert a new entry before the specified head.
75  * This is useful for implementing queues.
76  */
77 static INLINE void list_add_tail(struct list_head *new_node, struct list_head *head)
78 {
79         __list_add(new_node, head->prev, head);
80 }
81
82 /*
83  * Delete a list entry by making the prev/next entries
84  * point to each other.
85  *
86  * This is only for internal list manipulation where we know
87  * the prev/next entries already!
88  */
89 static INLINE void __list_del(struct list_head * prev, struct list_head * next)
90 {
91         next->prev = prev;
92         prev->next = next;
93 }
94
95 /**
96  * list_del - deletes entry from list.
97  * @entry: the element to delete from the list.
98  * Note: list_empty on entry does not return true after this, the entry is
99  * in an undefined state.
100  */
101 static INLINE void list_del(struct list_head *entry)
102 {
103         __list_del(entry->prev, entry->next);
104         entry->next = NULL;
105         entry->prev = NULL;
106 }
107
108
109 /**
110  * list_del_init - deletes entry from list and reinitialize it.
111  * @entry: the element to delete from the list.
112  */
113 static INLINE void list_del_init(struct list_head *entry)
114 {
115         __list_del(entry->prev, entry->next);
116         INIT_LIST_HEAD(entry);
117 }
118
119 /**
120  * list_move - delete from one list and add as another's head
121  * @list: the entry to move
122  * @head: the head that will precede our entry
123  */
124 static INLINE void list_move(struct list_head *list, struct list_head *head)
125 {
126         __list_del(list->prev, list->next);
127         list_add(list, head);
128 }
129
130 /**
131  * list_move_tail - delete from one list and add as another's tail
132  * @list: the entry to move
133  * @head: the head that will follow our entry
134  */
135 static INLINE void list_move_tail(struct list_head *list,
136                                   struct list_head *head)
137 {
138         __list_del(list->prev, list->next);
139         list_add_tail(list, head);
140 }
141
142 /**
143  * list_empty - tests whether a list is empty
144  * @head: the list to test.
145  */
146 static INLINE int list_empty(const struct list_head *head)
147 {
148         return head->next == head;
149 }
150
151 static INLINE void __list_splice(struct list_head *list,
152                                  struct list_head *head)
153 {
154         struct list_head *first = list->next;
155         struct list_head *last = list->prev;
156         struct list_head *at = head->next;
157
158         first->prev = head;
159         head->next = first;
160
161         last->next = at;
162         at->prev = last;
163 }
164
165 /**
166  * list_splice - join two lists
167  * @list: the new list to add.
168  * @head: the place to add it in the first list.
169  */
170 static INLINE void list_splice(struct list_head *list, struct list_head *head)
171 {
172         if (!list_empty(list))
173                 __list_splice(list, head);
174 }
175
176 /**
177  * list_splice_init - join two lists and reinitialise the emptied list.
178  * @list: the new list to add.
179  * @head: the place to add it in the first list.
180  *
181  * The list at @list is reinitialised
182  */
183 static INLINE void list_splice_init(struct list_head *list,
184                                     struct list_head *head)
185 {
186         if (!list_empty(list)) {
187                 __list_splice(list, head);
188                 INIT_LIST_HEAD(list);
189         }
190 }
191
192 /**
193  * list_entry - get the struct for this entry
194  * @ptr:        the &struct list_head pointer.
195  * @type:       the type of the struct this is embedded in.
196  * @member:     the name of the list_struct within the struct.
197  */
198 #define list_entry(ptr, type, member) \
199         _list_container_of(ptr, type, member)
200
201 /**
202  * list_for_each        -       iterate over a list
203  * @pos:        the &struct list_head to use as a loop counter.
204  * @head:       the head for your list.
205  */
206 #define list_for_each(pos, head) \
207         for (pos = (head)->next, (pos->next); pos != (head); \
208                 pos = pos->next, (pos->next))
209
210 /**
211  * __list_for_each      -       iterate over a list
212  * @pos:        the &struct list_head to use as a loop counter.
213  * @head:       the head for your list.
214  *
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.
219  */
220 #define __list_for_each(pos, head) \
221         for (pos = (head)->next; pos != (head); pos = pos->next)
222
223 /**
224  * list_for_each_prev   -       iterate over a list backwards
225  * @pos:        the &struct list_head to use as a loop counter.
226  * @head:       the head for your list.
227  */
228 #define list_for_each_prev(pos, head) \
229         for (pos = (head)->prev, (pos->prev); pos != (head); \
230                 pos = pos->prev, (pos->prev))
231
232 /**
233  * list_for_each_safe   -       iterate over a list safe against removal of list entry
234  * @pos:        the &struct list_head to use as a loop counter.
235  * @n:          another &struct list_head to use as temporary storage
236  * @head:       the head for your list.
237  */
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)
241
242 /**
243  * list_for_each_entry  -       iterate over list of given type
244  * @pos:        the type * to use as a loop counter.
245  * @head:       the head for your list.
246  * @member:     the name of the list_struct within the struct.
247  */
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))
252
253 /**
254  * list_for_each_entry_reverse - iterate backwards over list of given type.
255  * @pos:        the type * to use as a loop counter.
256  * @head:       the head for your list.
257  * @member:     the name of the list_struct within the struct.
258  */
259 #define list_for_each_entry_reverse(type, pos, head, member)                    \
260         for (pos = list_entry((head)->prev, type, member);      \
261              &pos->member != (head);                                    \
262              pos = list_entry(pos->member.prev, type, member))
263
264
265 /**
266  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
267  * @pos:        the type * to use as a loop counter.
268  * @n:          another type * to use as temporary storage
269  * @head:       the head for your list.
270  * @member:     the name of the list_struct within the struct.
271  */
272 #define list_for_each_entry_safe(type, pos, n, head, member)                    \
273         for (pos = list_entry((head)->next, type, member),      \
274                 n = list_entry(pos->member.next, type, member); \
275              &pos->member != (head);                                    \
276              pos = n, n = list_entry(n->member.next, type, member))
277
278
279 #endif