list_nulls.h 3.1 KB

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