/*1* Copyright (C) 2006-2011 B.A.T.M.A.N. contributors:2*3* Simon Wunderlich, Marek Lindner4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of version 2 of the GNU General Public7* License as published by the Free Software Foundation.8*9* This program is distributed in the hope that it will be useful, but10* WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12* General Public License for more details.13*14* You should have received a copy of the GNU General Public License15* along with this program; if not, write to the Free Software16* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA17* 02110-1301, USA18*19*/2021#include "main.h"22#include "hash.h"2324/* clears the hash */25static void hash_init(struct hashtable_t *hash)26{27int i;2829for (i = 0 ; i < hash->size; i++) {30INIT_HLIST_HEAD(&hash->table[i]);31spin_lock_init(&hash->list_locks[i]);32}33}3435/* free only the hashtable and the hash itself. */36void hash_destroy(struct hashtable_t *hash)37{38kfree(hash->list_locks);39kfree(hash->table);40kfree(hash);41}4243/* allocates and clears the hash */44struct hashtable_t *hash_new(int size)45{46struct hashtable_t *hash;4748hash = kmalloc(sizeof(struct hashtable_t), GFP_ATOMIC);49if (!hash)50return NULL;5152hash->table = kmalloc(sizeof(struct element_t *) * size, GFP_ATOMIC);53if (!hash->table)54goto free_hash;5556hash->list_locks = kmalloc(sizeof(spinlock_t) * size, GFP_ATOMIC);57if (!hash->list_locks)58goto free_table;5960hash->size = size;61hash_init(hash);62return hash;6364free_table:65kfree(hash->table);66free_hash:67kfree(hash);68return NULL;69}707172