hash.c 1.8 KB

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