[34] | 1 | #ifndef _VNETD_LIST_H_ |
---|
| 2 | #define _VNETD_LIST_H_ |
---|
| 3 | |
---|
| 4 | /* Taken from Linux kernel code, but de-kernelized for userspace. */ |
---|
| 5 | #include <stddef.h> |
---|
| 6 | |
---|
| 7 | /* |
---|
| 8 | * These are non-NULL pointers that will result in page faults |
---|
| 9 | * under normal circumstances, used to verify that nobody uses |
---|
| 10 | * non-initialized list entries. |
---|
| 11 | */ |
---|
| 12 | #define LIST_POISON1 ((void *) 0x00100100) |
---|
| 13 | #define LIST_POISON2 ((void *) 0x00200200) |
---|
| 14 | |
---|
| 15 | #define container_of(ptr, type, member) ({ \ |
---|
| 16 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
---|
| 17 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
---|
| 18 | |
---|
| 19 | /* |
---|
| 20 | * Simple doubly linked list implementation. |
---|
| 21 | * |
---|
| 22 | * Some of the internal functions ("__xxx") are useful when |
---|
| 23 | * manipulating whole lists rather than single entries, as |
---|
| 24 | * sometimes we already know the next/prev entries and we can |
---|
| 25 | * generate better code by using them directly rather than |
---|
| 26 | * using the generic single-entry routines. |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | struct list_head { |
---|
| 30 | struct list_head *next, *prev; |
---|
| 31 | }; |
---|
| 32 | |
---|
| 33 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
---|
| 34 | |
---|
| 35 | #define LIST_HEAD(name) \ |
---|
| 36 | struct list_head name = LIST_HEAD_INIT(name) |
---|
| 37 | |
---|
| 38 | #define INIT_LIST_HEAD(ptr) do { \ |
---|
| 39 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ |
---|
| 40 | } while (0) |
---|
| 41 | |
---|
| 42 | /* |
---|
| 43 | * Insert a new entry between two known consecutive entries. |
---|
| 44 | * |
---|
| 45 | * This is only for internal list manipulation where we know |
---|
| 46 | * the prev/next entries already! |
---|
| 47 | */ |
---|
| 48 | static inline void __list_add(struct list_head *new, |
---|
| 49 | struct list_head *prev, |
---|
| 50 | struct list_head *next) |
---|
| 51 | { |
---|
| 52 | next->prev = new; |
---|
| 53 | new->next = next; |
---|
| 54 | new->prev = prev; |
---|
| 55 | prev->next = new; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * list_add - add a new entry |
---|
| 60 | * @new: new entry to be added |
---|
| 61 | * @head: list head to add it after |
---|
| 62 | * |
---|
| 63 | * Insert a new entry after the specified head. |
---|
| 64 | * This is good for implementing stacks. |
---|
| 65 | */ |
---|
| 66 | static inline void list_add(struct list_head *new, struct list_head *head) |
---|
| 67 | { |
---|
| 68 | __list_add(new, head, head->next); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * list_add_tail - add a new entry |
---|
| 73 | * @new: new entry to be added |
---|
| 74 | * @head: list head to add it before |
---|
| 75 | * |
---|
| 76 | * Insert a new entry before the specified head. |
---|
| 77 | * This is useful for implementing queues. |
---|
| 78 | */ |
---|
| 79 | static inline void list_add_tail(struct list_head *new, struct list_head *head) |
---|
| 80 | { |
---|
| 81 | __list_add(new, head->prev, head); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /* |
---|
| 85 | * Delete a list entry by making the prev/next entries |
---|
| 86 | * point to each other. |
---|
| 87 | * |
---|
| 88 | * This is only for internal list manipulation where we know |
---|
| 89 | * the prev/next entries already! |
---|
| 90 | */ |
---|
| 91 | static inline void __list_del(struct list_head * prev, struct list_head * next) |
---|
| 92 | { |
---|
| 93 | next->prev = prev; |
---|
| 94 | prev->next = next; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * list_del - deletes entry from list. |
---|
| 99 | * @entry: the element to delete from the list. |
---|
| 100 | * Note: list_empty on entry does not return true after this, the entry is |
---|
| 101 | * in an undefined state. |
---|
| 102 | */ |
---|
| 103 | static inline void list_del(struct list_head *entry) |
---|
| 104 | { |
---|
| 105 | __list_del(entry->prev, entry->next); |
---|
| 106 | entry->next = LIST_POISON1; |
---|
| 107 | entry->prev = LIST_POISON2; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | * list_del_rcu - deletes entry from list without re-initialization |
---|
| 112 | * @entry: the element to delete from the list. |
---|
| 113 | * |
---|
| 114 | * Note: list_empty on entry does not return true after this, |
---|
| 115 | * the entry is in an undefined state. It is useful for RCU based |
---|
| 116 | * lockfree traversal. |
---|
| 117 | * |
---|
| 118 | * In particular, it means that we can not poison the forward |
---|
| 119 | * pointers that may still be used for walking the list. |
---|
| 120 | */ |
---|
| 121 | static inline void list_del_rcu(struct list_head *entry) |
---|
| 122 | { |
---|
| 123 | __list_del(entry->prev, entry->next); |
---|
| 124 | entry->prev = LIST_POISON2; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * list_del_init - deletes entry from list and reinitialize it. |
---|
| 129 | * @entry: the element to delete from the list. |
---|
| 130 | */ |
---|
| 131 | static inline void list_del_init(struct list_head *entry) |
---|
| 132 | { |
---|
| 133 | __list_del(entry->prev, entry->next); |
---|
| 134 | INIT_LIST_HEAD(entry); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * list_move - delete from one list and add as another's head |
---|
| 139 | * @list: the entry to move |
---|
| 140 | * @head: the head that will precede our entry |
---|
| 141 | */ |
---|
| 142 | static inline void list_move(struct list_head *list, struct list_head *head) |
---|
| 143 | { |
---|
| 144 | __list_del(list->prev, list->next); |
---|
| 145 | list_add(list, head); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * list_move_tail - delete from one list and add as another's tail |
---|
| 150 | * @list: the entry to move |
---|
| 151 | * @head: the head that will follow our entry |
---|
| 152 | */ |
---|
| 153 | static inline void list_move_tail(struct list_head *list, |
---|
| 154 | struct list_head *head) |
---|
| 155 | { |
---|
| 156 | __list_del(list->prev, list->next); |
---|
| 157 | list_add_tail(list, head); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * list_empty - tests whether a list is empty |
---|
| 162 | * @head: the list to test. |
---|
| 163 | */ |
---|
| 164 | static inline int list_empty(struct list_head *head) |
---|
| 165 | { |
---|
| 166 | return head->next == head; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | static inline void __list_splice(struct list_head *list, |
---|
| 170 | struct list_head *head) |
---|
| 171 | { |
---|
| 172 | struct list_head *first = list->next; |
---|
| 173 | struct list_head *last = list->prev; |
---|
| 174 | struct list_head *at = head->next; |
---|
| 175 | |
---|
| 176 | first->prev = head; |
---|
| 177 | head->next = first; |
---|
| 178 | |
---|
| 179 | last->next = at; |
---|
| 180 | at->prev = last; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * list_splice - join two lists |
---|
| 185 | * @list: the new list to add. |
---|
| 186 | * @head: the place to add it in the first list. |
---|
| 187 | */ |
---|
| 188 | static inline void list_splice(struct list_head *list, struct list_head *head) |
---|
| 189 | { |
---|
| 190 | if (!list_empty(list)) |
---|
| 191 | __list_splice(list, head); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | * list_splice_init - join two lists and reinitialise the emptied list. |
---|
| 196 | * @list: the new list to add. |
---|
| 197 | * @head: the place to add it in the first list. |
---|
| 198 | * |
---|
| 199 | * The list at @list is reinitialised |
---|
| 200 | */ |
---|
| 201 | static inline void list_splice_init(struct list_head *list, |
---|
| 202 | struct list_head *head) |
---|
| 203 | { |
---|
| 204 | if (!list_empty(list)) { |
---|
| 205 | __list_splice(list, head); |
---|
| 206 | INIT_LIST_HEAD(list); |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | * list_entry - get the struct for this entry |
---|
| 212 | * @ptr: the &struct list_head pointer. |
---|
| 213 | * @type: the type of the struct this is embedded in. |
---|
| 214 | * @member: the name of the list_struct within the struct. |
---|
| 215 | */ |
---|
| 216 | #define list_entry(ptr, type, member) \ |
---|
| 217 | container_of(ptr, type, member) |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | * list_for_each - iterate over a list |
---|
| 221 | * @pos: the &struct list_head to use as a loop counter. |
---|
| 222 | * @head: the head for your list. |
---|
| 223 | */ |
---|
| 224 | #define list_for_each(pos, head) \ |
---|
| 225 | for (pos = (head)->next; pos != (head); pos = pos->next) |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * list_for_each_prev - iterate over a list backwards |
---|
| 229 | * @pos: the &struct list_head to use as a loop counter. |
---|
| 230 | * @head: the head for your list. |
---|
| 231 | */ |
---|
| 232 | #define list_for_each_prev(pos, head) \ |
---|
| 233 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
---|
| 234 | |
---|
| 235 | /** |
---|
| 236 | * list_for_each_safe - iterate over a list safe against removal of list entry |
---|
| 237 | * @pos: the &struct list_head to use as a loop counter. |
---|
| 238 | * @n: another &struct list_head to use as temporary storage |
---|
| 239 | * @head: the head for your list. |
---|
| 240 | */ |
---|
| 241 | #define list_for_each_safe(pos, n, head) \ |
---|
| 242 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
---|
| 243 | pos = n, n = pos->next) |
---|
| 244 | |
---|
| 245 | /** |
---|
| 246 | * list_for_each_entry - iterate over list of given type |
---|
| 247 | * @pos: the type * to use as a loop counter. |
---|
| 248 | * @head: the head for your list. |
---|
| 249 | * @member: the name of the list_struct within the struct. |
---|
| 250 | */ |
---|
| 251 | #define list_for_each_entry(pos, head, member) \ |
---|
| 252 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
---|
| 253 | &pos->member != (head); \ |
---|
| 254 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
---|
| 255 | |
---|
| 256 | /** |
---|
| 257 | * list_for_each_entry_reverse - iterate backwards over list of given type. |
---|
| 258 | * @pos: the type * to use as a loop counter. |
---|
| 259 | * @head: the head for your list. |
---|
| 260 | * @member: the name of the list_struct within the struct. |
---|
| 261 | */ |
---|
| 262 | #define list_for_each_entry_reverse(pos, head, member) \ |
---|
| 263 | for (pos = list_entry((head)->prev, typeof(*pos), member); \ |
---|
| 264 | &pos->member != (head); \ |
---|
| 265 | pos = list_entry(pos->member.prev, typeof(*pos), member)) |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | /** |
---|
| 269 | * list_for_each_entry_safe - iterate over list of given type safe against |
---|
| 270 | * removal of list entry |
---|
| 271 | * @pos: the type * to use as a loop counter. |
---|
| 272 | * @n: another type * to use as temporary storage |
---|
| 273 | * @head: the head for your list. |
---|
| 274 | * @member: the name of the list_struct within the struct. |
---|
| 275 | */ |
---|
| 276 | #define list_for_each_entry_safe(pos, n, head, member) \ |
---|
| 277 | for (pos = list_entry((head)->next, typeof(*pos), member), \ |
---|
| 278 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
---|
| 279 | &pos->member != (head); \ |
---|
| 280 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
---|
| 281 | |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | #endif /* _VNETD_LIST_H_ */ |
---|