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