hash.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (C) 2006-2010 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. }
  30. /* free only the hashtable and the hash itself. */
  31. void hash_destroy(struct hashtable_t *hash)
  32. {
  33. kfree(hash->table);
  34. kfree(hash);
  35. }
  36. /* allocates and clears the hash */
  37. struct hashtable_t *hash_new(int size)
  38. {
  39. struct hashtable_t *hash;
  40. hash = kmalloc(sizeof(struct hashtable_t) , GFP_ATOMIC);
  41. if (!hash)
  42. return NULL;
  43. hash->size = size;
  44. hash->table = kmalloc(sizeof(struct element_t *) * size, GFP_ATOMIC);
  45. if (!hash->table) {
  46. kfree(hash);
  47. return NULL;
  48. }
  49. hash_init(hash);
  50. return hash;
  51. }