hash.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright (C) 2006-2015 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, return 0 if same and not 0 if not same
  29. */
  30. typedef int (*batadv_hashdata_compare_cb)(const struct hlist_node *,
  31. const void *);
  32. /* the hashfunction, should return an index
  33. * based on the key in the data of the first
  34. * argument and the size the second
  35. */
  36. typedef u32 (*batadv_hashdata_choose_cb)(const void *, u32);
  37. typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
  38. struct batadv_hashtable {
  39. struct hlist_head *table; /* the hashtable itself with the buckets */
  40. spinlock_t *list_locks; /* spinlock for each hash list entry */
  41. u32 size; /* size of hashtable */
  42. };
  43. /* allocates and clears the hash */
  44. struct batadv_hashtable *batadv_hash_new(u32 size);
  45. /* set class key for all locks */
  46. void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
  47. struct lock_class_key *key);
  48. /* free only the hashtable and the hash itself. */
  49. void batadv_hash_destroy(struct batadv_hashtable *hash);
  50. /* remove the hash structure. if hashdata_free_cb != NULL, this function will be
  51. * called to remove the elements inside of the hash. if you don't remove the
  52. * elements, memory might be leaked.
  53. */
  54. static inline void batadv_hash_delete(struct batadv_hashtable *hash,
  55. batadv_hashdata_free_cb free_cb,
  56. void *arg)
  57. {
  58. struct hlist_head *head;
  59. struct hlist_node *node, *node_tmp;
  60. spinlock_t *list_lock; /* spinlock to protect write access */
  61. u32 i;
  62. for (i = 0; i < hash->size; i++) {
  63. head = &hash->table[i];
  64. list_lock = &hash->list_locks[i];
  65. spin_lock_bh(list_lock);
  66. hlist_for_each_safe(node, node_tmp, head) {
  67. hlist_del_rcu(node);
  68. if (free_cb)
  69. free_cb(node, arg);
  70. }
  71. spin_unlock_bh(list_lock);
  72. }
  73. batadv_hash_destroy(hash);
  74. }
  75. /**
  76. * batadv_hash_add - adds data to the hashtable
  77. * @hash: storage hash table
  78. * @compare: callback to determine if 2 hash elements are identical
  79. * @choose: callback calculating the hash index
  80. * @data: data passed to the aforementioned callbacks as argument
  81. * @data_node: to be added element
  82. *
  83. * Returns 0 on success, 1 if the element already is in the hash
  84. * and -1 on error.
  85. */
  86. static inline int batadv_hash_add(struct batadv_hashtable *hash,
  87. batadv_hashdata_compare_cb compare,
  88. batadv_hashdata_choose_cb choose,
  89. const void *data,
  90. struct hlist_node *data_node)
  91. {
  92. u32 index;
  93. int ret = -1;
  94. struct hlist_head *head;
  95. struct hlist_node *node;
  96. spinlock_t *list_lock; /* spinlock to protect write access */
  97. if (!hash)
  98. goto out;
  99. index = choose(data, hash->size);
  100. head = &hash->table[index];
  101. list_lock = &hash->list_locks[index];
  102. spin_lock_bh(list_lock);
  103. hlist_for_each(node, head) {
  104. if (!compare(node, data))
  105. continue;
  106. ret = 1;
  107. goto unlock;
  108. }
  109. /* no duplicate found in list, add new element */
  110. hlist_add_head_rcu(data_node, head);
  111. ret = 0;
  112. unlock:
  113. spin_unlock_bh(list_lock);
  114. out:
  115. return ret;
  116. }
  117. /* removes data from hash, if found. returns pointer do data on success, so you
  118. * can remove the used structure yourself, or NULL on error . data could be the
  119. * structure you use with just the key filled, we just need the key for
  120. * comparing.
  121. */
  122. static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
  123. batadv_hashdata_compare_cb compare,
  124. batadv_hashdata_choose_cb choose,
  125. void *data)
  126. {
  127. u32 index;
  128. struct hlist_node *node;
  129. struct hlist_head *head;
  130. void *data_save = NULL;
  131. index = choose(data, hash->size);
  132. head = &hash->table[index];
  133. spin_lock_bh(&hash->list_locks[index]);
  134. hlist_for_each(node, head) {
  135. if (!compare(node, data))
  136. continue;
  137. data_save = node;
  138. hlist_del_rcu(node);
  139. break;
  140. }
  141. spin_unlock_bh(&hash->list_locks[index]);
  142. return data_save;
  143. }
  144. #endif /* _NET_BATMAN_ADV_HASH_H_ */