libFirm
list.h
1 #ifndef FIRM_ADT_LIST_H
2 #define FIRM_ADT_LIST_H
3 
4 #include <stdlib.h>
5 
6 #include "../begin.h"
7 
27 typedef struct list_head list_head;
28 
30 #define LIST_HEAD_INIT(name) { &(name), &(name) }
31 
33 #define LIST_HEAD(name) \
34  struct list_head name = LIST_HEAD_INIT(name)
35 
37 #define INIT_LIST_HEAD(ptr) do { \
38  (ptr)->next = (ptr); (ptr)->prev = (ptr); \
39 } while (0)
40 
42 struct list_head {
43  struct list_head *next, *prev;
44 };
45 
46 #define _list_offsetof(type,member) \
47  ((char *) &(((type *) 0)->member) - (char *) 0)
48 
49 #define _list_container_of(ptr, type, member) \
50  ((type *) ((char *) (ptr) - _list_offsetof(type, member)))
51 
59 static inline void __list_add(struct list_head *new_node,
60  struct list_head *prev,
61  struct list_head *next)
62 {
63  next->prev = new_node;
64  new_node->next = next;
65  new_node->prev = prev;
66  prev->next = new_node;
67 }
68 
77 static inline void list_add(struct list_head *new_node, struct list_head *head)
78 {
79  __list_add(new_node, head, head->next);
80 }
81 
90 static inline void list_add_tail(struct list_head *new_node, struct list_head *head)
91 {
92  __list_add(new_node, head->prev, head);
93 }
94 
102 static inline void __list_del(struct list_head * prev, struct list_head * next)
103 {
104  next->prev = prev;
105  prev->next = next;
106 }
107 
116 static inline void list_del(struct list_head *entry)
117 {
118  __list_del(entry->prev, entry->next);
119  entry->next = NULL;
120  entry->prev = NULL;
121 }
122 
123 
128 static inline void list_del_init(struct list_head *entry)
129 {
130  __list_del(entry->prev, entry->next);
131  INIT_LIST_HEAD(entry);
132 }
133 
139 static inline void list_move(struct list_head *list, struct list_head *head)
140 {
141  __list_del(list->prev, list->next);
142  list_add(list, head);
143 }
144 
150 static inline void list_move_tail(struct list_head *list,
151  struct list_head *head)
152 {
153  __list_del(list->prev, list->next);
154  list_add_tail(list, head);
155 }
156 
161 static inline int list_empty(const struct list_head *head)
162 {
163  return head->next == head;
164 }
165 
173 static inline void __list_splice(struct list_head *list,
174  struct list_head *head)
175 {
176  struct list_head *first = list->next;
177  struct list_head *last = list->prev;
178  struct list_head *at = head->next;
179 
180  first->prev = head;
181  head->next = first;
182 
183  last->next = at;
184  at->prev = last;
185 }
186 
192 static inline void list_splice(struct list_head *list, struct list_head *head)
193 {
194  if (!list_empty(list))
195  __list_splice(list, head);
196 }
197 
205 static inline void list_splice_init(struct list_head *list,
206  struct list_head *head)
207 {
208  if (!list_empty(list)) {
209  __list_splice(list, head);
210  INIT_LIST_HEAD(list);
211  }
212 }
213 
220 #define list_entry(ptr, type, member) \
221  _list_container_of(ptr, type, member)
222 
228 #define list_for_each(pos, head) \
229  for (pos = (head)->next; pos != (head); pos = pos->next)
230 
236 #define list_for_each_prev(pos, head) \
237  for (pos = (head)->prev; pos != (head); pos = pos->prev)
238 
245 #define list_for_each_safe(pos, n, head) \
246  for (pos = (head)->next, n = pos->next; pos != (head); \
247  pos = n, n = pos->next)
248 
256 #define list_for_each_entry(type, pos, head, member) \
257  for (type *pos = list_entry((head)->next, type, member); \
258  &pos->member != (head); \
259  pos = list_entry(pos->member.next, type, member))
260 
268 #define list_for_each_entry_reverse(type, pos, head, member) \
269  for (type *pos = list_entry((head)->prev, type, member); \
270  &pos->member != (head); \
271  pos = list_entry(pos->member.prev, type, member))
272 
273 
282 #define list_for_each_entry_safe(type, pos, n, head, member) \
283  for (type *pos = list_entry((head)->next, type, member), \
284  *n = list_entry(pos->member.next, type, member); \
285  &pos->member != (head); \
286  pos = n, n = list_entry(n->member.next, type, member))
287 
290 #include "../end.h"
291 
292 #endif