hash.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2006-2011 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)(struct hlist_node *, 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 int (*hashdata_choose_cb)(void *, int);
  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. int size; /* size of hashtable */
  38. };
  39. /* allocates and clears the hash */
  40. struct hashtable_t *hash_new(int 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. int 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. /* adds data to the hashtable. returns 0 on success, -1 on error */
  67. static inline int hash_add(struct hashtable_t *hash,
  68. hashdata_compare_cb compare,
  69. hashdata_choose_cb choose,
  70. void *data, struct hlist_node *data_node)
  71. {
  72. int index;
  73. struct hlist_head *head;
  74. struct hlist_node *node;
  75. spinlock_t *list_lock; /* spinlock to protect write access */
  76. if (!hash)
  77. goto err;
  78. index = choose(data, hash->size);
  79. head = &hash->table[index];
  80. list_lock = &hash->list_locks[index];
  81. rcu_read_lock();
  82. __hlist_for_each_rcu(node, head) {
  83. if (!compare(node, data))
  84. continue;
  85. goto err_unlock;
  86. }
  87. rcu_read_unlock();
  88. /* no duplicate found in list, add new element */
  89. spin_lock_bh(list_lock);
  90. hlist_add_head_rcu(data_node, head);
  91. spin_unlock_bh(list_lock);
  92. return 0;
  93. err_unlock:
  94. rcu_read_unlock();
  95. err:
  96. return -1;
  97. }
  98. /* removes data from hash, if found. returns pointer do data on success, so you
  99. * can remove the used structure yourself, or NULL on error . data could be the
  100. * structure you use with just the key filled, we just need the key for
  101. * comparing. */
  102. static inline void *hash_remove(struct hashtable_t *hash,
  103. hashdata_compare_cb compare,
  104. hashdata_choose_cb choose, void *data)
  105. {
  106. size_t index;
  107. struct hlist_node *node;
  108. struct hlist_head *head;
  109. void *data_save = NULL;
  110. index = choose(data, hash->size);
  111. head = &hash->table[index];
  112. spin_lock_bh(&hash->list_locks[index]);
  113. hlist_for_each(node, head) {
  114. if (!compare(node, data))
  115. continue;
  116. data_save = node;
  117. hlist_del_rcu(node);
  118. break;
  119. }
  120. spin_unlock_bh(&hash->list_locks[index]);
  121. return data_save;
  122. }
  123. #endif /* _NET_BATMAN_ADV_HASH_H_ */