n_tty: signal and flush atomically
[linux-2.6-block.git] / net / batman-adv / hash.c
CommitLineData
9f6446c7 1/* Copyright (C) 2006-2015 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
3 * Simon Wunderlich, Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
ebf38fb7 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
c6c8fea2
SE
16 */
17
c6c8fea2 18#include "hash.h"
1e2c2a4f
SE
19#include "main.h"
20
21#include <linux/fs.h>
22#include <linux/lockdep.h>
23#include <linux/slab.h>
c6c8fea2
SE
24
25/* clears the hash */
5bf74e9c 26static void batadv_hash_init(struct batadv_hashtable *hash)
c6c8fea2 27{
c90681b8 28 uint32_t i;
c6c8fea2 29
cb4cca71 30 for (i = 0; i < hash->size; i++) {
c6c8fea2 31 INIT_HLIST_HEAD(&hash->table[i]);
fb778ea1
ML
32 spin_lock_init(&hash->list_locks[i]);
33 }
c6c8fea2
SE
34}
35
36/* free only the hashtable and the hash itself. */
5bf74e9c 37void batadv_hash_destroy(struct batadv_hashtable *hash)
c6c8fea2 38{
fb778ea1 39 kfree(hash->list_locks);
c6c8fea2
SE
40 kfree(hash->table);
41 kfree(hash);
42}
43
44/* allocates and clears the hash */
5bf74e9c 45struct batadv_hashtable *batadv_hash_new(uint32_t size)
c6c8fea2 46{
5bf74e9c 47 struct batadv_hashtable *hash;
c6c8fea2 48
704509b8 49 hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
c6c8fea2
SE
50 if (!hash)
51 return NULL;
52
0185dda6 53 hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC);
fb778ea1
ML
54 if (!hash->table)
55 goto free_hash;
c6c8fea2 56
0185dda6
AQ
57 hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks),
58 GFP_ATOMIC);
fb778ea1
ML
59 if (!hash->list_locks)
60 goto free_table;
c6c8fea2 61
fb778ea1 62 hash->size = size;
7f9f02cb 63 batadv_hash_init(hash);
c6c8fea2 64 return hash;
fb778ea1
ML
65
66free_table:
67 kfree(hash->table);
68free_hash:
69 kfree(hash);
70 return NULL;
71}
5d52dad2 72
5bf74e9c 73void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
5d52dad2
SE
74 struct lock_class_key *key)
75{
76 uint32_t i;
77
78 for (i = 0; i < hash->size; i++)
79 lockdep_set_class(&hash->list_locks[i], key);
80}