hash.c 1.7 KB

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