hashtable.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef HASHTABLE_H
  3. #define HASHTABLE_H
  4. #include "array_size.h"
  5. #include "list.h"
  6. #define HASH_SIZE(name) (ARRAY_SIZE(name))
  7. #define HASHTABLE_DECLARE(name, size) struct hlist_head name[size]
  8. #define HASHTABLE_DEFINE(name, size) \
  9. HASHTABLE_DECLARE(name, size) = \
  10. { [0 ... ((size) - 1)] = HLIST_HEAD_INIT }
  11. #define hash_head(table, key) (&(table)[(key) % HASH_SIZE(table)])
  12. static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
  13. {
  14. unsigned int i;
  15. for (i = 0; i < sz; i++)
  16. INIT_HLIST_HEAD(&ht[i]);
  17. }
  18. /**
  19. * hash_init - initialize a hash table
  20. * @table: hashtable to be initialized
  21. *
  22. * This has to be a macro since HASH_SIZE() will not work on pointers since
  23. * it calculates the size during preprocessing.
  24. */
  25. #define hash_init(table) __hash_init(table, HASH_SIZE(table))
  26. /**
  27. * hash_add - add an object to a hashtable
  28. * @table: hashtable to add to
  29. * @node: the &struct hlist_node of the object to be added
  30. * @key: the key of the object to be added
  31. */
  32. #define hash_add(table, node, key) \
  33. hlist_add_head(node, hash_head(table, key))
  34. /**
  35. * hash_del - remove an object from a hashtable
  36. * @node: &struct hlist_node of the object to remove
  37. */
  38. static inline void hash_del(struct hlist_node *node)
  39. {
  40. hlist_del_init(node);
  41. }
  42. /**
  43. * hash_for_each - iterate over a hashtable
  44. * @table: hashtable to iterate
  45. * @obj: the type * to use as a loop cursor for each entry
  46. * @member: the name of the hlist_node within the struct
  47. */
  48. #define hash_for_each(table, obj, member) \
  49. for (int _bkt = 0; _bkt < HASH_SIZE(table); _bkt++) \
  50. hlist_for_each_entry(obj, &table[_bkt], member)
  51. /**
  52. * hash_for_each_safe - iterate over a hashtable safe against removal of
  53. * hash entry
  54. * @table: hashtable to iterate
  55. * @obj: the type * to use as a loop cursor for each entry
  56. * @tmp: a &struct hlist_node used for temporary storage
  57. * @member: the name of the hlist_node within the struct
  58. */
  59. #define hash_for_each_safe(table, obj, tmp, member) \
  60. for (int _bkt = 0; _bkt < HASH_SIZE(table); _bkt++) \
  61. hlist_for_each_entry_safe(obj, tmp, &table[_bkt], member)
  62. /**
  63. * hash_for_each_possible - iterate over all possible objects hashing to the
  64. * same bucket
  65. * @table: hashtable to iterate
  66. * @obj: the type * to use as a loop cursor for each entry
  67. * @member: the name of the hlist_node within the struct
  68. * @key: the key of the objects to iterate over
  69. */
  70. #define hash_for_each_possible(table, obj, member, key) \
  71. hlist_for_each_entry(obj, hash_head(table, key), member)
  72. /**
  73. * hash_for_each_possible_safe - iterate over all possible objects hashing to the
  74. * same bucket safe against removals
  75. * @table: hashtable to iterate
  76. * @obj: the type * to use as a loop cursor for each entry
  77. * @tmp: a &struct hlist_node used for temporary storage
  78. * @member: the name of the hlist_node within the struct
  79. * @key: the key of the objects to iterate over
  80. */
  81. #define hash_for_each_possible_safe(table, obj, tmp, member, key) \
  82. hlist_for_each_entry_safe(obj, tmp, hash_head(table, key), member)
  83. #endif /* HASHTABLE_H */