1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#ifndef _NET_BATMAN_ADV_HASH_H_
20#define _NET_BATMAN_ADV_HASH_H_
21
22#include "main.h"
23
24#include <linux/compiler.h>
25#include <linux/list.h>
26#include <linux/rculist.h>
27#include <linux/spinlock.h>
28#include <linux/stddef.h>
29#include <linux/types.h>
30
31struct lock_class_key;
32
33
34
35
36
37
38typedef bool (*batadv_hashdata_compare_cb)(const struct hlist_node *,
39 const void *);
40
41
42
43
44
45
46typedef u32 (*batadv_hashdata_choose_cb)(const void *, u32);
47typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
48
49
50
51
52struct batadv_hashtable {
53
54 struct hlist_head *table;
55
56
57 spinlock_t *list_locks;
58
59
60 u32 size;
61};
62
63
64struct batadv_hashtable *batadv_hash_new(u32 size);
65
66
67void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
68 struct lock_class_key *key);
69
70
71void batadv_hash_destroy(struct batadv_hashtable *hash);
72
73
74
75
76
77
78
79
80
81
82
83
84static inline int batadv_hash_add(struct batadv_hashtable *hash,
85 batadv_hashdata_compare_cb compare,
86 batadv_hashdata_choose_cb choose,
87 const void *data,
88 struct hlist_node *data_node)
89{
90 u32 index;
91 int ret = -1;
92 struct hlist_head *head;
93 struct hlist_node *node;
94 spinlock_t *list_lock;
95
96 if (!hash)
97 goto out;
98
99 index = choose(data, hash->size);
100 head = &hash->table[index];
101 list_lock = &hash->list_locks[index];
102
103 spin_lock_bh(list_lock);
104
105 hlist_for_each(node, head) {
106 if (!compare(node, data))
107 continue;
108
109 ret = 1;
110 goto unlock;
111 }
112
113
114 hlist_add_head_rcu(data_node, head);
115
116 ret = 0;
117
118unlock:
119 spin_unlock_bh(list_lock);
120out:
121 return ret;
122}
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
138 batadv_hashdata_compare_cb compare,
139 batadv_hashdata_choose_cb choose,
140 void *data)
141{
142 u32 index;
143 struct hlist_node *node;
144 struct hlist_head *head;
145 void *data_save = NULL;
146
147 index = choose(data, hash->size);
148 head = &hash->table[index];
149
150 spin_lock_bh(&hash->list_locks[index]);
151 hlist_for_each(node, head) {
152 if (!compare(node, data))
153 continue;
154
155 data_save = node;
156 hlist_del_rcu(node);
157 break;
158 }
159 spin_unlock_bh(&hash->list_locks[index]);
160
161 return data_save;
162}
163
164#endif
165