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