Home | History | Annotate | Download | only in include
      1 #ifndef _LIST_H
      2 #define _LIST_H
      3 
      4 /*
      5  * This code was taken from the Linux 2.4.0 kernel. [jaroslav]
      6  */
      7 
      8 /*
      9  * Simple doubly linked list implementation.
     10  *
     11  * Some of the internal functions ("__xxx") are useful when
     12  * manipulating whole lists rather than single entries, as
     13  * sometimes we already know the next/prev entries and we can
     14  * generate better code by using them directly rather than
     15  * using the generic single-entry routines.
     16  */
     17 
     18 #ifndef LIST_HEAD_IS_DEFINED
     19 struct list_head {
     20 	struct list_head *next, *prev;
     21 };
     22 #endif
     23 
     24 #define LIST_HEAD_INIT(name) { &(name), &(name) }
     25 
     26 #define LIST_HEAD(name) \
     27 	struct list_head name = LIST_HEAD_INIT(name)
     28 
     29 #define INIT_LIST_HEAD(ptr) do { \
     30 	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
     31 } while (0)
     32 
     33 /*
     34  * Insert a new entry between two known consecutive entries.
     35  *
     36  * This is only for internal list manipulation where we know
     37  * the prev/next entries already!
     38  */
     39 static __inline__ void __list_add(struct list_head * _new,
     40 				  struct list_head * prev,
     41 				  struct list_head * next)
     42 {
     43 	next->prev = _new;
     44 	_new->next = next;
     45 	_new->prev = prev;
     46 	prev->next = _new;
     47 }
     48 
     49 /**
     50  * list_add - add a new entry
     51  * @new: new entry to be added
     52  * @head: list head to add it after
     53  *
     54  * Insert a new entry after the specified head.
     55  * This is good for implementing stacks.
     56  */
     57 static __inline__ void list_add(struct list_head *_new, struct list_head *head)
     58 {
     59 	__list_add(_new, head, head->next);
     60 }
     61 
     62 /**
     63  * list_add_tail - add a new entry
     64  * @new: new entry to be added
     65  * @head: list head to add it before
     66  *
     67  * Insert a new entry before the specified head.
     68  * This is useful for implementing queues.
     69  */
     70 static __inline__ void list_add_tail(struct list_head *_new, struct list_head *head)
     71 {
     72 	__list_add(_new, head->prev, head);
     73 }
     74 
     75 /*
     76  * Delete a list entry by making the prev/next entries
     77  * point to each other.
     78  *
     79  * This is only for internal list manipulation where we know
     80  * the prev/next entries already!
     81  */
     82 static __inline__ void __list_del(struct list_head * prev,
     83 				  struct list_head * next)
     84 {
     85 	next->prev = prev;
     86 	prev->next = next;
     87 }
     88 
     89 /**
     90  * list_del - deletes entry from list.
     91  * @entry: the element to delete from the list.
     92  * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
     93  */
     94 static __inline__ void list_del(struct list_head *entry)
     95 {
     96 	__list_del(entry->prev, entry->next);
     97 }
     98 
     99 /**
    100  * list_del_init - deletes entry from list and reinitialize it.
    101  * @entry: the element to delete from the list.n
    102  */
    103 static __inline__ void list_del_init(struct list_head *entry)
    104 {
    105 	__list_del(entry->prev, entry->next);
    106 	INIT_LIST_HEAD(entry);
    107 }
    108 
    109 /**
    110  * list_empty - tests whether a list is empty
    111  * @head: the list to test.
    112  */
    113 static __inline__ int list_empty(struct list_head *head)
    114 {
    115 	return head->next == head;
    116 }
    117 
    118 /**
    119  * list_splice - join two lists
    120  * @list: the new list to add.
    121  * @head: the place to add it in the first list.
    122  */
    123 static __inline__ void list_splice(struct list_head *list, struct list_head *head)
    124 {
    125 	struct list_head *first = list->next;
    126 
    127 	if (first != list) {
    128 		struct list_head *last = list->prev;
    129 		struct list_head *at = head->next;
    130 
    131 		first->prev = head;
    132 		head->next = first;
    133 
    134 		last->next = at;
    135 		at->prev = last;
    136 	}
    137 }
    138 
    139 /**
    140  * list_for_each	-	iterate over a list
    141  * @pos:	the &struct list_head to use as a loop counter.
    142  * @head:	the head for your list.
    143  */
    144 #define list_for_each(pos, head) \
    145 	for (pos = (head)->next ; pos != (head); pos = pos->next)
    146 
    147 /**
    148  * list_for_each_safe	-	iterate over a list safely (actual pointer can be invalidated)
    149  * @pos:	the &struct list_head to use as a loop counter.
    150  * @next:	the &struct list_head to use to save next.
    151  * @head:	the head for your list.
    152  */
    153 #define list_for_each_safe(pos, npos, head) \
    154 	for (pos = (head)->next, npos = pos->next ; pos != (head); pos = npos, npos = pos->next)
    155 
    156 /**
    157  * list_entry - get the struct for this entry
    158  * @ptr:	the &struct list_head pointer.
    159  * @type:	the type of the struct this is embedded in.
    160  * @member:	the name of the list_struct within the struct.
    161  */
    162 #define list_entry(ptr, type, member) \
    163 	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
    164 
    165 
    166 #endif /* _LIST_H */
    167