hash.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Copyright (C) 2006-2016 B.A.T.M.A.N. contributors:
  2. *
  3. * Simon Wunderlich, Marek Lindner
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef _NET_BATMAN_ADV_HASH_H_
  18. #define _NET_BATMAN_ADV_HASH_H_
  19. #include "main.h"
  20. #include <linux/compiler.h>
  21. #include <linux/list.h>
  22. #include <linux/rculist.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/stddef.h>
  25. #include <linux/types.h>
  26. struct lock_class_key;
  27. /* callback to a compare function. should compare 2 element datas for their
  28. * keys
  29. *
  30. * Return: true if same and false if not same
  31. */
  32. typedef bool (*batadv_hashdata_compare_cb)(const struct hlist_node *,
  33. const void *);
  34. /* the hashfunction
  35. *
  36. * Return: an index based on the key in the data of the first argument and the
  37. * size the second
  38. */
  39. typedef u32 (*batadv_hashdata_choose_cb)(const void *, u32);
  40. typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
  41. struct batadv_hashtable {
  42. struct hlist_head *table; /* the hashtable itself with the buckets */
  43. spinlock_t *list_locks; /* spinlock for each hash list entry */
  44. u32 size; /* size of hashtable */
  45. };
  46. /* allocates and clears the hash */
  47. struct batadv_hashtable *batadv_hash_new(u32 size);
  48. /* set class key for all locks */
  49. void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
  50. struct lock_class_key *key);
  51. /* free only the hashtable and the hash itself. */
  52. void batadv_hash_destroy(struct batadv_hashtable *hash);
  53. /* remove the hash structure. if hashdata_free_cb != NULL, this function will be
  54. * called to remove the elements inside of the hash. if you don't remove the
  55. * elements, memory might be leaked.
  56. */
  57. static inline void batadv_hash_delete(struct batadv_hashtable *hash,
  58. batadv_hashdata_free_cb free_cb,
  59. void *arg)
  60. {
  61. struct hlist_head *head;
  62. struct hlist_node *node, *node_tmp;
  63. spinlock_t *list_lock; /* spinlock to protect write access */
  64. u32 i;
  65. for (i = 0; i < hash->size; i++) {
  66. head = &hash->table[i];
  67. list_lock = &hash->list_locks[i];
  68. spin_lock_bh(list_lock);
  69. hlist_for_each_safe(node, node_tmp, head) {
  70. hlist_del_rcu(node);
  71. if (free_cb)
  72. free_cb(node, arg);
  73. }
  74. spin_unlock_bh(list_lock);
  75. }
  76. batadv_hash_destroy(hash);
  77. }
  78. /**
  79. * batadv_hash_add - adds data to the hashtable
  80. * @hash: storage hash table
  81. * @compare: callback to determine if 2 hash elements are identical
  82. * @choose: callback calculating the hash index
  83. * @data: data passed to the aforementioned callbacks as argument
  84. * @data_node: to be added element
  85. *
  86. * Return: 0 on success, 1 if the element already is in the hash
  87. * and -1 on error.
  88. */
  89. static inline int batadv_hash_add(struct batadv_hashtable *hash,
  90. batadv_hashdata_compare_cb compare,
  91. batadv_hashdata_choose_cb choose,
  92. const void *data,
  93. struct hlist_node *data_node)
  94. {
  95. u32 index;
  96. int ret = -1;
  97. struct hlist_head *head;
  98. struct hlist_node *node;
  99. spinlock_t *list_lock; /* spinlock to protect write access */
  100. if (!hash)
  101. goto out;
  102. index = choose(data, hash->size);
  103. head = &hash->table[index];
  104. list_lock = &hash->list_locks[index];
  105. spin_lock_bh(list_lock);
  106. hlist_for_each(node, head) {
  107. if (!compare(node, data))
  108. continue;
  109. ret = 1;
  110. goto unlock;
  111. }
  112. /* no duplicate found in list, add new element */
  113. hlist_add_head_rcu(data_node, head);
  114. ret = 0;
  115. unlock:
  116. spin_unlock_bh(list_lock);
  117. out:
  118. return ret;
  119. }
  120. /* removes data from hash, if found. data could be the structure you use with
  121. * just the key filled, we just need the key for comparing.
  122. *
  123. * Return: returns pointer do data on success, so you can remove the used
  124. * structure yourself, or NULL on error
  125. */
  126. static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
  127. batadv_hashdata_compare_cb compare,
  128. batadv_hashdata_choose_cb choose,
  129. void *data)
  130. {
  131. u32 index;
  132. struct hlist_node *node;
  133. struct hlist_head *head;
  134. void *data_save = NULL;
  135. index = choose(data, hash->size);
  136. head = &hash->table[index];
  137. spin_lock_bh(&hash->list_locks[index]);
  138. hlist_for_each(node, head) {
  139. if (!compare(node, data))
  140. continue;
  141. data_save = node;
  142. hlist_del_rcu(node);
  143. break;
  144. }
  145. spin_unlock_bh(&hash->list_locks[index]);
  146. return data_save;
  147. }
  148. #endif /* _NET_BATMAN_ADV_HASH_H_ */