blob: 02653666a26bee9776b96517a28c77cadfe9419a [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2006-2011 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
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
22#include "main.h"
23#include "hash.h"
24
25/* clears the hash */
26static void hash_init(struct hashtable_t *hash)
27{
28 int i;
29
Marek Lindnerfb778ea2011-01-19 20:01:40 +000030 for (i = 0 ; i < hash->size; i++) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031 INIT_HLIST_HEAD(&hash->table[i]);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000032 spin_lock_init(&hash->list_locks[i]);
33 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034}
35
36/* free only the hashtable and the hash itself. */
37void hash_destroy(struct hashtable_t *hash)
38{
Marek Lindnerfb778ea2011-01-19 20:01:40 +000039 kfree(hash->list_locks);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040 kfree(hash->table);
41 kfree(hash);
42}
43
44/* allocates and clears the hash */
45struct hashtable_t *hash_new(int size)
46{
47 struct hashtable_t *hash;
48
Marek Lindnerfb778ea2011-01-19 20:01:40 +000049 hash = kmalloc(sizeof(struct hashtable_t), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050 if (!hash)
51 return NULL;
52
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053 hash->table = kmalloc(sizeof(struct element_t *) * size, GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000054 if (!hash->table)
55 goto free_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056
Marek Lindnerfb778ea2011-01-19 20:01:40 +000057 hash->list_locks = kmalloc(sizeof(spinlock_t) * size, GFP_ATOMIC);
58 if (!hash->list_locks)
59 goto free_table;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000060
Marek Lindnerfb778ea2011-01-19 20:01:40 +000061 hash->size = size;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062 hash_init(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063 return hash;
Marek Lindnerfb778ea2011-01-19 20:01:40 +000064
65free_table:
66 kfree(hash->table);
67free_hash:
68 kfree(hash);
69 return NULL;
70}
71
72void bucket_free_rcu(struct rcu_head *rcu)
73{
74 struct element_t *bucket;
75
76 bucket = container_of(rcu, struct element_t, rcu);
77 kfree(bucket);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000078}