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