hash.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2006-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Simon Wunderlich, Marek Lindner
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #ifndef _NET_BATMAN_ADV_HASH_H_
  22. #define _NET_BATMAN_ADV_HASH_H_
  23. #include <linux/list.h>
  24. /* callback to a compare function. should
  25. * compare 2 element datas for their keys,
  26. * return 0 if same and not 0 if not
  27. * same */
  28. typedef int (*hashdata_compare_cb)(const struct hlist_node *, const void *);
  29. /* the hashfunction, should return an index
  30. * based on the key in the data of the first
  31. * argument and the size the second */
  32. typedef uint32_t (*hashdata_choose_cb)(const void *, uint32_t);
  33. typedef void (*hashdata_free_cb)(struct hlist_node *, void *);
  34. struct hashtable_t {
  35. struct hlist_head *table; /* the hashtable itself with the buckets */
  36. spinlock_t *list_locks; /* spinlock for each hash list entry */
  37. uint32_t size; /* size of hashtable */
  38. };
  39. /* allocates and clears the hash */
  40. struct hashtable_t *batadv_hash_new(uint32_t size);
  41. /* set class key for all locks */
  42. void batadv_hash_set_lock_class(struct hashtable_t *hash,
  43. struct lock_class_key *key);
  44. /* free only the hashtable and the hash itself. */
  45. void batadv_hash_destroy(struct hashtable_t *hash);
  46. /* remove the hash structure. if hashdata_free_cb != NULL, this function will be
  47. * called to remove the elements inside of the hash. if you don't remove the
  48. * elements, memory might be leaked. */
  49. static inline void hash_delete(struct hashtable_t *hash,
  50. hashdata_free_cb free_cb, void *arg)
  51. {
  52. struct hlist_head *head;
  53. struct hlist_node *node, *node_tmp;
  54. spinlock_t *list_lock; /* spinlock to protect write access */
  55. uint32_t i;
  56. for (i = 0; i < hash->size; i++) {
  57. head = &hash->table[i];
  58. list_lock = &hash->list_locks[i];
  59. spin_lock_bh(list_lock);
  60. hlist_for_each_safe(node, node_tmp, head) {
  61. hlist_del_rcu(node);
  62. if (free_cb)
  63. free_cb(node, arg);
  64. }
  65. spin_unlock_bh(list_lock);
  66. }
  67. batadv_hash_destroy(hash);
  68. }
  69. /**
  70. * hash_add - adds data to the hashtable
  71. * @hash: storage hash table
  72. * @compare: callback to determine if 2 hash elements are identical
  73. * @choose: callback calculating the hash index
  74. * @data: data passed to the aforementioned callbacks as argument
  75. * @data_node: to be added element
  76. *
  77. * Returns 0 on success, 1 if the element already is in the hash
  78. * and -1 on error.
  79. */
  80. static inline int hash_add(struct hashtable_t *hash,
  81. hashdata_compare_cb compare,
  82. hashdata_choose_cb choose,
  83. const void *data, struct hlist_node *data_node)
  84. {
  85. uint32_t index;
  86. int ret = -1;
  87. struct hlist_head *head;
  88. struct hlist_node *node;
  89. spinlock_t *list_lock; /* spinlock to protect write access */
  90. if (!hash)
  91. goto out;
  92. index = choose(data, hash->size);
  93. head = &hash->table[index];
  94. list_lock = &hash->list_locks[index];
  95. spin_lock_bh(list_lock);
  96. hlist_for_each(node, head) {
  97. if (!compare(node, data))
  98. continue;
  99. ret = 1;
  100. goto unlock;
  101. }
  102. /* no duplicate found in list, add new element */
  103. hlist_add_head_rcu(data_node, head);
  104. ret = 0;
  105. unlock:
  106. spin_unlock_bh(list_lock);
  107. out:
  108. return ret;
  109. }
  110. /* removes data from hash, if found. returns pointer do data on success, so you
  111. * can remove the used structure yourself, or NULL on error . data could be the
  112. * structure you use with just the key filled, we just need the key for
  113. * comparing. */
  114. static inline void *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_ */