libFirm
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
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 "../begin.h"
9 
29 typedef struct list_head list_head;
30 
32 #define LIST_HEAD_INIT(name) { &(name), &(name) }
33 
35 #define LIST_HEAD(name) \
36  struct list_head name = LIST_HEAD_INIT(name)
37 
39 #define INIT_LIST_HEAD(ptr) do { \
40  (ptr)->next = (ptr); (ptr)->prev = (ptr); \
41 } while (0)
42 
44 struct list_head {
45  struct list_head *next, *prev;
46 };
47 
48 #define _list_container_of(ptr, type, member) \
49  ((type *) ((char *) (ptr) - offsetof(type, member)))
50 
58 static inline void __list_add(struct list_head *new_node,
59  struct list_head *prev,
60  struct list_head *next)
61 {
62  next->prev = new_node;
63  new_node->next = next;
64  new_node->prev = prev;
65  prev->next = new_node;
66 }
67 
76 static inline void list_add(struct list_head *new_node, struct list_head *head)
77 {
78  __list_add(new_node, head, head->next);
79 }
80 
89 static inline void list_add_tail(struct list_head *new_node, struct list_head *head)
90 {
91  __list_add(new_node, head->prev, head);
92 }
93 
101 static inline void __list_del(struct list_head * prev, struct list_head * next)
102 {
103  next->prev = prev;
104  prev->next = next;
105 }
106 
115 static inline void list_del(struct list_head *entry)
116 {
117  __list_del(entry->prev, entry->next);
118  entry->next = 0;
119  entry->prev = 0;
120 }
121 
122 
127 static inline void list_del_init(struct list_head *entry)
128 {
129  __list_del(entry->prev, entry->next);
130  INIT_LIST_HEAD(entry);
131 }
132 
138 static inline void list_move(struct list_head *list, struct list_head *head)
139 {
140  __list_del(list->prev, list->next);
141  list_add(list, head);
142 }
143 
149 static inline void list_move_tail(struct list_head *list,
150  struct list_head *head)
151 {
152  __list_del(list->prev, list->next);
153  list_add_tail(list, head);
154 }
155 
160 static inline int list_empty(const struct list_head *head)
161 {
162  return head->next == head;
163 }
164 
172 static inline void __list_splice(struct list_head *list,
173  struct list_head *head)
174 {
175  struct list_head *first = list->next;
176  struct list_head *last = list->prev;
177  struct list_head *at = head->next;
178 
179  first->prev = head;
180  head->next = first;
181 
182  last->next = at;
183  at->prev = last;
184 }
185 
191 static inline void list_splice(struct list_head *list, struct list_head *head)
192 {
193  if (!list_empty(list))
194  __list_splice(list, head);
195 }
196 
204 static inline void list_splice_init(struct list_head *list,
205  struct list_head *head)
206 {
207  if (!list_empty(list)) {
208  __list_splice(list, head);
209  INIT_LIST_HEAD(list);
210  }
211 }
212 
219 #define list_entry(ptr, type, member) \
220  _list_container_of(ptr, type, member)
221 
227 #define list_for_each(pos, head) \
228  for (pos = (head)->next; pos != (head); pos = pos->next)
229 
235 #define list_for_each_prev(pos, head) \
236  for (pos = (head)->prev; pos != (head); pos = pos->prev)
237 
244 #define list_for_each_safe(pos, n, head) \
245  for (pos = (head)->next, n = pos->next; pos != (head); \
246  pos = n, n = pos->next)
247 
255 #define list_for_each_entry(type, pos, head, member) \
256  for (type *pos = list_entry((head)->next, type, member); \
257  &pos->member != (head); \
258  pos = list_entry(pos->member.next, type, member))
259 
267 #define list_for_each_entry_reverse(type, pos, head, member) \
268  for (type *pos = list_entry((head)->prev, type, member); \
269  &pos->member != (head); \
270  pos = list_entry(pos->member.prev, type, member))
271 
272 
281 #define list_for_each_entry_safe(type, pos, n, head, member) \
282  for (type *pos = list_entry((head)->next, type, member), \
283  *n = list_entry(pos->member.next, type, member); \
284  &pos->member != (head); \
285  pos = n, n = list_entry(n->member.next, type, member))
286 
289 #include "../end.h"
290 
291 #endif
static void __list_del(struct list_head *prev, struct list_head *next)
Deletes a list entry by making the prev/next entries point to each other.
Definition: list.h:101
struct list_head list_head
List Header.
Definition: list.h:29
static void list_del(struct list_head *entry)
Deletes entry from list.
Definition: list.h:115
static void __list_splice(struct list_head *list, struct list_head *head)
Join two nonempty lists.
Definition: list.h:172
static void list_move_tail(struct list_head *list, struct list_head *head)
Deletes from one list and add as another's tail.
Definition: list.h:149
static void list_add(struct list_head *new_node, struct list_head *head)
Adds a new entry.
Definition: list.h:76
static void list_move(struct list_head *list, struct list_head *head)
Deletes from one list and add as another's head.
Definition: list.h:138
static void list_del_init(struct list_head *entry)
Deletes entry from list and reinitialize it.
Definition: list.h:127
static void list_splice(struct list_head *list, struct list_head *head)
list_splice - join two lists
Definition: list.h:191
#define INIT_LIST_HEAD(ptr)
Initializes a list header.
Definition: list.h:39
static void __list_add(struct list_head *new_node, struct list_head *prev, struct list_head *next)
Inserts a new entry between two known consecutive entries.
Definition: list.h:58
static void list_add_tail(struct list_head *new_node, struct list_head *head)
Adds a new entry.
Definition: list.h:89
static void list_splice_init(struct list_head *list, struct list_head *head)
list_splice_init - join two lists and reinitialize the emptied list.
Definition: list.h:204
static int list_empty(const struct list_head *head)
Tests whether a list is empty.
Definition: list.h:160