hashtable.h 6.5 KB

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