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