akaros/kern/include/list.h
<<
>>
Prefs
   1/* Linux's list definitions.
   2 *
   3 * Up to date with commit 569dbb88e80d ("Linux 4.13") */
   4
   5#pragma once
   6
   7/*
   8 * Architectures might want to move the poison pointer offset
   9 * into some well-recognized area such as 0xdead000000000000,
  10 * that is also not mappable by user-space exploits:
  11 */
  12#ifdef CONFIG_ILLEGAL_POINTER_VALUE
  13# define POISON_POINTER_DELTA _AC(CONFIG_ILLEGAL_POINTER_VALUE, UL)
  14#else
  15# define POISON_POINTER_DELTA 0
  16#endif
  17
  18/*
  19 * These are non-NULL pointers that will result in page faults
  20 * under normal circumstances, used to verify that nobody uses
  21 * non-initialized list entries.
  22 */
  23#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
  24#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)
  25
  26struct list_head {
  27    struct list_head *next, *prev;
  28};
  29
  30struct hlist_head {
  31    struct hlist_node *first;
  32};
  33
  34struct hlist_node {
  35    struct hlist_node *next, **pprev;
  36};
  37
  38/*
  39 * Simple doubly linked list implementation.
  40 *
  41 * Some of the internal functions ("__xxx") are useful when
  42 * manipulating whole lists rather than single entries, as
  43 * sometimes we already know the next/prev entries and we can
  44 * generate better code by using them directly rather than
  45 * using the generic single-entry routines.
  46 */
  47
  48#define LIST_HEAD_INIT(name) { &(name), &(name) }
  49
  50#define LINUX_LIST_HEAD(name) \
  51        struct list_head name = LIST_HEAD_INIT(name)
  52
  53static inline void INIT_LIST_HEAD(struct list_head *list)
  54{
  55        WRITE_ONCE(list->next, list);
  56        list->prev = list;
  57}
  58
  59#ifdef CONFIG_DEBUG_LIST
  60extern bool __list_add_valid(struct list_head *new,
  61                              struct list_head *prev,
  62                              struct list_head *next);
  63extern bool __list_del_entry_valid(struct list_head *entry);
  64#else
  65static inline bool __list_add_valid(struct list_head *new,
  66                                struct list_head *prev,
  67                                struct list_head *next)
  68{
  69        return true;
  70}
  71static inline bool __list_del_entry_valid(struct list_head *entry)
  72{
  73        return true;
  74}
  75#endif
  76
  77/*
  78 * Insert a new entry between two known consecutive entries.
  79 *
  80 * This is only for internal list manipulation where we know
  81 * the prev/next entries already!
  82 */
  83static inline void __list_add(struct list_head *new,
  84                              struct list_head *prev,
  85                              struct list_head *next)
  86{
  87        if (!__list_add_valid(new, prev, next))
  88                return;
  89
  90        next->prev = new;
  91        new->next = next;
  92        new->prev = prev;
  93        WRITE_ONCE(prev->next, new);
  94}
  95
  96/**
  97 * list_add - add a new entry
  98 * @new: new entry to be added
  99 * @head: list head to add it after
 100 *
 101 * Insert a new entry after the specified head.
 102 * This is good for implementing stacks.
 103 */
 104static inline void list_add(struct list_head *new, struct list_head *head)
 105{
 106        __list_add(new, head, head->next);
 107}
 108
 109
 110/**
 111 * list_add_tail - add a new entry
 112 * @new: new entry to be added
 113 * @head: list head to add it before
 114 *
 115 * Insert a new entry before the specified head.
 116 * This is useful for implementing queues.
 117 */
 118static inline void list_add_tail(struct list_head *new, struct list_head *head)
 119{
 120        __list_add(new, head->prev, head);
 121}
 122
 123/*
 124 * Delete a list entry by making the prev/next entries
 125 * point to each other.
 126 *
 127 * This is only for internal list manipulation where we know
 128 * the prev/next entries already!
 129 */
 130static inline void __list_del(struct list_head * prev, struct list_head * next)
 131{
 132        next->prev = prev;
 133        WRITE_ONCE(prev->next, next);
 134}
 135
 136/**
 137 * list_del - deletes entry from list.
 138 * @entry: the element to delete from the list.
 139 * Note: list_empty() on entry does not return true after this, the entry is
 140 * in an undefined state.
 141 */
 142static inline void __list_del_entry(struct list_head *entry)
 143{
 144        if (!__list_del_entry_valid(entry))
 145                return;
 146
 147        __list_del(entry->prev, entry->next);
 148}
 149
 150static inline void list_del(struct list_head *entry)
 151{
 152        __list_del_entry(entry);
 153        entry->next = LIST_POISON1;
 154        entry->prev = LIST_POISON2;
 155}
 156
 157/**
 158 * list_replace - replace old entry by new one
 159 * @old : the element to be replaced
 160 * @new : the new element to insert
 161 *
 162 * If @old was empty, it will be overwritten.
 163 */
 164static inline void list_replace(struct list_head *old,
 165                                struct list_head *new)
 166{
 167        new->next = old->next;
 168        new->next->prev = new;
 169        new->prev = old->prev;
 170        new->prev->next = new;
 171}
 172
 173static inline void list_replace_init(struct list_head *old,
 174                                        struct list_head *new)
 175{
 176        list_replace(old, new);
 177        INIT_LIST_HEAD(old);
 178}
 179
 180/**
 181 * list_del_init - deletes entry from list and reinitialize it.
 182 * @entry: the element to delete from the list.
 183 */
 184static inline void list_del_init(struct list_head *entry)
 185{
 186        __list_del_entry(entry);
 187        INIT_LIST_HEAD(entry);
 188}
 189
 190/**
 191 * list_move - delete from one list and add as another's head
 192 * @list: the entry to move
 193 * @head: the head that will precede our entry
 194 */
 195static inline void list_move(struct list_head *list, struct list_head *head)
 196{
 197        __list_del_entry(list);
 198        list_add(list, head);
 199}
 200
 201/**
 202 * list_move_tail - delete from one list and add as another's tail
 203 * @list: the entry to move
 204 * @head: the head that will follow our entry
 205 */
 206static inline void list_move_tail(struct list_head *list,
 207                                  struct list_head *head)
 208{
 209        __list_del_entry(list);
 210        list_add_tail(list, head);
 211}
 212
 213/**
 214 * list_is_last - tests whether @list is the last entry in list @head
 215 * @list: the entry to test
 216 * @head: the head of the list
 217 */
 218static inline int list_is_last(const struct list_head *list,
 219                                const struct list_head *head)
 220{
 221        return list->next == head;
 222}
 223
 224/**
 225 * list_empty - tests whether a list is empty
 226 * @head: the list to test.
 227 */
 228static inline int list_empty(const struct list_head *head)
 229{
 230        return READ_ONCE(head->next) == head;
 231}
 232
 233/**
 234 * list_empty_careful - tests whether a list is empty and not being modified
 235 * @head: the list to test
 236 *
 237 * Description:
 238 * tests whether a list is empty _and_ checks that no other CPU might be
 239 * in the process of modifying either member (next or prev)
 240 *
 241 * NOTE: using list_empty_careful() without synchronization
 242 * can only be safe if the only activity that can happen
 243 * to the list entry is list_del_init(). Eg. it cannot be used
 244 * if another CPU could re-list_add() it.
 245 */
 246static inline int list_empty_careful(const struct list_head *head)
 247{
 248        struct list_head *next = head->next;
 249        return (next == head) && (next == head->prev);
 250}
 251
 252/**
 253 * list_rotate_left - rotate the list to the left
 254 * @head: the head of the list
 255 */
 256static inline void list_rotate_left(struct list_head *head)
 257{
 258        struct list_head *first;
 259
 260        if (!list_empty(head)) {
 261                first = head->next;
 262                list_move_tail(first, head);
 263        }
 264}
 265
 266/**
 267 * list_is_singular - tests whether a list has just one entry.
 268 * @head: the list to test.
 269 */
 270static inline int list_is_singular(const struct list_head *head)
 271{
 272        return !list_empty(head) && (head->next == head->prev);
 273}
 274
 275static inline void __list_cut_position(struct list_head *list,
 276                struct list_head *head, struct list_head *entry)
 277{
 278        struct list_head *new_first = entry->next;
 279        list->next = head->next;
 280        list->next->prev = list;
 281        list->prev = entry;
 282        entry->next = list;
 283        head->next = new_first;
 284        new_first->prev = head;
 285}
 286
 287/**
 288 * list_cut_position - cut a list into two
 289 * @list: a new list to add all removed entries
 290 * @head: a list with entries
 291 * @entry: an entry within head, could be the head itself
 292 *      and if so we won't cut the list
 293 *
 294 * This helper moves the initial part of @head, up to and
 295 * including @entry, from @head to @list. You should
 296 * pass on @entry an element you know is on @head. @list
 297 * should be an empty list or a list you do not care about
 298 * losing its data.
 299 *
 300 */
 301static inline void list_cut_position(struct list_head *list,
 302                struct list_head *head, struct list_head *entry)
 303{
 304        if (list_empty(head))
 305                return;
 306        if (list_is_singular(head) &&
 307                (head->next != entry && head != entry))
 308                return;
 309        if (entry == head)
 310                INIT_LIST_HEAD(list);
 311        else
 312                __list_cut_position(list, head, entry);
 313}
 314
 315static inline void __list_splice(const struct list_head *list,
 316                                 struct list_head *prev,
 317                                 struct list_head *next)
 318{
 319        struct list_head *first = list->next;
 320        struct list_head *last = list->prev;
 321
 322        first->prev = prev;
 323        prev->next = first;
 324
 325        last->next = next;
 326        next->prev = last;
 327}
 328
 329/**
 330 * list_splice - join two lists, this is designed for stacks
 331 * @list: the new list to add.
 332 * @head: the place to add it in the first list.
 333 */
 334static inline void list_splice(const struct list_head *list,
 335                                struct list_head *head)
 336{
 337        if (!list_empty(list))
 338                __list_splice(list, head, head->next);
 339}
 340
 341/**
 342 * list_splice_tail - join two lists, each list being a queue
 343 * @list: the new list to add.
 344 * @head: the place to add it in the first list.
 345 */
 346static inline void list_splice_tail(struct list_head *list,
 347                                struct list_head *head)
 348{
 349        if (!list_empty(list))
 350                __list_splice(list, head->prev, head);
 351}
 352
 353/**
 354 * list_splice_init - join two lists and reinitialise the emptied list.
 355 * @list: the new list to add.
 356 * @head: the place to add it in the first list.
 357 *
 358 * The list at @list is reinitialised
 359 */
 360static inline void list_splice_init(struct list_head *list,
 361                                    struct list_head *head)
 362{
 363        if (!list_empty(list)) {
 364                __list_splice(list, head, head->next);
 365                INIT_LIST_HEAD(list);
 366        }
 367}
 368
 369/**
 370 * list_splice_tail_init - join two lists and reinitialise the emptied list
 371 * @list: the new list to add.
 372 * @head: the place to add it in the first list.
 373 *
 374 * Each of the lists is a queue.
 375 * The list at @list is reinitialised
 376 */
 377static inline void list_splice_tail_init(struct list_head *list,
 378                                         struct list_head *head)
 379{
 380        if (!list_empty(list)) {
 381                __list_splice(list, head->prev, head);
 382                INIT_LIST_HEAD(list);
 383        }
 384}
 385
 386/**
 387 * list_entry - get the struct for this entry
 388 * @ptr:        the &struct list_head pointer.
 389 * @type:       the type of the struct this is embedded in.
 390 * @member:     the name of the list_head within the struct.
 391 */
 392#define list_entry(ptr, type, member) \
 393        container_of(ptr, type, member)
 394
 395/**
 396 * list_first_entry - get the first element from a list
 397 * @ptr:        the list head to take the element from.
 398 * @type:       the type of the struct this is embedded in.
 399 * @member:     the name of the list_head within the struct.
 400 *
 401 * Note, that list is expected to be not empty.
 402 */
 403#define list_first_entry(ptr, type, member) \
 404        list_entry((ptr)->next, type, member)
 405
 406/**
 407 * list_last_entry - get the last element from a list
 408 * @ptr:        the list head to take the element from.
 409 * @type:       the type of the struct this is embedded in.
 410 * @member:     the name of the list_head within the struct.
 411 *
 412 * Note, that list is expected to be not empty.
 413 */
 414#define list_last_entry(ptr, type, member) \
 415        list_entry((ptr)->prev, type, member)
 416
 417/**
 418 * list_first_entry_or_null - get the first element from a list
 419 * @ptr:        the list head to take the element from.
 420 * @type:       the type of the struct this is embedded in.
 421 * @member:     the name of the list_head within the struct.
 422 *
 423 * Note that if the list is empty, it returns NULL.
 424 */
 425#define list_first_entry_or_null(ptr, type, member) ({ \
 426        struct list_head *head__ = (ptr); \
 427        struct list_head *pos__ = READ_ONCE(head__->next); \
 428        pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
 429})
 430
 431/**
 432 * list_next_entry - get the next element in list
 433 * @pos:        the type * to cursor
 434 * @member:     the name of the list_head within the struct.
 435 */
 436#define list_next_entry(pos, member) \
 437        list_entry((pos)->member.next, typeof(*(pos)), member)
 438
 439/**
 440 * list_prev_entry - get the prev element in list
 441 * @pos:        the type * to cursor
 442 * @member:     the name of the list_head within the struct.
 443 */
 444#define list_prev_entry(pos, member) \
 445        list_entry((pos)->member.prev, typeof(*(pos)), member)
 446
 447/**
 448 * list_for_each        -       iterate over a list
 449 * @pos:        the &struct list_head to use as a loop cursor.
 450 * @head:       the head for your list.
 451 */
 452#define list_for_each(pos, head) \
 453        for (pos = (head)->next; pos != (head); pos = pos->next)
 454
 455/**
 456 * list_for_each_prev   -       iterate over a list backwards
 457 * @pos:        the &struct list_head to use as a loop cursor.
 458 * @head:       the head for your list.
 459 */
 460#define list_for_each_prev(pos, head) \
 461        for (pos = (head)->prev; pos != (head); pos = pos->prev)
 462
 463/**
 464 * list_for_each_safe - iterate over a list safe against removal of list entry
 465 * @pos:        the &struct list_head to use as a loop cursor.
 466 * @n:          another &struct list_head to use as temporary storage
 467 * @head:       the head for your list.
 468 */
 469#define list_for_each_safe(pos, n, head) \
 470        for (pos = (head)->next, n = pos->next; pos != (head); \
 471                pos = n, n = pos->next)
 472
 473/**
 474 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
 475 * @pos:        the &struct list_head to use as a loop cursor.
 476 * @n:          another &struct list_head to use as temporary storage
 477 * @head:       the head for your list.
 478 */
 479#define list_for_each_prev_safe(pos, n, head) \
 480        for (pos = (head)->prev, n = pos->prev; \
 481             pos != (head); \
 482             pos = n, n = pos->prev)
 483
 484/**
 485 * list_for_each_entry  -       iterate over list of given type
 486 * @pos:        the type * to use as a loop cursor.
 487 * @head:       the head for your list.
 488 * @member:     the name of the list_head within the struct.
 489 */
 490#define list_for_each_entry(pos, head, member)                          \
 491        for (pos = list_first_entry(head, typeof(*pos), member);        \
 492             &pos->member != (head);                                    \
 493             pos = list_next_entry(pos, member))
 494
 495/**
 496 * list_for_each_entry_reverse - iterate backwards over list of given type.
 497 * @pos:        the type * to use as a loop cursor.
 498 * @head:       the head for your list.
 499 * @member:     the name of the list_head within the struct.
 500 */
 501#define list_for_each_entry_reverse(pos, head, member)                  \
 502        for (pos = list_last_entry(head, typeof(*pos), member);         \
 503             &pos->member != (head);                                    \
 504             pos = list_prev_entry(pos, member))
 505
 506/**
 507 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
 508 * @pos:        the type * to use as a start point
 509 * @head:       the head of the list
 510 * @member:     the name of the list_head within the struct.
 511 *
 512 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
 513 */
 514#define list_prepare_entry(pos, head, member) \
 515        ((pos) ? : list_entry(head, typeof(*pos), member))
 516
 517/**
 518 * list_for_each_entry_continue - continue iteration over list of given type
 519 * @pos:        the type * to use as a loop cursor.
 520 * @head:       the head for your list.
 521 * @member:     the name of the list_head within the struct.
 522 *
 523 * Continue to iterate over list of given type, continuing after
 524 * the current position.
 525 */
 526#define list_for_each_entry_continue(pos, head, member)                 \
 527        for (pos = list_next_entry(pos, member);                        \
 528             &pos->member != (head);                                    \
 529             pos = list_next_entry(pos, member))
 530
 531/**
 532 * list_for_each_entry_continue_reverse - iterate backwards from the given point
 533 * @pos:        the type * to use as a loop cursor.
 534 * @head:       the head for your list.
 535 * @member:     the name of the list_head within the struct.
 536 *
 537 * Start to iterate over list of given type backwards, continuing after
 538 * the current position.
 539 */
 540#define list_for_each_entry_continue_reverse(pos, head, member)         \
 541        for (pos = list_prev_entry(pos, member);                        \
 542             &pos->member != (head);                                    \
 543             pos = list_prev_entry(pos, member))
 544
 545/**
 546 * list_for_each_entry_from - iterate over list of given type from the current point
 547 * @pos:        the type * to use as a loop cursor.
 548 * @head:       the head for your list.
 549 * @member:     the name of the list_head within the struct.
 550 *
 551 * Iterate over list of given type, continuing from current position.
 552 */
 553#define list_for_each_entry_from(pos, head, member)                     \
 554        for (; &pos->member != (head);                                  \
 555             pos = list_next_entry(pos, member))
 556
 557/**
 558 * list_for_each_entry_from_reverse - iterate backwards over list of given type
 559 *                                    from the current point
 560 * @pos:        the type * to use as a loop cursor.
 561 * @head:       the head for your list.
 562 * @member:     the name of the list_head within the struct.
 563 *
 564 * Iterate backwards over list of given type, continuing from current position.
 565 */
 566#define list_for_each_entry_from_reverse(pos, head, member)             \
 567        for (; &pos->member != (head);                                  \
 568             pos = list_prev_entry(pos, member))
 569
 570/**
 571 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 572 * @pos:        the type * to use as a loop cursor.
 573 * @n:          another type * to use as temporary storage
 574 * @head:       the head for your list.
 575 * @member:     the name of the list_head within the struct.
 576 */
 577#define list_for_each_entry_safe(pos, n, head, member)                  \
 578        for (pos = list_first_entry(head, typeof(*pos), member),        \
 579                n = list_next_entry(pos, member);                       \
 580             &pos->member != (head);                                    \
 581             pos = n, n = list_next_entry(n, member))
 582
 583/**
 584 * list_for_each_entry_safe_continue - continue list iteration safe against removal
 585 * @pos:        the type * to use as a loop cursor.
 586 * @n:          another type * to use as temporary storage
 587 * @head:       the head for your list.
 588 * @member:     the name of the list_head within the struct.
 589 *
 590 * Iterate over list of given type, continuing after current point,
 591 * safe against removal of list entry.
 592 */
 593#define list_for_each_entry_safe_continue(pos, n, head, member)                 \
 594        for (pos = list_next_entry(pos, member),                                \
 595                n = list_next_entry(pos, member);                               \
 596             &pos->member != (head);                                            \
 597             pos = n, n = list_next_entry(n, member))
 598
 599/**
 600 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
 601 * @pos:        the type * to use as a loop cursor.
 602 * @n:          another type * to use as temporary storage
 603 * @head:       the head for your list.
 604 * @member:     the name of the list_head within the struct.
 605 *
 606 * Iterate over list of given type from current point, safe against
 607 * removal of list entry.
 608 */
 609#define list_for_each_entry_safe_from(pos, n, head, member)                     \
 610        for (n = list_next_entry(pos, member);                                  \
 611             &pos->member != (head);                                            \
 612             pos = n, n = list_next_entry(n, member))
 613
 614/**
 615 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
 616 * @pos:        the type * to use as a loop cursor.
 617 * @n:          another type * to use as temporary storage
 618 * @head:       the head for your list.
 619 * @member:     the name of the list_head within the struct.
 620 *
 621 * Iterate backwards over list of given type, safe against removal
 622 * of list entry.
 623 */
 624#define list_for_each_entry_safe_reverse(pos, n, head, member)          \
 625        for (pos = list_last_entry(head, typeof(*pos), member),         \
 626                n = list_prev_entry(pos, member);                       \
 627             &pos->member != (head);                                    \
 628             pos = n, n = list_prev_entry(n, member))
 629
 630/**
 631 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
 632 * @pos:        the loop cursor used in the list_for_each_entry_safe loop
 633 * @n:          temporary storage used in list_for_each_entry_safe
 634 * @member:     the name of the list_head within the struct.
 635 *
 636 * list_safe_reset_next is not safe to use in general if the list may be
 637 * modified concurrently (eg. the lock is dropped in the loop body). An
 638 * exception to this is if the cursor element (pos) is pinned in the list,
 639 * and list_safe_reset_next is called after re-taking the lock and before
 640 * completing the current iteration of the loop body.
 641 */
 642#define list_safe_reset_next(pos, n, member)                            \
 643        n = list_next_entry(pos, member)
 644
 645/*
 646 * Double linked lists with a single pointer list head.
 647 * Mostly useful for hash tables where the two pointer list head is
 648 * too wasteful.
 649 * You lose the ability to access the tail in O(1).
 650 */
 651
 652#define HLIST_HEAD_INIT { .first = NULL }
 653#define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
 654#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
 655static inline void INIT_HLIST_NODE(struct hlist_node *h)
 656{
 657        h->next = NULL;
 658        h->pprev = NULL;
 659}
 660
 661static inline int hlist_unhashed(const struct hlist_node *h)
 662{
 663        return !h->pprev;
 664}
 665
 666static inline int hlist_empty(const struct hlist_head *h)
 667{
 668        return !READ_ONCE(h->first);
 669}
 670
 671static inline void __hlist_del(struct hlist_node *n)
 672{
 673        struct hlist_node *next = n->next;
 674        struct hlist_node **pprev = n->pprev;
 675
 676        WRITE_ONCE(*pprev, next);
 677        if (next)
 678                next->pprev = pprev;
 679}
 680
 681static inline void hlist_del(struct hlist_node *n)
 682{
 683        __hlist_del(n);
 684        n->next = LIST_POISON1;
 685        n->pprev = LIST_POISON2;
 686}
 687
 688static inline void hlist_del_init(struct hlist_node *n)
 689{
 690        if (!hlist_unhashed(n)) {
 691                __hlist_del(n);
 692                INIT_HLIST_NODE(n);
 693        }
 694}
 695
 696static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
 697{
 698        struct hlist_node *first = h->first;
 699        n->next = first;
 700        if (first)
 701                first->pprev = &n->next;
 702        WRITE_ONCE(h->first, n);
 703        n->pprev = &h->first;
 704}
 705
 706/* next must be != NULL */
 707static inline void hlist_add_before(struct hlist_node *n,
 708                                        struct hlist_node *next)
 709{
 710        n->pprev = next->pprev;
 711        n->next = next;
 712        next->pprev = &n->next;
 713        WRITE_ONCE(*(n->pprev), n);
 714}
 715
 716static inline void hlist_add_behind(struct hlist_node *n,
 717                                    struct hlist_node *prev)
 718{
 719        n->next = prev->next;
 720        WRITE_ONCE(prev->next, n);
 721        n->pprev = &prev->next;
 722
 723        if (n->next)
 724                n->next->pprev  = &n->next;
 725}
 726
 727/* after that we'll appear to be on some hlist and hlist_del will work */
 728static inline void hlist_add_fake(struct hlist_node *n)
 729{
 730        n->pprev = &n->next;
 731}
 732
 733static inline bool hlist_fake(struct hlist_node *h)
 734{
 735        return h->pprev == &h->next;
 736}
 737
 738/*
 739 * Check whether the node is the only node of the head without
 740 * accessing head:
 741 */
 742static inline bool
 743hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
 744{
 745        return !n->next && n->pprev == &h->first;
 746}
 747
 748/*
 749 * Move a list from one list head to another. Fixup the pprev
 750 * reference of the first entry if it exists.
 751 */
 752static inline void hlist_move_list(struct hlist_head *old,
 753                                   struct hlist_head *new)
 754{
 755        new->first = old->first;
 756        if (new->first)
 757                new->first->pprev = &new->first;
 758        old->first = NULL;
 759}
 760
 761#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
 762
 763#define hlist_for_each(pos, head) \
 764        for (pos = (head)->first; pos ; pos = pos->next)
 765
 766#define hlist_for_each_safe(pos, n, head) \
 767        for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
 768             pos = n)
 769
 770#define hlist_entry_safe(ptr, type, member) \
 771        ({ typeof(ptr) ____ptr = (ptr); \
 772           ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
 773        })
 774
 775/**
 776 * hlist_for_each_entry - iterate over list of given type
 777 * @pos:        the type * to use as a loop cursor.
 778 * @head:       the head for your list.
 779 * @member:     the name of the hlist_node within the struct.
 780 */
 781#define hlist_for_each_entry(pos, head, member)                         \
 782        for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
 783             pos;                                                       \
 784             pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
 785
 786/**
 787 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
 788 * @pos:        the type * to use as a loop cursor.
 789 * @member:     the name of the hlist_node within the struct.
 790 */
 791#define hlist_for_each_entry_continue(pos, member)                      \
 792        for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
 793             pos;                                                       \
 794             pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
 795
 796/**
 797 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
 798 * @pos:        the type * to use as a loop cursor.
 799 * @member:     the name of the hlist_node within the struct.
 800 */
 801#define hlist_for_each_entry_from(pos, member)                          \
 802        for (; pos;                                                     \
 803             pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
 804
 805/**
 806 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 807 * @pos:        the type * to use as a loop cursor.
 808 * @n:          another &struct hlist_node to use as temporary storage
 809 * @head:       the head for your list.
 810 * @member:     the name of the hlist_node within the struct.
 811 */
 812#define hlist_for_each_entry_safe(pos, n, head, member)                 \
 813        for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
 814             pos && ({ n = pos->member.next; 1; });                     \
 815             pos = hlist_entry_safe(n, typeof(*pos), member))
 816