hash.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (C) 2006-2013 B.A.T.M.A.N. contributors:
  2. *
  3. * Simon Wunderlich, Marek Lindner
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "hash.h"
  21. /* clears the hash */
  22. static void batadv_hash_init(struct batadv_hashtable *hash)
  23. {
  24. uint32_t i;
  25. for (i = 0; i < hash->size; i++) {
  26. INIT_HLIST_HEAD(&hash->table[i]);
  27. spin_lock_init(&hash->list_locks[i]);
  28. }
  29. }
  30. /* free only the hashtable and the hash itself. */
  31. void batadv_hash_destroy(struct batadv_hashtable *hash)
  32. {
  33. kfree(hash->list_locks);
  34. kfree(hash->table);
  35. kfree(hash);
  36. }
  37. /* allocates and clears the hash */
  38. struct batadv_hashtable *batadv_hash_new(uint32_t size)
  39. {
  40. struct batadv_hashtable *hash;
  41. hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
  42. if (!hash)
  43. return NULL;
  44. hash->table = kmalloc(sizeof(*hash->table) * size, GFP_ATOMIC);
  45. if (!hash->table)
  46. goto free_hash;
  47. hash->list_locks = kmalloc(sizeof(*hash->list_locks) * size,
  48. GFP_ATOMIC);
  49. if (!hash->list_locks)
  50. goto free_table;
  51. hash->size = size;
  52. batadv_hash_init(hash);
  53. return hash;
  54. free_table:
  55. kfree(hash->table);
  56. free_hash:
  57. kfree(hash);
  58. return NULL;
  59. }
  60. void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
  61. struct lock_class_key *key)
  62. {
  63. uint32_t i;
  64. for (i = 0; i < hash->size; i++)
  65. lockdep_set_class(&hash->list_locks[i], key);
  66. }