list_bl.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef _LINUX_LIST_BL_H
  2. #define _LINUX_LIST_BL_H
  3. #include <linux/list.h>
  4. /*
  5. * Special version of lists, where head of the list has a lock in the lowest
  6. * bit. This is useful for scalable hash tables without increasing memory
  7. * footprint overhead.
  8. *
  9. * For modification operations, the 0 bit of hlist_bl_head->first
  10. * pointer must be set.
  11. *
  12. * With some small modifications, this can easily be adapted to store several
  13. * arbitrary bits (not just a single lock bit), if the need arises to store
  14. * some fast and compact auxiliary data.
  15. */
  16. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  17. #define LIST_BL_LOCKMASK 1UL
  18. #else
  19. #define LIST_BL_LOCKMASK 0UL
  20. #endif
  21. #ifdef CONFIG_DEBUG_LIST
  22. #define LIST_BL_BUG_ON(x) BUG_ON(x)
  23. #else
  24. #define LIST_BL_BUG_ON(x)
  25. #endif
  26. struct hlist_bl_head {
  27. struct hlist_bl_node *first;
  28. };
  29. struct hlist_bl_node {
  30. struct hlist_bl_node *next, **pprev;
  31. };
  32. #define INIT_HLIST_BL_HEAD(ptr) \
  33. ((ptr)->first = NULL)
  34. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  35. {
  36. h->next = NULL;
  37. h->pprev = NULL;
  38. }
  39. #define hlist_bl_entry(ptr, type, member) container_of(ptr,type,member)
  40. static inline int hlist_bl_unhashed(const struct hlist_bl_node *h)
  41. {
  42. return !h->pprev;
  43. }
  44. static inline struct hlist_bl_node *hlist_bl_first(struct hlist_bl_head *h)
  45. {
  46. return (struct hlist_bl_node *)
  47. ((unsigned long)h->first & ~LIST_BL_LOCKMASK);
  48. }
  49. static inline void hlist_bl_set_first(struct hlist_bl_head *h,
  50. struct hlist_bl_node *n)
  51. {
  52. LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
  53. LIST_BL_BUG_ON(((unsigned long)h->first & LIST_BL_LOCKMASK) !=
  54. LIST_BL_LOCKMASK);
  55. h->first = (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK);
  56. }
  57. static inline int hlist_bl_empty(const struct hlist_bl_head *h)
  58. {
  59. return !((unsigned long)h->first & ~LIST_BL_LOCKMASK);
  60. }
  61. static inline void hlist_bl_add_head(struct hlist_bl_node *n,
  62. struct hlist_bl_head *h)
  63. {
  64. struct hlist_bl_node *first = hlist_bl_first(h);
  65. n->next = first;
  66. if (first)
  67. first->pprev = &n->next;
  68. n->pprev = &h->first;
  69. hlist_bl_set_first(h, n);
  70. }
  71. static inline void __hlist_bl_del(struct hlist_bl_node *n)
  72. {
  73. struct hlist_bl_node *next = n->next;
  74. struct hlist_bl_node **pprev = n->pprev;
  75. LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
  76. /* pprev may be `first`, so be careful not to lose the lock bit */
  77. *pprev = (struct hlist_bl_node *)
  78. ((unsigned long)next |
  79. ((unsigned long)*pprev & LIST_BL_LOCKMASK));
  80. if (next)
  81. next->pprev = pprev;
  82. }
  83. static inline void hlist_bl_del(struct hlist_bl_node *n)
  84. {
  85. __hlist_bl_del(n);
  86. n->next = LIST_POISON1;
  87. n->pprev = LIST_POISON2;
  88. }
  89. static inline void hlist_bl_del_init(struct hlist_bl_node *n)
  90. {
  91. if (!hlist_bl_unhashed(n)) {
  92. __hlist_bl_del(n);
  93. INIT_HLIST_BL_NODE(n);
  94. }
  95. }
  96. /**
  97. * hlist_bl_for_each_entry - iterate over list of given type
  98. * @tpos: the type * to use as a loop cursor.
  99. * @pos: the &struct hlist_node to use as a loop cursor.
  100. * @head: the head for your list.
  101. * @member: the name of the hlist_node within the struct.
  102. *
  103. */
  104. #define hlist_bl_for_each_entry(tpos, pos, head, member) \
  105. for (pos = hlist_bl_first(head); \
  106. pos && \
  107. ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1;}); \
  108. pos = pos->next)
  109. /**
  110. * hlist_bl_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  111. * @tpos: the type * to use as a loop cursor.
  112. * @pos: the &struct hlist_node to use as a loop cursor.
  113. * @n: another &struct hlist_node to use as temporary storage
  114. * @head: the head for your list.
  115. * @member: the name of the hlist_node within the struct.
  116. */
  117. #define hlist_bl_for_each_entry_safe(tpos, pos, n, head, member) \
  118. for (pos = hlist_bl_first(head); \
  119. pos && ({ n = pos->next; 1; }) && \
  120. ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1;}); \
  121. pos = n)
  122. #endif