1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Statically sized hash table implementation
4*4882a593Smuzhiyun * (C) 2012 Sasha Levin <levinsasha928@gmail.com>
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #ifndef _LINUX_HASHTABLE_H
8*4882a593Smuzhiyun #define _LINUX_HASHTABLE_H
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/list.h>
11*4882a593Smuzhiyun #include <linux/types.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/hash.h>
14*4882a593Smuzhiyun #include <linux/rculist.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #define DEFINE_HASHTABLE(name, bits) \
17*4882a593Smuzhiyun struct hlist_head name[1 << (bits)] = \
18*4882a593Smuzhiyun { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define DEFINE_READ_MOSTLY_HASHTABLE(name, bits) \
21*4882a593Smuzhiyun struct hlist_head name[1 << (bits)] __read_mostly = \
22*4882a593Smuzhiyun { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #define DECLARE_HASHTABLE(name, bits) \
25*4882a593Smuzhiyun struct hlist_head name[1 << (bits)]
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #define HASH_SIZE(name) (ARRAY_SIZE(name))
28*4882a593Smuzhiyun #define HASH_BITS(name) ilog2(HASH_SIZE(name))
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
31*4882a593Smuzhiyun #define hash_min(val, bits) \
32*4882a593Smuzhiyun (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
33*4882a593Smuzhiyun
__hash_init(struct hlist_head * ht,unsigned int sz)34*4882a593Smuzhiyun static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun unsigned int i;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun for (i = 0; i < sz; i++)
39*4882a593Smuzhiyun INIT_HLIST_HEAD(&ht[i]);
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /**
43*4882a593Smuzhiyun * hash_init - initialize a hash table
44*4882a593Smuzhiyun * @hashtable: hashtable to be initialized
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * Calculates the size of the hashtable from the given parameter, otherwise
47*4882a593Smuzhiyun * same as hash_init_size.
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * This has to be a macro since HASH_BITS() will not work on pointers since
50*4882a593Smuzhiyun * it calculates the size during preprocessing.
51*4882a593Smuzhiyun */
52*4882a593Smuzhiyun #define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun * hash_add - add an object to a hashtable
56*4882a593Smuzhiyun * @hashtable: hashtable to add to
57*4882a593Smuzhiyun * @node: the &struct hlist_node of the object to be added
58*4882a593Smuzhiyun * @key: the key of the object to be added
59*4882a593Smuzhiyun */
60*4882a593Smuzhiyun #define hash_add(hashtable, node, key) \
61*4882a593Smuzhiyun hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /**
64*4882a593Smuzhiyun * hash_add_rcu - add an object to a rcu enabled hashtable
65*4882a593Smuzhiyun * @hashtable: hashtable to add to
66*4882a593Smuzhiyun * @node: the &struct hlist_node of the object to be added
67*4882a593Smuzhiyun * @key: the key of the object to be added
68*4882a593Smuzhiyun */
69*4882a593Smuzhiyun #define hash_add_rcu(hashtable, node, key) \
70*4882a593Smuzhiyun hlist_add_head_rcu(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /**
73*4882a593Smuzhiyun * hash_hashed - check whether an object is in any hashtable
74*4882a593Smuzhiyun * @node: the &struct hlist_node of the object to be checked
75*4882a593Smuzhiyun */
hash_hashed(struct hlist_node * node)76*4882a593Smuzhiyun static inline bool hash_hashed(struct hlist_node *node)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun return !hlist_unhashed(node);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
__hash_empty(struct hlist_head * ht,unsigned int sz)81*4882a593Smuzhiyun static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun unsigned int i;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun for (i = 0; i < sz; i++)
86*4882a593Smuzhiyun if (!hlist_empty(&ht[i]))
87*4882a593Smuzhiyun return false;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun return true;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun * hash_empty - check whether a hashtable is empty
94*4882a593Smuzhiyun * @hashtable: hashtable to check
95*4882a593Smuzhiyun *
96*4882a593Smuzhiyun * This has to be a macro since HASH_BITS() will not work on pointers since
97*4882a593Smuzhiyun * it calculates the size during preprocessing.
98*4882a593Smuzhiyun */
99*4882a593Smuzhiyun #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /**
102*4882a593Smuzhiyun * hash_del - remove an object from a hashtable
103*4882a593Smuzhiyun * @node: &struct hlist_node of the object to remove
104*4882a593Smuzhiyun */
hash_del(struct hlist_node * node)105*4882a593Smuzhiyun static inline void hash_del(struct hlist_node *node)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun hlist_del_init(node);
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /**
111*4882a593Smuzhiyun * hash_del_rcu - remove an object from a rcu enabled hashtable
112*4882a593Smuzhiyun * @node: &struct hlist_node of the object to remove
113*4882a593Smuzhiyun */
hash_del_rcu(struct hlist_node * node)114*4882a593Smuzhiyun static inline void hash_del_rcu(struct hlist_node *node)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun hlist_del_init_rcu(node);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /**
120*4882a593Smuzhiyun * hash_for_each - iterate over a hashtable
121*4882a593Smuzhiyun * @name: hashtable to iterate
122*4882a593Smuzhiyun * @bkt: integer to use as bucket loop cursor
123*4882a593Smuzhiyun * @obj: the type * to use as a loop cursor for each entry
124*4882a593Smuzhiyun * @member: the name of the hlist_node within the struct
125*4882a593Smuzhiyun */
126*4882a593Smuzhiyun #define hash_for_each(name, bkt, obj, member) \
127*4882a593Smuzhiyun for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
128*4882a593Smuzhiyun (bkt)++)\
129*4882a593Smuzhiyun hlist_for_each_entry(obj, &name[bkt], member)
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /**
132*4882a593Smuzhiyun * hash_for_each_rcu - iterate over a rcu enabled hashtable
133*4882a593Smuzhiyun * @name: hashtable to iterate
134*4882a593Smuzhiyun * @bkt: integer to use as bucket loop cursor
135*4882a593Smuzhiyun * @obj: the type * to use as a loop cursor for each entry
136*4882a593Smuzhiyun * @member: the name of the hlist_node within the struct
137*4882a593Smuzhiyun */
138*4882a593Smuzhiyun #define hash_for_each_rcu(name, bkt, obj, member) \
139*4882a593Smuzhiyun for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
140*4882a593Smuzhiyun (bkt)++)\
141*4882a593Smuzhiyun hlist_for_each_entry_rcu(obj, &name[bkt], member)
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun * hash_for_each_safe - iterate over a hashtable safe against removal of
145*4882a593Smuzhiyun * hash entry
146*4882a593Smuzhiyun * @name: hashtable to iterate
147*4882a593Smuzhiyun * @bkt: integer to use as bucket loop cursor
148*4882a593Smuzhiyun * @tmp: a &struct hlist_node used for temporary storage
149*4882a593Smuzhiyun * @obj: the type * to use as a loop cursor for each entry
150*4882a593Smuzhiyun * @member: the name of the hlist_node within the struct
151*4882a593Smuzhiyun */
152*4882a593Smuzhiyun #define hash_for_each_safe(name, bkt, tmp, obj, member) \
153*4882a593Smuzhiyun for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
154*4882a593Smuzhiyun (bkt)++)\
155*4882a593Smuzhiyun hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /**
158*4882a593Smuzhiyun * hash_for_each_possible - iterate over all possible objects hashing to the
159*4882a593Smuzhiyun * same bucket
160*4882a593Smuzhiyun * @name: hashtable to iterate
161*4882a593Smuzhiyun * @obj: the type * to use as a loop cursor for each entry
162*4882a593Smuzhiyun * @member: the name of the hlist_node within the struct
163*4882a593Smuzhiyun * @key: the key of the objects to iterate over
164*4882a593Smuzhiyun */
165*4882a593Smuzhiyun #define hash_for_each_possible(name, obj, member, key) \
166*4882a593Smuzhiyun hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /**
169*4882a593Smuzhiyun * hash_for_each_possible_rcu - iterate over all possible objects hashing to the
170*4882a593Smuzhiyun * same bucket in an rcu enabled hashtable
171*4882a593Smuzhiyun * @name: hashtable to iterate
172*4882a593Smuzhiyun * @obj: the type * to use as a loop cursor for each entry
173*4882a593Smuzhiyun * @member: the name of the hlist_node within the struct
174*4882a593Smuzhiyun * @key: the key of the objects to iterate over
175*4882a593Smuzhiyun */
176*4882a593Smuzhiyun #define hash_for_each_possible_rcu(name, obj, member, key, cond...) \
177*4882a593Smuzhiyun hlist_for_each_entry_rcu(obj, &name[hash_min(key, HASH_BITS(name))],\
178*4882a593Smuzhiyun member, ## cond)
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /**
181*4882a593Smuzhiyun * hash_for_each_possible_rcu_notrace - iterate over all possible objects hashing
182*4882a593Smuzhiyun * to the same bucket in an rcu enabled hashtable in a rcu enabled hashtable
183*4882a593Smuzhiyun * @name: hashtable to iterate
184*4882a593Smuzhiyun * @obj: the type * to use as a loop cursor for each entry
185*4882a593Smuzhiyun * @member: the name of the hlist_node within the struct
186*4882a593Smuzhiyun * @key: the key of the objects to iterate over
187*4882a593Smuzhiyun *
188*4882a593Smuzhiyun * This is the same as hash_for_each_possible_rcu() except that it does
189*4882a593Smuzhiyun * not do any RCU debugging or tracing.
190*4882a593Smuzhiyun */
191*4882a593Smuzhiyun #define hash_for_each_possible_rcu_notrace(name, obj, member, key) \
192*4882a593Smuzhiyun hlist_for_each_entry_rcu_notrace(obj, \
193*4882a593Smuzhiyun &name[hash_min(key, HASH_BITS(name))], member)
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /**
196*4882a593Smuzhiyun * hash_for_each_possible_safe - iterate over all possible objects hashing to the
197*4882a593Smuzhiyun * same bucket safe against removals
198*4882a593Smuzhiyun * @name: hashtable to iterate
199*4882a593Smuzhiyun * @obj: the type * to use as a loop cursor for each entry
200*4882a593Smuzhiyun * @tmp: a &struct hlist_node used for temporary storage
201*4882a593Smuzhiyun * @member: the name of the hlist_node within the struct
202*4882a593Smuzhiyun * @key: the key of the objects to iterate over
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun #define hash_for_each_possible_safe(name, obj, tmp, member, key) \
205*4882a593Smuzhiyun hlist_for_each_entry_safe(obj, tmp,\
206*4882a593Smuzhiyun &name[hash_min(key, HASH_BITS(name))], member)
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun #endif
210