src/dlist.h

Go to the documentation of this file.
00001 #ifndef __DLIST_H__
00002 #define __DLIST_H__
00003 #include "prefetch.h"
00004 
00005 /*
00006  * Double linked lists with a single pointer list head.
00007  * Mostly useful for hash tables where the two pointer list head is
00008  * too wasteful.
00009  * You lose the ability to access the tail in O(1).
00010  */
00011 
00012 struct hlist_head {
00013     struct hlist_node *first;
00014 };
00015 
00016 struct hlist_node {
00017     struct hlist_node *next, **pprev;
00018 };
00019 
00020 #define HLIST_HEAD_INIT { .first = NULL }
00021 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
00022 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
00023 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
00024 
00025 static inline int hlist_unhashed(const struct hlist_node *h)
00026 {
00027     return !h->pprev;
00028 }
00029 
00030 static inline int hlist_empty(const struct hlist_head *h)
00031 {
00032     return !h->first;
00033 }
00034 
00035 static inline void __hlist_del(struct hlist_node *n)
00036 {
00037     struct hlist_node *next = n->next;
00038     struct hlist_node **pprev = n->pprev;
00039     *pprev = next;
00040     if (next)
00041         next->pprev = pprev;
00042 }
00043 
00044 static inline void hlist_del(struct hlist_node *n)
00045 {
00046     __hlist_del(n);
00047     n->next = LIST_POISON1;
00048     n->pprev = LIST_POISON2;
00049 }
00050 
00051 static inline void hlist_del_init(struct hlist_node *n)
00052 {
00053     if (n->pprev)  {
00054         __hlist_del(n);
00055         INIT_HLIST_NODE(n);
00056     }
00057 }
00058 
00059 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
00060 {
00061     struct hlist_node *first = h->first;
00062     n->next = first;
00063     if (first)
00064         first->pprev = &n->next;
00065     h->first = n;
00066     n->pprev = &h->first;
00067 }
00068 
00069 
00070 static inline void hlist_add_before(struct hlist_node *n,
00071                     struct hlist_node *next)
00072 {
00073     n->pprev = next->pprev;
00074     n->next = next;
00075     next->pprev = &n->next;
00076     *(n->pprev) = n;
00077 }
00078 
00079 static inline void hlist_add_after(struct hlist_node *n,
00080                     struct hlist_node *next)
00081 {
00082     next->next = n->next;
00083     n->next = next;
00084     next->pprev = &n->next;
00085 
00086     if(next->next)
00087         next->next->pprev  = &next->next;
00088 }
00089 
00090 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
00091 
00092 #define hlist_for_each(pos, head) \
00093     for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
00094          pos = pos->next)
00095 
00096 #define hlist_for_each_safe(pos, n, head) \
00097     for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
00098          pos = n)
00099 
00107 #define hlist_for_each_entry(tpos, pos, head, member)            \
00108     for (pos = (head)->first;                    \
00109          pos && ({ prefetch(pos->next); 1;}) &&          \
00110         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
00111          pos = pos->next)
00112 
00119 #define hlist_for_each_entry_continue(tpos, pos, member)         \
00120     for (pos = (pos)->next;                      \
00121          pos && ({ prefetch(pos->next); 1;}) &&          \
00122         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
00123          pos = pos->next)
00124 
00131 #define hlist_for_each_entry_from(tpos, pos, member)             \
00132     for (; pos && ({ prefetch(pos->next); 1;}) &&            \
00133         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
00134          pos = pos->next)
00135 
00144 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)        \
00145     for (pos = (head)->first;                    \
00146          pos && ({ n = pos->next; 1; }) &&               \
00147         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
00148          pos = n)
00149 
00150 
00151 
00152 #endif

Generated on Mon May 28 04:25:19 2007 for BattletechMUX by  doxygen 1.4.7