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