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