1 #if !defined(_BLKID_LIST_H) && !defined(LIST_HEAD_INIT) 2 #define _BLKID_LIST_H 3 4 #ifdef __cplusplus 5 extern "C" { 6 #endif 7 8 #ifdef __GNUC__ 9 #define _INLINE_ static __inline__ 10 #else /* For Watcom C */ 11 #define _INLINE_ static inline 12 #endif 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 INIT_LIST_HEAD(ptr) do { \ 31 (ptr)->next = (ptr); (ptr)->prev = (ptr); \ 32 } while (0) 33 34 /* 35 * Insert a new entry between two known consecutive entries. 36 * 37 * This is only for internal list manipulation where we know 38 * the prev/next entries already! 39 */ 40 _INLINE_ void __list_add(struct list_head * add, 41 struct list_head * prev, 42 struct list_head * next) 43 { 44 next->prev = add; 45 add->next = next; 46 add->prev = prev; 47 prev->next = add; 48 } 49 50 /** 51 * list_add - add a new entry 52 * @add: new entry to be added 53 * @head: list head to add it after 54 * 55 * Insert a new entry after the specified head. 56 * This is good for implementing stacks. 57 */ 58 _INLINE_ void list_add(struct list_head *add, struct list_head *head) 59 { 60 __list_add(add, head, head->next); 61 } 62 63 /** 64 * list_add_tail - add a new entry 65 * @add: new entry to be added 66 * @head: list head to add it before 67 * 68 * Insert a new entry before the specified head. 69 * This is useful for implementing queues. 70 */ 71 _INLINE_ void list_add_tail(struct list_head *add, struct list_head *head) 72 { 73 __list_add(add, head->prev, head); 74 } 75 76 /* 77 * Delete a list entry by making the prev/next entries 78 * point to each other. 79 * 80 * This is only for internal list manipulation where we know 81 * the prev/next entries already! 82 */ 83 _INLINE_ void __list_del(struct list_head * prev, 84 struct list_head * next) 85 { 86 next->prev = prev; 87 prev->next = next; 88 } 89 90 /** 91 * list_del - deletes entry from list. 92 * @entry: the element to delete from the list. 93 * 94 * list_empty() on @entry does not return true after this, @entry is 95 * in an undefined state. 96 */ 97 _INLINE_ void list_del(struct list_head *entry) 98 { 99 __list_del(entry->prev, entry->next); 100 } 101 102 /** 103 * list_del_init - deletes entry from list and reinitialize it. 104 * @entry: the element to delete from the list. 105 */ 106 _INLINE_ void list_del_init(struct list_head *entry) 107 { 108 __list_del(entry->prev, entry->next); 109 INIT_LIST_HEAD(entry); 110 } 111 112 /** 113 * list_empty - tests whether a list is empty 114 * @head: the list to test. 115 */ 116 _INLINE_ int list_empty(struct list_head *head) 117 { 118 return head->next == head; 119 } 120 121 /** 122 * list_splice - join two lists 123 * @list: the new list to add. 124 * @head: the place to add it in the first list. 125 */ 126 _INLINE_ void list_splice(struct list_head *list, struct list_head *head) 127 { 128 struct list_head *first = list->next; 129 130 if (first != list) { 131 struct list_head *last = list->prev; 132 struct list_head *at = head->next; 133 134 first->prev = head; 135 head->next = first; 136 137 last->next = at; 138 at->prev = last; 139 } 140 } 141 142 /** 143 * list_entry - get the struct for this entry 144 * @ptr: the &struct list_head pointer. 145 * @type: the type of the struct this is embedded in. 146 * @member: the name of the list_struct within the struct. 147 */ 148 #define list_entry(ptr, type, member) \ 149 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) 150 151 /** 152 * list_for_each - iterate over elements in a list 153 * @pos: the &struct list_head to use as a loop counter. 154 * @head: the head for your list. 155 */ 156 #define list_for_each(pos, head) \ 157 for (pos = (head)->next; pos != (head); pos = pos->next) 158 159 /** 160 * list_for_each_safe - iterate over elements in a list, but don't dereference 161 * pos after the body is done (in case it is freed) 162 * @pos: the &struct list_head to use as a loop counter. 163 * @pnext: the &struct list_head to use as a pointer to the next item. 164 * @head: the head for your list (not included in iteration). 165 */ 166 #define list_for_each_safe(pos, pnext, head) \ 167 for (pos = (head)->next, pnext = pos->next; pos != (head); \ 168 pos = pnext, pnext = pos->next) 169 170 #undef _INLINE_ 171 172 #ifdef __cplusplus 173 } 174 #endif 175 176 #endif /* _BLKID_LIST_H */ 177