Fixed memory leak
[libfirm] / ir / adt / list.h
1
2 /**
3  * @file
4  * @brief   Doubly linked lists.
5  * @version $Id$
6  *
7  * Simple doubly linked list implementation.
8  *
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.
14   */
15 #ifndef FIRM_ADT_LIST_H
16 #define FIRM_ADT_LIST_H
17
18 #include "firm_config.h"
19
20 struct list_head {
21         struct list_head *next, *prev;
22 };
23
24 #define LIST_HEAD_INIT(name) { &(name), &(name) }
25
26 #define LIST_HEAD(name) \
27         struct list_head name = LIST_HEAD_INIT(name)
28
29 #define INIT_LIST_HEAD(ptr) do { \
30         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
31 } while (0)
32
33 #define _list_offsetof(type,member) \
34   ((char *) &(((type *) 0)->member) - (char *) 0)
35
36 #define _list_container_of(ptr, type, member) \
37         ((type *) ((char *) (ptr) - _list_offsetof(type, member)))
38
39 /*
40  * Insert a new entry between two known consecutive entries.
41  *
42  * This is only for internal list manipulation where we know
43  * the prev/next entries already!
44  */
45 static INLINE void __list_add(struct list_head *new_node,
46                               struct list_head *prev,
47                               struct list_head *next)
48 {
49         next->prev = new_node;
50         new_node->next = next;
51         new_node->prev = prev;
52         prev->next = new_node;
53 }
54
55 /**
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
59  *
60  * Insert a new entry after the specified head.
61  * This is good for implementing stacks.
62  */
63 static INLINE void list_add(struct list_head *new_node, struct list_head *head)
64 {
65         __list_add(new_node, head, head->next);
66 }
67
68 /**
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
72  *
73  * Insert a new entry before the specified head.
74  * This is useful for implementing queues.
75  */
76 static INLINE void list_add_tail(struct list_head *new_node, struct list_head *head)
77 {
78         __list_add(new_node, head->prev, head);
79 }
80
81 /*
82  * Delete a list entry by making the prev/next entries
83  * point to each other.
84  *
85  * This is only for internal list manipulation where we know
86  * the prev/next entries already!
87  */
88 static INLINE void __list_del(struct list_head * prev, struct list_head * next)
89 {
90         next->prev = prev;
91         prev->next = next;
92 }
93
94 /**
95  * list_del - deletes entry from list.
96  * @param entry  the element to delete from the list.
97  *
98  * @Note
99  *   list_empty on entry does not return true after this, the entry is
100  *   in an undefined state.
101  */
102 static INLINE void list_del(struct list_head *entry)
103 {
104         __list_del(entry->prev, entry->next);
105         entry->next = NULL;
106         entry->prev = NULL;
107 }
108
109
110 /**
111  * list_del_init - deletes entry from list and reinitialize it.
112  * @param entry   the element to delete from the list.
113  */
114 static INLINE void list_del_init(struct list_head *entry)
115 {
116         __list_del(entry->prev, entry->next);
117         INIT_LIST_HEAD(entry);
118 }
119
120 /**
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
124  */
125 static INLINE void list_move(struct list_head *list, struct list_head *head)
126 {
127         __list_del(list->prev, list->next);
128         list_add(list, head);
129 }
130
131 /**
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
135  */
136 static INLINE void list_move_tail(struct list_head *list,
137                                   struct list_head *head)
138 {
139         __list_del(list->prev, list->next);
140         list_add_tail(list, head);
141 }
142
143 /**
144  * list_empty - tests whether a list is empty
145  * @param head   the list to test.
146  */
147 static INLINE int list_empty(const struct list_head *head)
148 {
149         return head->next == head;
150 }
151
152 static INLINE void __list_splice(struct list_head *list,
153                                  struct list_head *head)
154 {
155         struct list_head *first = list->next;
156         struct list_head *last = list->prev;
157         struct list_head *at = head->next;
158
159         first->prev = head;
160         head->next = first;
161
162         last->next = at;
163         at->prev = last;
164 }
165
166 /**
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.
170  */
171 static INLINE void list_splice(struct list_head *list, struct list_head *head)
172 {
173         if (!list_empty(list))
174                 __list_splice(list, head);
175 }
176
177 /**
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.
181  *
182  * The list at list is reinitialized
183  */
184 static INLINE void list_splice_init(struct list_head *list,
185                                     struct list_head *head)
186 {
187         if (!list_empty(list)) {
188                 __list_splice(list, head);
189                 INIT_LIST_HEAD(list);
190         }
191 }
192
193 /**
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.
198  */
199 #define list_entry(ptr, type, member) \
200         _list_container_of(ptr, type, member)
201
202 /**
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.
206  */
207 #define list_for_each(pos, head) \
208         for (pos = (head)->next; pos != (head); pos = pos->next)
209
210 /**
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.
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  * @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_prev(pos, head) \
229         for (pos = (head)->prev; pos != (head); pos = pos->prev)
230
231 /**
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.
236  */
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)
240
241 /**
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.
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  * @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.
259  */
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))
264
265
266 /**
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.
273  */
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))
279
280
281 #endif