hash.h 4.3 KB

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