This source file includes following definitions.
- __hash_init
- hash_hashed
- __hash_empty
- hash_del
- hash_del_rcu
1
2
3
4
5
6
7 #ifndef _LINUX_HASHTABLE_H
8 #define _LINUX_HASHTABLE_H
9
10 #include <linux/list.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/hash.h>
14 #include <linux/rculist.h>
15
16 #define DEFINE_HASHTABLE(name, bits) \
17 struct hlist_head name[1 << (bits)] = \
18 { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
19
20 #define DEFINE_READ_MOSTLY_HASHTABLE(name, bits) \
21 struct hlist_head name[1 << (bits)] __read_mostly = \
22 { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
23
24 #define DECLARE_HASHTABLE(name, bits) \
25 struct hlist_head name[1 << (bits)]
26
27 #define HASH_SIZE(name) (ARRAY_SIZE(name))
28 #define HASH_BITS(name) ilog2(HASH_SIZE(name))
29
30
31 #define hash_min(val, bits) \
32 (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
33
34 static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
35 {
36 unsigned int i;
37
38 for (i = 0; i < sz; i++)
39 INIT_HLIST_HEAD(&ht[i]);
40 }
41
42
43
44
45
46
47
48
49
50
51
52 #define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
53
54
55
56
57
58
59
60 #define hash_add(hashtable, node, key) \
61 hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
62
63
64
65
66
67
68
69 #define hash_add_rcu(hashtable, node, key) \
70 hlist_add_head_rcu(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
71
72
73
74
75
76 static inline bool hash_hashed(struct hlist_node *node)
77 {
78 return !hlist_unhashed(node);
79 }
80
81 static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
82 {
83 unsigned int i;
84
85 for (i = 0; i < sz; i++)
86 if (!hlist_empty(&ht[i]))
87 return false;
88
89 return true;
90 }
91
92
93
94
95
96
97
98
99 #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
100
101
102
103
104
105 static inline void hash_del(struct hlist_node *node)
106 {
107 hlist_del_init(node);
108 }
109
110
111
112
113
114 static inline void hash_del_rcu(struct hlist_node *node)
115 {
116 hlist_del_init_rcu(node);
117 }
118
119
120
121
122
123
124
125
126 #define hash_for_each(name, bkt, obj, member) \
127 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
128 (bkt)++)\
129 hlist_for_each_entry(obj, &name[bkt], member)
130
131
132
133
134
135
136
137
138 #define hash_for_each_rcu(name, bkt, obj, member) \
139 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
140 (bkt)++)\
141 hlist_for_each_entry_rcu(obj, &name[bkt], member)
142
143
144
145
146
147
148
149
150
151
152 #define hash_for_each_safe(name, bkt, tmp, obj, member) \
153 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
154 (bkt)++)\
155 hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
156
157
158
159
160
161
162
163
164
165 #define hash_for_each_possible(name, obj, member, key) \
166 hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
167
168
169
170
171
172
173
174
175
176 #define hash_for_each_possible_rcu(name, obj, member, key) \
177 hlist_for_each_entry_rcu(obj, &name[hash_min(key, HASH_BITS(name))],\
178 member)
179
180
181
182
183
184
185
186
187
188
189
190
191 #define hash_for_each_possible_rcu_notrace(name, obj, member, key) \
192 hlist_for_each_entry_rcu_notrace(obj, \
193 &name[hash_min(key, HASH_BITS(name))], member)
194
195
196
197
198
199
200
201
202
203
204 #define hash_for_each_possible_safe(name, obj, tmp, member, key) \
205 hlist_for_each_entry_safe(obj, tmp,\
206 &name[hash_min(key, HASH_BITS(name))], member)
207
208
209 #endif