hashtable.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Statically sized hash table implementation
  4. * (C) 2012 Sasha Levin <[email protected]>
  5. */
  6. #ifndef _GENERIC_HASHTABLE_H
  7. #define _GENERIC_HASHTABLE_H
  8. #include "list.h"
  9. #include "hash.h"
  10. #include "bitmap.h"
  11. #ifndef __same_type
  12. # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
  13. #endif
  14. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  15. #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
  16. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
  17. #define DEFINE_HASHTABLE(name, bits) \
  18. struct hlist_head name[1 << (bits)] = \
  19. { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
  20. #define DECLARE_HASHTABLE(name, bits) \
  21. struct hlist_head name[1 << (bits)]
  22. #define HASH_SIZE(name) (ARRAY_SIZE(name))
  23. #define HASH_BITS(name) ilog2(HASH_SIZE(name))
  24. /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
  25. #define hash_min(val, bits) \
  26. (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
  27. static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
  28. {
  29. unsigned int i;
  30. for (i = 0; i < sz; i++)
  31. INIT_HLIST_HEAD(&ht[i]);
  32. }
  33. /**
  34. * hash_init - initialize a hash table
  35. * @hashtable: hashtable to be initialized
  36. *
  37. * Calculates the size of the hashtable from the given parameter, otherwise
  38. * same as hash_init_size.
  39. *
  40. * This has to be a macro since HASH_BITS() will not work on pointers since
  41. * it calculates the size during preprocessing.
  42. */
  43. #define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
  44. /**
  45. * hash_add - add an object to a hashtable
  46. * @hashtable: hashtable to add to
  47. * @node: the &struct hlist_node of the object to be added
  48. * @key: the key of the object to be added
  49. */
  50. #define hash_add(hashtable, node, key) \
  51. hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
  52. /**
  53. * hash_hashed - check whether an object is in any hashtable
  54. * @node: the &struct hlist_node of the object to be checked
  55. */
  56. static inline bool hash_hashed(struct hlist_node *node)
  57. {
  58. return !hlist_unhashed(node);
  59. }
  60. static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
  61. {
  62. unsigned int i;
  63. for (i = 0; i < sz; i++)
  64. if (!hlist_empty(&ht[i]))
  65. return false;
  66. return true;
  67. }
  68. /**
  69. * hash_empty - check whether a hashtable is empty
  70. * @hashtable: hashtable to check
  71. *
  72. * This has to be a macro since HASH_BITS() will not work on pointers since
  73. * it calculates the size during preprocessing.
  74. */
  75. #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
  76. /**
  77. * hash_del - remove an object from a hashtable
  78. * @node: &struct hlist_node of the object to remove
  79. */
  80. static inline void hash_del(struct hlist_node *node)
  81. {
  82. hlist_del_init(node);
  83. }
  84. /**
  85. * hash_for_each - iterate over a hashtable
  86. * @name: hashtable to iterate
  87. * @bkt: integer to use as bucket loop cursor
  88. * @obj: the type * to use as a loop cursor for each entry
  89. * @member: the name of the hlist_node within the struct
  90. */
  91. #define hash_for_each(name, bkt, obj, member) \
  92. for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
  93. (bkt)++)\
  94. hlist_for_each_entry(obj, &name[bkt], member)
  95. /**
  96. * hash_for_each_safe - iterate over a hashtable safe against removal of
  97. * hash entry
  98. * @name: hashtable to iterate
  99. * @bkt: integer to use as bucket loop cursor
  100. * @tmp: a &struct used for temporary storage
  101. * @obj: the type * to use as a loop cursor for each entry
  102. * @member: the name of the hlist_node within the struct
  103. */
  104. #define hash_for_each_safe(name, bkt, tmp, obj, member) \
  105. for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
  106. (bkt)++)\
  107. hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
  108. /**
  109. * hash_for_each_possible - iterate over all possible objects hashing to the
  110. * same bucket
  111. * @name: hashtable to iterate
  112. * @obj: the type * to use as a loop cursor for each entry
  113. * @member: the name of the hlist_node within the struct
  114. * @key: the key of the objects to iterate over
  115. */
  116. #define hash_for_each_possible(name, obj, member, key) \
  117. hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
  118. /**
  119. * hash_for_each_possible_safe - iterate over all possible objects hashing to the
  120. * same bucket safe against removals
  121. * @name: hashtable to iterate
  122. * @obj: the type * to use as a loop cursor for each entry
  123. * @tmp: a &struct used for temporary storage
  124. * @member: the name of the hlist_node within the struct
  125. * @key: the key of the objects to iterate over
  126. */
  127. #define hash_for_each_possible_safe(name, obj, tmp, member, key) \
  128. hlist_for_each_entry_safe(obj, tmp,\
  129. &name[hash_min(key, HASH_BITS(name))], member)
  130. #endif