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