list_nulls.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef _LINUX_LIST_NULLS_H
  2. #define _LINUX_LIST_NULLS_H
  3. #include <linux/poison.h>
  4. #include <linux/const.h>
  5. /*
  6. * Special version of lists, where end of list is not a NULL pointer,
  7. * but a 'nulls' marker, which can have many different values.
  8. * (up to 2^31 different values guaranteed on all platforms)
  9. *
  10. * In the standard hlist, termination of a list is the NULL pointer.
  11. * In this special 'nulls' variant, we use the fact that objects stored in
  12. * a list are aligned on a word (4 or 8 bytes alignment).
  13. * We therefore use the last significant bit of 'ptr' :
  14. * Set to 1 : This is a 'nulls' end-of-list marker (ptr >> 1)
  15. * Set to 0 : This is a pointer to some object (ptr)
  16. */
  17. struct hlist_nulls_head {
  18. struct hlist_nulls_node *first;
  19. };
  20. struct hlist_nulls_node {
  21. struct hlist_nulls_node *next, **pprev;
  22. };
  23. #define NULLS_MARKER(value) (1UL | (((long)value) << 1))
  24. #define INIT_HLIST_NULLS_HEAD(ptr, nulls) \
  25. ((ptr)->first = (struct hlist_nulls_node *) NULLS_MARKER(nulls))
  26. #define hlist_nulls_entry(ptr, type, member) container_of(ptr,type,member)
  27. #define hlist_nulls_entry_safe(ptr, type, member) \
  28. ({ typeof(ptr) ____ptr = (ptr); \
  29. !is_a_nulls(____ptr) ? hlist_nulls_entry(____ptr, type, member) : NULL; \
  30. })
  31. /**
  32. * ptr_is_a_nulls - Test if a ptr is a nulls
  33. * @ptr: ptr to be tested
  34. *
  35. */
  36. static inline int is_a_nulls(const struct hlist_nulls_node *ptr)
  37. {
  38. return ((unsigned long)ptr & 1);
  39. }
  40. /**
  41. * get_nulls_value - Get the 'nulls' value of the end of chain
  42. * @ptr: end of chain
  43. *
  44. * Should be called only if is_a_nulls(ptr);
  45. */
  46. static inline unsigned long get_nulls_value(const struct hlist_nulls_node *ptr)
  47. {
  48. return ((unsigned long)ptr) >> 1;
  49. }
  50. static inline int hlist_nulls_unhashed(const struct hlist_nulls_node *h)
  51. {
  52. return !h->pprev;
  53. }
  54. static inline int hlist_nulls_empty(const struct hlist_nulls_head *h)
  55. {
  56. return is_a_nulls(READ_ONCE(h->first));
  57. }
  58. static inline void hlist_nulls_add_head(struct hlist_nulls_node *n,
  59. struct hlist_nulls_head *h)
  60. {
  61. struct hlist_nulls_node *first = h->first;
  62. n->next = first;
  63. n->pprev = &h->first;
  64. h->first = n;
  65. if (!is_a_nulls(first))
  66. first->pprev = &n->next;
  67. }
  68. static inline void __hlist_nulls_del(struct hlist_nulls_node *n)
  69. {
  70. struct hlist_nulls_node *next = n->next;
  71. struct hlist_nulls_node **pprev = n->pprev;
  72. WRITE_ONCE(*pprev, next);
  73. if (!is_a_nulls(next))
  74. next->pprev = pprev;
  75. }
  76. static inline void hlist_nulls_del(struct hlist_nulls_node *n)
  77. {
  78. __hlist_nulls_del(n);
  79. n->pprev = LIST_POISON2;
  80. }
  81. /**
  82. * hlist_nulls_for_each_entry - iterate over list of given type
  83. * @tpos: the type * to use as a loop cursor.
  84. * @pos: the &struct hlist_node to use as a loop cursor.
  85. * @head: the head for your list.
  86. * @member: the name of the hlist_node within the struct.
  87. *
  88. */
  89. #define hlist_nulls_for_each_entry(tpos, pos, head, member) \
  90. for (pos = (head)->first; \
  91. (!is_a_nulls(pos)) && \
  92. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
  93. pos = pos->next)
  94. /**
  95. * hlist_nulls_for_each_entry_from - iterate over a hlist continuing from current point
  96. * @tpos: the type * to use as a loop cursor.
  97. * @pos: the &struct hlist_node to use as a loop cursor.
  98. * @member: the name of the hlist_node within the struct.
  99. *
  100. */
  101. #define hlist_nulls_for_each_entry_from(tpos, pos, member) \
  102. for (; (!is_a_nulls(pos)) && \
  103. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
  104. pos = pos->next)
  105. #endif