hashtable.h 4.4 KB

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