hash.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (C) 2006-2016 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "hash.h"
  18. #include "main.h"
  19. #include <linux/fs.h>
  20. #include <linux/lockdep.h>
  21. #include <linux/slab.h>
  22. /* clears the hash */
  23. static void batadv_hash_init(struct batadv_hashtable *hash)
  24. {
  25. u32 i;
  26. for (i = 0; i < hash->size; i++) {
  27. INIT_HLIST_HEAD(&hash->table[i]);
  28. spin_lock_init(&hash->list_locks[i]);
  29. }
  30. }
  31. /* free only the hashtable and the hash itself. */
  32. void batadv_hash_destroy(struct batadv_hashtable *hash)
  33. {
  34. kfree(hash->list_locks);
  35. kfree(hash->table);
  36. kfree(hash);
  37. }
  38. /* allocates and clears the hash */
  39. struct batadv_hashtable *batadv_hash_new(u32 size)
  40. {
  41. struct batadv_hashtable *hash;
  42. hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
  43. if (!hash)
  44. return NULL;
  45. hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC);
  46. if (!hash->table)
  47. goto free_hash;
  48. hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks),
  49. GFP_ATOMIC);
  50. if (!hash->list_locks)
  51. goto free_table;
  52. hash->size = size;
  53. batadv_hash_init(hash);
  54. return hash;
  55. free_table:
  56. kfree(hash->table);
  57. free_hash:
  58. kfree(hash);
  59. return NULL;
  60. }
  61. void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
  62. struct lock_class_key *key)
  63. {
  64. u32 i;
  65. for (i = 0; i < hash->size; i++)
  66. lockdep_set_class(&hash->list_locks[i], key);
  67. }