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 *hash_new(uint32_t size);
  41. /* free only the hashtable and the hash itself. */
  42. void hash_destroy(struct hashtable_t *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. static inline void hash_delete(struct hashtable_t *hash,
  47. hashdata_free_cb free_cb, void *arg)
  48. {
  49. struct hlist_head *head;
  50. struct hlist_node *node, *node_tmp;
  51. spinlock_t *list_lock; /* spinlock to protect write access */
  52. uint32_t i;
  53. for (i = 0; i < hash->size; i++) {
  54. head = &hash->table[i];
  55. list_lock = &hash->list_locks[i];
  56. spin_lock_bh(list_lock);
  57. hlist_for_each_safe(node, node_tmp, head) {
  58. hlist_del_rcu(node);
  59. if (free_cb)
  60. free_cb(node, arg);
  61. }
  62. spin_unlock_bh(list_lock);
  63. }
  64. hash_destroy(hash);
  65. }
  66. /**
  67. * hash_add - adds data to the hashtable
  68. * @hash: storage hash table
  69. * @compare: callback to determine if 2 hash elements are identical
  70. * @choose: callback calculating the hash index
  71. * @data: data passed to the aforementioned callbacks as argument
  72. * @data_node: to be added element
  73. *
  74. * Returns 0 on success, 1 if the element already is in the hash
  75. * and -1 on error.
  76. */
  77. static inline int hash_add(struct hashtable_t *hash,
  78. hashdata_compare_cb compare,
  79. hashdata_choose_cb choose,
  80. const void *data, struct hlist_node *data_node)
  81. {
  82. uint32_t index;
  83. int ret = -1;
  84. struct hlist_head *head;
  85. struct hlist_node *node;
  86. spinlock_t *list_lock; /* spinlock to protect write access */
  87. if (!hash)
  88. goto out;
  89. index = choose(data, hash->size);
  90. head = &hash->table[index];
  91. list_lock = &hash->list_locks[index];
  92. rcu_read_lock();
  93. __hlist_for_each_rcu(node, head) {
  94. if (!compare(node, data))
  95. continue;
  96. ret = 1;
  97. goto err_unlock;
  98. }
  99. rcu_read_unlock();
  100. /* no duplicate found in list, add new element */
  101. spin_lock_bh(list_lock);
  102. hlist_add_head_rcu(data_node, head);
  103. spin_unlock_bh(list_lock);
  104. ret = 0;
  105. goto out;
  106. err_unlock:
  107. rcu_read_unlock();
  108. out:
  109. return ret;
  110. }
  111. /* removes data from hash, if found. returns pointer do data on success, so you
  112. * can remove the used structure yourself, or NULL on error . data could be the
  113. * structure you use with just the key filled, we just need the key for
  114. * comparing. */
  115. static inline void *hash_remove(struct hashtable_t *hash,
  116. hashdata_compare_cb compare,
  117. hashdata_choose_cb choose, void *data)
  118. {
  119. uint32_t index;
  120. struct hlist_node *node;
  121. struct hlist_head *head;
  122. void *data_save = NULL;
  123. index = choose(data, hash->size);
  124. head = &hash->table[index];
  125. spin_lock_bh(&hash->list_locks[index]);
  126. hlist_for_each(node, head) {
  127. if (!compare(node, data))
  128. continue;
  129. data_save = node;
  130. hlist_del_rcu(node);
  131. break;
  132. }
  133. spin_unlock_bh(&hash->list_locks[index]);
  134. return data_save;
  135. }
  136. #endif /* _NET_BATMAN_ADV_HASH_H_ */