comment
[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 "config.h"
10
11 /*
12  * These are non-NULL pointers that will result in page faults
13  * under normal circumstances, used to verify that nobody uses
14  * non-initialized list entries.
15  */
16 #define LIST_POISON1  ((void *) 0x00100100)
17 #define LIST_POISON2  ((void *) 0x00200200)
18
19 /*
20  * Simple doubly linked list implementation.
21  *
22  * Some of the internal functions ("__xxx") are useful when
23  * manipulating whole lists rather than single entries, as
24  * sometimes we already know the next/prev entries and we can
25  * generate better code by using them directly rather than
26  * using the generic single-entry routines.
27  */
28
29 struct list_head {
30         struct list_head *next, *prev;
31 };
32
33 #define LIST_HEAD_INIT(name) { &(name), &(name) }
34
35 #define LIST_HEAD(name) \
36         struct list_head name = LIST_HEAD_INIT(name)
37
38 #define INIT_LIST_HEAD(ptr) do { \
39         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
40 } while (0)
41
42 #define _list_offsetof(type,member) \
43   ((char *) &(((type *) 0)->member) - (char *) 0)
44
45 #define _list_container_of(ptr, type, member) \
46         ((type *) ((char *) (ptr) - _list_offsetof(type, member)))
47
48 /*
49  * Insert a new entry between two known consecutive entries.
50  *
51  * This is only for internal list manipulation where we know
52  * the prev/next entries already!
53  */
54 static INLINE void __list_add(struct list_head *new,
55                               struct list_head *prev,
56                               struct list_head *next)
57 {
58         next->prev = new;
59         new->next = next;
60         new->prev = prev;
61         prev->next = new;
62 }
63
64 /**
65  * list_add - add a new entry
66  * @new: new entry to be added
67  * @head: list head to add it after
68  *
69  * Insert a new entry after the specified head.
70  * This is good for implementing stacks.
71  */
72 static INLINE void list_add(struct list_head *new, struct list_head *head)
73 {
74         __list_add(new, head, head->next);
75 }
76
77 /**
78  * list_add_tail - add a new entry
79  * @new: new entry to be added
80  * @head: list head to add it before
81  *
82  * Insert a new entry before the specified head.
83  * This is useful for implementing queues.
84  */
85 static INLINE void list_add_tail(struct list_head *new, struct list_head *head)
86 {
87         __list_add(new, head->prev, head);
88 }
89
90 /*
91  * Delete a list entry by making the prev/next entries
92  * point to each other.
93  *
94  * This is only for internal list manipulation where we know
95  * the prev/next entries already!
96  */
97 static INLINE void __list_del(struct list_head * prev, struct list_head * next)
98 {
99         next->prev = prev;
100         prev->next = next;
101 }
102
103 /**
104  * list_del - deletes entry from list.
105  * @entry: the element to delete from the list.
106  * Note: list_empty on entry does not return true after this, the entry is
107  * in an undefined state.
108  */
109 static INLINE void list_del(struct list_head *entry)
110 {
111         __list_del(entry->prev, entry->next);
112         entry->next = LIST_POISON1;
113         entry->prev = LIST_POISON2;
114 }
115
116
117 /**
118  * list_del_init - deletes entry from list and reinitialize it.
119  * @entry: the element to delete from the list.
120  */
121 static INLINE void list_del_init(struct list_head *entry)
122 {
123         __list_del(entry->prev, entry->next);
124         INIT_LIST_HEAD(entry);
125 }
126
127 /**
128  * list_move - delete from one list and add as another's head
129  * @list: the entry to move
130  * @head: the head that will precede our entry
131  */
132 static INLINE void list_move(struct list_head *list, struct list_head *head)
133 {
134         __list_del(list->prev, list->next);
135         list_add(list, head);
136 }
137
138 /**
139  * list_move_tail - delete from one list and add as another's tail
140  * @list: the entry to move
141  * @head: the head that will follow our entry
142  */
143 static INLINE void list_move_tail(struct list_head *list,
144                                   struct list_head *head)
145 {
146         __list_del(list->prev, list->next);
147         list_add_tail(list, head);
148 }
149
150 /**
151  * list_empty - tests whether a list is empty
152  * @head: the list to test.
153  */
154 static INLINE int list_empty(struct list_head *head)
155 {
156         return head->next == head;
157 }
158
159 static INLINE void __list_splice(struct list_head *list,
160                                  struct list_head *head)
161 {
162         struct list_head *first = list->next;
163         struct list_head *last = list->prev;
164         struct list_head *at = head->next;
165
166         first->prev = head;
167         head->next = first;
168
169         last->next = at;
170         at->prev = last;
171 }
172
173 /**
174  * list_splice - join two lists
175  * @list: the new list to add.
176  * @head: the place to add it in the first list.
177  */
178 static INLINE void list_splice(struct list_head *list, struct list_head *head)
179 {
180         if (!list_empty(list))
181                 __list_splice(list, head);
182 }
183
184 /**
185  * list_splice_init - join two lists and reinitialise the emptied list.
186  * @list: the new list to add.
187  * @head: the place to add it in the first list.
188  *
189  * The list at @list is reinitialised
190  */
191 static INLINE void list_splice_init(struct list_head *list,
192                                     struct list_head *head)
193 {
194         if (!list_empty(list)) {
195                 __list_splice(list, head);
196                 INIT_LIST_HEAD(list);
197         }
198 }
199
200 /**
201  * list_entry - get the struct for this entry
202  * @ptr:        the &struct list_head pointer.
203  * @type:       the type of the struct this is embedded in.
204  * @member:     the name of the list_struct within the struct.
205  */
206 #define list_entry(ptr, type, member) \
207         _list_container_of(ptr, type, member)
208
209 /**
210  * list_for_each        -       iterate over a list
211  * @pos:        the &struct list_head to use as a loop counter.
212  * @head:       the head for your list.
213  */
214 #define list_for_each(pos, head) \
215         for (pos = (head)->next, (pos->next); pos != (head); \
216                 pos = pos->next, (pos->next))
217
218 /**
219  * __list_for_each      -       iterate over a list
220  * @pos:        the &struct list_head to use as a loop counter.
221  * @head:       the head for your list.
222  *
223  * This variant differs from list_for_each() in that it's the
224  * simplest possible list iteration code, no ing is done.
225  * Use this for code that knows the list to be very short (empty
226  * or 1 entry) most of the time.
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  * @pos:        the &struct list_head to use as a loop counter.
234  * @head:       the head for your list.
235  */
236 #define list_for_each_prev(pos, head) \
237         for (pos = (head)->prev, (pos->prev); pos != (head); \
238                 pos = pos->prev, (pos->prev))
239
240 /**
241  * list_for_each_safe   -       iterate over a list safe against removal of list entry
242  * @pos:        the &struct list_head to use as a loop counter.
243  * @n:          another &struct list_head to use as temporary storage
244  * @head:       the head for your list.
245  */
246 #define list_for_each_safe(pos, n, head) \
247         for (pos = (head)->next, n = pos->next; pos != (head); \
248                 pos = n, n = pos->next)
249
250 /**
251  * list_for_each_entry  -       iterate over list of given type
252  * @pos:        the type * to use as a loop counter.
253  * @head:       the head for your list.
254  * @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  * @pos:        the type * to use as a loop counter.
264  * @head:       the head for your list.
265  * @member:     the name of the list_struct within the struct.
266  */
267 #define list_for_each_entry_reverse(type, pos, head, member)                    \
268         for (pos = list_entry((head)->prev, type, member);      \
269              &pos->member != (head);                                    \
270              pos = list_entry(pos->member.prev, type, member))
271
272
273 /**
274  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
275  * @pos:        the type * to use as a loop counter.
276  * @n:          another type * to use as temporary storage
277  * @head:       the head for your list.
278  * @member:     the name of the list_struct within the struct.
279  */
280 #define list_for_each_entry_safe(type, pos, n, head, member)                    \
281         for (pos = list_entry((head)->next, type, member),      \
282                 n = list_entry(pos->member.next, type, member); \
283              &pos->member != (head);                                    \
284              pos = n, n = list_entry(n->member.next, type, member))
285
286
287 #endif