list_nulls.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_del(struct hlist_nulls_node *n)
  52. {
  53. struct hlist_nulls_node *next = n->next;
  54. struct hlist_nulls_node **pprev = n->pprev;
  55. *pprev = next;
  56. if (!is_a_nulls(next))
  57. next->pprev = pprev;
  58. }
  59. /**
  60. * hlist_nulls_for_each_entry - iterate over list of given type
  61. * @tpos: the type * to use as a loop cursor.
  62. * @pos: the &struct hlist_node to use as a loop cursor.
  63. * @head: the head for your list.
  64. * @member: the name of the hlist_node within the struct.
  65. *
  66. */
  67. #define hlist_nulls_for_each_entry(tpos, pos, head, member) \
  68. for (pos = (head)->first; \
  69. (!is_a_nulls(pos)) && \
  70. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
  71. pos = pos->next)
  72. /**
  73. * hlist_nulls_for_each_entry_from - iterate over a hlist continuing from current point
  74. * @tpos: the type * to use as a loop cursor.
  75. * @pos: the &struct hlist_node to use as a loop cursor.
  76. * @member: the name of the hlist_node within the struct.
  77. *
  78. */
  79. #define hlist_nulls_for_each_entry_from(tpos, pos, member) \
  80. for (; (!is_a_nulls(pos)) && \
  81. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
  82. pos = pos->next)
  83. #endif