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 | */ |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 64 | #ifndef CONFIG_DEBUG_LIST |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static inline void list_add(struct list_head *new, struct list_head *head) |
| 66 | { |
| 67 | __list_add(new, head, head->next); |
| 68 | } |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 69 | #else |
| 70 | extern void list_add(struct list_head *new, struct list_head *head); |
| 71 | #endif |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * list_add_tail - add a new entry |
| 76 | * @new: new entry to be added |
| 77 | * @head: list head to add it before |
| 78 | * |
| 79 | * Insert a new entry before the specified head. |
| 80 | * This is useful for implementing queues. |
| 81 | */ |
| 82 | static inline void list_add_tail(struct list_head *new, struct list_head *head) |
| 83 | { |
| 84 | __list_add(new, head->prev, head); |
| 85 | } |
| 86 | |
| 87 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * Delete a list entry by making the prev/next entries |
| 89 | * point to each other. |
| 90 | * |
| 91 | * This is only for internal list manipulation where we know |
| 92 | * the prev/next entries already! |
| 93 | */ |
| 94 | static inline void __list_del(struct list_head * prev, struct list_head * next) |
| 95 | { |
| 96 | next->prev = prev; |
| 97 | prev->next = next; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * list_del - deletes entry from list. |
| 102 | * @entry: the element to delete from the list. |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 103 | * 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] | 104 | * in an undefined state. |
| 105 | */ |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 106 | #ifndef CONFIG_DEBUG_LIST |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | static inline void list_del(struct list_head *entry) |
| 108 | { |
| 109 | __list_del(entry->prev, entry->next); |
| 110 | entry->next = LIST_POISON1; |
| 111 | entry->prev = LIST_POISON2; |
| 112 | } |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 113 | #else |
| 114 | extern void list_del(struct list_head *entry); |
| 115 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
Oleg Nesterov | 54e7377 | 2006-06-23 02:05:54 -0700 | [diff] [blame] | 118 | * list_replace - replace old entry by new one |
| 119 | * @old : the element to be replaced |
| 120 | * @new : the new element to insert |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 121 | * |
| 122 | * If @old was empty, it will be overwritten. |
Oleg Nesterov | 54e7377 | 2006-06-23 02:05:54 -0700 | [diff] [blame] | 123 | */ |
| 124 | static inline void list_replace(struct list_head *old, |
| 125 | struct list_head *new) |
| 126 | { |
| 127 | new->next = old->next; |
| 128 | new->next->prev = new; |
| 129 | new->prev = old->prev; |
| 130 | new->prev->next = new; |
| 131 | } |
| 132 | |
| 133 | static inline void list_replace_init(struct list_head *old, |
| 134 | struct list_head *new) |
| 135 | { |
| 136 | list_replace(old, new); |
| 137 | INIT_LIST_HEAD(old); |
| 138 | } |
| 139 | |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 140 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | * list_del_init - deletes entry from list and reinitialize it. |
| 142 | * @entry: the element to delete from the list. |
| 143 | */ |
| 144 | static inline void list_del_init(struct list_head *entry) |
| 145 | { |
| 146 | __list_del(entry->prev, entry->next); |
| 147 | INIT_LIST_HEAD(entry); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * list_move - delete from one list and add as another's head |
| 152 | * @list: the entry to move |
| 153 | * @head: the head that will precede our entry |
| 154 | */ |
| 155 | static inline void list_move(struct list_head *list, struct list_head *head) |
| 156 | { |
Daniel Walker | 78db2ad | 2007-05-12 16:28:35 -0700 | [diff] [blame] | 157 | __list_del(list->prev, list->next); |
| 158 | list_add(list, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * list_move_tail - delete from one list and add as another's tail |
| 163 | * @list: the entry to move |
| 164 | * @head: the head that will follow our entry |
| 165 | */ |
| 166 | static inline void list_move_tail(struct list_head *list, |
| 167 | struct list_head *head) |
| 168 | { |
Daniel Walker | 78db2ad | 2007-05-12 16:28:35 -0700 | [diff] [blame] | 169 | __list_del(list->prev, list->next); |
| 170 | list_add_tail(list, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
Shailabh Nagar | e8f4d97 | 2006-07-14 00:24:35 -0700 | [diff] [blame] | 174 | * list_is_last - tests whether @list is the last entry in list @head |
| 175 | * @list: the entry to test |
| 176 | * @head: the head of the list |
| 177 | */ |
| 178 | static inline int list_is_last(const struct list_head *list, |
| 179 | const struct list_head *head) |
| 180 | { |
| 181 | return list->next == head; |
| 182 | } |
| 183 | |
| 184 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | * list_empty - tests whether a list is empty |
| 186 | * @head: the list to test. |
| 187 | */ |
| 188 | static inline int list_empty(const struct list_head *head) |
| 189 | { |
| 190 | return head->next == head; |
| 191 | } |
| 192 | |
| 193 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 194 | * list_empty_careful - tests whether a list is empty and not being modified |
| 195 | * @head: the list to test |
| 196 | * |
| 197 | * Description: |
| 198 | * tests whether a list is empty _and_ checks that no other CPU might be |
| 199 | * in the process of modifying either member (next or prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | * |
| 201 | * NOTE: using list_empty_careful() without synchronization |
| 202 | * can only be safe if the only activity that can happen |
| 203 | * to the list entry is list_del_init(). Eg. it cannot be used |
| 204 | * if another CPU could re-list_add() it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | */ |
| 206 | static inline int list_empty_careful(const struct list_head *head) |
| 207 | { |
| 208 | struct list_head *next = head->next; |
| 209 | return (next == head) && (next == head->prev); |
| 210 | } |
| 211 | |
Masami Hiramatsu | 9960257 | 2008-04-28 02:14:27 -0700 | [diff] [blame] | 212 | /** |
| 213 | * list_is_singular - tests whether a list has just one entry. |
| 214 | * @head: the list to test. |
| 215 | */ |
| 216 | static inline int list_is_singular(const struct list_head *head) |
| 217 | { |
| 218 | return !list_empty(head) && (head->next == head->prev); |
| 219 | } |
| 220 | |
Robert P. J. Day | 95d8c36 | 2008-04-29 00:59:29 -0700 | [diff] [blame] | 221 | static inline void __list_splice(const struct list_head *list, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | struct list_head *head) |
| 223 | { |
| 224 | struct list_head *first = list->next; |
| 225 | struct list_head *last = list->prev; |
| 226 | struct list_head *at = head->next; |
| 227 | |
| 228 | first->prev = head; |
| 229 | head->next = first; |
| 230 | |
| 231 | last->next = at; |
| 232 | at->prev = last; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * list_splice - join two lists |
| 237 | * @list: the new list to add. |
| 238 | * @head: the place to add it in the first list. |
| 239 | */ |
Robert P. J. Day | 95d8c36 | 2008-04-29 00:59:29 -0700 | [diff] [blame] | 240 | static inline void list_splice(const struct list_head *list, |
| 241 | struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
| 243 | if (!list_empty(list)) |
| 244 | __list_splice(list, head); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * list_splice_init - join two lists and reinitialise the emptied list. |
| 249 | * @list: the new list to add. |
| 250 | * @head: the place to add it in the first list. |
| 251 | * |
| 252 | * The list at @list is reinitialised |
| 253 | */ |
| 254 | static inline void list_splice_init(struct list_head *list, |
| 255 | struct list_head *head) |
| 256 | { |
| 257 | if (!list_empty(list)) { |
| 258 | __list_splice(list, head); |
| 259 | INIT_LIST_HEAD(list); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * list_entry - get the struct for this entry |
| 265 | * @ptr: the &struct list_head pointer. |
| 266 | * @type: the type of the struct this is embedded in. |
| 267 | * @member: the name of the list_struct within the struct. |
| 268 | */ |
| 269 | #define list_entry(ptr, type, member) \ |
| 270 | container_of(ptr, type, member) |
| 271 | |
| 272 | /** |
Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 273 | * list_first_entry - get the first element from a list |
| 274 | * @ptr: the list head to take the element from. |
| 275 | * @type: the type of the struct this is embedded in. |
| 276 | * @member: the name of the list_struct within the struct. |
| 277 | * |
| 278 | * Note, that list is expected to be not empty. |
| 279 | */ |
| 280 | #define list_first_entry(ptr, type, member) \ |
| 281 | list_entry((ptr)->next, type, member) |
| 282 | |
| 283 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | * list_for_each - iterate over a list |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 285 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | * @head: the head for your list. |
| 287 | */ |
| 288 | #define list_for_each(pos, head) \ |
| 289 | for (pos = (head)->next; prefetch(pos->next), pos != (head); \ |
| 290 | pos = pos->next) |
| 291 | |
| 292 | /** |
| 293 | * __list_for_each - iterate over a list |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 294 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | * @head: the head for your list. |
| 296 | * |
| 297 | * This variant differs from list_for_each() in that it's the |
| 298 | * simplest possible list iteration code, no prefetching is done. |
| 299 | * Use this for code that knows the list to be very short (empty |
| 300 | * or 1 entry) most of the time. |
| 301 | */ |
| 302 | #define __list_for_each(pos, head) \ |
| 303 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 304 | |
| 305 | /** |
| 306 | * list_for_each_prev - iterate over a list backwards |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 307 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | * @head: the head for your list. |
| 309 | */ |
| 310 | #define list_for_each_prev(pos, head) \ |
| 311 | for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ |
| 312 | pos = pos->prev) |
| 313 | |
| 314 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 315 | * 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] | 316 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | * @n: another &struct list_head to use as temporary storage |
| 318 | * @head: the head for your list. |
| 319 | */ |
| 320 | #define list_for_each_safe(pos, n, head) \ |
| 321 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
| 322 | pos = n, n = pos->next) |
| 323 | |
| 324 | /** |
Randy Dunlap | 8f731f7 | 2007-10-18 23:39:28 -0700 | [diff] [blame] | 325 | * 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] | 326 | * @pos: the &struct list_head to use as a loop cursor. |
| 327 | * @n: another &struct list_head to use as temporary storage |
| 328 | * @head: the head for your list. |
| 329 | */ |
| 330 | #define list_for_each_prev_safe(pos, n, head) \ |
| 331 | for (pos = (head)->prev, n = pos->prev; \ |
| 332 | prefetch(pos->prev), pos != (head); \ |
| 333 | pos = n, n = pos->prev) |
| 334 | |
| 335 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | * list_for_each_entry - iterate over list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 337 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | * @head: the head for your list. |
| 339 | * @member: the name of the list_struct within the struct. |
| 340 | */ |
| 341 | #define list_for_each_entry(pos, head, member) \ |
| 342 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
| 343 | prefetch(pos->member.next), &pos->member != (head); \ |
| 344 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 345 | |
| 346 | /** |
| 347 | * list_for_each_entry_reverse - iterate backwards over list of given type. |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 348 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | * @head: the head for your list. |
| 350 | * @member: the name of the list_struct within the struct. |
| 351 | */ |
| 352 | #define list_for_each_entry_reverse(pos, head, member) \ |
| 353 | for (pos = list_entry((head)->prev, typeof(*pos), member); \ |
| 354 | prefetch(pos->member.prev), &pos->member != (head); \ |
| 355 | pos = list_entry(pos->member.prev, typeof(*pos), member)) |
| 356 | |
| 357 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 358 | * 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] | 359 | * @pos: the type * to use as a start point |
| 360 | * @head: the head of the list |
| 361 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 362 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 363 | * 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] | 364 | */ |
| 365 | #define list_prepare_entry(pos, head, member) \ |
| 366 | ((pos) ? : list_entry(head, typeof(*pos), member)) |
| 367 | |
| 368 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 369 | * list_for_each_entry_continue - continue iteration over list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 370 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | * @head: the head for your list. |
| 372 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 373 | * |
| 374 | * Continue to iterate over list of given type, continuing after |
| 375 | * the current position. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | */ |
| 377 | #define list_for_each_entry_continue(pos, head, member) \ |
| 378 | for (pos = list_entry(pos->member.next, typeof(*pos), member); \ |
| 379 | prefetch(pos->member.next), &pos->member != (head); \ |
| 380 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 381 | |
| 382 | /** |
Pavel Emelyanov | 768f3591 | 2007-09-18 13:20:41 -0700 | [diff] [blame] | 383 | * list_for_each_entry_continue_reverse - iterate backwards from the given point |
| 384 | * @pos: the type * to use as a loop cursor. |
| 385 | * @head: the head for your list. |
| 386 | * @member: the name of the list_struct within the struct. |
| 387 | * |
| 388 | * Start to iterate over list of given type backwards, continuing after |
| 389 | * the current position. |
| 390 | */ |
| 391 | #define list_for_each_entry_continue_reverse(pos, head, member) \ |
| 392 | for (pos = list_entry(pos->member.prev, typeof(*pos), member); \ |
| 393 | prefetch(pos->member.prev), &pos->member != (head); \ |
| 394 | pos = list_entry(pos->member.prev, typeof(*pos), member)) |
| 395 | |
| 396 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 397 | * 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] | 398 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | e229c2f | 2006-03-20 17:19:17 -0800 | [diff] [blame] | 399 | * @head: the head for your list. |
| 400 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 401 | * |
| 402 | * Iterate over list of given type, continuing from current position. |
Arnaldo Carvalho de Melo | e229c2f | 2006-03-20 17:19:17 -0800 | [diff] [blame] | 403 | */ |
| 404 | #define list_for_each_entry_from(pos, head, member) \ |
| 405 | for (; prefetch(pos->member.next), &pos->member != (head); \ |
| 406 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 407 | |
| 408 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | * 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] | 410 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * @n: another type * to use as temporary storage |
| 412 | * @head: the head for your list. |
| 413 | * @member: the name of the list_struct within the struct. |
| 414 | */ |
| 415 | #define list_for_each_entry_safe(pos, n, head, member) \ |
| 416 | for (pos = list_entry((head)->next, typeof(*pos), member), \ |
| 417 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
| 418 | &pos->member != (head); \ |
| 419 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 420 | |
| 421 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 422 | * list_for_each_entry_safe_continue |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 423 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 424 | * @n: another type * to use as temporary storage |
| 425 | * @head: the head for your list. |
| 426 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 427 | * |
| 428 | * Iterate over list of given type, continuing after current point, |
| 429 | * safe against removal of list entry. |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 430 | */ |
| 431 | #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] | 432 | for (pos = list_entry(pos->member.next, typeof(*pos), member), \ |
| 433 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 434 | &pos->member != (head); \ |
| 435 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 436 | |
| 437 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 438 | * list_for_each_entry_safe_from |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 439 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | d8dcffe | 2006-03-20 17:18:05 -0800 | [diff] [blame] | 440 | * @n: another type * to use as temporary storage |
| 441 | * @head: the head for your list. |
| 442 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 443 | * |
| 444 | * Iterate over list of given type from current point, safe against |
| 445 | * removal of list entry. |
Arnaldo Carvalho de Melo | d8dcffe | 2006-03-20 17:18:05 -0800 | [diff] [blame] | 446 | */ |
| 447 | #define list_for_each_entry_safe_from(pos, n, head, member) \ |
| 448 | for (n = list_entry(pos->member.next, typeof(*pos), member); \ |
| 449 | &pos->member != (head); \ |
| 450 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 451 | |
| 452 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 453 | * list_for_each_entry_safe_reverse |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 454 | * @pos: the type * to use as a loop cursor. |
David Howells | 0ad4235 | 2006-01-09 20:51:31 -0800 | [diff] [blame] | 455 | * @n: another type * to use as temporary storage |
| 456 | * @head: the head for your list. |
| 457 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 458 | * |
| 459 | * Iterate backwards over list of given type, safe against removal |
| 460 | * of list entry. |
David Howells | 0ad4235 | 2006-01-09 20:51:31 -0800 | [diff] [blame] | 461 | */ |
| 462 | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ |
| 463 | for (pos = list_entry((head)->prev, typeof(*pos), member), \ |
| 464 | n = list_entry(pos->member.prev, typeof(*pos), member); \ |
| 465 | &pos->member != (head); \ |
| 466 | pos = n, n = list_entry(n->member.prev, typeof(*n), member)) |
| 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | /* |
| 469 | * Double linked lists with a single pointer list head. |
| 470 | * Mostly useful for hash tables where the two pointer list head is |
| 471 | * too wasteful. |
| 472 | * You lose the ability to access the tail in O(1). |
| 473 | */ |
| 474 | |
| 475 | struct hlist_head { |
| 476 | struct hlist_node *first; |
| 477 | }; |
| 478 | |
| 479 | struct hlist_node { |
| 480 | struct hlist_node *next, **pprev; |
| 481 | }; |
| 482 | |
| 483 | #define HLIST_HEAD_INIT { .first = NULL } |
| 484 | #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } |
| 485 | #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) |
Zach Brown | 490d6ab | 2006-02-03 03:03:56 -0800 | [diff] [blame] | 486 | static inline void INIT_HLIST_NODE(struct hlist_node *h) |
| 487 | { |
| 488 | h->next = NULL; |
| 489 | h->pprev = NULL; |
| 490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | static inline int hlist_unhashed(const struct hlist_node *h) |
| 493 | { |
| 494 | return !h->pprev; |
| 495 | } |
| 496 | |
| 497 | static inline int hlist_empty(const struct hlist_head *h) |
| 498 | { |
| 499 | return !h->first; |
| 500 | } |
| 501 | |
| 502 | static inline void __hlist_del(struct hlist_node *n) |
| 503 | { |
| 504 | struct hlist_node *next = n->next; |
| 505 | struct hlist_node **pprev = n->pprev; |
| 506 | *pprev = next; |
| 507 | if (next) |
| 508 | next->pprev = pprev; |
| 509 | } |
| 510 | |
| 511 | static inline void hlist_del(struct hlist_node *n) |
| 512 | { |
| 513 | __hlist_del(n); |
| 514 | n->next = LIST_POISON1; |
| 515 | n->pprev = LIST_POISON2; |
| 516 | } |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | static inline void hlist_del_init(struct hlist_node *n) |
| 519 | { |
Akinobu Mita | da753be | 2006-04-28 15:21:23 -0700 | [diff] [blame] | 520 | if (!hlist_unhashed(n)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | __hlist_del(n); |
| 522 | INIT_HLIST_NODE(n); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
| 527 | { |
| 528 | struct hlist_node *first = h->first; |
| 529 | n->next = first; |
| 530 | if (first) |
| 531 | first->pprev = &n->next; |
| 532 | h->first = n; |
| 533 | n->pprev = &h->first; |
| 534 | } |
| 535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | /* next must be != NULL */ |
| 537 | static inline void hlist_add_before(struct hlist_node *n, |
| 538 | struct hlist_node *next) |
| 539 | { |
| 540 | n->pprev = next->pprev; |
| 541 | n->next = next; |
| 542 | next->pprev = &n->next; |
| 543 | *(n->pprev) = n; |
| 544 | } |
| 545 | |
| 546 | static inline void hlist_add_after(struct hlist_node *n, |
| 547 | struct hlist_node *next) |
| 548 | { |
| 549 | next->next = n->next; |
| 550 | n->next = next; |
| 551 | next->pprev = &n->next; |
| 552 | |
| 553 | if(next->next) |
| 554 | next->next->pprev = &next->next; |
| 555 | } |
| 556 | |
| 557 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) |
| 558 | |
| 559 | #define hlist_for_each(pos, head) \ |
| 560 | for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ |
| 561 | pos = pos->next) |
| 562 | |
| 563 | #define hlist_for_each_safe(pos, n, head) \ |
| 564 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ |
| 565 | pos = n) |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | /** |
| 568 | * hlist_for_each_entry - iterate over list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 569 | * @tpos: the type * to use as a loop cursor. |
| 570 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | * @head: the head for your list. |
| 572 | * @member: the name of the hlist_node within the struct. |
| 573 | */ |
| 574 | #define hlist_for_each_entry(tpos, pos, head, member) \ |
| 575 | for (pos = (head)->first; \ |
| 576 | pos && ({ prefetch(pos->next); 1;}) && \ |
| 577 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 578 | pos = pos->next) |
| 579 | |
| 580 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 581 | * 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] | 582 | * @tpos: the type * to use as a loop cursor. |
| 583 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | * @member: the name of the hlist_node within the struct. |
| 585 | */ |
| 586 | #define hlist_for_each_entry_continue(tpos, pos, member) \ |
| 587 | for (pos = (pos)->next; \ |
| 588 | pos && ({ prefetch(pos->next); 1;}) && \ |
| 589 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 590 | pos = pos->next) |
| 591 | |
| 592 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 593 | * 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] | 594 | * @tpos: the type * to use as a loop cursor. |
| 595 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | * @member: the name of the hlist_node within the struct. |
| 597 | */ |
| 598 | #define hlist_for_each_entry_from(tpos, pos, member) \ |
| 599 | for (; pos && ({ prefetch(pos->next); 1;}) && \ |
| 600 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 601 | pos = pos->next) |
| 602 | |
| 603 | /** |
| 604 | * 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] | 605 | * @tpos: the type * to use as a loop cursor. |
| 606 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | * @n: another &struct hlist_node to use as temporary storage |
| 608 | * @head: the head for your list. |
| 609 | * @member: the name of the hlist_node within the struct. |
| 610 | */ |
| 611 | #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ |
| 612 | for (pos = (head)->first; \ |
| 613 | pos && ({ n = pos->next; 1; }) && \ |
| 614 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 615 | pos = n) |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | #endif |