hash.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Copyright (C) 2006-2013 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_bytes - hash some bytes and add them to the previous hash
  72. * @hash: previous hash value
  73. * @data: data to be hashed
  74. * @size: number of bytes to be hashed
  75. *
  76. * Returns the new hash value.
  77. */
  78. static inline uint32_t batadv_hash_bytes(uint32_t hash, const void *data,
  79. uint32_t size)
  80. {
  81. const unsigned char *key = data;
  82. int i;
  83. for (i = 0; i < size; i++) {
  84. hash += key[i];
  85. hash += (hash << 10);
  86. hash ^= (hash >> 6);
  87. }
  88. return hash;
  89. }
  90. /**
  91. * batadv_hash_add - adds data to the hashtable
  92. * @hash: storage hash table
  93. * @compare: callback to determine if 2 hash elements are identical
  94. * @choose: callback calculating the hash index
  95. * @data: data passed to the aforementioned callbacks as argument
  96. * @data_node: to be added element
  97. *
  98. * Returns 0 on success, 1 if the element already is in the hash
  99. * and -1 on error.
  100. */
  101. static inline int batadv_hash_add(struct batadv_hashtable *hash,
  102. batadv_hashdata_compare_cb compare,
  103. batadv_hashdata_choose_cb choose,
  104. const void *data,
  105. struct hlist_node *data_node)
  106. {
  107. uint32_t index;
  108. int ret = -1;
  109. struct hlist_head *head;
  110. struct hlist_node *node;
  111. spinlock_t *list_lock; /* spinlock to protect write access */
  112. if (!hash)
  113. goto out;
  114. index = choose(data, hash->size);
  115. head = &hash->table[index];
  116. list_lock = &hash->list_locks[index];
  117. spin_lock_bh(list_lock);
  118. hlist_for_each(node, head) {
  119. if (!compare(node, data))
  120. continue;
  121. ret = 1;
  122. goto unlock;
  123. }
  124. /* no duplicate found in list, add new element */
  125. hlist_add_head_rcu(data_node, head);
  126. ret = 0;
  127. unlock:
  128. spin_unlock_bh(list_lock);
  129. out:
  130. return ret;
  131. }
  132. /* removes data from hash, if found. returns pointer do data on success, so you
  133. * can remove the used structure yourself, or NULL on error . data could be the
  134. * structure you use with just the key filled, we just need the key for
  135. * comparing.
  136. */
  137. static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
  138. batadv_hashdata_compare_cb compare,
  139. batadv_hashdata_choose_cb choose,
  140. void *data)
  141. {
  142. uint32_t index;
  143. struct hlist_node *node;
  144. struct hlist_head *head;
  145. void *data_save = NULL;
  146. index = choose(data, hash->size);
  147. head = &hash->table[index];
  148. spin_lock_bh(&hash->list_locks[index]);
  149. hlist_for_each(node, head) {
  150. if (!compare(node, data))
  151. continue;
  152. data_save = node;
  153. hlist_del_rcu(node);
  154. break;
  155. }
  156. spin_unlock_bh(&hash->list_locks[index]);
  157. return data_save;
  158. }
  159. #endif /* _NET_BATMAN_ADV_HASH_H_ */