home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / pvfs2 / orangefs-2.8.3-20110323.tar.gz / orangefs-2.8.3-20110323.tar / orangefs / src / io / buffer / ncac-list.h < prev    next >
C/C++ Source or Header  |  2006-09-16  |  5KB  |  215 lines

  1. #ifndef _LIST_H
  2. #define _LIST_H
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. /*
  7.  * These are non-NULL pointers that will result in page faults
  8.  * under normal circumstances, used to verify that nobody uses
  9.  * non-initialized list entries.
  10.  */
  11. #define LIST_POISON1  ((void *) 0x00100100)
  12. #define LIST_POISON2  ((void *) 0x00200200)
  13.  
  14. /*
  15.  * Simple doubly linked list implementation.
  16.  *
  17.  * Some of the internal functions ("__xxx") are useful when
  18.  * manipulating whole lists rather than single entries, as
  19.  * sometimes we already know the next/prev entries and we can
  20.  * generate better code by using them directly rather than
  21.  * using the generic single-entry routines.
  22.  */
  23.  
  24. struct list_head {
  25.     struct list_head *next, *prev;
  26. };
  27.  
  28. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  29.  
  30. #define LIST_HEAD(name) \
  31.     struct list_head name = LIST_HEAD_INIT(name)
  32.  
  33. #define INIT_LIST_HEAD(ptr) do { \
  34.     (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  35. } while (0)
  36.  
  37. /*
  38.  * Insert a new entry between two known consecutive entries. 
  39.  *
  40.  * This is only for internal list manipulation where we know
  41.  * the prev/next entries already!
  42.  */
  43. static inline void __list_add(struct list_head *new,
  44.                   struct list_head *prev,
  45.                   struct list_head *next)
  46. {
  47.     next->prev = new;
  48.     new->next = next;
  49.     new->prev = prev;
  50.     prev->next = new;
  51. }
  52.  
  53. /**
  54.  * list_add - add a new entry
  55.  * @new: new entry to be added
  56.  * @head: list head to add it after
  57.  *
  58.  * Insert a new entry after the specified head.
  59.  * This is good for implementing stacks.
  60.  */
  61. static inline void list_add(struct list_head *new, struct list_head *head)
  62. {
  63.     __list_add(new, head, head->next);
  64. }
  65.  
  66. /**
  67.  * list_add_tail - add a new entry
  68.  * @new: new entry to be added
  69.  * @head: list head to add it before
  70.  *
  71.  * Insert a new entry before the specified head.
  72.  * This is useful for implementing queues.
  73.  */
  74. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  75. {
  76.     __list_add(new, head->prev, head);
  77. }
  78.  
  79. /*
  80.  * Delete a list entry by making the prev/next entries
  81.  * point to each other.
  82.  *
  83.  * This is only for internal list manipulation where we know
  84.  * the prev/next entries already!
  85.  */
  86. static inline void __list_del(struct list_head * prev, struct list_head * next)
  87. {
  88.     next->prev = prev;
  89.     prev->next = next;
  90. }
  91.  
  92. /**
  93.  * list_del - deletes entry from list.
  94.  * @entry: the element to delete from the list.
  95.  * Note: list_empty on entry does not return true after this, the entry is
  96.  * in an undefined state.
  97.  */
  98. static inline void list_del(struct list_head *entry)
  99. {
  100.     __list_del(entry->prev, entry->next);
  101.     entry->next = LIST_POISON1;
  102.     entry->prev = LIST_POISON2;
  103. }
  104.  
  105. /**
  106.  * list_del_init - deletes entry from list and reinitialize it.
  107.  * @entry: the element to delete from the list.
  108.  */
  109. static inline void list_del_init(struct list_head *entry)
  110. {
  111.     __list_del(entry->prev, entry->next);
  112.     INIT_LIST_HEAD(entry); 
  113. }
  114.  
  115. /**
  116.  * list_move - delete from one list and add as another's head
  117.  * @list: the entry to move
  118.  * @head: the head that will precede our entry
  119.  */
  120. static inline void list_move(struct list_head *list, struct list_head *head)
  121. {
  122.         __list_del(list->prev, list->next);
  123.         list_add(list, head);
  124. }
  125.  
  126. /**
  127.  * list_move_tail - delete from one list and add as another's tail
  128.  * @list: the entry to move
  129.  * @head: the head that will follow our entry
  130.  */
  131. static inline void list_move_tail(struct list_head *list,
  132.                   struct list_head *head)
  133. {
  134.         __list_del(list->prev, list->next);
  135.         list_add_tail(list, head);
  136. }
  137.  
  138. /**
  139.  * list_empty - tests whether a list is empty
  140.  * @head: the list to test.
  141.  */
  142. static inline int list_empty(struct list_head *head)
  143. {
  144.     return head->next == head;
  145. }
  146.  
  147. static inline void __list_splice(struct list_head *list,
  148.                  struct list_head *head)
  149. {
  150.     struct list_head *first = list->next;
  151.     struct list_head *last = list->prev;
  152.     struct list_head *at = head->next;
  153.  
  154.     first->prev = head;
  155.     head->next = first;
  156.  
  157.     last->next = at;
  158.     at->prev = last;
  159. }
  160.  
  161. /**
  162.  * list_splice - join two lists
  163.  * @list: the new list to add.
  164.  * @head: the place to add it in the first list.
  165.  */
  166. static inline void list_splice(struct list_head *list, struct list_head *head)
  167. {
  168.     if (!list_empty(list))
  169.         __list_splice(list, head);
  170. }
  171.  
  172. /**
  173.  * list_splice_init - join two lists and reinitialise the emptied list.
  174.  * @list: the new list to add.
  175.  * @head: the place to add it in the first list.
  176.  *
  177.  * The list at @list is reinitialised
  178.  */
  179. static inline void list_splice_init(struct list_head *list,
  180.                     struct list_head *head)
  181. {
  182.     if (!list_empty(list)) {
  183.         __list_splice(list, head);
  184.         INIT_LIST_HEAD(list);
  185.     }
  186. }
  187.  
  188. #undef offsetof
  189. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  190.  
  191. /**
  192.  * container_of - cast a member of a structure out to the containing structure
  193.  *
  194.  * @ptr:        the pointer to the member.
  195.  * @type:       the type of the container struct this is embedded in.
  196.  * @member:     the name of the member within the struct.
  197.  *
  198.  */
  199. #define container_of(ptr, type, member) ({                      \
  200.         __typeof__( ((type *)0)->member ) *__mptr = (ptr);    \
  201.         (type *)( (char *)__mptr - offsetof(type,member) );})
  202.  
  203.  
  204.  
  205. /**
  206.  * list_entry - get the struct for this entry
  207.  * @ptr:        the &struct list_head pointer.
  208.  * @type:       the type of the struct this is embedded in.
  209.  * @member:     the name of the list_struct within the struct.
  210.  */
  211. #define list_entry(ptr, type, member) \
  212.         container_of(ptr, type, member)
  213.  
  214. #endif
  215.