hash.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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)(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. /* free only the hashtable and the hash itself. */
  45. void 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 *walk, *safe;
  54. struct element_t *bucket;
  55. int i;
  56. for (i = 0; i < hash->size; i++) {
  57. head = &hash->table[i];
  58. hlist_for_each_safe(walk, safe, head) {
  59. bucket = hlist_entry(walk, struct element_t, hlist);
  60. if (free_cb)
  61. free_cb(bucket->data, arg);
  62. hlist_del(walk);
  63. kfree(bucket);
  64. }
  65. }
  66. hash_destroy(hash);
  67. }
  68. /* adds data to the hashtable. returns 0 on success, -1 on error */
  69. static inline int hash_add(struct hashtable_t *hash,
  70. hashdata_compare_cb compare,
  71. hashdata_choose_cb choose, void *data)
  72. {
  73. int index;
  74. struct hlist_head *head;
  75. struct hlist_node *walk, *safe;
  76. struct element_t *bucket;
  77. if (!hash)
  78. return -1;
  79. index = choose(data, hash->size);
  80. head = &hash->table[index];
  81. hlist_for_each_safe(walk, safe, head) {
  82. bucket = hlist_entry(walk, struct element_t, hlist);
  83. if (compare(bucket->data, data))
  84. return -1;
  85. }
  86. /* no duplicate found in list, add new element */
  87. bucket = kmalloc(sizeof(struct element_t), GFP_ATOMIC);
  88. if (!bucket)
  89. return -1;
  90. bucket->data = data;
  91. hlist_add_head(&bucket->hlist, head);
  92. return 0;
  93. }
  94. /* removes data from hash, if found. returns pointer do data on success, so you
  95. * can remove the used structure yourself, or NULL on error . data could be the
  96. * structure you use with just the key filled, we just need the key for
  97. * comparing. */
  98. static inline void *hash_remove(struct hashtable_t *hash,
  99. hashdata_compare_cb compare,
  100. hashdata_choose_cb choose, void *data)
  101. {
  102. size_t index;
  103. struct hlist_node *walk;
  104. struct element_t *bucket;
  105. struct hlist_head *head;
  106. void *data_save;
  107. index = choose(data, hash->size);
  108. head = &hash->table[index];
  109. hlist_for_each_entry(bucket, walk, head, hlist) {
  110. if (compare(bucket->data, data)) {
  111. data_save = bucket->data;
  112. hlist_del(walk);
  113. kfree(bucket);
  114. return data_save;
  115. }
  116. }
  117. return NULL;
  118. }
  119. /* finds data, based on the key in keydata. returns the found data on success,
  120. * or NULL on error */
  121. static inline void *hash_find(struct hashtable_t *hash,
  122. hashdata_compare_cb compare,
  123. hashdata_choose_cb choose, void *keydata)
  124. {
  125. int index;
  126. struct hlist_head *head;
  127. struct hlist_node *walk;
  128. struct element_t *bucket;
  129. if (!hash)
  130. return NULL;
  131. index = choose(keydata , hash->size);
  132. head = &hash->table[index];
  133. hlist_for_each(walk, head) {
  134. bucket = hlist_entry(walk, struct element_t, hlist);
  135. if (compare(bucket->data, keydata))
  136. return bucket->data;
  137. }
  138. return NULL;
  139. }
  140. #endif /* _NET_BATMAN_ADV_HASH_H_ */