libFirm 1.20
libfirm/adt/list.h
00001 #ifndef FIRM_ADT_LIST_H
00002 #define FIRM_ADT_LIST_H
00003 
00004 #include <stdlib.h>
00005 
00006 #include "../begin.h"
00007 
00027 typedef struct list_head list_head;
00028 
00030 #define LIST_HEAD_INIT(name) { &(name), &(name) }
00031 
00033 #define LIST_HEAD(name) \
00034     struct list_head name = LIST_HEAD_INIT(name)
00035 
00037 #define INIT_LIST_HEAD(ptr) do { \
00038     (ptr)->next = (ptr); (ptr)->prev = (ptr); \
00039 } while (0)
00040 
00042 struct list_head {
00043     struct list_head *next, *prev;
00044 };
00045 
00046 #define _list_offsetof(type,member) \
00047   ((char *) &(((type *) 0)->member) - (char *) 0)
00048 
00049 #define _list_container_of(ptr, type, member) \
00050     ((type *) ((char *) (ptr) - _list_offsetof(type, member)))
00051 
00059 static inline void __list_add(struct list_head *new_node,
00060                               struct list_head *prev,
00061                               struct list_head *next)
00062 {
00063     next->prev = new_node;
00064     new_node->next = next;
00065     new_node->prev = prev;
00066     prev->next = new_node;
00067 }
00068 
00077 static inline void list_add(struct list_head *new_node, struct list_head *head)
00078 {
00079     __list_add(new_node, head, head->next);
00080 }
00081 
00090 static inline void list_add_tail(struct list_head *new_node, struct list_head *head)
00091 {
00092     __list_add(new_node, head->prev, head);
00093 }
00094 
00102 static inline void __list_del(struct list_head * prev, struct list_head * next)
00103 {
00104     next->prev = prev;
00105     prev->next = next;
00106 }
00107 
00116 static inline void list_del(struct list_head *entry)
00117 {
00118     __list_del(entry->prev, entry->next);
00119     entry->next = NULL;
00120     entry->prev = NULL;
00121 }
00122 
00123 
00128 static inline void list_del_init(struct list_head *entry)
00129 {
00130     __list_del(entry->prev, entry->next);
00131     INIT_LIST_HEAD(entry);
00132 }
00133 
00139 static inline void list_move(struct list_head *list, struct list_head *head)
00140 {
00141         __list_del(list->prev, list->next);
00142         list_add(list, head);
00143 }
00144 
00150 static inline void list_move_tail(struct list_head *list,
00151                                   struct list_head *head)
00152 {
00153         __list_del(list->prev, list->next);
00154         list_add_tail(list, head);
00155 }
00156 
00161 static inline int list_empty(const struct list_head *head)
00162 {
00163     return head->next == head;
00164 }
00165 
00173 static inline void __list_splice(struct list_head *list,
00174                                  struct list_head *head)
00175 {
00176     struct list_head *first = list->next;
00177     struct list_head *last = list->prev;
00178     struct list_head *at = head->next;
00179 
00180     first->prev = head;
00181     head->next = first;
00182 
00183     last->next = at;
00184     at->prev = last;
00185 }
00186 
00192 static inline void list_splice(struct list_head *list, struct list_head *head)
00193 {
00194     if (!list_empty(list))
00195         __list_splice(list, head);
00196 }
00197 
00205 static inline void list_splice_init(struct list_head *list,
00206                     struct list_head *head)
00207 {
00208     if (!list_empty(list)) {
00209         __list_splice(list, head);
00210         INIT_LIST_HEAD(list);
00211     }
00212 }
00213 
00220 #define list_entry(ptr, type, member) \
00221     _list_container_of(ptr, type, member)
00222 
00228 #define list_for_each(pos, head) \
00229     for (pos = (head)->next; pos != (head); pos = pos->next)
00230 
00241 #define __list_for_each(pos, head) \
00242     for (pos = (head)->next; pos != (head); pos = pos->next)
00243 
00249 #define list_for_each_prev(pos, head) \
00250     for (pos = (head)->prev; pos != (head); pos = pos->prev)
00251 
00258 #define list_for_each_safe(pos, n, head) \
00259     for (pos = (head)->next, n = pos->next; pos != (head); \
00260         pos = n, n = pos->next)
00261 
00269 #define list_for_each_entry(type, pos, head, member)    \
00270     for (pos = list_entry((head)->next, type, member);  \
00271          &pos->member != (head);                        \
00272          pos = list_entry(pos->member.next, type, member))
00273 
00281 #define list_for_each_entry_reverse(type, pos, head, member) \
00282     for (pos = list_entry((head)->prev, type, member);       \
00283          &pos->member != (head);                             \
00284          pos = list_entry(pos->member.prev, type, member))
00285 
00286 
00295 #define list_for_each_entry_safe(type, pos, n, head, member) \
00296     for (pos = list_entry((head)->next, type, member),       \
00297         n = list_entry(pos->member.next, type, member);      \
00298          &pos->member != (head);                             \
00299          pos = n, n = list_entry(n->member.next, type, member))
00300 
00303 #include "../end.h"
00304 
00305 #endif