usermode/library/common/list.h

00001 #ifndef _LIST_H
00002 #define _LIST_H
00003 
00004 #include <stddef.h>
00005 
00013 #define container_of(ptr, type, member) ({                \
00014   const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
00015   (type *)( (char *)__mptr - offsetof(type,member) );})
00016 
00017 static inline void prefetch(const void *x) {;}
00018 
00019 /*
00020  * These are non-NULL pointers that will result in page faults
00021  * under normal circumstances, used to verify that nobody uses
00022  * non-initialized list entries.
00023  */
00024 #define LIST_POISON1  ((void *) 0x00100100)
00025 #define LIST_POISON2  ((void *) 0x00200200)
00026 
00027 
00028 /*
00029  * Simple doubly linked list implementation.
00030  *
00031  * Some of the internal functions ("__xxx") are useful when
00032  * manipulating whole lists rather than single entries, as
00033  * sometimes we already know the next/prev entries and we can
00034  * generate better code by using them directly rather than
00035  * using the generic single-entry routines.
00036  */
00037 
00038 struct list_head {
00039         struct list_head *next, *prev;
00040 };
00041 
00042 #define LIST_HEAD_INIT(name) { &(name), &(name) }
00043 
00044 #define LIST_HEAD(name) \
00045         struct list_head name = LIST_HEAD_INIT(name)
00046 
00047 static inline void INIT_LIST_HEAD(struct list_head *list)
00048 {
00049         list->next = list;
00050         list->prev = list;
00051 }
00052 
00053 /*
00054  * Insert a new entry between two known consecutive entries.
00055  *
00056  * This is only for internal list manipulation where we know
00057  * the prev/next entries already!
00058  */
00059 static inline void __list_add(struct list_head *_new,
00060                               struct list_head *prev,
00061                               struct list_head *next)
00062 {
00063         next->prev = _new;
00064         _new->next = next;
00065         _new->prev = prev;
00066         prev->next = _new;
00067 }
00068 
00077 static inline void list_add(struct list_head *_new, struct list_head *head)
00078 {
00079         __list_add(_new, head, head->next);
00080 }
00081 
00090 static inline void list_add_tail(struct list_head *_new, struct list_head *head)
00091 {
00092         __list_add(_new, head->prev, head);
00093 }
00094 
00095 /*
00096  * Delete a list entry by making the prev/next entries
00097  * point to each other.
00098  *
00099  * This is only for internal list manipulation where we know
00100  * the prev/next entries already!
00101  */
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 
00114 static inline void list_del(struct list_head *entry)
00115 {
00116         __list_del(entry->prev, entry->next);
00117         entry->next = (struct list_head *) LIST_POISON1;
00118         entry->prev = (struct list_head *) LIST_POISON2;
00119 }
00120 
00127 static inline void list_replace(struct list_head *old,
00128                                 struct list_head *_new)
00129 {
00130         _new->next = old->next;
00131         _new->next->prev = _new;
00132         _new->prev = old->prev;
00133         _new->prev->next = _new;
00134 }
00135 
00136 static inline void list_replace_init(struct list_head *old,
00137                                         struct list_head *_new)
00138 {
00139         list_replace(old, _new);
00140         INIT_LIST_HEAD(old);
00141 }
00142 
00147 static inline void list_del_init(struct list_head *entry)
00148 {
00149         __list_del(entry->prev, entry->next);
00150         INIT_LIST_HEAD(entry);
00151 }
00152 
00158 static inline void list_move(struct list_head *list, struct list_head *head)
00159 {
00160         __list_del(list->prev, list->next);
00161         list_add(list, head);
00162 }
00163 
00169 static inline void list_move_tail(struct list_head *list,
00170                                   struct list_head *head)
00171 {
00172         __list_del(list->prev, list->next);
00173         list_add_tail(list, head);
00174 }
00175 
00181 static inline int list_is_last(const struct list_head *list,
00182                                 const struct list_head *head)
00183 {
00184         return list->next == head;
00185 }
00186 
00191 static inline int list_empty(const struct list_head *head)
00192 {
00193         return head->next == head;
00194 }
00195 
00209 static inline int list_empty_careful(const struct list_head *head)
00210 {
00211         struct list_head *next = head->next;
00212         return (next == head) && (next == head->prev);
00213 }
00214 
00215 static inline void __list_splice(struct list_head *list,
00216                                  struct list_head *head)
00217 {
00218         struct list_head *first = list->next;
00219         struct list_head *last = list->prev;
00220         struct list_head *at = head->next;
00221 
00222         first->prev = head;
00223         head->next = first;
00224 
00225         last->next = at;
00226         at->prev = last;
00227 }
00228 
00234 static inline void list_splice(struct list_head *list, struct list_head *head)
00235 {
00236         if (!list_empty(list))
00237                 __list_splice(list, head);
00238 }
00239 
00247 static inline void list_splice_init(struct list_head *list,
00248                                     struct list_head *head)
00249 {
00250         if (!list_empty(list)) {
00251                 __list_splice(list, head);
00252                 INIT_LIST_HEAD(list);
00253         }
00254 }
00255 
00262 #define list_entry(ptr, type, member) \
00263         container_of(ptr, type, member)
00264 
00270 #define list_for_each(pos, head) \
00271         for (pos = (head)->next; prefetch(pos->next), pos != (head); \
00272                 pos = pos->next)
00273 
00284 #define __list_for_each(pos, head) \
00285         for (pos = (head)->next; pos != (head); pos = pos->next)
00286 
00292 #define list_for_each_prev(pos, head) \
00293         for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
00294                 pos = pos->prev)
00295 
00302 #define list_for_each_safe(pos, n, head) \
00303         for (pos = (head)->next, n = pos->next; pos != (head); \
00304                 pos = n, n = pos->next)
00305 
00312 #define list_for_each_entry(pos, head, member)                          \
00313         for (pos = list_entry((head)->next, typeof(*pos), member);      \
00314              prefetch(pos->member.next), &pos->member != (head);        \
00315              pos = list_entry(pos->member.next, typeof(*pos), member))
00316 
00323 #define list_for_each_entry_reverse(pos, head, member)                  \
00324         for (pos = list_entry((head)->prev, typeof(*pos), member);      \
00325              prefetch(pos->member.prev), &pos->member != (head);        \
00326              pos = list_entry(pos->member.prev, typeof(*pos), member))
00327 
00336 #define list_prepare_entry(pos, head, member) \
00337         ((pos) ? : list_entry(head, typeof(*pos), member))
00338 
00348 #define list_for_each_entry_continue(pos, head, member)                 \
00349         for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
00350              prefetch(pos->member.next), &pos->member != (head);        \
00351              pos = list_entry(pos->member.next, typeof(*pos), member))
00352 
00361 #define list_for_each_entry_from(pos, head, member)                     \
00362         for (; prefetch(pos->member.next), &pos->member != (head);      \
00363              pos = list_entry(pos->member.next, typeof(*pos), member))
00364 
00372 #define list_for_each_entry_safe(pos, n, head, member)                  \
00373         for (pos = list_entry((head)->next, typeof(*pos), member),      \
00374                 n = list_entry(pos->member.next, typeof(*pos), member); \
00375              &pos->member != (head);                                    \
00376              pos = n, n = list_entry(n->member.next, typeof(*n), member))
00377 
00388 #define list_for_each_entry_safe_continue(pos, n, head, member)                 \
00389         for (pos = list_entry(pos->member.next, typeof(*pos), member),          \
00390                 n = list_entry(pos->member.next, typeof(*pos), member);         \
00391              &pos->member != (head);                                            \
00392              pos = n, n = list_entry(n->member.next, typeof(*n), member))
00393 
00404 #define list_for_each_entry_safe_from(pos, n, head, member)                     \
00405         for (n = list_entry(pos->member.next, typeof(*pos), member);            \
00406              &pos->member != (head);                                            \
00407              pos = n, n = list_entry(n->member.next, typeof(*n), member))
00408 
00419 #define list_for_each_entry_safe_reverse(pos, n, head, member)          \
00420         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
00421                 n = list_entry(pos->member.prev, typeof(*pos), member); \
00422              &pos->member != (head);                                    \
00423              pos = n, n = list_entry(n->member.prev, typeof(*n), member))
00424 
00425 
00426 /*
00427  * Double linked lists with a single pointer list head.
00428  * Mostly useful for hash tables where the two pointer list head is
00429  * too wasteful.
00430  * You lose the ability to access the tail in O(1).
00431  */
00432 
00433 struct hlist_head {
00434         struct hlist_node *first;
00435 };
00436 
00437 struct hlist_node {
00438         struct hlist_node *next, **pprev;
00439 };
00440 
00441 #define HLIST_HEAD_INIT { .first = NULL }
00442 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
00443 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
00444 static inline void INIT_HLIST_NODE(struct hlist_node *h)
00445 {
00446         h->next = NULL;
00447         h->pprev = NULL;
00448 }
00449 
00450 static inline int hlist_unhashed(const struct hlist_node *h)
00451 {
00452         return !h->pprev;
00453 }
00454 
00455 static inline int hlist_empty(const struct hlist_head *h)
00456 {
00457         return !h->first;
00458 }
00459 
00460 static inline void __hlist_del(struct hlist_node *n)
00461 {
00462         struct hlist_node *next = n->next;
00463         struct hlist_node **pprev = n->pprev;
00464         *pprev = next;
00465         if (next)
00466                 next->pprev = pprev;
00467 }
00468 
00469 static inline void hlist_del(struct hlist_node *n)
00470 {
00471         __hlist_del(n);
00472         n->next = (struct hlist_node *) LIST_POISON1;
00473         n->pprev = (struct hlist_node **) LIST_POISON2;
00474 }
00475 
00476 
00477 static inline void hlist_del_init(struct hlist_node *n)
00478 {
00479         if (!hlist_unhashed(n)) {
00480                 __hlist_del(n);
00481                 INIT_HLIST_NODE(n);
00482         }
00483 }
00484 
00485 
00486 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
00487 {
00488         struct hlist_node *first = h->first;
00489         n->next = first;
00490         if (first)
00491                 first->pprev = &n->next;
00492         h->first = n;
00493         n->pprev = &h->first;
00494 }
00495 
00496 
00497 /* next must be != NULL */
00498 static inline void hlist_add_before(struct hlist_node *n,
00499                                         struct hlist_node *next)
00500 {
00501         n->pprev = next->pprev;
00502         n->next = next;
00503         next->pprev = &n->next;
00504         *(n->pprev) = n;
00505 }
00506 
00507 static inline void hlist_add_after(struct hlist_node *n,
00508                                         struct hlist_node *next)
00509 {
00510         next->next = n->next;
00511         n->next = next;
00512         next->pprev = &n->next;
00513 
00514         if(next->next)
00515                 next->next->pprev  = &next->next;
00516 }
00517 
00518 
00519 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
00520 
00521 #define hlist_for_each(pos, head) \
00522         for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
00523              pos = pos->next)
00524 
00525 #define hlist_for_each_safe(pos, n, head) \
00526         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
00527              pos = n)
00528 
00536 #define hlist_for_each_entry(tpos, pos, head, member)                    \
00537         for (pos = (head)->first;                                        \
00538              pos && ({ prefetch(pos->next); 1;}) &&                      \
00539                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
00540              pos = pos->next)
00541 
00548 #define hlist_for_each_entry_continue(tpos, pos, member)                 \
00549         for (pos = (pos)->next;                                          \
00550              pos && ({ prefetch(pos->next); 1;}) &&                      \
00551                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
00552              pos = pos->next)
00553 
00560 #define hlist_for_each_entry_from(tpos, pos, member)                     \
00561         for (; pos && ({ prefetch(pos->next); 1;}) &&                    \
00562                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
00563              pos = pos->next)
00564 
00573 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
00574         for (pos = (head)->first;                                        \
00575              pos && ({ n = pos->next; 1; }) &&                           \
00576                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
00577              pos = n)
00578 
00590 #define hlist_for_each_entry_rcu(tpos, pos, head, member)                \
00591         for (pos = (head)->first;                                        \
00592              rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) &&     \
00593                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
00594              pos = pos->next)
00595 
00596 #endif /* _LIST_H */

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7