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