hashtable.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef ARRAY_SIZE
  14. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  15. #endif
  16. #define DEFINE_HASHTABLE(name, bits) \
  17. struct hlist_head name[1 << (bits)] = \
  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_hashed - check whether an object is in any hashtable
  53. * @node: the &struct hlist_node of the object to be checked
  54. */
  55. static inline bool hash_hashed(struct hlist_node *node)
  56. {
  57. return !hlist_unhashed(node);
  58. }
  59. static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
  60. {
  61. unsigned int i;
  62. for (i = 0; i < sz; i++)
  63. if (!hlist_empty(&ht[i]))
  64. return false;
  65. return true;
  66. }
  67. /**
  68. * hash_empty - check whether a hashtable is empty
  69. * @hashtable: hashtable to check
  70. *
  71. * This has to be a macro since HASH_BITS() will not work on pointers since
  72. * it calculates the size during preprocessing.
  73. */
  74. #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
  75. /**
  76. * hash_del - remove an object from a hashtable
  77. * @node: &struct hlist_node of the object to remove
  78. */
  79. static inline void hash_del(struct hlist_node *node)
  80. {
  81. hlist_del_init(node);
  82. }
  83. /**
  84. * hash_for_each - iterate over a hashtable
  85. * @name: hashtable to iterate
  86. * @bkt: integer to use as bucket loop cursor
  87. * @obj: the type * to use as a loop cursor for each entry
  88. * @member: the name of the hlist_node within the struct
  89. */
  90. #define hash_for_each(name, bkt, obj, member) \
  91. for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
  92. (bkt)++)\
  93. hlist_for_each_entry(obj, &name[bkt], member)
  94. /**
  95. * hash_for_each_safe - iterate over a hashtable safe against removal of
  96. * hash entry
  97. * @name: hashtable to iterate
  98. * @bkt: integer to use as bucket loop cursor
  99. * @tmp: a &struct used for temporary storage
  100. * @obj: the type * to use as a loop cursor for each entry
  101. * @member: the name of the hlist_node within the struct
  102. */
  103. #define hash_for_each_safe(name, bkt, tmp, obj, member) \
  104. for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
  105. (bkt)++)\
  106. hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
  107. /**
  108. * hash_for_each_possible - iterate over all possible objects hashing to the
  109. * same bucket
  110. * @name: hashtable to iterate
  111. * @obj: the type * to use as a loop cursor for each entry
  112. * @member: the name of the hlist_node within the struct
  113. * @key: the key of the objects to iterate over
  114. */
  115. #define hash_for_each_possible(name, obj, member, key) \
  116. hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
  117. /**
  118. * hash_for_each_possible_safe - iterate over all possible objects hashing to the
  119. * same bucket safe against removals
  120. * @name: hashtable to iterate
  121. * @obj: the type * to use as a loop cursor for each entry
  122. * @tmp: a &struct used for temporary storage
  123. * @member: the name of the hlist_node within the struct
  124. * @key: the key of the objects to iterate over
  125. */
  126. #define hash_for_each_possible_safe(name, obj, tmp, member, key) \
  127. hlist_for_each_entry_safe(obj, tmp,\
  128. &name[hash_min(key, HASH_BITS(name))], member)
  129. #endif