hash.h 4.5 KB

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