list_nulls.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  28. * ptr_is_a_nulls - Test if a ptr is a nulls
  29. * @ptr: ptr to be tested
  30. *
  31. */
  32. static inline int is_a_nulls(const struct hlist_nulls_node *ptr)
  33. {
  34. return ((unsigned long)ptr & 1);
  35. }
  36. /**
  37. * get_nulls_value - Get the 'nulls' value of the end of chain
  38. * @ptr: end of chain
  39. *
  40. * Should be called only if is_a_nulls(ptr);
  41. */
  42. static inline unsigned long get_nulls_value(const struct hlist_nulls_node *ptr)
  43. {
  44. return ((unsigned long)ptr) >> 1;
  45. }
  46. static inline int hlist_nulls_unhashed(const struct hlist_nulls_node *h)
  47. {
  48. return !h->pprev;
  49. }
  50. static inline int hlist_nulls_empty(const struct hlist_nulls_head *h)
  51. {
  52. return is_a_nulls(h->first);
  53. }
  54. static inline void hlist_nulls_add_head(struct hlist_nulls_node *n,
  55. struct hlist_nulls_head *h)
  56. {
  57. struct hlist_nulls_node *first = h->first;
  58. n->next = first;
  59. n->pprev = &h->first;
  60. h->first = n;
  61. if (!is_a_nulls(first))
  62. first->pprev = &n->next;
  63. }
  64. static inline void __hlist_nulls_del(struct hlist_nulls_node *n)
  65. {
  66. struct hlist_nulls_node *next = n->next;
  67. struct hlist_nulls_node **pprev = n->pprev;
  68. WRITE_ONCE(*pprev, next);
  69. if (!is_a_nulls(next))
  70. next->pprev = pprev;
  71. }
  72. static inline void hlist_nulls_del(struct hlist_nulls_node *n)
  73. {
  74. __hlist_nulls_del(n);
  75. n->pprev = LIST_POISON2;
  76. }
  77. /**
  78. * hlist_nulls_for_each_entry - iterate over list of given type
  79. * @tpos: the type * to use as a loop cursor.
  80. * @pos: the &struct hlist_node to use as a loop cursor.
  81. * @head: the head for your list.
  82. * @member: the name of the hlist_node within the struct.
  83. *
  84. */
  85. #define hlist_nulls_for_each_entry(tpos, pos, head, member) \
  86. for (pos = (head)->first; \
  87. (!is_a_nulls(pos)) && \
  88. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
  89. pos = pos->next)
  90. /**
  91. * hlist_nulls_for_each_entry_from - iterate over a hlist continuing from current point
  92. * @tpos: the type * to use as a loop cursor.
  93. * @pos: the &struct hlist_node to use as a loop cursor.
  94. * @member: the name of the hlist_node within the struct.
  95. *
  96. */
  97. #define hlist_nulls_for_each_entry_from(tpos, pos, member) \
  98. for (; (!is_a_nulls(pos)) && \
  99. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
  100. pos = pos->next)
  101. #endif