hash.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Copyright (C) 2006-2015 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. }