Blame view
net/batman-adv/hash.c
1.73 KB
7db7d9f36
|
1 |
// SPDX-License-Identifier: GPL-2.0 |
7a79d717e
|
2 |
/* Copyright (C) 2006-2019 B.A.T.M.A.N. contributors: |
c6c8fea29
|
3 4 |
* * Simon Wunderlich, Marek Lindner |
c6c8fea29
|
5 |
*/ |
c6c8fea29
|
6 |
#include "hash.h" |
1e2c2a4fe
|
7 |
#include "main.h" |
b92b94ac7
|
8 |
#include <linux/gfp.h> |
1e2c2a4fe
|
9 10 |
#include <linux/lockdep.h> #include <linux/slab.h> |
c6c8fea29
|
11 12 |
/* clears the hash */ |
5bf74e9ca
|
13 |
static void batadv_hash_init(struct batadv_hashtable *hash) |
c6c8fea29
|
14 |
{ |
6b5e971a2
|
15 |
u32 i; |
c6c8fea29
|
16 |
|
cb4cca710
|
17 |
for (i = 0; i < hash->size; i++) { |
c6c8fea29
|
18 |
INIT_HLIST_HEAD(&hash->table[i]); |
fb778ea17
|
19 20 |
spin_lock_init(&hash->list_locks[i]); } |
05abd7bcc
|
21 22 |
atomic_set(&hash->generation, 0); |
c6c8fea29
|
23 |
} |
ff15c27c9
|
24 25 26 27 |
/** * batadv_hash_destroy() - Free only the hashtable and the hash itself * @hash: hash object to destroy */ |
5bf74e9ca
|
28 |
void batadv_hash_destroy(struct batadv_hashtable *hash) |
c6c8fea29
|
29 |
{ |
fb778ea17
|
30 |
kfree(hash->list_locks); |
c6c8fea29
|
31 32 33 |
kfree(hash->table); kfree(hash); } |
ff15c27c9
|
34 35 36 37 38 39 |
/** * batadv_hash_new() - Allocates and clears the hashtable * @size: number of hash buckets to allocate * * Return: newly allocated hashtable, NULL on errors */ |
6b5e971a2
|
40 |
struct batadv_hashtable *batadv_hash_new(u32 size) |
c6c8fea29
|
41 |
{ |
5bf74e9ca
|
42 |
struct batadv_hashtable *hash; |
c6c8fea29
|
43 |
|
704509b8d
|
44 |
hash = kmalloc(sizeof(*hash), GFP_ATOMIC); |
c6c8fea29
|
45 46 |
if (!hash) return NULL; |
0185dda64
|
47 |
hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC); |
fb778ea17
|
48 49 |
if (!hash->table) goto free_hash; |
c6c8fea29
|
50 |
|
0185dda64
|
51 52 |
hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks), GFP_ATOMIC); |
fb778ea17
|
53 54 |
if (!hash->list_locks) goto free_table; |
c6c8fea29
|
55 |
|
fb778ea17
|
56 |
hash->size = size; |
7f9f02cb8
|
57 |
batadv_hash_init(hash); |
c6c8fea29
|
58 |
return hash; |
fb778ea17
|
59 60 61 62 63 64 65 |
free_table: kfree(hash->table); free_hash: kfree(hash); return NULL; } |
5d52dad27
|
66 |
|
ff15c27c9
|
67 68 69 70 71 |
/** * batadv_hash_set_lock_class() - Set specific lockdep class for hash spinlocks * @hash: hash object to modify * @key: lockdep class key address */ |
5bf74e9ca
|
72 |
void batadv_hash_set_lock_class(struct batadv_hashtable *hash, |
5d52dad27
|
73 74 |
struct lock_class_key *key) { |
6b5e971a2
|
75 |
u32 i; |
5d52dad27
|
76 77 78 79 |
for (i = 0; i < hash->size; i++) lockdep_set_class(&hash->list_locks[i], key); } |