1 #if !defined(_BLKID_LIST_H) && !defined(LIST_HEAD) 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 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 _INLINE_ void __list_add(struct list_head * add, 44 struct list_head * prev, 45 struct list_head * next) 46 { 47 next->prev = add; 48 add->next = next; 49 add->prev = prev; 50 prev->next = add; 51 } 52 53 /** 54 * list_add - add a new entry 55 * @add: 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 _INLINE_ void list_add(struct list_head *add, struct list_head *head) 62 { 63 __list_add(add, head, head->next); 64 } 65 66 /** 67 * list_add_tail - add a new entry 68 * @add: 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 _INLINE_ void list_add_tail(struct list_head *add, struct list_head *head) 75 { 76 __list_add(add, 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 _INLINE_ void __list_del(struct list_head * prev, 87 struct list_head * next) 88 { 89 next->prev = prev; 90 prev->next = next; 91 } 92 93 /** 94 * list_del - deletes entry from list. 95 * @entry: the element to delete from the list. 96 * 97 * list_empty() on @entry does not return true after this, @entry is 98 * in an undefined state. 99 */ 100 _INLINE_ void list_del(struct list_head *entry) 101 { 102 __list_del(entry->prev, entry->next); 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 _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_empty - tests whether a list is empty 117 * @head: the list to test. 118 */ 119 _INLINE_ int list_empty(struct list_head *head) 120 { 121 return head->next == head; 122 } 123 124 /** 125 * list_splice - join two lists 126 * @list: the new list to add. 127 * @head: the place to add it in the first list. 128 */ 129 _INLINE_ void list_splice(struct list_head *list, struct list_head *head) 130 { 131 struct list_head *first = list->next; 132 133 if (first != list) { 134 struct list_head *last = list->prev; 135 struct list_head *at = head->next; 136 137 first->prev = head; 138 head->next = first; 139 140 last->next = at; 141 at->prev = last; 142 } 143 } 144 145 /** 146 * list_entry - get the struct for this entry 147 * @ptr: the &struct list_head pointer. 148 * @type: the type of the struct this is embedded in. 149 * @member: the name of the list_struct within the struct. 150 */ 151 #define list_entry(ptr, type, member) \ 152 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) 153 154 /** 155 * list_for_each - iterate over elements in a list 156 * @pos: the &struct list_head to use as a loop counter. 157 * @head: the head for your list. 158 */ 159 #define list_for_each(pos, head) \ 160 for (pos = (head)->next; pos != (head); pos = pos->next) 161 162 /** 163 * list_for_each_safe - iterate over elements in a list, but don't dereference 164 * pos after the body is done (in case it is freed) 165 * @pos: the &struct list_head to use as a loop counter. 166 * @pnext: the &struct list_head to use as a pointer to the next item. 167 * @head: the head for your list (not included in iteration). 168 */ 169 #define list_for_each_safe(pos, pnext, head) \ 170 for (pos = (head)->next, pnext = pos->next; pos != (head); \ 171 pos = pnext, pnext = pos->next) 172 173 #undef _INLINE_ 174 175 #ifdef __cplusplus 176 } 177 #endif 178 179 #endif /* _BLKID_LIST_H */ 180