hashtable.h 4.4 KB

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