Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_LIST_H |
| 2 | #define _LINUX_LIST_H |
| 3 | |
| 4 | #ifdef __KERNEL__ |
| 5 | |
| 6 | #include <linux/stddef.h> |
| 7 | #include <linux/prefetch.h> |
| 8 | #include <asm/system.h> |
| 9 | |
| 10 | /* |
| 11 | * These are non-NULL pointers that will result in page faults |
| 12 | * under normal circumstances, used to verify that nobody uses |
| 13 | * non-initialized list entries. |
| 14 | */ |
| 15 | #define LIST_POISON1 ((void *) 0x00100100) |
| 16 | #define LIST_POISON2 ((void *) 0x00200200) |
| 17 | |
| 18 | /* |
| 19 | * Simple doubly linked list implementation. |
| 20 | * |
| 21 | * Some of the internal functions ("__xxx") are useful when |
| 22 | * manipulating whole lists rather than single entries, as |
| 23 | * sometimes we already know the next/prev entries and we can |
| 24 | * generate better code by using them directly rather than |
| 25 | * using the generic single-entry routines. |
| 26 | */ |
| 27 | |
| 28 | struct list_head { |
| 29 | struct list_head *next, *prev; |
| 30 | }; |
| 31 | |
| 32 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
| 33 | |
| 34 | #define LIST_HEAD(name) \ |
| 35 | struct list_head name = LIST_HEAD_INIT(name) |
| 36 | |
| 37 | #define INIT_LIST_HEAD(ptr) do { \ |
| 38 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ |
| 39 | } while (0) |
| 40 | |
| 41 | /* |
| 42 | * Insert a new entry between two known consecutive entries. |
| 43 | * |
| 44 | * This is only for internal list manipulation where we know |
| 45 | * the prev/next entries already! |
| 46 | */ |
| 47 | static inline void __list_add(struct list_head *new, |
| 48 | struct list_head *prev, |
| 49 | struct list_head *next) |
| 50 | { |
| 51 | next->prev = new; |
| 52 | new->next = next; |
| 53 | new->prev = prev; |
| 54 | prev->next = new; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * list_add - add a new entry |
| 59 | * @new: new entry to be added |
| 60 | * @head: list head to add it after |
| 61 | * |
| 62 | * Insert a new entry after the specified head. |
| 63 | * This is good for implementing stacks. |
| 64 | */ |
| 65 | static inline void list_add(struct list_head *new, struct list_head *head) |
| 66 | { |
| 67 | __list_add(new, head, head->next); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * list_add_tail - add a new entry |
| 72 | * @new: new entry to be added |
| 73 | * @head: list head to add it before |
| 74 | * |
| 75 | * Insert a new entry before the specified head. |
| 76 | * This is useful for implementing queues. |
| 77 | */ |
| 78 | static inline void list_add_tail(struct list_head *new, struct list_head *head) |
| 79 | { |
| 80 | __list_add(new, head->prev, head); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Insert a new entry between two known consecutive entries. |
| 85 | * |
| 86 | * This is only for internal list manipulation where we know |
| 87 | * the prev/next entries already! |
| 88 | */ |
| 89 | static inline void __list_add_rcu(struct list_head * new, |
| 90 | struct list_head * prev, struct list_head * next) |
| 91 | { |
| 92 | new->next = next; |
| 93 | new->prev = prev; |
| 94 | smp_wmb(); |
| 95 | next->prev = new; |
| 96 | prev->next = new; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * list_add_rcu - add a new entry to rcu-protected list |
| 101 | * @new: new entry to be added |
| 102 | * @head: list head to add it after |
| 103 | * |
| 104 | * Insert a new entry after the specified head. |
| 105 | * This is good for implementing stacks. |
| 106 | * |
| 107 | * The caller must take whatever precautions are necessary |
| 108 | * (such as holding appropriate locks) to avoid racing |
| 109 | * with another list-mutation primitive, such as list_add_rcu() |
| 110 | * or list_del_rcu(), running on this same list. |
| 111 | * However, it is perfectly legal to run concurrently with |
| 112 | * the _rcu list-traversal primitives, such as |
| 113 | * list_for_each_entry_rcu(). |
| 114 | */ |
| 115 | static inline void list_add_rcu(struct list_head *new, struct list_head *head) |
| 116 | { |
| 117 | __list_add_rcu(new, head, head->next); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * list_add_tail_rcu - add a new entry to rcu-protected list |
| 122 | * @new: new entry to be added |
| 123 | * @head: list head to add it before |
| 124 | * |
| 125 | * Insert a new entry before the specified head. |
| 126 | * This is useful for implementing queues. |
| 127 | * |
| 128 | * The caller must take whatever precautions are necessary |
| 129 | * (such as holding appropriate locks) to avoid racing |
| 130 | * with another list-mutation primitive, such as list_add_tail_rcu() |
| 131 | * or list_del_rcu(), running on this same list. |
| 132 | * However, it is perfectly legal to run concurrently with |
| 133 | * the _rcu list-traversal primitives, such as |
| 134 | * list_for_each_entry_rcu(). |
| 135 | */ |
| 136 | static inline void list_add_tail_rcu(struct list_head *new, |
| 137 | struct list_head *head) |
| 138 | { |
| 139 | __list_add_rcu(new, head->prev, head); |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Delete a list entry by making the prev/next entries |
| 144 | * point to each other. |
| 145 | * |
| 146 | * This is only for internal list manipulation where we know |
| 147 | * the prev/next entries already! |
| 148 | */ |
| 149 | static inline void __list_del(struct list_head * prev, struct list_head * next) |
| 150 | { |
| 151 | next->prev = prev; |
| 152 | prev->next = next; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * list_del - deletes entry from list. |
| 157 | * @entry: the element to delete from the list. |
| 158 | * Note: list_empty on entry does not return true after this, the entry is |
| 159 | * in an undefined state. |
| 160 | */ |
| 161 | static inline void list_del(struct list_head *entry) |
| 162 | { |
| 163 | __list_del(entry->prev, entry->next); |
| 164 | entry->next = LIST_POISON1; |
| 165 | entry->prev = LIST_POISON2; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * list_del_rcu - deletes entry from list without re-initialization |
| 170 | * @entry: the element to delete from the list. |
| 171 | * |
| 172 | * Note: list_empty on entry does not return true after this, |
| 173 | * the entry is in an undefined state. It is useful for RCU based |
| 174 | * lockfree traversal. |
| 175 | * |
| 176 | * In particular, it means that we can not poison the forward |
| 177 | * pointers that may still be used for walking the list. |
| 178 | * |
| 179 | * The caller must take whatever precautions are necessary |
| 180 | * (such as holding appropriate locks) to avoid racing |
| 181 | * with another list-mutation primitive, such as list_del_rcu() |
| 182 | * or list_add_rcu(), running on this same list. |
| 183 | * However, it is perfectly legal to run concurrently with |
| 184 | * the _rcu list-traversal primitives, such as |
| 185 | * list_for_each_entry_rcu(). |
| 186 | * |
| 187 | * Note that the caller is not permitted to immediately free |
Paul E. McKenney | b2b1866 | 2005-06-25 14:55:38 -0700 | [diff] [blame] | 188 | * the newly deleted entry. Instead, either synchronize_rcu() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | * or call_rcu() must be used to defer freeing until an RCU |
| 190 | * grace period has elapsed. |
| 191 | */ |
| 192 | static inline void list_del_rcu(struct list_head *entry) |
| 193 | { |
| 194 | __list_del(entry->prev, entry->next); |
| 195 | entry->prev = LIST_POISON2; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * list_replace_rcu - replace old entry by new one |
| 200 | * @old : the element to be replaced |
| 201 | * @new : the new element to insert |
| 202 | * |
| 203 | * The old entry will be replaced with the new entry atomically. |
| 204 | */ |
Ingo Molnar | b88cb42 | 2005-12-12 00:37:11 -0800 | [diff] [blame^] | 205 | static inline void list_replace_rcu(struct list_head *old, |
| 206 | struct list_head *new) |
| 207 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | new->next = old->next; |
| 209 | new->prev = old->prev; |
| 210 | smp_wmb(); |
| 211 | new->next->prev = new; |
| 212 | new->prev->next = new; |
Ingo Molnar | b88cb42 | 2005-12-12 00:37:11 -0800 | [diff] [blame^] | 213 | old->prev = LIST_POISON2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
| 217 | * list_del_init - deletes entry from list and reinitialize it. |
| 218 | * @entry: the element to delete from the list. |
| 219 | */ |
| 220 | static inline void list_del_init(struct list_head *entry) |
| 221 | { |
| 222 | __list_del(entry->prev, entry->next); |
| 223 | INIT_LIST_HEAD(entry); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * list_move - delete from one list and add as another's head |
| 228 | * @list: the entry to move |
| 229 | * @head: the head that will precede our entry |
| 230 | */ |
| 231 | static inline void list_move(struct list_head *list, struct list_head *head) |
| 232 | { |
| 233 | __list_del(list->prev, list->next); |
| 234 | list_add(list, head); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * list_move_tail - delete from one list and add as another's tail |
| 239 | * @list: the entry to move |
| 240 | * @head: the head that will follow our entry |
| 241 | */ |
| 242 | static inline void list_move_tail(struct list_head *list, |
| 243 | struct list_head *head) |
| 244 | { |
| 245 | __list_del(list->prev, list->next); |
| 246 | list_add_tail(list, head); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * list_empty - tests whether a list is empty |
| 251 | * @head: the list to test. |
| 252 | */ |
| 253 | static inline int list_empty(const struct list_head *head) |
| 254 | { |
| 255 | return head->next == head; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * list_empty_careful - tests whether a list is |
| 260 | * empty _and_ checks that no other CPU might be |
| 261 | * in the process of still modifying either member |
| 262 | * |
| 263 | * NOTE: using list_empty_careful() without synchronization |
| 264 | * can only be safe if the only activity that can happen |
| 265 | * to the list entry is list_del_init(). Eg. it cannot be used |
| 266 | * if another CPU could re-list_add() it. |
| 267 | * |
| 268 | * @head: the list to test. |
| 269 | */ |
| 270 | static inline int list_empty_careful(const struct list_head *head) |
| 271 | { |
| 272 | struct list_head *next = head->next; |
| 273 | return (next == head) && (next == head->prev); |
| 274 | } |
| 275 | |
| 276 | static inline void __list_splice(struct list_head *list, |
| 277 | struct list_head *head) |
| 278 | { |
| 279 | struct list_head *first = list->next; |
| 280 | struct list_head *last = list->prev; |
| 281 | struct list_head *at = head->next; |
| 282 | |
| 283 | first->prev = head; |
| 284 | head->next = first; |
| 285 | |
| 286 | last->next = at; |
| 287 | at->prev = last; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * list_splice - join two lists |
| 292 | * @list: the new list to add. |
| 293 | * @head: the place to add it in the first list. |
| 294 | */ |
| 295 | static inline void list_splice(struct list_head *list, struct list_head *head) |
| 296 | { |
| 297 | if (!list_empty(list)) |
| 298 | __list_splice(list, head); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * list_splice_init - join two lists and reinitialise the emptied list. |
| 303 | * @list: the new list to add. |
| 304 | * @head: the place to add it in the first list. |
| 305 | * |
| 306 | * The list at @list is reinitialised |
| 307 | */ |
| 308 | static inline void list_splice_init(struct list_head *list, |
| 309 | struct list_head *head) |
| 310 | { |
| 311 | if (!list_empty(list)) { |
| 312 | __list_splice(list, head); |
| 313 | INIT_LIST_HEAD(list); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * list_entry - get the struct for this entry |
| 319 | * @ptr: the &struct list_head pointer. |
| 320 | * @type: the type of the struct this is embedded in. |
| 321 | * @member: the name of the list_struct within the struct. |
| 322 | */ |
| 323 | #define list_entry(ptr, type, member) \ |
| 324 | container_of(ptr, type, member) |
| 325 | |
| 326 | /** |
| 327 | * list_for_each - iterate over a list |
| 328 | * @pos: the &struct list_head to use as a loop counter. |
| 329 | * @head: the head for your list. |
| 330 | */ |
| 331 | #define list_for_each(pos, head) \ |
| 332 | for (pos = (head)->next; prefetch(pos->next), pos != (head); \ |
| 333 | pos = pos->next) |
| 334 | |
| 335 | /** |
| 336 | * __list_for_each - iterate over a list |
| 337 | * @pos: the &struct list_head to use as a loop counter. |
| 338 | * @head: the head for your list. |
| 339 | * |
| 340 | * This variant differs from list_for_each() in that it's the |
| 341 | * simplest possible list iteration code, no prefetching is done. |
| 342 | * Use this for code that knows the list to be very short (empty |
| 343 | * or 1 entry) most of the time. |
| 344 | */ |
| 345 | #define __list_for_each(pos, head) \ |
| 346 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 347 | |
| 348 | /** |
| 349 | * list_for_each_prev - iterate over a list backwards |
| 350 | * @pos: the &struct list_head to use as a loop counter. |
| 351 | * @head: the head for your list. |
| 352 | */ |
| 353 | #define list_for_each_prev(pos, head) \ |
| 354 | for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ |
| 355 | pos = pos->prev) |
| 356 | |
| 357 | /** |
| 358 | * list_for_each_safe - iterate over a list safe against removal of list entry |
| 359 | * @pos: the &struct list_head to use as a loop counter. |
| 360 | * @n: another &struct list_head to use as temporary storage |
| 361 | * @head: the head for your list. |
| 362 | */ |
| 363 | #define list_for_each_safe(pos, n, head) \ |
| 364 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
| 365 | pos = n, n = pos->next) |
| 366 | |
| 367 | /** |
| 368 | * list_for_each_entry - iterate over list of given type |
| 369 | * @pos: the type * to use as a loop counter. |
| 370 | * @head: the head for your list. |
| 371 | * @member: the name of the list_struct within the struct. |
| 372 | */ |
| 373 | #define list_for_each_entry(pos, head, member) \ |
| 374 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
| 375 | prefetch(pos->member.next), &pos->member != (head); \ |
| 376 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 377 | |
| 378 | /** |
| 379 | * list_for_each_entry_reverse - iterate backwards over list of given type. |
| 380 | * @pos: the type * to use as a loop counter. |
| 381 | * @head: the head for your list. |
| 382 | * @member: the name of the list_struct within the struct. |
| 383 | */ |
| 384 | #define list_for_each_entry_reverse(pos, head, member) \ |
| 385 | for (pos = list_entry((head)->prev, typeof(*pos), member); \ |
| 386 | prefetch(pos->member.prev), &pos->member != (head); \ |
| 387 | pos = list_entry(pos->member.prev, typeof(*pos), member)) |
| 388 | |
| 389 | /** |
| 390 | * list_prepare_entry - prepare a pos entry for use as a start point in |
| 391 | * list_for_each_entry_continue |
| 392 | * @pos: the type * to use as a start point |
| 393 | * @head: the head of the list |
| 394 | * @member: the name of the list_struct within the struct. |
| 395 | */ |
| 396 | #define list_prepare_entry(pos, head, member) \ |
| 397 | ((pos) ? : list_entry(head, typeof(*pos), member)) |
| 398 | |
| 399 | /** |
| 400 | * list_for_each_entry_continue - iterate over list of given type |
| 401 | * continuing after existing point |
| 402 | * @pos: the type * to use as a loop counter. |
| 403 | * @head: the head for your list. |
| 404 | * @member: the name of the list_struct within the struct. |
| 405 | */ |
| 406 | #define list_for_each_entry_continue(pos, head, member) \ |
| 407 | for (pos = list_entry(pos->member.next, typeof(*pos), member); \ |
| 408 | prefetch(pos->member.next), &pos->member != (head); \ |
| 409 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 410 | |
| 411 | /** |
| 412 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
| 413 | * @pos: the type * to use as a loop counter. |
| 414 | * @n: another type * to use as temporary storage |
| 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_safe(pos, n, head, member) \ |
| 419 | for (pos = list_entry((head)->next, typeof(*pos), member), \ |
| 420 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
| 421 | &pos->member != (head); \ |
| 422 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 423 | |
| 424 | /** |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 425 | * list_for_each_entry_safe_continue - iterate over list of given type |
| 426 | * continuing after existing point safe against removal of list entry |
| 427 | * @pos: the type * to use as a loop counter. |
| 428 | * @n: another type * to use as temporary storage |
| 429 | * @head: the head for your list. |
| 430 | * @member: the name of the list_struct within the struct. |
| 431 | */ |
| 432 | #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] | 433 | for (pos = list_entry(pos->member.next, typeof(*pos), member), \ |
| 434 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 435 | &pos->member != (head); \ |
| 436 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 437 | |
| 438 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | * list_for_each_rcu - iterate over an rcu-protected list |
| 440 | * @pos: the &struct list_head to use as a loop counter. |
| 441 | * @head: the head for your list. |
| 442 | * |
| 443 | * This list-traversal primitive may safely run concurrently with |
| 444 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 445 | * as long as the traversal is guarded by rcu_read_lock(). |
| 446 | */ |
| 447 | #define list_for_each_rcu(pos, head) \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 448 | for (pos = (head)->next; \ |
| 449 | prefetch(rcu_dereference(pos)->next), pos != (head); \ |
| 450 | pos = pos->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | #define __list_for_each_rcu(pos, head) \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 453 | for (pos = (head)->next; \ |
| 454 | rcu_dereference(pos) != (head); \ |
| 455 | pos = pos->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * list_for_each_safe_rcu - iterate over an rcu-protected list safe |
| 459 | * against removal of list entry |
| 460 | * @pos: the &struct list_head to use as a loop counter. |
| 461 | * @n: another &struct list_head to use as temporary storage |
| 462 | * @head: the head for your list. |
| 463 | * |
| 464 | * This list-traversal primitive may safely run concurrently with |
| 465 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 466 | * as long as the traversal is guarded by rcu_read_lock(). |
| 467 | */ |
| 468 | #define list_for_each_safe_rcu(pos, n, head) \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 469 | for (pos = (head)->next; \ |
| 470 | n = rcu_dereference(pos)->next, pos != (head); \ |
| 471 | pos = n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
| 473 | /** |
| 474 | * list_for_each_entry_rcu - iterate over rcu list of given type |
| 475 | * @pos: the type * to use as a loop counter. |
| 476 | * @head: the head for your list. |
| 477 | * @member: the name of the list_struct within the struct. |
| 478 | * |
| 479 | * This list-traversal primitive may safely run concurrently with |
| 480 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 481 | * as long as the traversal is guarded by rcu_read_lock(). |
| 482 | */ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 483 | #define list_for_each_entry_rcu(pos, head, member) \ |
| 484 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
| 485 | prefetch(rcu_dereference(pos)->member.next), \ |
| 486 | &pos->member != (head); \ |
| 487 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | |
| 490 | /** |
| 491 | * list_for_each_continue_rcu - iterate over an rcu-protected list |
| 492 | * continuing after existing point. |
| 493 | * @pos: the &struct list_head to use as a loop counter. |
| 494 | * @head: the head for your list. |
| 495 | * |
| 496 | * This list-traversal primitive may safely run concurrently with |
| 497 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 498 | * as long as the traversal is guarded by rcu_read_lock(). |
| 499 | */ |
| 500 | #define list_for_each_continue_rcu(pos, head) \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 501 | for ((pos) = (pos)->next; \ |
| 502 | prefetch(rcu_dereference((pos))->next), (pos) != (head); \ |
| 503 | (pos) = (pos)->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | /* |
| 506 | * Double linked lists with a single pointer list head. |
| 507 | * Mostly useful for hash tables where the two pointer list head is |
| 508 | * too wasteful. |
| 509 | * You lose the ability to access the tail in O(1). |
| 510 | */ |
| 511 | |
| 512 | struct hlist_head { |
| 513 | struct hlist_node *first; |
| 514 | }; |
| 515 | |
| 516 | struct hlist_node { |
| 517 | struct hlist_node *next, **pprev; |
| 518 | }; |
| 519 | |
| 520 | #define HLIST_HEAD_INIT { .first = NULL } |
| 521 | #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } |
| 522 | #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) |
| 523 | #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL) |
| 524 | |
| 525 | static inline int hlist_unhashed(const struct hlist_node *h) |
| 526 | { |
| 527 | return !h->pprev; |
| 528 | } |
| 529 | |
| 530 | static inline int hlist_empty(const struct hlist_head *h) |
| 531 | { |
| 532 | return !h->first; |
| 533 | } |
| 534 | |
| 535 | static inline void __hlist_del(struct hlist_node *n) |
| 536 | { |
| 537 | struct hlist_node *next = n->next; |
| 538 | struct hlist_node **pprev = n->pprev; |
| 539 | *pprev = next; |
| 540 | if (next) |
| 541 | next->pprev = pprev; |
| 542 | } |
| 543 | |
| 544 | static inline void hlist_del(struct hlist_node *n) |
| 545 | { |
| 546 | __hlist_del(n); |
| 547 | n->next = LIST_POISON1; |
| 548 | n->pprev = LIST_POISON2; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * hlist_del_rcu - deletes entry from hash list without re-initialization |
| 553 | * @n: the element to delete from the hash list. |
| 554 | * |
| 555 | * Note: list_unhashed() on entry does not return true after this, |
| 556 | * the entry is in an undefined state. It is useful for RCU based |
| 557 | * lockfree traversal. |
| 558 | * |
| 559 | * In particular, it means that we can not poison the forward |
| 560 | * pointers that may still be used for walking the hash list. |
| 561 | * |
| 562 | * The caller must take whatever precautions are necessary |
| 563 | * (such as holding appropriate locks) to avoid racing |
| 564 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 565 | * or hlist_del_rcu(), running on this same list. |
| 566 | * However, it is perfectly legal to run concurrently with |
| 567 | * the _rcu list-traversal primitives, such as |
| 568 | * hlist_for_each_entry(). |
| 569 | */ |
| 570 | static inline void hlist_del_rcu(struct hlist_node *n) |
| 571 | { |
| 572 | __hlist_del(n); |
| 573 | n->pprev = LIST_POISON2; |
| 574 | } |
| 575 | |
| 576 | static inline void hlist_del_init(struct hlist_node *n) |
| 577 | { |
| 578 | if (n->pprev) { |
| 579 | __hlist_del(n); |
| 580 | INIT_HLIST_NODE(n); |
| 581 | } |
| 582 | } |
| 583 | |
Ingo Molnar | b88cb42 | 2005-12-12 00:37:11 -0800 | [diff] [blame^] | 584 | /* |
| 585 | * hlist_replace_rcu - replace old entry by new one |
| 586 | * @old : the element to be replaced |
| 587 | * @new : the new element to insert |
| 588 | * |
| 589 | * The old entry will be replaced with the new entry atomically. |
| 590 | */ |
| 591 | static inline void hlist_replace_rcu(struct hlist_node *old, |
| 592 | struct hlist_node *new) |
| 593 | { |
| 594 | struct hlist_node *next = old->next; |
| 595 | |
| 596 | new->next = next; |
| 597 | new->pprev = old->pprev; |
| 598 | smp_wmb(); |
| 599 | if (next) |
| 600 | new->next->pprev = &new->next; |
| 601 | *new->pprev = new; |
| 602 | old->pprev = LIST_POISON2; |
| 603 | } |
| 604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
| 606 | { |
| 607 | struct hlist_node *first = h->first; |
| 608 | n->next = first; |
| 609 | if (first) |
| 610 | first->pprev = &n->next; |
| 611 | h->first = n; |
| 612 | n->pprev = &h->first; |
| 613 | } |
| 614 | |
| 615 | |
| 616 | /** |
| 617 | * hlist_add_head_rcu - adds the specified element to the specified hlist, |
| 618 | * while permitting racing traversals. |
| 619 | * @n: the element to add to the hash list. |
| 620 | * @h: the list to add to. |
| 621 | * |
| 622 | * The caller must take whatever precautions are necessary |
| 623 | * (such as holding appropriate locks) to avoid racing |
| 624 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 625 | * or hlist_del_rcu(), running on this same list. |
| 626 | * However, it is perfectly legal to run concurrently with |
| 627 | * the _rcu list-traversal primitives, such as |
Paul E. McKenney | 665a758 | 2005-11-07 00:59:17 -0800 | [diff] [blame] | 628 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | * problems on Alpha CPUs. Regardless of the type of CPU, the |
| 630 | * list-traversal primitive must be guarded by rcu_read_lock(). |
| 631 | */ |
| 632 | static inline void hlist_add_head_rcu(struct hlist_node *n, |
| 633 | struct hlist_head *h) |
| 634 | { |
| 635 | struct hlist_node *first = h->first; |
| 636 | n->next = first; |
| 637 | n->pprev = &h->first; |
| 638 | smp_wmb(); |
| 639 | if (first) |
| 640 | first->pprev = &n->next; |
| 641 | h->first = n; |
| 642 | } |
| 643 | |
| 644 | /* next must be != NULL */ |
| 645 | static inline void hlist_add_before(struct hlist_node *n, |
| 646 | struct hlist_node *next) |
| 647 | { |
| 648 | n->pprev = next->pprev; |
| 649 | n->next = next; |
| 650 | next->pprev = &n->next; |
| 651 | *(n->pprev) = n; |
| 652 | } |
| 653 | |
| 654 | static inline void hlist_add_after(struct hlist_node *n, |
| 655 | struct hlist_node *next) |
| 656 | { |
| 657 | next->next = n->next; |
| 658 | n->next = next; |
| 659 | next->pprev = &n->next; |
| 660 | |
| 661 | if(next->next) |
| 662 | next->next->pprev = &next->next; |
| 663 | } |
| 664 | |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 665 | /** |
| 666 | * hlist_add_before_rcu - adds the specified element to the specified hlist |
| 667 | * before the specified node while permitting racing traversals. |
| 668 | * @n: the new element to add to the hash list. |
| 669 | * @next: the existing element to add the new element before. |
| 670 | * |
| 671 | * The caller must take whatever precautions are necessary |
| 672 | * (such as holding appropriate locks) to avoid racing |
| 673 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 674 | * or hlist_del_rcu(), running on this same list. |
| 675 | * However, it is perfectly legal to run concurrently with |
| 676 | * the _rcu list-traversal primitives, such as |
Paul E. McKenney | 665a758 | 2005-11-07 00:59:17 -0800 | [diff] [blame] | 677 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 678 | * problems on Alpha CPUs. |
| 679 | */ |
Robert Olsson | e5b4376 | 2005-08-25 13:01:03 -0700 | [diff] [blame] | 680 | static inline void hlist_add_before_rcu(struct hlist_node *n, |
| 681 | struct hlist_node *next) |
| 682 | { |
| 683 | n->pprev = next->pprev; |
| 684 | n->next = next; |
| 685 | smp_wmb(); |
| 686 | next->pprev = &n->next; |
| 687 | *(n->pprev) = n; |
| 688 | } |
| 689 | |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 690 | /** |
| 691 | * hlist_add_after_rcu - adds the specified element to the specified hlist |
| 692 | * after the specified node while permitting racing traversals. |
| 693 | * @prev: the existing element to add the new element after. |
| 694 | * @n: the new element to add to the hash list. |
| 695 | * |
| 696 | * The caller must take whatever precautions are necessary |
| 697 | * (such as holding appropriate locks) to avoid racing |
| 698 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 699 | * or hlist_del_rcu(), running on this same list. |
| 700 | * However, it is perfectly legal to run concurrently with |
| 701 | * the _rcu list-traversal primitives, such as |
Paul E. McKenney | 665a758 | 2005-11-07 00:59:17 -0800 | [diff] [blame] | 702 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 703 | * problems on Alpha CPUs. |
| 704 | */ |
Robert Olsson | e5b4376 | 2005-08-25 13:01:03 -0700 | [diff] [blame] | 705 | static inline void hlist_add_after_rcu(struct hlist_node *prev, |
| 706 | struct hlist_node *n) |
| 707 | { |
| 708 | n->next = prev->next; |
| 709 | n->pprev = &prev->next; |
| 710 | smp_wmb(); |
| 711 | prev->next = n; |
| 712 | if (n->next) |
| 713 | n->next->pprev = &n->next; |
| 714 | } |
| 715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) |
| 717 | |
| 718 | #define hlist_for_each(pos, head) \ |
| 719 | for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ |
| 720 | pos = pos->next) |
| 721 | |
| 722 | #define hlist_for_each_safe(pos, n, head) \ |
| 723 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ |
| 724 | pos = n) |
| 725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | /** |
| 727 | * hlist_for_each_entry - iterate over list of given type |
| 728 | * @tpos: the type * to use as a loop counter. |
| 729 | * @pos: the &struct hlist_node to use as a loop counter. |
| 730 | * @head: the head for your list. |
| 731 | * @member: the name of the hlist_node within the struct. |
| 732 | */ |
| 733 | #define hlist_for_each_entry(tpos, pos, head, member) \ |
| 734 | for (pos = (head)->first; \ |
| 735 | pos && ({ prefetch(pos->next); 1;}) && \ |
| 736 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 737 | pos = pos->next) |
| 738 | |
| 739 | /** |
| 740 | * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point |
| 741 | * @tpos: the type * to use as a loop counter. |
| 742 | * @pos: the &struct hlist_node to use as a loop counter. |
| 743 | * @member: the name of the hlist_node within the struct. |
| 744 | */ |
| 745 | #define hlist_for_each_entry_continue(tpos, pos, member) \ |
| 746 | for (pos = (pos)->next; \ |
| 747 | pos && ({ prefetch(pos->next); 1;}) && \ |
| 748 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 749 | pos = pos->next) |
| 750 | |
| 751 | /** |
| 752 | * hlist_for_each_entry_from - iterate over a hlist continuing from existing point |
| 753 | * @tpos: the type * to use as a loop counter. |
| 754 | * @pos: the &struct hlist_node to use as a loop counter. |
| 755 | * @member: the name of the hlist_node within the struct. |
| 756 | */ |
| 757 | #define hlist_for_each_entry_from(tpos, pos, member) \ |
| 758 | for (; pos && ({ prefetch(pos->next); 1;}) && \ |
| 759 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 760 | pos = pos->next) |
| 761 | |
| 762 | /** |
| 763 | * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
| 764 | * @tpos: the type * to use as a loop counter. |
| 765 | * @pos: the &struct hlist_node to use as a loop counter. |
| 766 | * @n: another &struct hlist_node to use as temporary storage |
| 767 | * @head: the head for your list. |
| 768 | * @member: the name of the hlist_node within the struct. |
| 769 | */ |
| 770 | #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ |
| 771 | for (pos = (head)->first; \ |
| 772 | pos && ({ n = pos->next; 1; }) && \ |
| 773 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 774 | pos = n) |
| 775 | |
| 776 | /** |
| 777 | * hlist_for_each_entry_rcu - iterate over rcu list of given type |
Paul E. McKenney | 665a758 | 2005-11-07 00:59:17 -0800 | [diff] [blame] | 778 | * @tpos: the type * to use as a loop counter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | * @pos: the &struct hlist_node to use as a loop counter. |
| 780 | * @head: the head for your list. |
| 781 | * @member: the name of the hlist_node within the struct. |
| 782 | * |
| 783 | * This list-traversal primitive may safely run concurrently with |
Paul E. McKenney | e1ba0da | 2005-04-16 15:25:51 -0700 | [diff] [blame] | 784 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | * as long as the traversal is guarded by rcu_read_lock(). |
| 786 | */ |
| 787 | #define hlist_for_each_entry_rcu(tpos, pos, head, member) \ |
| 788 | for (pos = (head)->first; \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 789 | rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 791 | pos = pos->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
| 793 | #else |
| 794 | #warning "don't include kernel headers in userspace" |
| 795 | #endif /* __KERNEL__ */ |
| 796 | #endif |