Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_LIST_H |
| 2 | #define _LINUX_LIST_H |
| 3 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/stddef.h> |
Randy Dunlap | c9cf552 | 2006-06-27 02:53:52 -0700 | [diff] [blame] | 5 | #include <linux/poison.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/prefetch.h> |
| 7 | #include <asm/system.h> |
| 8 | |
| 9 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Simple doubly linked list implementation. |
| 11 | * |
| 12 | * Some of the internal functions ("__xxx") are useful when |
| 13 | * manipulating whole lists rather than single entries, as |
| 14 | * sometimes we already know the next/prev entries and we can |
| 15 | * generate better code by using them directly rather than |
| 16 | * using the generic single-entry routines. |
| 17 | */ |
| 18 | |
| 19 | struct list_head { |
| 20 | struct list_head *next, *prev; |
| 21 | }; |
| 22 | |
| 23 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
| 24 | |
| 25 | #define LIST_HEAD(name) \ |
| 26 | struct list_head name = LIST_HEAD_INIT(name) |
| 27 | |
Zach Brown | 490d6ab | 2006-02-03 03:03:56 -0800 | [diff] [blame] | 28 | static inline void INIT_LIST_HEAD(struct list_head *list) |
| 29 | { |
| 30 | list->next = list; |
| 31 | list->prev = list; |
| 32 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 40 | #ifndef CONFIG_DEBUG_LIST |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 50 | #else |
| 51 | extern void __list_add(struct list_head *new, |
| 52 | struct list_head *prev, |
| 53 | struct list_head *next); |
| 54 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * list_add - add a new entry |
| 58 | * @new: new entry to be added |
| 59 | * @head: list head to add it after |
| 60 | * |
| 61 | * Insert a new entry after the specified head. |
| 62 | * This is good for implementing stacks. |
| 63 | */ |
| 64 | static inline void list_add(struct list_head *new, struct list_head *head) |
| 65 | { |
| 66 | __list_add(new, head, head->next); |
| 67 | } |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * list_add_tail - add a new entry |
| 72 | * @new: new entry to be added |
| 73 | * @head: list head to add it before |
| 74 | * |
| 75 | * Insert a new entry before the specified head. |
| 76 | * This is useful for implementing queues. |
| 77 | */ |
| 78 | static inline void list_add_tail(struct list_head *new, struct list_head *head) |
| 79 | { |
| 80 | __list_add(new, head->prev, head); |
| 81 | } |
| 82 | |
| 83 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * Delete a list entry by making the prev/next entries |
| 85 | * point to each other. |
| 86 | * |
| 87 | * This is only for internal list manipulation where we know |
| 88 | * the prev/next entries already! |
| 89 | */ |
| 90 | static inline void __list_del(struct list_head * prev, struct list_head * next) |
| 91 | { |
| 92 | next->prev = prev; |
| 93 | prev->next = next; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * list_del - deletes entry from list. |
| 98 | * @entry: the element to delete from the list. |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 99 | * Note: list_empty() on entry does not return true after this, the entry is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | * in an undefined state. |
| 101 | */ |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 102 | #ifndef CONFIG_DEBUG_LIST |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 109 | #else |
| 110 | extern void list_del(struct list_head *entry); |
| 111 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | /** |
Oleg Nesterov | 54e7377 | 2006-06-23 02:05:54 -0700 | [diff] [blame] | 114 | * list_replace - replace old entry by new one |
| 115 | * @old : the element to be replaced |
| 116 | * @new : the new element to insert |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 117 | * |
| 118 | * If @old was empty, it will be overwritten. |
Oleg Nesterov | 54e7377 | 2006-06-23 02:05:54 -0700 | [diff] [blame] | 119 | */ |
| 120 | static inline void list_replace(struct list_head *old, |
| 121 | struct list_head *new) |
| 122 | { |
| 123 | new->next = old->next; |
| 124 | new->next->prev = new; |
| 125 | new->prev = old->prev; |
| 126 | new->prev->next = new; |
| 127 | } |
| 128 | |
| 129 | static inline void list_replace_init(struct list_head *old, |
| 130 | struct list_head *new) |
| 131 | { |
| 132 | list_replace(old, new); |
| 133 | INIT_LIST_HEAD(old); |
| 134 | } |
| 135 | |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 136 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | * list_del_init - deletes entry from list and reinitialize it. |
| 138 | * @entry: the element to delete from the list. |
| 139 | */ |
| 140 | static inline void list_del_init(struct list_head *entry) |
| 141 | { |
| 142 | __list_del(entry->prev, entry->next); |
| 143 | INIT_LIST_HEAD(entry); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * list_move - delete from one list and add as another's head |
| 148 | * @list: the entry to move |
| 149 | * @head: the head that will precede our entry |
| 150 | */ |
| 151 | static inline void list_move(struct list_head *list, struct list_head *head) |
| 152 | { |
Daniel Walker | 78db2ad | 2007-05-12 16:28:35 -0700 | [diff] [blame] | 153 | __list_del(list->prev, list->next); |
| 154 | list_add(list, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * list_move_tail - delete from one list and add as another's tail |
| 159 | * @list: the entry to move |
| 160 | * @head: the head that will follow our entry |
| 161 | */ |
| 162 | static inline void list_move_tail(struct list_head *list, |
| 163 | struct list_head *head) |
| 164 | { |
Daniel Walker | 78db2ad | 2007-05-12 16:28:35 -0700 | [diff] [blame] | 165 | __list_del(list->prev, list->next); |
| 166 | list_add_tail(list, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
Shailabh Nagar | e8f4d97 | 2006-07-14 00:24:35 -0700 | [diff] [blame] | 170 | * list_is_last - tests whether @list is the last entry in list @head |
| 171 | * @list: the entry to test |
| 172 | * @head: the head of the list |
| 173 | */ |
| 174 | static inline int list_is_last(const struct list_head *list, |
| 175 | const struct list_head *head) |
| 176 | { |
| 177 | return list->next == head; |
| 178 | } |
| 179 | |
| 180 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | * list_empty - tests whether a list is empty |
| 182 | * @head: the list to test. |
| 183 | */ |
| 184 | static inline int list_empty(const struct list_head *head) |
| 185 | { |
| 186 | return head->next == head; |
| 187 | } |
| 188 | |
| 189 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 190 | * list_empty_careful - tests whether a list is empty and not being modified |
| 191 | * @head: the list to test |
| 192 | * |
| 193 | * Description: |
| 194 | * tests whether a list is empty _and_ checks that no other CPU might be |
| 195 | * in the process of modifying either member (next or prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | * |
| 197 | * NOTE: using list_empty_careful() without synchronization |
| 198 | * can only be safe if the only activity that can happen |
| 199 | * to the list entry is list_del_init(). Eg. it cannot be used |
| 200 | * if another CPU could re-list_add() it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | */ |
| 202 | static inline int list_empty_careful(const struct list_head *head) |
| 203 | { |
| 204 | struct list_head *next = head->next; |
| 205 | return (next == head) && (next == head->prev); |
| 206 | } |
| 207 | |
Masami Hiramatsu | 9960257 | 2008-04-28 02:14:27 -0700 | [diff] [blame] | 208 | /** |
Frederic Weisbecker | 5908cdc | 2010-01-09 20:53:14 +0100 | [diff] [blame] | 209 | * list_rotate_left - rotate the list to the left |
| 210 | * @head: the head of the list |
| 211 | */ |
| 212 | static inline void list_rotate_left(struct list_head *head) |
| 213 | { |
| 214 | struct list_head *first; |
| 215 | |
| 216 | if (!list_empty(head)) { |
| 217 | first = head->next; |
| 218 | list_move_tail(first, head); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
Masami Hiramatsu | 9960257 | 2008-04-28 02:14:27 -0700 | [diff] [blame] | 223 | * list_is_singular - tests whether a list has just one entry. |
| 224 | * @head: the list to test. |
| 225 | */ |
| 226 | static inline int list_is_singular(const struct list_head *head) |
| 227 | { |
| 228 | return !list_empty(head) && (head->next == head->prev); |
| 229 | } |
| 230 | |
Luis R. Rodriguez | 00e8a4d | 2008-08-06 13:28:54 -0700 | [diff] [blame] | 231 | static inline void __list_cut_position(struct list_head *list, |
| 232 | struct list_head *head, struct list_head *entry) |
| 233 | { |
| 234 | struct list_head *new_first = entry->next; |
| 235 | list->next = head->next; |
| 236 | list->next->prev = list; |
| 237 | list->prev = entry; |
| 238 | entry->next = list; |
| 239 | head->next = new_first; |
| 240 | new_first->prev = head; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * list_cut_position - cut a list into two |
| 245 | * @list: a new list to add all removed entries |
| 246 | * @head: a list with entries |
| 247 | * @entry: an entry within head, could be the head itself |
| 248 | * and if so we won't cut the list |
| 249 | * |
| 250 | * This helper moves the initial part of @head, up to and |
| 251 | * including @entry, from @head to @list. You should |
| 252 | * pass on @entry an element you know is on @head. @list |
| 253 | * should be an empty list or a list you do not care about |
| 254 | * losing its data. |
| 255 | * |
| 256 | */ |
| 257 | static inline void list_cut_position(struct list_head *list, |
| 258 | struct list_head *head, struct list_head *entry) |
| 259 | { |
| 260 | if (list_empty(head)) |
| 261 | return; |
| 262 | if (list_is_singular(head) && |
| 263 | (head->next != entry && head != entry)) |
| 264 | return; |
| 265 | if (entry == head) |
| 266 | INIT_LIST_HEAD(list); |
| 267 | else |
| 268 | __list_cut_position(list, head, entry); |
| 269 | } |
| 270 | |
Robert P. J. Day | 95d8c36 | 2008-04-29 00:59:29 -0700 | [diff] [blame] | 271 | static inline void __list_splice(const struct list_head *list, |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 272 | struct list_head *prev, |
| 273 | struct list_head *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
| 275 | struct list_head *first = list->next; |
| 276 | struct list_head *last = list->prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 278 | first->prev = prev; |
| 279 | prev->next = first; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 281 | last->next = next; |
| 282 | next->prev = last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 286 | * list_splice - join two lists, this is designed for stacks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | * @list: the new list to add. |
| 288 | * @head: the place to add it in the first list. |
| 289 | */ |
Robert P. J. Day | 95d8c36 | 2008-04-29 00:59:29 -0700 | [diff] [blame] | 290 | static inline void list_splice(const struct list_head *list, |
| 291 | struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
| 293 | if (!list_empty(list)) |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 294 | __list_splice(list, head, head->next); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * list_splice_tail - join two lists, each list being a queue |
| 299 | * @list: the new list to add. |
| 300 | * @head: the place to add it in the first list. |
| 301 | */ |
| 302 | static inline void list_splice_tail(struct list_head *list, |
| 303 | struct list_head *head) |
| 304 | { |
| 305 | if (!list_empty(list)) |
| 306 | __list_splice(list, head->prev, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * list_splice_init - join two lists and reinitialise the emptied list. |
| 311 | * @list: the new list to add. |
| 312 | * @head: the place to add it in the first list. |
| 313 | * |
| 314 | * The list at @list is reinitialised |
| 315 | */ |
| 316 | static inline void list_splice_init(struct list_head *list, |
| 317 | struct list_head *head) |
| 318 | { |
| 319 | if (!list_empty(list)) { |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 320 | __list_splice(list, head, head->next); |
| 321 | INIT_LIST_HEAD(list); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
Randy Dunlap | 6724cce | 2008-08-08 13:56:20 -0700 | [diff] [blame] | 326 | * list_splice_tail_init - join two lists and reinitialise the emptied list |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 327 | * @list: the new list to add. |
| 328 | * @head: the place to add it in the first list. |
| 329 | * |
Randy Dunlap | 6724cce | 2008-08-08 13:56:20 -0700 | [diff] [blame] | 330 | * Each of the lists is a queue. |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 331 | * The list at @list is reinitialised |
| 332 | */ |
| 333 | static inline void list_splice_tail_init(struct list_head *list, |
| 334 | struct list_head *head) |
| 335 | { |
| 336 | if (!list_empty(list)) { |
| 337 | __list_splice(list, head->prev, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | INIT_LIST_HEAD(list); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * list_entry - get the struct for this entry |
| 344 | * @ptr: the &struct list_head pointer. |
| 345 | * @type: the type of the struct this is embedded in. |
| 346 | * @member: the name of the list_struct within the struct. |
| 347 | */ |
| 348 | #define list_entry(ptr, type, member) \ |
| 349 | container_of(ptr, type, member) |
| 350 | |
| 351 | /** |
Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 352 | * list_first_entry - get the first element from a list |
| 353 | * @ptr: the list head to take the element from. |
| 354 | * @type: the type of the struct this is embedded in. |
| 355 | * @member: the name of the list_struct within the struct. |
| 356 | * |
| 357 | * Note, that list is expected to be not empty. |
| 358 | */ |
| 359 | #define list_first_entry(ptr, type, member) \ |
| 360 | list_entry((ptr)->next, type, member) |
| 361 | |
| 362 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | * list_for_each - iterate over a list |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 364 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | * @head: the head for your list. |
| 366 | */ |
| 367 | #define list_for_each(pos, head) \ |
| 368 | for (pos = (head)->next; prefetch(pos->next), pos != (head); \ |
| 369 | pos = pos->next) |
| 370 | |
| 371 | /** |
| 372 | * __list_for_each - iterate over a list |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 373 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | * @head: the head for your list. |
| 375 | * |
| 376 | * This variant differs from list_for_each() in that it's the |
| 377 | * simplest possible list iteration code, no prefetching is done. |
| 378 | * Use this for code that knows the list to be very short (empty |
| 379 | * or 1 entry) most of the time. |
| 380 | */ |
| 381 | #define __list_for_each(pos, head) \ |
| 382 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 383 | |
| 384 | /** |
| 385 | * list_for_each_prev - iterate over a list backwards |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 386 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | * @head: the head for your list. |
| 388 | */ |
| 389 | #define list_for_each_prev(pos, head) \ |
| 390 | for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ |
| 391 | pos = pos->prev) |
| 392 | |
| 393 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 394 | * list_for_each_safe - iterate over a list safe against removal of list entry |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 395 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | * @n: another &struct list_head to use as temporary storage |
| 397 | * @head: the head for your list. |
| 398 | */ |
| 399 | #define list_for_each_safe(pos, n, head) \ |
| 400 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
| 401 | pos = n, n = pos->next) |
| 402 | |
| 403 | /** |
Randy Dunlap | 8f731f7 | 2007-10-18 23:39:28 -0700 | [diff] [blame] | 404 | * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry |
Denis V. Lunev | 37c4252 | 2007-10-16 23:29:53 -0700 | [diff] [blame] | 405 | * @pos: the &struct list_head to use as a loop cursor. |
| 406 | * @n: another &struct list_head to use as temporary storage |
| 407 | * @head: the head for your list. |
| 408 | */ |
| 409 | #define list_for_each_prev_safe(pos, n, head) \ |
| 410 | for (pos = (head)->prev, n = pos->prev; \ |
| 411 | prefetch(pos->prev), pos != (head); \ |
| 412 | pos = n, n = pos->prev) |
| 413 | |
| 414 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | * list_for_each_entry - iterate over list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 416 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | * @head: the head for your list. |
| 418 | * @member: the name of the list_struct within the struct. |
| 419 | */ |
| 420 | #define list_for_each_entry(pos, head, member) \ |
| 421 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
| 422 | prefetch(pos->member.next), &pos->member != (head); \ |
| 423 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 424 | |
| 425 | /** |
| 426 | * list_for_each_entry_reverse - iterate backwards over list of given type. |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 427 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | * @head: the head for your list. |
| 429 | * @member: the name of the list_struct within the struct. |
| 430 | */ |
| 431 | #define list_for_each_entry_reverse(pos, head, member) \ |
| 432 | for (pos = list_entry((head)->prev, typeof(*pos), member); \ |
| 433 | prefetch(pos->member.prev), &pos->member != (head); \ |
| 434 | pos = list_entry(pos->member.prev, typeof(*pos), member)) |
| 435 | |
| 436 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 437 | * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | * @pos: the type * to use as a start point |
| 439 | * @head: the head of the list |
| 440 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 441 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 442 | * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | */ |
| 444 | #define list_prepare_entry(pos, head, member) \ |
| 445 | ((pos) ? : list_entry(head, typeof(*pos), member)) |
| 446 | |
| 447 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 448 | * list_for_each_entry_continue - continue iteration over list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 449 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | * @head: the head for your list. |
| 451 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 452 | * |
| 453 | * Continue to iterate over list of given type, continuing after |
| 454 | * the current position. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | */ |
| 456 | #define list_for_each_entry_continue(pos, head, member) \ |
| 457 | for (pos = list_entry(pos->member.next, typeof(*pos), member); \ |
| 458 | prefetch(pos->member.next), &pos->member != (head); \ |
| 459 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 460 | |
| 461 | /** |
Pavel Emelyanov | 768f3591 | 2007-09-18 13:20:41 -0700 | [diff] [blame] | 462 | * list_for_each_entry_continue_reverse - iterate backwards from the given point |
| 463 | * @pos: the type * to use as a loop cursor. |
| 464 | * @head: the head for your list. |
| 465 | * @member: the name of the list_struct within the struct. |
| 466 | * |
| 467 | * Start to iterate over list of given type backwards, continuing after |
| 468 | * the current position. |
| 469 | */ |
| 470 | #define list_for_each_entry_continue_reverse(pos, head, member) \ |
| 471 | for (pos = list_entry(pos->member.prev, typeof(*pos), member); \ |
| 472 | prefetch(pos->member.prev), &pos->member != (head); \ |
| 473 | pos = list_entry(pos->member.prev, typeof(*pos), member)) |
| 474 | |
| 475 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 476 | * list_for_each_entry_from - iterate over list of given type from the current point |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 477 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | e229c2f | 2006-03-20 17:19:17 -0800 | [diff] [blame] | 478 | * @head: the head for your list. |
| 479 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 480 | * |
| 481 | * Iterate over list of given type, continuing from current position. |
Arnaldo Carvalho de Melo | e229c2f | 2006-03-20 17:19:17 -0800 | [diff] [blame] | 482 | */ |
| 483 | #define list_for_each_entry_from(pos, head, member) \ |
| 484 | for (; prefetch(pos->member.next), &pos->member != (head); \ |
| 485 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 486 | |
| 487 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 489 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | * @n: another type * to use as temporary storage |
| 491 | * @head: the head for your list. |
| 492 | * @member: the name of the list_struct within the struct. |
| 493 | */ |
| 494 | #define list_for_each_entry_safe(pos, n, head, member) \ |
| 495 | for (pos = list_entry((head)->next, typeof(*pos), member), \ |
| 496 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
| 497 | &pos->member != (head); \ |
| 498 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 499 | |
| 500 | /** |
Ben Hutchings | 9a86e2b | 2010-03-05 13:43:17 -0800 | [diff] [blame] | 501 | * list_for_each_entry_safe_continue - continue list iteration safe against removal |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 502 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 503 | * @n: another type * to use as temporary storage |
| 504 | * @head: the head for your list. |
| 505 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 506 | * |
| 507 | * Iterate over list of given type, continuing after current point, |
| 508 | * safe against removal of list entry. |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 509 | */ |
| 510 | #define list_for_each_entry_safe_continue(pos, n, head, member) \ |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 511 | for (pos = list_entry(pos->member.next, typeof(*pos), member), \ |
| 512 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 513 | &pos->member != (head); \ |
| 514 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 515 | |
| 516 | /** |
Ben Hutchings | 9a86e2b | 2010-03-05 13:43:17 -0800 | [diff] [blame] | 517 | * list_for_each_entry_safe_from - iterate over list from current point safe against removal |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 518 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | d8dcffe | 2006-03-20 17:18:05 -0800 | [diff] [blame] | 519 | * @n: another type * to use as temporary storage |
| 520 | * @head: the head for your list. |
| 521 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 522 | * |
| 523 | * Iterate over list of given type from current point, safe against |
| 524 | * removal of list entry. |
Arnaldo Carvalho de Melo | d8dcffe | 2006-03-20 17:18:05 -0800 | [diff] [blame] | 525 | */ |
| 526 | #define list_for_each_entry_safe_from(pos, n, head, member) \ |
| 527 | for (n = list_entry(pos->member.next, typeof(*pos), member); \ |
| 528 | &pos->member != (head); \ |
| 529 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 530 | |
| 531 | /** |
Ben Hutchings | 9a86e2b | 2010-03-05 13:43:17 -0800 | [diff] [blame] | 532 | * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 533 | * @pos: the type * to use as a loop cursor. |
David Howells | 0ad4235 | 2006-01-09 20:51:31 -0800 | [diff] [blame] | 534 | * @n: another type * to use as temporary storage |
| 535 | * @head: the head for your list. |
| 536 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 537 | * |
| 538 | * Iterate backwards over list of given type, safe against removal |
| 539 | * of list entry. |
David Howells | 0ad4235 | 2006-01-09 20:51:31 -0800 | [diff] [blame] | 540 | */ |
| 541 | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ |
| 542 | for (pos = list_entry((head)->prev, typeof(*pos), member), \ |
| 543 | n = list_entry(pos->member.prev, typeof(*pos), member); \ |
| 544 | &pos->member != (head); \ |
| 545 | pos = n, n = list_entry(n->member.prev, typeof(*n), member)) |
| 546 | |
npiggin@suse.de | 57439f8 | 2010-06-24 13:02:14 +1000 | [diff] [blame] | 547 | /** |
| 548 | * list_safe_reset_next - reset a stale list_for_each_entry_safe loop |
| 549 | * @pos: the loop cursor used in the list_for_each_entry_safe loop |
| 550 | * @n: temporary storage used in list_for_each_entry_safe |
| 551 | * @member: the name of the list_struct within the struct. |
| 552 | * |
| 553 | * list_safe_reset_next is not safe to use in general if the list may be |
| 554 | * modified concurrently (eg. the lock is dropped in the loop body). An |
| 555 | * exception to this is if the cursor element (pos) is pinned in the list, |
| 556 | * and list_safe_reset_next is called after re-taking the lock and before |
| 557 | * completing the current iteration of the loop body. |
| 558 | */ |
| 559 | #define list_safe_reset_next(pos, n, member) \ |
| 560 | n = list_entry(pos->member.next, typeof(*pos), member) |
| 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | /* |
| 563 | * Double linked lists with a single pointer list head. |
| 564 | * Mostly useful for hash tables where the two pointer list head is |
| 565 | * too wasteful. |
| 566 | * You lose the ability to access the tail in O(1). |
| 567 | */ |
| 568 | |
| 569 | struct hlist_head { |
| 570 | struct hlist_node *first; |
| 571 | }; |
| 572 | |
| 573 | struct hlist_node { |
| 574 | struct hlist_node *next, **pprev; |
| 575 | }; |
| 576 | |
| 577 | #define HLIST_HEAD_INIT { .first = NULL } |
| 578 | #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } |
| 579 | #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) |
Zach Brown | 490d6ab | 2006-02-03 03:03:56 -0800 | [diff] [blame] | 580 | static inline void INIT_HLIST_NODE(struct hlist_node *h) |
| 581 | { |
| 582 | h->next = NULL; |
| 583 | h->pprev = NULL; |
| 584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | static inline int hlist_unhashed(const struct hlist_node *h) |
| 587 | { |
| 588 | return !h->pprev; |
| 589 | } |
| 590 | |
| 591 | static inline int hlist_empty(const struct hlist_head *h) |
| 592 | { |
| 593 | return !h->first; |
| 594 | } |
| 595 | |
| 596 | static inline void __hlist_del(struct hlist_node *n) |
| 597 | { |
| 598 | struct hlist_node *next = n->next; |
| 599 | struct hlist_node **pprev = n->pprev; |
| 600 | *pprev = next; |
| 601 | if (next) |
| 602 | next->pprev = pprev; |
| 603 | } |
| 604 | |
| 605 | static inline void hlist_del(struct hlist_node *n) |
| 606 | { |
| 607 | __hlist_del(n); |
| 608 | n->next = LIST_POISON1; |
| 609 | n->pprev = LIST_POISON2; |
| 610 | } |
| 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | static inline void hlist_del_init(struct hlist_node *n) |
| 613 | { |
Akinobu Mita | da753be | 2006-04-28 15:21:23 -0700 | [diff] [blame] | 614 | if (!hlist_unhashed(n)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | __hlist_del(n); |
| 616 | INIT_HLIST_NODE(n); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
| 621 | { |
| 622 | struct hlist_node *first = h->first; |
| 623 | n->next = first; |
| 624 | if (first) |
| 625 | first->pprev = &n->next; |
| 626 | h->first = n; |
| 627 | n->pprev = &h->first; |
| 628 | } |
| 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | /* next must be != NULL */ |
| 631 | static inline void hlist_add_before(struct hlist_node *n, |
| 632 | struct hlist_node *next) |
| 633 | { |
| 634 | n->pprev = next->pprev; |
| 635 | n->next = next; |
| 636 | next->pprev = &n->next; |
| 637 | *(n->pprev) = n; |
| 638 | } |
| 639 | |
| 640 | static inline void hlist_add_after(struct hlist_node *n, |
| 641 | struct hlist_node *next) |
| 642 | { |
| 643 | next->next = n->next; |
| 644 | n->next = next; |
| 645 | next->pprev = &n->next; |
| 646 | |
| 647 | if(next->next) |
| 648 | next->next->pprev = &next->next; |
| 649 | } |
| 650 | |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 651 | /* |
| 652 | * Move a list from one list head to another. Fixup the pprev |
| 653 | * reference of the first entry if it exists. |
| 654 | */ |
| 655 | static inline void hlist_move_list(struct hlist_head *old, |
| 656 | struct hlist_head *new) |
| 657 | { |
| 658 | new->first = old->first; |
| 659 | if (new->first) |
| 660 | new->first->pprev = &new->first; |
| 661 | old->first = NULL; |
| 662 | } |
| 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) |
| 665 | |
| 666 | #define hlist_for_each(pos, head) \ |
| 667 | for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ |
| 668 | pos = pos->next) |
| 669 | |
| 670 | #define hlist_for_each_safe(pos, n, head) \ |
| 671 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ |
| 672 | pos = n) |
| 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | /** |
| 675 | * hlist_for_each_entry - iterate over list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 676 | * @tpos: the type * to use as a loop cursor. |
| 677 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | * @head: the head for your list. |
| 679 | * @member: the name of the hlist_node within the struct. |
| 680 | */ |
| 681 | #define hlist_for_each_entry(tpos, pos, head, member) \ |
| 682 | for (pos = (head)->first; \ |
| 683 | pos && ({ prefetch(pos->next); 1;}) && \ |
| 684 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 685 | pos = pos->next) |
| 686 | |
| 687 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 688 | * hlist_for_each_entry_continue - iterate over a hlist continuing after current point |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 689 | * @tpos: the type * to use as a loop cursor. |
| 690 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | * @member: the name of the hlist_node within the struct. |
| 692 | */ |
| 693 | #define hlist_for_each_entry_continue(tpos, pos, member) \ |
| 694 | for (pos = (pos)->next; \ |
| 695 | pos && ({ prefetch(pos->next); 1;}) && \ |
| 696 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 697 | pos = pos->next) |
| 698 | |
| 699 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 700 | * hlist_for_each_entry_from - iterate over a hlist continuing from current point |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 701 | * @tpos: the type * to use as a loop cursor. |
| 702 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | * @member: the name of the hlist_node within the struct. |
| 704 | */ |
| 705 | #define hlist_for_each_entry_from(tpos, pos, member) \ |
| 706 | for (; pos && ({ prefetch(pos->next); 1;}) && \ |
| 707 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 708 | pos = pos->next) |
| 709 | |
| 710 | /** |
| 711 | * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 712 | * @tpos: the type * to use as a loop cursor. |
| 713 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | * @n: another &struct hlist_node to use as temporary storage |
| 715 | * @head: the head for your list. |
| 716 | * @member: the name of the hlist_node within the struct. |
| 717 | */ |
| 718 | #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ |
| 719 | for (pos = (head)->first; \ |
| 720 | pos && ({ n = pos->next; 1; }) && \ |
| 721 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 722 | pos = n) |
| 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | #endif |