| 1 | #ifndef __LIST_H |
| 2 | #define __LIST_H |
| 3 | |
| 4 | /* This file is from Linux Kernel (include/linux/list.h) |
| 5 | * and modified by simply removing hardware prefetching of list items. |
| 6 | * Here by copyright, credits attributed to wherever they belong. |
| 7 | * Kulesh Shanmugasundaram (kulesh [squiggly] isis.poly.edu) |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Simple doubly linked list implementation. |
| 12 | * |
| 13 | * Some of the internal functions ("__xxx") are useful when |
| 14 | * manipulating whole lists rather than single entries, as |
| 15 | * sometimes we already know the next/prev entries and we can |
| 16 | * generate better code by using them directly rather than |
| 17 | * using the generic single-entry routines. |
| 18 | */ |
| 19 | |
| 20 | struct list_head { |
| 21 | struct list_head *next, *prev; |
| 22 | }; |
| 23 | |
| 24 | #define typeof __typeof__ |
| 25 | |
| 26 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
| 27 | |
| 28 | #define LIST_HEAD(name) \ |
| 29 | struct list_head name = LIST_HEAD_INIT(name) |
| 30 | |
| 31 | #define INIT_LIST_HEAD(ptr) do { \ |
| 32 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ |
| 33 | } while (0) |
| 34 | |
| 35 | /* |
| 36 | * Insert a new entry between two known consecutive entries. |
| 37 | * |
| 38 | * This is only for internal list manipulation where we know |
| 39 | * the prev/next entries already! |
| 40 | */ |
| 41 | static inline void __list_add(struct list_head *new, |
| 42 | struct list_head *prev, |
| 43 | struct list_head *next) |
| 44 | { |
| 45 | next->prev = new; |
| 46 | new->next = next; |
| 47 | new->prev = prev; |
| 48 | prev->next = new; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * list_add - add a new entry |
| 53 | * @new: new entry to be added |
| 54 | * @head: list head to add it after |
| 55 | * |
| 56 | * Insert a new entry after the specified head. |
| 57 | * This is good for implementing stacks. |
| 58 | */ |
| 59 | static inline void list_add(struct list_head *new, struct list_head *head) |
| 60 | { |
| 61 | __list_add(new, head, head->next); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * list_add_tail - add a new entry |
| 66 | * @new: new entry to be added |
| 67 | * @head: list head to add it before |
| 68 | * |
| 69 | * Insert a new entry before the specified head. |
| 70 | * This is useful for implementing queues. |
| 71 | */ |
| 72 | static inline void list_add_tail(struct list_head *new, struct list_head *head) |
| 73 | { |
| 74 | __list_add(new, head->prev, head); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Delete a list entry by making the prev/next entries |
| 79 | * point to each other. |
| 80 | * |
| 81 | * This is only for internal list manipulation where we know |
| 82 | * the prev/next entries already! |
| 83 | */ |
| 84 | static inline void __list_del(struct list_head *prev, 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 | * Note: list_empty on entry does not return true after this, the entry is in an undefined state. |
| 94 | */ |
| 95 | static inline void list_del(struct list_head *entry) |
| 96 | { |
| 97 | __list_del(entry->prev, entry->next); |
| 98 | entry->next = (void *) 0; |
| 99 | entry->prev = (void *) 0; |
| 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 | static 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_move - delete from one list and add as another's head |
| 114 | * @list: the entry to move |
| 115 | * @head: the head that will precede our entry |
| 116 | */ |
| 117 | static inline void list_move(struct list_head *list, struct list_head *head) |
| 118 | { |
| 119 | __list_del(list->prev, list->next); |
| 120 | list_add(list, head); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * list_move_tail - delete from one list and add as another's tail |
| 125 | * @list: the entry to move |
| 126 | * @head: the head that will follow our entry |
| 127 | */ |
| 128 | static inline void list_move_tail(struct list_head *list, |
| 129 | struct list_head *head) |
| 130 | { |
| 131 | __list_del(list->prev, list->next); |
| 132 | list_add_tail(list, head); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * list_empty - tests whether a list is empty |
| 137 | * @head: the list to test. |
| 138 | */ |
| 139 | static inline int list_empty(struct list_head *head) |
| 140 | { |
| 141 | return head->next == head; |
| 142 | } |
| 143 | |
| 144 | static inline void __list_splice(struct list_head *list, |
| 145 | struct list_head *head) |
| 146 | { |
| 147 | struct list_head *first = list->next; |
| 148 | struct list_head *last = list->prev; |
| 149 | struct list_head *at = head->next; |
| 150 | |
| 151 | first->prev = head; |
| 152 | head->next = first; |
| 153 | |
| 154 | last->next = at; |
| 155 | at->prev = last; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * list_splice - join two lists |
| 160 | * @list: the new list to add. |
| 161 | * @head: the place to add it in the first list. |
| 162 | */ |
| 163 | static inline void list_splice(struct list_head *list, struct list_head *head) |
| 164 | { |
| 165 | if (!list_empty(list)) |
| 166 | __list_splice(list, head); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * list_splice_init - join two lists and reinitialise the emptied list. |
| 171 | * @list: the new list to add. |
| 172 | * @head: the place to add it in the first list. |
| 173 | * |
| 174 | * The list at @list is reinitialised |
| 175 | */ |
| 176 | static inline void list_splice_init(struct list_head *list, |
| 177 | struct list_head *head) |
| 178 | { |
| 179 | if (!list_empty(list)) { |
| 180 | __list_splice(list, head); |
| 181 | INIT_LIST_HEAD(list); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * list_entry - get the struct for this entry |
| 187 | * @ptr: the &struct list_head pointer. |
| 188 | * @type: the type of the struct this is embedded in. |
| 189 | * @member: the name of the list_struct within the struct. |
| 190 | */ |
| 191 | #define list_entry(ptr, type, member) \ |
| 192 | ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) |
| 193 | |
| 194 | /** |
| 195 | * list_for_each - iterate over a list |
| 196 | * @pos: the &struct list_head to use as a loop counter. |
| 197 | * @head: the head for your list. |
| 198 | */ |
| 199 | #define list_for_each(pos, head) \ |
| 200 | for (pos = (head)->next; pos != (head); \ |
| 201 | pos = pos->next) |
| 202 | /** |
| 203 | * list_for_each_prev - iterate over a list backwards |
| 204 | * @pos: the &struct list_head to use as a loop counter. |
| 205 | * @head: the head for your list. |
| 206 | */ |
| 207 | #define list_for_each_prev(pos, head) \ |
| 208 | for (pos = (head)->prev; pos != (head); \ |
| 209 | pos = pos->prev) |
| 210 | |
| 211 | /** |
| 212 | * list_for_each_safe - iterate over a list safe against removal of list entry |
| 213 | * @pos: the &struct list_head to use as a loop counter. |
| 214 | * @n: another &struct list_head to use as temporary storage |
| 215 | * @head: the head for your list. |
| 216 | */ |
| 217 | #define list_for_each_safe(pos, n, head) \ |
| 218 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
| 219 | pos = n, n = pos->next) |
| 220 | |
| 221 | /** |
| 222 | * list_for_each_entry - iterate over list of given type |
| 223 | * @pos: the type * to use as a loop counter. |
| 224 | * @head: the head for your list. |
| 225 | * @member: the name of the list_struct within the struct. |
| 226 | */ |
| 227 | #define list_for_each_entry(pos, head, member) \ |
| 228 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
| 229 | &pos->member != (head); \ |
| 230 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 231 | |
| 232 | /** |
| 233 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
| 234 | * @pos: the type * to use as a loop counter. |
| 235 | * @n: another type * to use as temporary storage |
| 236 | * @head: the head for your list. |
| 237 | * @member: the name of the list_struct within the struct. |
| 238 | */ |
| 239 | #define list_for_each_entry_safe(pos, n, head, member) \ |
| 240 | for (pos = list_entry((head)->next, typeof(*pos), member), \ |
| 241 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
| 242 | &pos->member != (head); \ |
| 243 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 244 | |
| 245 | |
| 246 | #endif |