libisdn
dlist.h
Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  */
00005 #ifndef _DLIST_H_
00006 #define _DLIST_H_
00007 
00008 #include "common.h"
00009 
00010 /*
00011  * Double-linked list implementation
00012  */
00013 struct dlist_head;
00014 
00018 struct dlist_head {
00019         struct dlist_head *head;        
00020         struct dlist_head *next;        
00021         struct dlist_head *prev;        
00022 };
00023 
00024 #define DLIST_HEAD(_name)       \
00025         struct dlist_head _name = { &(_name), NULL, NULL }
00026 
00032 void dlist_init_head(struct dlist_head *h);
00033 
00034 void dlist_init(struct dlist_head *e);
00035 
00040 #define dlist_entry(ptr, type, member) \
00041         container_of(ptr, type, member)
00042 
00047 #define dlist_get_head(ptr)     \
00048         (ptr)->head
00049 
00056 void dlist_insert_after(struct dlist_head *elem, struct dlist_head *enew);
00057 
00058 
00065 #define dlist_insert_head(list, enew) \
00066         dlist_insert_after((list)->head, enew)
00067 
00068 #define dlist_push_head(list, enew)     \
00069         dlist_insert_after((list)->head, enew)
00070 
00071 
00078 void dlist_insert_before(struct dlist_head *elem, struct dlist_head *enew);
00079 
00080 
00087 void dlist_insert_tail(struct dlist_head *list, struct dlist_head *enew);
00088 
00089 #define dlist_push_tail(list, enew)     \
00090         dlist_insert_tail(list, enew)
00091 
00092 
00098 void dlist_remove(struct dlist_head *elem);
00099 
00100 
00107 struct dlist_head *dlist_first(const struct dlist_head *elem);
00108 
00109 
00116 struct dlist_head *dlist_last(const struct dlist_head *elem);
00117 
00118 
00125 struct dlist_head *dlist_next(const struct dlist_head *elem);
00126 
00127 
00134 struct dlist_head *dlist_prev(const struct dlist_head *elem);
00135 
00136 
00143 int dlist_is_head(const struct dlist_head *elem);
00144 
00145 
00152 int dlist_is_first(const struct dlist_head *elem);
00153 
00154 
00161 int dlist_is_last(const struct dlist_head *elem);
00162 
00163 
00170 void dlist_replace(struct dlist_head *old, struct dlist_head *enew);
00171 
00172 
00178 struct dlist_head *dlist_pop_head(const struct dlist_head *elem);
00179 
00180 
00186 struct dlist_head *dlist_pop_tail(const struct dlist_head *elem);
00187 
00188 
00196 #define dlist_for_each(head, p)         \
00197         for (p = (typeof(*p) *)(head)->next;    \
00198         p != (typeof(*p) *)(head) && p;         \
00199         p = (typeof(*p) *)(p)->next)
00200 
00201 
00210 #define dlist_for_each_safe(head, n, p) \
00211         for (p = (typeof(*p) *)(head)->next, n = (typeof(*p) *)(p)->next; \
00212         p != (typeof(*p) *)(head) && p;         \
00213         p = (typeof(*p) *)(n), n = (typeof(*p) *)(p)->next)
00214 
00215 
00223 #define dlist_for_each_reverse(head, p) \
00224         for (p = (typeof(*p) *)(head)->prev;    \
00225         p != (typeof(*p) *)(head) && p;         \
00226         p = (typeof(*p) *)(p)->prev)
00227 
00228 
00235 #define dlist_is_empty(head) ({         \
00236                 ((head)->next == head); \
00237         })
00238 
00239 
00246 #define dlist_num_entries(head) ({              \
00247                 struct dlist_head *p = (head);  \
00248                 int _x = 0;                     \
00249                 for (; p && p->next && p->next != (head); _x++) \
00250                         p = p->next;            \
00251                 _x;                             \
00252         })
00253 
00254 #endif /* _DLIST_H_ */
00255