hash.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Simon Wunderlich, Marek Lindner
  5. */
  6. #ifndef _NET_BATMAN_ADV_HASH_H_
  7. #define _NET_BATMAN_ADV_HASH_H_
  8. #include "main.h"
  9. #include <linux/atomic.h>
  10. #include <linux/compiler.h>
  11. #include <linux/list.h>
  12. #include <linux/lockdep.h>
  13. #include <linux/rculist.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/stddef.h>
  16. #include <linux/types.h>
  17. /* callback to a compare function. should compare 2 element data for their
  18. * keys
  19. *
  20. * Return: true if same and false if not same
  21. */
  22. typedef bool (*batadv_hashdata_compare_cb)(const struct hlist_node *,
  23. const void *);
  24. /* the hashfunction
  25. *
  26. * Return: an index based on the key in the data of the first argument and the
  27. * size the second
  28. */
  29. typedef u32 (*batadv_hashdata_choose_cb)(const void *, u32);
  30. typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
  31. /**
  32. * struct batadv_hashtable - Wrapper of simple hlist based hashtable
  33. */
  34. struct batadv_hashtable {
  35. /** @table: the hashtable itself with the buckets */
  36. struct hlist_head *table;
  37. /** @list_locks: spinlock for each hash list entry */
  38. spinlock_t *list_locks;
  39. /** @size: size of hashtable */
  40. u32 size;
  41. /** @generation: current (generation) sequence number */
  42. atomic_t generation;
  43. };
  44. /* allocates and clears the hash */
  45. struct batadv_hashtable *batadv_hash_new(u32 size);
  46. /* set class key for all locks */
  47. void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
  48. struct lock_class_key *key);
  49. /* free only the hashtable and the hash itself. */
  50. void batadv_hash_destroy(struct batadv_hashtable *hash);
  51. /**
  52. * batadv_hash_add() - adds data to the hashtable
  53. * @hash: storage hash table
  54. * @compare: callback to determine if 2 hash elements are identical
  55. * @choose: callback calculating the hash index
  56. * @data: data passed to the aforementioned callbacks as argument
  57. * @data_node: to be added element
  58. *
  59. * Return: 0 on success, 1 if the element already is in the hash
  60. * and -1 on error.
  61. */
  62. static inline int batadv_hash_add(struct batadv_hashtable *hash,
  63. batadv_hashdata_compare_cb compare,
  64. batadv_hashdata_choose_cb choose,
  65. const void *data,
  66. struct hlist_node *data_node)
  67. {
  68. u32 index;
  69. int ret = -1;
  70. struct hlist_head *head;
  71. struct hlist_node *node;
  72. spinlock_t *list_lock; /* spinlock to protect write access */
  73. if (!hash)
  74. goto out;
  75. index = choose(data, hash->size);
  76. head = &hash->table[index];
  77. list_lock = &hash->list_locks[index];
  78. spin_lock_bh(list_lock);
  79. hlist_for_each(node, head) {
  80. if (!compare(node, data))
  81. continue;
  82. ret = 1;
  83. goto unlock;
  84. }
  85. /* no duplicate found in list, add new element */
  86. hlist_add_head_rcu(data_node, head);
  87. atomic_inc(&hash->generation);
  88. ret = 0;
  89. unlock:
  90. spin_unlock_bh(list_lock);
  91. out:
  92. return ret;
  93. }
  94. /**
  95. * batadv_hash_remove() - Removes data from hash, if found
  96. * @hash: hash table
  97. * @compare: callback to determine if 2 hash elements are identical
  98. * @choose: callback calculating the hash index
  99. * @data: data passed to the aforementioned callbacks as argument
  100. *
  101. * ata could be the structure you use with just the key filled, we just need
  102. * the key for comparing.
  103. *
  104. * Return: returns pointer do data on success, so you can remove the used
  105. * structure yourself, or NULL on error
  106. */
  107. static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
  108. batadv_hashdata_compare_cb compare,
  109. batadv_hashdata_choose_cb choose,
  110. void *data)
  111. {
  112. u32 index;
  113. struct hlist_node *node;
  114. struct hlist_head *head;
  115. void *data_save = NULL;
  116. index = choose(data, hash->size);
  117. head = &hash->table[index];
  118. spin_lock_bh(&hash->list_locks[index]);
  119. hlist_for_each(node, head) {
  120. if (!compare(node, data))
  121. continue;
  122. data_save = node;
  123. hlist_del_rcu(node);
  124. atomic_inc(&hash->generation);
  125. break;
  126. }
  127. spin_unlock_bh(&hash->list_locks[index]);
  128. return data_save;
  129. }
  130. #endif /* _NET_BATMAN_ADV_HASH_H_ */