hash.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Copyright (C) 2006-2014 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 <linux/list.h>
  20. /* callback to a compare function. should compare 2 element datas for their
  21. * keys, return 0 if same and not 0 if not same
  22. */
  23. typedef int (*batadv_hashdata_compare_cb)(const struct hlist_node *,
  24. const void *);
  25. /* the hashfunction, should return an index
  26. * based on the key in the data of the first
  27. * argument and the size the second
  28. */
  29. typedef uint32_t (*batadv_hashdata_choose_cb)(const void *, uint32_t);
  30. typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
  31. struct batadv_hashtable {
  32. struct hlist_head *table; /* the hashtable itself with the buckets */
  33. spinlock_t *list_locks; /* spinlock for each hash list entry */
  34. uint32_t size; /* size of hashtable */
  35. };
  36. /* allocates and clears the hash */
  37. struct batadv_hashtable *batadv_hash_new(uint32_t size);
  38. /* set class key for all locks */
  39. void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
  40. struct lock_class_key *key);
  41. /* free only the hashtable and the hash itself. */
  42. void batadv_hash_destroy(struct batadv_hashtable *hash);
  43. /* remove the hash structure. if hashdata_free_cb != NULL, this function will be
  44. * called to remove the elements inside of the hash. if you don't remove the
  45. * elements, memory might be leaked.
  46. */
  47. static inline void batadv_hash_delete(struct batadv_hashtable *hash,
  48. batadv_hashdata_free_cb free_cb,
  49. 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. /**
  69. * batadv_hash_bytes - hash some bytes and add them to the previous hash
  70. * @hash: previous hash value
  71. * @data: data to be hashed
  72. * @size: number of bytes to be hashed
  73. *
  74. * Returns the new hash value.
  75. */
  76. static inline uint32_t batadv_hash_bytes(uint32_t hash, const void *data,
  77. uint32_t size)
  78. {
  79. const unsigned char *key = data;
  80. int i;
  81. for (i = 0; i < size; i++) {
  82. hash += key[i];
  83. hash += (hash << 10);
  84. hash ^= (hash >> 6);
  85. }
  86. return hash;
  87. }
  88. /**
  89. * batadv_hash_add - adds data to the hashtable
  90. * @hash: storage hash table
  91. * @compare: callback to determine if 2 hash elements are identical
  92. * @choose: callback calculating the hash index
  93. * @data: data passed to the aforementioned callbacks as argument
  94. * @data_node: to be added element
  95. *
  96. * Returns 0 on success, 1 if the element already is in the hash
  97. * and -1 on error.
  98. */
  99. static inline int batadv_hash_add(struct batadv_hashtable *hash,
  100. batadv_hashdata_compare_cb compare,
  101. batadv_hashdata_choose_cb choose,
  102. const void *data,
  103. struct hlist_node *data_node)
  104. {
  105. uint32_t index;
  106. int ret = -1;
  107. struct hlist_head *head;
  108. struct hlist_node *node;
  109. spinlock_t *list_lock; /* spinlock to protect write access */
  110. if (!hash)
  111. goto out;
  112. index = choose(data, hash->size);
  113. head = &hash->table[index];
  114. list_lock = &hash->list_locks[index];
  115. spin_lock_bh(list_lock);
  116. hlist_for_each(node, head) {
  117. if (!compare(node, data))
  118. continue;
  119. ret = 1;
  120. goto unlock;
  121. }
  122. /* no duplicate found in list, add new element */
  123. hlist_add_head_rcu(data_node, head);
  124. ret = 0;
  125. unlock:
  126. spin_unlock_bh(list_lock);
  127. out:
  128. return ret;
  129. }
  130. /* removes data from hash, if found. returns pointer do data on success, so you
  131. * can remove the used structure yourself, or NULL on error . data could be the
  132. * structure you use with just the key filled, we just need the key for
  133. * comparing.
  134. */
  135. static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
  136. batadv_hashdata_compare_cb compare,
  137. batadv_hashdata_choose_cb choose,
  138. void *data)
  139. {
  140. uint32_t index;
  141. struct hlist_node *node;
  142. struct hlist_head *head;
  143. void *data_save = NULL;
  144. index = choose(data, hash->size);
  145. head = &hash->table[index];
  146. spin_lock_bh(&hash->list_locks[index]);
  147. hlist_for_each(node, head) {
  148. if (!compare(node, data))
  149. continue;
  150. data_save = node;
  151. hlist_del_rcu(node);
  152. break;
  153. }
  154. spin_unlock_bh(&hash->list_locks[index]);
  155. return data_save;
  156. }
  157. #endif /* _NET_BATMAN_ADV_HASH_H_ */