list_nulls.h 3.5 KB

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