Commit | Line | Data |
---|---|---|
7db7d9f3 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6b1aea8c | 2 | /* Copyright (C) 2006-2018 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
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 | |
ebf38fb7 | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
c6c8fea2 SE |
17 | */ |
18 | ||
c6c8fea2 | 19 | #include "hash.h" |
1e2c2a4f SE |
20 | #include "main.h" |
21 | ||
b92b94ac | 22 | #include <linux/gfp.h> |
1e2c2a4f SE |
23 | #include <linux/lockdep.h> |
24 | #include <linux/slab.h> | |
c6c8fea2 SE |
25 | |
26 | /* clears the hash */ | |
5bf74e9c | 27 | static void batadv_hash_init(struct batadv_hashtable *hash) |
c6c8fea2 | 28 | { |
6b5e971a | 29 | u32 i; |
c6c8fea2 | 30 | |
cb4cca71 | 31 | for (i = 0; i < hash->size; i++) { |
c6c8fea2 | 32 | INIT_HLIST_HEAD(&hash->table[i]); |
fb778ea1 ML |
33 | spin_lock_init(&hash->list_locks[i]); |
34 | } | |
05abd7bc SE |
35 | |
36 | atomic_set(&hash->generation, 0); | |
c6c8fea2 SE |
37 | } |
38 | ||
ff15c27c SE |
39 | /** |
40 | * batadv_hash_destroy() - Free only the hashtable and the hash itself | |
41 | * @hash: hash object to destroy | |
42 | */ | |
5bf74e9c | 43 | void batadv_hash_destroy(struct batadv_hashtable *hash) |
c6c8fea2 | 44 | { |
fb778ea1 | 45 | kfree(hash->list_locks); |
c6c8fea2 SE |
46 | kfree(hash->table); |
47 | kfree(hash); | |
48 | } | |
49 | ||
ff15c27c SE |
50 | /** |
51 | * batadv_hash_new() - Allocates and clears the hashtable | |
52 | * @size: number of hash buckets to allocate | |
53 | * | |
54 | * Return: newly allocated hashtable, NULL on errors | |
55 | */ | |
6b5e971a | 56 | struct batadv_hashtable *batadv_hash_new(u32 size) |
c6c8fea2 | 57 | { |
5bf74e9c | 58 | struct batadv_hashtable *hash; |
c6c8fea2 | 59 | |
704509b8 | 60 | hash = kmalloc(sizeof(*hash), GFP_ATOMIC); |
c6c8fea2 SE |
61 | if (!hash) |
62 | return NULL; | |
63 | ||
0185dda6 | 64 | hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC); |
fb778ea1 ML |
65 | if (!hash->table) |
66 | goto free_hash; | |
c6c8fea2 | 67 | |
0185dda6 AQ |
68 | hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks), |
69 | GFP_ATOMIC); | |
fb778ea1 ML |
70 | if (!hash->list_locks) |
71 | goto free_table; | |
c6c8fea2 | 72 | |
fb778ea1 | 73 | hash->size = size; |
7f9f02cb | 74 | batadv_hash_init(hash); |
c6c8fea2 | 75 | return hash; |
fb778ea1 ML |
76 | |
77 | free_table: | |
78 | kfree(hash->table); | |
79 | free_hash: | |
80 | kfree(hash); | |
81 | return NULL; | |
82 | } | |
5d52dad2 | 83 | |
ff15c27c SE |
84 | /** |
85 | * batadv_hash_set_lock_class() - Set specific lockdep class for hash spinlocks | |
86 | * @hash: hash object to modify | |
87 | * @key: lockdep class key address | |
88 | */ | |
5bf74e9c | 89 | void batadv_hash_set_lock_class(struct batadv_hashtable *hash, |
5d52dad2 SE |
90 | struct lock_class_key *key) |
91 | { | |
6b5e971a | 92 | u32 i; |
5d52dad2 SE |
93 | |
94 | for (i = 0; i < hash->size; i++) | |
95 | lockdep_set_class(&hash->list_locks[i], key); | |
96 | } |