hash.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2006-2010 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)(void *, 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)(void *, void *);
  34. struct element_t {
  35. void *data; /* pointer to the data */
  36. struct hlist_node hlist; /* bucket list pointer */
  37. };
  38. struct hashtable_t {
  39. struct hlist_head *table; /* the hashtable itself, with the buckets */
  40. int size; /* size of hashtable */
  41. };
  42. /* allocates and clears the hash */
  43. struct hashtable_t *hash_new(int size);
  44. /* remove element if you already found the element you want to delete and don't
  45. * need the overhead to find it again with hash_remove(). But usually, you
  46. * don't want to use this function, as it fiddles with hash-internals. */
  47. void *hash_remove_element(struct hashtable_t *hash, struct element_t *elem);
  48. /* free only the hashtable and the hash itself. */
  49. void hash_destroy(struct hashtable_t *hash);
  50. /* remove the hash structure. if hashdata_free_cb != NULL, this function will be
  51. * called to remove the elements inside of the hash. if you don't remove the
  52. * elements, memory might be leaked. */
  53. static inline void hash_delete(struct hashtable_t *hash,
  54. hashdata_free_cb free_cb, void *arg)
  55. {
  56. struct hlist_head *head;
  57. struct hlist_node *walk, *safe;
  58. struct element_t *bucket;
  59. int i;
  60. for (i = 0; i < hash->size; i++) {
  61. head = &hash->table[i];
  62. hlist_for_each_safe(walk, safe, head) {
  63. bucket = hlist_entry(walk, struct element_t, hlist);
  64. if (free_cb)
  65. free_cb(bucket->data, arg);
  66. hlist_del(walk);
  67. kfree(bucket);
  68. }
  69. }
  70. hash_destroy(hash);
  71. }
  72. /* adds data to the hashtable. returns 0 on success, -1 on error */
  73. static inline int hash_add(struct hashtable_t *hash,
  74. hashdata_compare_cb compare,
  75. hashdata_choose_cb choose, void *data)
  76. {
  77. int index;
  78. struct hlist_head *head;
  79. struct hlist_node *walk, *safe;
  80. struct element_t *bucket;
  81. if (!hash)
  82. return -1;
  83. index = choose(data, hash->size);
  84. head = &hash->table[index];
  85. hlist_for_each_safe(walk, safe, head) {
  86. bucket = hlist_entry(walk, struct element_t, hlist);
  87. if (compare(bucket->data, data))
  88. return -1;
  89. }
  90. /* no duplicate found in list, add new element */
  91. bucket = kmalloc(sizeof(struct element_t), GFP_ATOMIC);
  92. if (!bucket)
  93. return -1;
  94. bucket->data = data;
  95. hlist_add_head(&bucket->hlist, head);
  96. return 0;
  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 *walk;
  108. struct element_t *bucket;
  109. struct hlist_head *head;
  110. void *data_save;
  111. index = choose(data, hash->size);
  112. head = &hash->table[index];
  113. hlist_for_each_entry(bucket, walk, head, hlist) {
  114. if (compare(bucket->data, data)) {
  115. data_save = bucket->data;
  116. hlist_del(walk);
  117. kfree(bucket);
  118. return data_save;
  119. }
  120. }
  121. return NULL;
  122. }
  123. /* finds data, based on the key in keydata. returns the found data on success,
  124. * or NULL on error */
  125. static inline void *hash_find(struct hashtable_t *hash,
  126. hashdata_compare_cb compare,
  127. hashdata_choose_cb choose, void *keydata)
  128. {
  129. int index;
  130. struct hlist_head *head;
  131. struct hlist_node *walk;
  132. struct element_t *bucket;
  133. if (!hash)
  134. return NULL;
  135. index = choose(keydata , hash->size);
  136. head = &hash->table[index];
  137. hlist_for_each(walk, head) {
  138. bucket = hlist_entry(walk, struct element_t, hlist);
  139. if (compare(bucket->data, keydata))
  140. return bucket->data;
  141. }
  142. return NULL;
  143. }
  144. #endif /* _NET_BATMAN_ADV_HASH_H_ */