hashtable.h 6.6 KB

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