hash.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /**
  54. * batadv_hash_add - adds data to the hashtable
  55. * @hash: storage hash table
  56. * @compare: callback to determine if 2 hash elements are identical
  57. * @choose: callback calculating the hash index
  58. * @data: data passed to the aforementioned callbacks as argument
  59. * @data_node: to be added element
  60. *
  61. * Return: 0 on success, 1 if the element already is in the hash
  62. * and -1 on error.
  63. */
  64. static inline int batadv_hash_add(struct batadv_hashtable *hash,
  65. batadv_hashdata_compare_cb compare,
  66. batadv_hashdata_choose_cb choose,
  67. const void *data,
  68. struct hlist_node *data_node)
  69. {
  70. u32 index;
  71. int ret = -1;
  72. struct hlist_head *head;
  73. struct hlist_node *node;
  74. spinlock_t *list_lock; /* spinlock to protect write access */
  75. if (!hash)
  76. goto out;
  77. index = choose(data, hash->size);
  78. head = &hash->table[index];
  79. list_lock = &hash->list_locks[index];
  80. spin_lock_bh(list_lock);
  81. hlist_for_each(node, head) {
  82. if (!compare(node, data))
  83. continue;
  84. ret = 1;
  85. goto unlock;
  86. }
  87. /* no duplicate found in list, add new element */
  88. hlist_add_head_rcu(data_node, head);
  89. ret = 0;
  90. unlock:
  91. spin_unlock_bh(list_lock);
  92. out:
  93. return ret;
  94. }
  95. /* removes data from hash, if found. data could be the structure you use with
  96. * just the key filled, we just need the key for comparing.
  97. *
  98. * Return: returns pointer do data on success, so you can remove the used
  99. * structure yourself, or NULL on error
  100. */
  101. static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
  102. batadv_hashdata_compare_cb compare,
  103. batadv_hashdata_choose_cb choose,
  104. void *data)
  105. {
  106. u32 index;
  107. struct hlist_node *node;
  108. struct hlist_head *head;
  109. void *data_save = NULL;
  110. index = choose(data, hash->size);
  111. head = &hash->table[index];
  112. spin_lock_bh(&hash->list_locks[index]);
  113. hlist_for_each(node, head) {
  114. if (!compare(node, data))
  115. continue;
  116. data_save = node;
  117. hlist_del_rcu(node);
  118. break;
  119. }
  120. spin_unlock_bh(&hash->list_locks[index]);
  121. return data_save;
  122. }
  123. #endif /* _NET_BATMAN_ADV_HASH_H_ */