hash.h 4.6 KB

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