hash.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (C) 2006-2011 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. int 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(int size)
  41. {
  42. struct hashtable_t *hash;
  43. hash = kmalloc(sizeof(struct hashtable_t), GFP_ATOMIC);
  44. if (!hash)
  45. return NULL;
  46. hash->table = kmalloc(sizeof(struct element_t *) * size, GFP_ATOMIC);
  47. if (!hash->table)
  48. goto free_hash;
  49. hash->list_locks = kmalloc(sizeof(spinlock_t) * size, GFP_ATOMIC);
  50. if (!hash->list_locks)
  51. goto free_table;
  52. hash->size = size;
  53. hash_init(hash);
  54. return hash;
  55. free_table:
  56. kfree(hash->table);
  57. free_hash:
  58. kfree(hash);
  59. return NULL;
  60. }