hashtable.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Statically sized hash table implementation
  4. * (C) 2012 Sasha Levin <levinsasha928@gmail.com>
  5. */
  6. #ifndef _LINUX_HASHTABLE_H
  7. #define _LINUX_HASHTABLE_H
  8. #include <linux/list.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/hash.h>
  12. #include <linux/rculist.h>
  13. #define DEFINE_HASHTABLE(name, bits) \
  14. struct hlist_head name[1 << (bits)] = \
  15. { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
  16. #define DEFINE_READ_MOSTLY_HASHTABLE(name, bits) \
  17. struct hlist_head name[1 << (bits)] __read_mostly = \
  18. { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
  19. #define DECLARE_HASHTABLE(name, bits) \
  20. struct hlist_head name[1 << (bits)]
  21. #define HASH_SIZE(name) (ARRAY_SIZE(name))
  22. #define HASH_BITS(name) ilog2(HASH_SIZE(name))
  23. /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
  24. #define hash_min(val, bits) \
  25. (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
  26. static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
  27. {
  28. unsigned int i;
  29. for (i = 0; i < sz; i++)
  30. INIT_HLIST_HEAD(&ht[i]);
  31. }
  32. /**
  33. * hash_init - initialize a hash table
  34. * @hashtable: hashtable to be initialized
  35. *
  36. * Calculates the size of the hashtable from the given parameter, otherwise
  37. * same as hash_init_size.
  38. *
  39. * This has to be a macro since HASH_BITS() will not work on pointers since
  40. * it calculates the size during preprocessing.
  41. */
  42. #define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
  43. /**
  44. * hash_add - add an object to a hashtable
  45. * @hashtable: hashtable to add to
  46. * @node: the &struct hlist_node of the object to be added
  47. * @key: the key of the object to be added
  48. */
  49. #define hash_add(hashtable, node, key) \
  50. hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
  51. /**
  52. * hash_add_rcu - add an object to a rcu enabled hashtable
  53. * @hashtable: hashtable to add to
  54. * @node: the &struct hlist_node of the object to be added
  55. * @key: the key of the object to be added
  56. */
  57. #define hash_add_rcu(hashtable, node, key) \
  58. hlist_add_head_rcu(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
  59. /**
  60. * hash_hashed - check whether an object is in any hashtable
  61. * @node: the &struct hlist_node of the object to be checked
  62. */
  63. static inline bool hash_hashed(struct hlist_node *node)
  64. {
  65. return !hlist_unhashed(node);
  66. }
  67. static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
  68. {
  69. unsigned int i;
  70. for (i = 0; i < sz; i++)
  71. if (!hlist_empty(&ht[i]))
  72. return false;
  73. return true;
  74. }
  75. /**
  76. * hash_empty - check whether a hashtable is empty
  77. * @hashtable: hashtable to check
  78. *
  79. * This has to be a macro since HASH_BITS() will not work on pointers since
  80. * it calculates the size during preprocessing.
  81. */
  82. #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
  83. /**
  84. * hash_del - remove an object from a hashtable
  85. * @node: &struct hlist_node of the object to remove
  86. */
  87. static inline void hash_del(struct hlist_node *node)
  88. {
  89. hlist_del_init(node);
  90. }
  91. /**
  92. * hash_del_rcu - remove an object from a rcu enabled hashtable
  93. * @node: &struct hlist_node of the object to remove
  94. */
  95. static inline void hash_del_rcu(struct hlist_node *node)
  96. {
  97. hlist_del_init_rcu(node);
  98. }
  99. /**
  100. * hash_for_each - iterate over a hashtable
  101. * @name: hashtable to iterate
  102. * @bkt: integer to use as bucket loop cursor
  103. * @obj: the type * to use as a loop cursor for each entry
  104. * @member: the name of the hlist_node within the struct
  105. */
  106. #define hash_for_each(name, bkt, obj, member) \
  107. for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
  108. (bkt)++)\
  109. hlist_for_each_entry(obj, &name[bkt], member)
  110. /**
  111. * hash_for_each_rcu - iterate over a rcu enabled hashtable
  112. * @name: hashtable to iterate
  113. * @bkt: integer to use as bucket loop cursor
  114. * @obj: the type * to use as a loop cursor for each entry
  115. * @member: the name of the hlist_node within the struct
  116. */
  117. #define hash_for_each_rcu(name, bkt, obj, member) \
  118. for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
  119. (bkt)++)\
  120. hlist_for_each_entry_rcu(obj, &name[bkt], member)
  121. /**
  122. * hash_for_each_safe - iterate over a hashtable safe against removal of
  123. * hash entry
  124. * @name: hashtable to iterate
  125. * @bkt: integer to use as bucket loop cursor
  126. * @tmp: a &struct used for temporary storage
  127. * @obj: the type * to use as a loop cursor for each entry
  128. * @member: the name of the hlist_node within the struct
  129. */
  130. #define hash_for_each_safe(name, bkt, tmp, obj, member) \
  131. for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
  132. (bkt)++)\
  133. hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
  134. /**
  135. * hash_for_each_possible - iterate over all possible objects hashing to the
  136. * same bucket
  137. * @name: hashtable to iterate
  138. * @obj: the type * to use as a loop cursor for each entry
  139. * @member: the name of the hlist_node within the struct
  140. * @key: the key of the objects to iterate over
  141. */
  142. #define hash_for_each_possible(name, obj, member, key) \
  143. hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
  144. /**
  145. * hash_for_each_possible_rcu - iterate over all possible objects hashing to the
  146. * same bucket in an rcu enabled hashtable
  147. * @name: hashtable to iterate
  148. * @obj: the type * to use as a loop cursor for each entry
  149. * @member: the name of the hlist_node within the struct
  150. * @key: the key of the objects to iterate over
  151. */
  152. #define hash_for_each_possible_rcu(name, obj, member, key) \
  153. hlist_for_each_entry_rcu(obj, &name[hash_min(key, HASH_BITS(name))],\
  154. member)
  155. /**
  156. * hash_for_each_possible_rcu_notrace - iterate over all possible objects hashing
  157. * to the same bucket in an rcu enabled hashtable in a rcu enabled hashtable
  158. * @name: hashtable to iterate
  159. * @obj: the type * to use as a loop cursor for each entry
  160. * @member: the name of the hlist_node within the struct
  161. * @key: the key of the objects to iterate over
  162. *
  163. * This is the same as hash_for_each_possible_rcu() except that it does
  164. * not do any RCU debugging or tracing.
  165. */
  166. #define hash_for_each_possible_rcu_notrace(name, obj, member, key) \
  167. hlist_for_each_entry_rcu_notrace(obj, \
  168. &name[hash_min(key, HASH_BITS(name))], member)
  169. /**
  170. * hash_for_each_possible_safe - iterate over all possible objects hashing to the
  171. * same bucket safe against removals
  172. * @name: hashtable to iterate
  173. * @obj: the type * to use as a loop cursor for each entry
  174. * @tmp: a &struct used for temporary storage
  175. * @member: the name of the hlist_node within the struct
  176. * @key: the key of the objects to iterate over
  177. */
  178. #define hash_for_each_possible_safe(name, obj, tmp, member, key) \
  179. hlist_for_each_entry_safe(obj, tmp,\
  180. &name[hash_min(key, HASH_BITS(name))], member)
  181. #endif