hash.c 1.87 KB
Newer Older
1
/* Copyright (C) 2006-2012 B.A.T.M.A.N. contributors:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * Simon Wunderlich, Marek Lindner
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

#include "main.h"
#include "hash.h"

/* clears the hash */
24
static void batadv_hash_init(struct hashtable_t *hash)
25
{
26
	uint32_t i;
27

28
	for (i = 0 ; i < hash->size; i++) {
29
		INIT_HLIST_HEAD(&hash->table[i]);
30
31
		spin_lock_init(&hash->list_locks[i]);
	}
32
33
34
}

/* free only the hashtable and the hash itself. */
35
void batadv_hash_destroy(struct hashtable_t *hash)
36
{
37
	kfree(hash->list_locks);
38
39
40
41
42
	kfree(hash->table);
	kfree(hash);
}

/* allocates and clears the hash */
43
struct hashtable_t *batadv_hash_new(uint32_t size)
44
45
46
{
	struct hashtable_t *hash;

47
	hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
48
49
50
	if (!hash)
		return NULL;

51
	hash->table = kmalloc(sizeof(*hash->table) * size, GFP_ATOMIC);
52
53
	if (!hash->table)
		goto free_hash;
54

55
56
	hash->list_locks = kmalloc(sizeof(*hash->list_locks) * size,
				   GFP_ATOMIC);
57
58
	if (!hash->list_locks)
		goto free_table;
59

60
	hash->size = size;
61
	batadv_hash_init(hash);
62
	return hash;
63
64
65
66
67
68
69

free_table:
	kfree(hash->table);
free_hash:
	kfree(hash);
	return NULL;
}
70
71
72
73
74
75
76
77
78

void batadv_hash_set_lock_class(struct hashtable_t *hash,
				struct lock_class_key *key)
{
	uint32_t i;

	for (i = 0; i < hash->size; i++)
		lockdep_set_class(&hash->list_locks[i], key);
}