1*33b1fccfSAndroid Build Coastguard Worker /* SPDX-License-Identifier: GPL-2.0 */
2*33b1fccfSAndroid Build Coastguard Worker /*
3*33b1fccfSAndroid Build Coastguard Worker * Original code taken from 'linux/include/linux/hash{,table}.h'
4*33b1fccfSAndroid Build Coastguard Worker */
5*33b1fccfSAndroid Build Coastguard Worker #ifndef __EROFS_HASHTABLE_H
6*33b1fccfSAndroid Build Coastguard Worker #define __EROFS_HASHTABLE_H
7*33b1fccfSAndroid Build Coastguard Worker
8*33b1fccfSAndroid Build Coastguard Worker #ifdef __cplusplus
9*33b1fccfSAndroid Build Coastguard Worker extern "C"
10*33b1fccfSAndroid Build Coastguard Worker {
11*33b1fccfSAndroid Build Coastguard Worker #endif
12*33b1fccfSAndroid Build Coastguard Worker
13*33b1fccfSAndroid Build Coastguard Worker /*
14*33b1fccfSAndroid Build Coastguard Worker * Fast hashing routine for ints, longs and pointers.
15*33b1fccfSAndroid Build Coastguard Worker * (C) 2002 Nadia Yvette Chambers, IBM
16*33b1fccfSAndroid Build Coastguard Worker */
17*33b1fccfSAndroid Build Coastguard Worker
18*33b1fccfSAndroid Build Coastguard Worker /*
19*33b1fccfSAndroid Build Coastguard Worker * Statically sized hash table implementation
20*33b1fccfSAndroid Build Coastguard Worker * (C) 2012 Sasha Levin <[email protected]>
21*33b1fccfSAndroid Build Coastguard Worker */
22*33b1fccfSAndroid Build Coastguard Worker
23*33b1fccfSAndroid Build Coastguard Worker #include "defs.h"
24*33b1fccfSAndroid Build Coastguard Worker
25*33b1fccfSAndroid Build Coastguard Worker /*
26*33b1fccfSAndroid Build Coastguard Worker * The "GOLDEN_RATIO_PRIME" is used in ifs/btrfs/brtfs_inode.h and
27*33b1fccfSAndroid Build Coastguard Worker * fs/inode.c. It's not actually prime any more (the previous primes
28*33b1fccfSAndroid Build Coastguard Worker * were actively bad for hashing), but the name remains.
29*33b1fccfSAndroid Build Coastguard Worker */
30*33b1fccfSAndroid Build Coastguard Worker #if BITS_PER_LONG == 32
31*33b1fccfSAndroid Build Coastguard Worker #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_32
32*33b1fccfSAndroid Build Coastguard Worker #define hash_long(val, bits) hash_32(val, bits)
33*33b1fccfSAndroid Build Coastguard Worker #elif BITS_PER_LONG == 64
34*33b1fccfSAndroid Build Coastguard Worker #define hash_long(val, bits) hash_64(val, bits)
35*33b1fccfSAndroid Build Coastguard Worker #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_64
36*33b1fccfSAndroid Build Coastguard Worker #else
37*33b1fccfSAndroid Build Coastguard Worker #error Wordsize not 32 or 64
38*33b1fccfSAndroid Build Coastguard Worker #endif
39*33b1fccfSAndroid Build Coastguard Worker
40*33b1fccfSAndroid Build Coastguard Worker /*
41*33b1fccfSAndroid Build Coastguard Worker * This hash multiplies the input by a large odd number and takes the
42*33b1fccfSAndroid Build Coastguard Worker * high bits. Since multiplication propagates changes to the most
43*33b1fccfSAndroid Build Coastguard Worker * significant end only, it is essential that the high bits of the
44*33b1fccfSAndroid Build Coastguard Worker * product be used for the hash value.
45*33b1fccfSAndroid Build Coastguard Worker *
46*33b1fccfSAndroid Build Coastguard Worker * Chuck Lever verified the effectiveness of this technique:
47*33b1fccfSAndroid Build Coastguard Worker * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
48*33b1fccfSAndroid Build Coastguard Worker *
49*33b1fccfSAndroid Build Coastguard Worker * Although a random odd number will do, it turns out that the golden
50*33b1fccfSAndroid Build Coastguard Worker * ratio phi = (sqrt(5)-1)/2, or its negative, has particularly nice
51*33b1fccfSAndroid Build Coastguard Worker * properties. (See Knuth vol 3, section 6.4, exercise 9.)
52*33b1fccfSAndroid Build Coastguard Worker *
53*33b1fccfSAndroid Build Coastguard Worker * These are the negative, (1 - phi) = phi**2 = (3 - sqrt(5))/2,
54*33b1fccfSAndroid Build Coastguard Worker * which is very slightly easier to multiply by and makes no
55*33b1fccfSAndroid Build Coastguard Worker * difference to the hash distribution.
56*33b1fccfSAndroid Build Coastguard Worker */
57*33b1fccfSAndroid Build Coastguard Worker #define GOLDEN_RATIO_32 0x61C88647
58*33b1fccfSAndroid Build Coastguard Worker #define GOLDEN_RATIO_64 0x61C8864680B583EBull
59*33b1fccfSAndroid Build Coastguard Worker
60*33b1fccfSAndroid Build Coastguard Worker struct hlist_head {
61*33b1fccfSAndroid Build Coastguard Worker struct hlist_node *first;
62*33b1fccfSAndroid Build Coastguard Worker };
63*33b1fccfSAndroid Build Coastguard Worker
64*33b1fccfSAndroid Build Coastguard Worker struct hlist_node {
65*33b1fccfSAndroid Build Coastguard Worker struct hlist_node *next, **pprev;
66*33b1fccfSAndroid Build Coastguard Worker };
67*33b1fccfSAndroid Build Coastguard Worker
68*33b1fccfSAndroid Build Coastguard Worker /*
69*33b1fccfSAndroid Build Coastguard Worker * Architectures might want to move the poison pointer offset
70*33b1fccfSAndroid Build Coastguard Worker * into some well-recognized area such as 0xdead000000000000,
71*33b1fccfSAndroid Build Coastguard Worker * that is also not mappable by user-space exploits:
72*33b1fccfSAndroid Build Coastguard Worker */
73*33b1fccfSAndroid Build Coastguard Worker #ifdef CONFIG_ILLEGAL_POINTER_VALUE
74*33b1fccfSAndroid Build Coastguard Worker # define POISON_POINTER_DELTA _AC(CONFIG_ILLEGAL_POINTER_VALUE, UL)
75*33b1fccfSAndroid Build Coastguard Worker #else
76*33b1fccfSAndroid Build Coastguard Worker # define POISON_POINTER_DELTA 0
77*33b1fccfSAndroid Build Coastguard Worker #endif
78*33b1fccfSAndroid Build Coastguard Worker
79*33b1fccfSAndroid Build Coastguard Worker /*
80*33b1fccfSAndroid Build Coastguard Worker * These are non-NULL pointers that will result in page faults
81*33b1fccfSAndroid Build Coastguard Worker * under normal circumstances, used to verify that nobody uses
82*33b1fccfSAndroid Build Coastguard Worker * non-initialized list entries.
83*33b1fccfSAndroid Build Coastguard Worker */
84*33b1fccfSAndroid Build Coastguard Worker #define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
85*33b1fccfSAndroid Build Coastguard Worker #define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
86*33b1fccfSAndroid Build Coastguard Worker
87*33b1fccfSAndroid Build Coastguard Worker /*
88*33b1fccfSAndroid Build Coastguard Worker * Double linked lists with a single pointer list head.
89*33b1fccfSAndroid Build Coastguard Worker * Mostly useful for hash tables where the two pointer list head is
90*33b1fccfSAndroid Build Coastguard Worker * too wasteful.
91*33b1fccfSAndroid Build Coastguard Worker * You lose the ability to access the tail in O(1).
92*33b1fccfSAndroid Build Coastguard Worker */
93*33b1fccfSAndroid Build Coastguard Worker
94*33b1fccfSAndroid Build Coastguard Worker #define HLIST_HEAD_INIT { .first = NULL }
95*33b1fccfSAndroid Build Coastguard Worker #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
96*33b1fccfSAndroid Build Coastguard Worker #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
INIT_HLIST_NODE(struct hlist_node * h)97*33b1fccfSAndroid Build Coastguard Worker static inline void INIT_HLIST_NODE(struct hlist_node *h)
98*33b1fccfSAndroid Build Coastguard Worker {
99*33b1fccfSAndroid Build Coastguard Worker h->next = NULL;
100*33b1fccfSAndroid Build Coastguard Worker h->pprev = NULL;
101*33b1fccfSAndroid Build Coastguard Worker }
102*33b1fccfSAndroid Build Coastguard Worker
hlist_unhashed(const struct hlist_node * h)103*33b1fccfSAndroid Build Coastguard Worker static inline int hlist_unhashed(const struct hlist_node *h)
104*33b1fccfSAndroid Build Coastguard Worker {
105*33b1fccfSAndroid Build Coastguard Worker return !h->pprev;
106*33b1fccfSAndroid Build Coastguard Worker }
107*33b1fccfSAndroid Build Coastguard Worker
hlist_empty(const struct hlist_head * h)108*33b1fccfSAndroid Build Coastguard Worker static inline int hlist_empty(const struct hlist_head *h)
109*33b1fccfSAndroid Build Coastguard Worker {
110*33b1fccfSAndroid Build Coastguard Worker return !h->first;
111*33b1fccfSAndroid Build Coastguard Worker }
112*33b1fccfSAndroid Build Coastguard Worker
__hlist_del(struct hlist_node * n)113*33b1fccfSAndroid Build Coastguard Worker static inline void __hlist_del(struct hlist_node *n)
114*33b1fccfSAndroid Build Coastguard Worker {
115*33b1fccfSAndroid Build Coastguard Worker struct hlist_node *next = n->next;
116*33b1fccfSAndroid Build Coastguard Worker struct hlist_node **pprev = n->pprev;
117*33b1fccfSAndroid Build Coastguard Worker
118*33b1fccfSAndroid Build Coastguard Worker *pprev = next;
119*33b1fccfSAndroid Build Coastguard Worker if (next)
120*33b1fccfSAndroid Build Coastguard Worker next->pprev = pprev;
121*33b1fccfSAndroid Build Coastguard Worker }
122*33b1fccfSAndroid Build Coastguard Worker
hlist_del(struct hlist_node * n)123*33b1fccfSAndroid Build Coastguard Worker static inline void hlist_del(struct hlist_node *n)
124*33b1fccfSAndroid Build Coastguard Worker {
125*33b1fccfSAndroid Build Coastguard Worker __hlist_del(n);
126*33b1fccfSAndroid Build Coastguard Worker n->next = LIST_POISON1;
127*33b1fccfSAndroid Build Coastguard Worker n->pprev = LIST_POISON2;
128*33b1fccfSAndroid Build Coastguard Worker }
129*33b1fccfSAndroid Build Coastguard Worker
hlist_del_init(struct hlist_node * n)130*33b1fccfSAndroid Build Coastguard Worker static inline void hlist_del_init(struct hlist_node *n)
131*33b1fccfSAndroid Build Coastguard Worker {
132*33b1fccfSAndroid Build Coastguard Worker if (!hlist_unhashed(n)) {
133*33b1fccfSAndroid Build Coastguard Worker __hlist_del(n);
134*33b1fccfSAndroid Build Coastguard Worker INIT_HLIST_NODE(n);
135*33b1fccfSAndroid Build Coastguard Worker }
136*33b1fccfSAndroid Build Coastguard Worker }
137*33b1fccfSAndroid Build Coastguard Worker
hlist_add_head(struct hlist_node * n,struct hlist_head * h)138*33b1fccfSAndroid Build Coastguard Worker static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
139*33b1fccfSAndroid Build Coastguard Worker {
140*33b1fccfSAndroid Build Coastguard Worker struct hlist_node *first = h->first;
141*33b1fccfSAndroid Build Coastguard Worker
142*33b1fccfSAndroid Build Coastguard Worker n->next = first;
143*33b1fccfSAndroid Build Coastguard Worker if (first)
144*33b1fccfSAndroid Build Coastguard Worker first->pprev = &n->next;
145*33b1fccfSAndroid Build Coastguard Worker h->first = n;
146*33b1fccfSAndroid Build Coastguard Worker n->pprev = &h->first;
147*33b1fccfSAndroid Build Coastguard Worker }
148*33b1fccfSAndroid Build Coastguard Worker
149*33b1fccfSAndroid Build Coastguard Worker /* next must be != NULL */
hlist_add_before(struct hlist_node * n,struct hlist_node * next)150*33b1fccfSAndroid Build Coastguard Worker static inline void hlist_add_before(struct hlist_node *n,
151*33b1fccfSAndroid Build Coastguard Worker struct hlist_node *next)
152*33b1fccfSAndroid Build Coastguard Worker {
153*33b1fccfSAndroid Build Coastguard Worker n->pprev = next->pprev;
154*33b1fccfSAndroid Build Coastguard Worker n->next = next;
155*33b1fccfSAndroid Build Coastguard Worker next->pprev = &n->next;
156*33b1fccfSAndroid Build Coastguard Worker *(n->pprev) = n;
157*33b1fccfSAndroid Build Coastguard Worker }
158*33b1fccfSAndroid Build Coastguard Worker
hlist_add_behind(struct hlist_node * n,struct hlist_node * prev)159*33b1fccfSAndroid Build Coastguard Worker static inline void hlist_add_behind(struct hlist_node *n,
160*33b1fccfSAndroid Build Coastguard Worker struct hlist_node *prev)
161*33b1fccfSAndroid Build Coastguard Worker {
162*33b1fccfSAndroid Build Coastguard Worker n->next = prev->next;
163*33b1fccfSAndroid Build Coastguard Worker prev->next = n;
164*33b1fccfSAndroid Build Coastguard Worker n->pprev = &prev->next;
165*33b1fccfSAndroid Build Coastguard Worker
166*33b1fccfSAndroid Build Coastguard Worker if (n->next)
167*33b1fccfSAndroid Build Coastguard Worker n->next->pprev = &n->next;
168*33b1fccfSAndroid Build Coastguard Worker }
169*33b1fccfSAndroid Build Coastguard Worker
170*33b1fccfSAndroid Build Coastguard Worker /* after that we'll appear to be on some hlist and hlist_del will work */
hlist_add_fake(struct hlist_node * n)171*33b1fccfSAndroid Build Coastguard Worker static inline void hlist_add_fake(struct hlist_node *n)
172*33b1fccfSAndroid Build Coastguard Worker {
173*33b1fccfSAndroid Build Coastguard Worker n->pprev = &n->next;
174*33b1fccfSAndroid Build Coastguard Worker }
175*33b1fccfSAndroid Build Coastguard Worker
176*33b1fccfSAndroid Build Coastguard Worker /*
177*33b1fccfSAndroid Build Coastguard Worker * Move a list from one list head to another. Fixup the pprev
178*33b1fccfSAndroid Build Coastguard Worker * reference of the first entry if it exists.
179*33b1fccfSAndroid Build Coastguard Worker */
hlist_move_list(struct hlist_head * old,struct hlist_head * new)180*33b1fccfSAndroid Build Coastguard Worker static inline void hlist_move_list(struct hlist_head *old,
181*33b1fccfSAndroid Build Coastguard Worker struct hlist_head *new)
182*33b1fccfSAndroid Build Coastguard Worker {
183*33b1fccfSAndroid Build Coastguard Worker new->first = old->first;
184*33b1fccfSAndroid Build Coastguard Worker if (new->first)
185*33b1fccfSAndroid Build Coastguard Worker new->first->pprev = &new->first;
186*33b1fccfSAndroid Build Coastguard Worker old->first = NULL;
187*33b1fccfSAndroid Build Coastguard Worker }
188*33b1fccfSAndroid Build Coastguard Worker
189*33b1fccfSAndroid Build Coastguard Worker #define hlist_entry(ptr, type, member) container_of(ptr, type, member)
190*33b1fccfSAndroid Build Coastguard Worker
191*33b1fccfSAndroid Build Coastguard Worker #define hlist_for_each(pos, head) \
192*33b1fccfSAndroid Build Coastguard Worker for (pos = (head)->first; pos; pos = pos->next)
193*33b1fccfSAndroid Build Coastguard Worker
194*33b1fccfSAndroid Build Coastguard Worker #define hlist_for_each_safe(pos, n, head) \
195*33b1fccfSAndroid Build Coastguard Worker for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
196*33b1fccfSAndroid Build Coastguard Worker pos = n)
197*33b1fccfSAndroid Build Coastguard Worker
198*33b1fccfSAndroid Build Coastguard Worker #define hlist_entry_safe(ptr, type, member) \
199*33b1fccfSAndroid Build Coastguard Worker ({ typeof(ptr) ____ptr = (ptr); \
200*33b1fccfSAndroid Build Coastguard Worker ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
201*33b1fccfSAndroid Build Coastguard Worker })
202*33b1fccfSAndroid Build Coastguard Worker
203*33b1fccfSAndroid Build Coastguard Worker /**
204*33b1fccfSAndroid Build Coastguard Worker * hlist_for_each_entry - iterate over list of given type
205*33b1fccfSAndroid Build Coastguard Worker * @pos:the type * to use as a loop cursor.
206*33b1fccfSAndroid Build Coastguard Worker * @head:the head for your list.
207*33b1fccfSAndroid Build Coastguard Worker * @member:the name of the hlist_node within the struct.
208*33b1fccfSAndroid Build Coastguard Worker */
209*33b1fccfSAndroid Build Coastguard Worker #define hlist_for_each_entry(pos, head, member) \
210*33b1fccfSAndroid Build Coastguard Worker for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
211*33b1fccfSAndroid Build Coastguard Worker pos; \
212*33b1fccfSAndroid Build Coastguard Worker pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
213*33b1fccfSAndroid Build Coastguard Worker
214*33b1fccfSAndroid Build Coastguard Worker /**
215*33b1fccfSAndroid Build Coastguard Worker * hlist_for_each_entry_continue
216*33b1fccfSAndroid Build Coastguard Worker * iterate over a hlist continuing after current point
217*33b1fccfSAndroid Build Coastguard Worker * @pos:the type * to use as a loop cursor.
218*33b1fccfSAndroid Build Coastguard Worker * @member:the name of the hlist_node within the struct.
219*33b1fccfSAndroid Build Coastguard Worker */
220*33b1fccfSAndroid Build Coastguard Worker #define hlist_for_each_entry_continue(pos, member) \
221*33b1fccfSAndroid Build Coastguard Worker for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
222*33b1fccfSAndroid Build Coastguard Worker pos; \
223*33b1fccfSAndroid Build Coastguard Worker pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
224*33b1fccfSAndroid Build Coastguard Worker
225*33b1fccfSAndroid Build Coastguard Worker /**
226*33b1fccfSAndroid Build Coastguard Worker * hlist_for_each_entry_from
227*33b1fccfSAndroid Build Coastguard Worker * iterate over a hlist continuing from current point
228*33b1fccfSAndroid Build Coastguard Worker * @pos: the type * to use as a loop cursor.
229*33b1fccfSAndroid Build Coastguard Worker * @member: the name of the hlist_node within the struct.
230*33b1fccfSAndroid Build Coastguard Worker */
231*33b1fccfSAndroid Build Coastguard Worker #define hlist_for_each_entry_from(pos, member) \
232*33b1fccfSAndroid Build Coastguard Worker for (; pos; \
233*33b1fccfSAndroid Build Coastguard Worker pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
234*33b1fccfSAndroid Build Coastguard Worker
235*33b1fccfSAndroid Build Coastguard Worker /**
236*33b1fccfSAndroid Build Coastguard Worker * hlist_for_each_entry_safe
237*33b1fccfSAndroid Build Coastguard Worker * iterate over list of given type safe against removal of list entry
238*33b1fccfSAndroid Build Coastguard Worker * @pos:the type * to use as a loop cursor.
239*33b1fccfSAndroid Build Coastguard Worker * @n:another &struct hlist_node to use as temporary storage
240*33b1fccfSAndroid Build Coastguard Worker * @head:the head for your list.
241*33b1fccfSAndroid Build Coastguard Worker * @member:the name of the hlist_node within the struct.
242*33b1fccfSAndroid Build Coastguard Worker */
243*33b1fccfSAndroid Build Coastguard Worker #define hlist_for_each_entry_safe(pos, n, head, member) \
244*33b1fccfSAndroid Build Coastguard Worker for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
245*33b1fccfSAndroid Build Coastguard Worker pos && ({ n = pos->member.next; 1; }); \
246*33b1fccfSAndroid Build Coastguard Worker pos = hlist_entry_safe(n, typeof(*pos), member))
247*33b1fccfSAndroid Build Coastguard Worker
__hash_32(u32 val)248*33b1fccfSAndroid Build Coastguard Worker static inline u32 __hash_32(u32 val)
249*33b1fccfSAndroid Build Coastguard Worker {
250*33b1fccfSAndroid Build Coastguard Worker return val * GOLDEN_RATIO_32;
251*33b1fccfSAndroid Build Coastguard Worker }
252*33b1fccfSAndroid Build Coastguard Worker
hash_32(u32 val,unsigned int bits)253*33b1fccfSAndroid Build Coastguard Worker static inline u32 hash_32(u32 val, unsigned int bits)
254*33b1fccfSAndroid Build Coastguard Worker {
255*33b1fccfSAndroid Build Coastguard Worker /* High bits are more random, so use them. */
256*33b1fccfSAndroid Build Coastguard Worker return __hash_32(val) >> (32 - bits);
257*33b1fccfSAndroid Build Coastguard Worker }
258*33b1fccfSAndroid Build Coastguard Worker
hash_64(u64 val,unsigned int bits)259*33b1fccfSAndroid Build Coastguard Worker static inline u32 hash_64(u64 val, unsigned int bits)
260*33b1fccfSAndroid Build Coastguard Worker {
261*33b1fccfSAndroid Build Coastguard Worker #if BITS_PER_LONG == 64
262*33b1fccfSAndroid Build Coastguard Worker /* 64x64-bit multiply is efficient on all 64-bit processors */
263*33b1fccfSAndroid Build Coastguard Worker return val * GOLDEN_RATIO_64 >> (64 - bits);
264*33b1fccfSAndroid Build Coastguard Worker #else
265*33b1fccfSAndroid Build Coastguard Worker /* Hash 64 bits using only 32x32-bit multiply. */
266*33b1fccfSAndroid Build Coastguard Worker return hash_32((u32)val ^ __hash_32(val >> 32), bits);
267*33b1fccfSAndroid Build Coastguard Worker #endif
268*33b1fccfSAndroid Build Coastguard Worker }
269*33b1fccfSAndroid Build Coastguard Worker
270*33b1fccfSAndroid Build Coastguard Worker #define DEFINE_HASHTABLE(name, bits) \
271*33b1fccfSAndroid Build Coastguard Worker struct hlist_head name[1 << (bits)] = \
272*33b1fccfSAndroid Build Coastguard Worker { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
273*33b1fccfSAndroid Build Coastguard Worker
274*33b1fccfSAndroid Build Coastguard Worker #define DECLARE_HASHTABLE(name, bits) \
275*33b1fccfSAndroid Build Coastguard Worker struct hlist_head name[1 << (bits)]
276*33b1fccfSAndroid Build Coastguard Worker
277*33b1fccfSAndroid Build Coastguard Worker #define HASH_SIZE(name) (ARRAY_SIZE(name))
278*33b1fccfSAndroid Build Coastguard Worker #define HASH_BITS(name) ilog2(HASH_SIZE(name))
279*33b1fccfSAndroid Build Coastguard Worker
280*33b1fccfSAndroid Build Coastguard Worker /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels*/
281*33b1fccfSAndroid Build Coastguard Worker #define hash_min(val, bits) \
282*33b1fccfSAndroid Build Coastguard Worker (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
283*33b1fccfSAndroid Build Coastguard Worker
__hash_init(struct hlist_head * ht,unsigned int sz)284*33b1fccfSAndroid Build Coastguard Worker static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
285*33b1fccfSAndroid Build Coastguard Worker {
286*33b1fccfSAndroid Build Coastguard Worker unsigned int i;
287*33b1fccfSAndroid Build Coastguard Worker
288*33b1fccfSAndroid Build Coastguard Worker for (i = 0; i < sz; i++)
289*33b1fccfSAndroid Build Coastguard Worker INIT_HLIST_HEAD(&ht[i]);
290*33b1fccfSAndroid Build Coastguard Worker }
291*33b1fccfSAndroid Build Coastguard Worker
292*33b1fccfSAndroid Build Coastguard Worker /**
293*33b1fccfSAndroid Build Coastguard Worker * hash_init - initialize a hash table
294*33b1fccfSAndroid Build Coastguard Worker * @hashtable: hashtable to be initialized
295*33b1fccfSAndroid Build Coastguard Worker *
296*33b1fccfSAndroid Build Coastguard Worker * Calculates the size of the hashtable from the given parameter, otherwise
297*33b1fccfSAndroid Build Coastguard Worker * same as hash_init_size.
298*33b1fccfSAndroid Build Coastguard Worker *
299*33b1fccfSAndroid Build Coastguard Worker * This has to be a macro since HASH_BITS() will not work on pointers since
300*33b1fccfSAndroid Build Coastguard Worker * it calculates the size during preprocessing.
301*33b1fccfSAndroid Build Coastguard Worker */
302*33b1fccfSAndroid Build Coastguard Worker #define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
303*33b1fccfSAndroid Build Coastguard Worker
304*33b1fccfSAndroid Build Coastguard Worker /**
305*33b1fccfSAndroid Build Coastguard Worker * hash_add - add an object to a hashtable
306*33b1fccfSAndroid Build Coastguard Worker * @hashtable: hashtable to add to
307*33b1fccfSAndroid Build Coastguard Worker * @node: the &struct hlist_node of the object to be added
308*33b1fccfSAndroid Build Coastguard Worker * @key: the key of the object to be added
309*33b1fccfSAndroid Build Coastguard Worker */
310*33b1fccfSAndroid Build Coastguard Worker #define hash_add(hashtable, node, key) \
311*33b1fccfSAndroid Build Coastguard Worker hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
312*33b1fccfSAndroid Build Coastguard Worker
313*33b1fccfSAndroid Build Coastguard Worker /**
314*33b1fccfSAndroid Build Coastguard Worker * hash_hashed - check whether an object is in any hashtable
315*33b1fccfSAndroid Build Coastguard Worker * @node: the &struct hlist_node of the object to be checked
316*33b1fccfSAndroid Build Coastguard Worker */
hash_hashed(struct hlist_node * node)317*33b1fccfSAndroid Build Coastguard Worker static inline bool hash_hashed(struct hlist_node *node)
318*33b1fccfSAndroid Build Coastguard Worker {
319*33b1fccfSAndroid Build Coastguard Worker return !hlist_unhashed(node);
320*33b1fccfSAndroid Build Coastguard Worker }
321*33b1fccfSAndroid Build Coastguard Worker
__hash_empty(struct hlist_head * ht,unsigned int sz)322*33b1fccfSAndroid Build Coastguard Worker static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
323*33b1fccfSAndroid Build Coastguard Worker {
324*33b1fccfSAndroid Build Coastguard Worker unsigned int i;
325*33b1fccfSAndroid Build Coastguard Worker
326*33b1fccfSAndroid Build Coastguard Worker for (i = 0; i < sz; i++)
327*33b1fccfSAndroid Build Coastguard Worker if (!hlist_empty(&ht[i]))
328*33b1fccfSAndroid Build Coastguard Worker return false;
329*33b1fccfSAndroid Build Coastguard Worker
330*33b1fccfSAndroid Build Coastguard Worker return true;
331*33b1fccfSAndroid Build Coastguard Worker }
332*33b1fccfSAndroid Build Coastguard Worker
333*33b1fccfSAndroid Build Coastguard Worker /**
334*33b1fccfSAndroid Build Coastguard Worker * hash_empty - check whether a hashtable is empty
335*33b1fccfSAndroid Build Coastguard Worker * @hashtable: hashtable to check
336*33b1fccfSAndroid Build Coastguard Worker *
337*33b1fccfSAndroid Build Coastguard Worker * This has to be a macro since HASH_BITS() will not work on pointers since
338*33b1fccfSAndroid Build Coastguard Worker * it calculates the size during preprocessing.
339*33b1fccfSAndroid Build Coastguard Worker */
340*33b1fccfSAndroid Build Coastguard Worker #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
341*33b1fccfSAndroid Build Coastguard Worker
342*33b1fccfSAndroid Build Coastguard Worker /**
343*33b1fccfSAndroid Build Coastguard Worker * hash_del - remove an object from a hashtable
344*33b1fccfSAndroid Build Coastguard Worker * @node: &struct hlist_node of the object to remove
345*33b1fccfSAndroid Build Coastguard Worker */
hash_del(struct hlist_node * node)346*33b1fccfSAndroid Build Coastguard Worker static inline void hash_del(struct hlist_node *node)
347*33b1fccfSAndroid Build Coastguard Worker {
348*33b1fccfSAndroid Build Coastguard Worker hlist_del_init(node);
349*33b1fccfSAndroid Build Coastguard Worker }
350*33b1fccfSAndroid Build Coastguard Worker
351*33b1fccfSAndroid Build Coastguard Worker /**
352*33b1fccfSAndroid Build Coastguard Worker * hash_for_each - iterate over a hashtable
353*33b1fccfSAndroid Build Coastguard Worker * @name: hashtable to iterate
354*33b1fccfSAndroid Build Coastguard Worker * @bkt: integer to use as bucket loop cursor
355*33b1fccfSAndroid Build Coastguard Worker * @obj: the type * to use as a loop cursor for each entry
356*33b1fccfSAndroid Build Coastguard Worker * @member: the name of the hlist_node within the struct
357*33b1fccfSAndroid Build Coastguard Worker */
358*33b1fccfSAndroid Build Coastguard Worker #define hash_for_each(name, bkt, obj, member) \
359*33b1fccfSAndroid Build Coastguard Worker for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
360*33b1fccfSAndroid Build Coastguard Worker (bkt)++)\
361*33b1fccfSAndroid Build Coastguard Worker hlist_for_each_entry(obj, &name[bkt], member)
362*33b1fccfSAndroid Build Coastguard Worker
363*33b1fccfSAndroid Build Coastguard Worker /**
364*33b1fccfSAndroid Build Coastguard Worker * hash_for_each_safe - iterate over a hashtable safe against removal of
365*33b1fccfSAndroid Build Coastguard Worker * hash entry
366*33b1fccfSAndroid Build Coastguard Worker * @name: hashtable to iterate
367*33b1fccfSAndroid Build Coastguard Worker * @bkt: integer to use as bucket loop cursor
368*33b1fccfSAndroid Build Coastguard Worker * @tmp: a &struct used for temporary storage
369*33b1fccfSAndroid Build Coastguard Worker * @obj: the type * to use as a loop cursor for each entry
370*33b1fccfSAndroid Build Coastguard Worker * @member: the name of the hlist_node within the struct
371*33b1fccfSAndroid Build Coastguard Worker */
372*33b1fccfSAndroid Build Coastguard Worker #define hash_for_each_safe(name, bkt, tmp, obj, member) \
373*33b1fccfSAndroid Build Coastguard Worker for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
374*33b1fccfSAndroid Build Coastguard Worker (bkt)++)\
375*33b1fccfSAndroid Build Coastguard Worker hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
376*33b1fccfSAndroid Build Coastguard Worker
377*33b1fccfSAndroid Build Coastguard Worker /**
378*33b1fccfSAndroid Build Coastguard Worker * hash_for_each_possible - iterate over all possible objects hashing to the
379*33b1fccfSAndroid Build Coastguard Worker * same bucket
380*33b1fccfSAndroid Build Coastguard Worker * @name: hashtable to iterate
381*33b1fccfSAndroid Build Coastguard Worker * @obj: the type * to use as a loop cursor for each entry
382*33b1fccfSAndroid Build Coastguard Worker * @member: the name of the hlist_node within the struct
383*33b1fccfSAndroid Build Coastguard Worker * @key: the key of the objects to iterate over
384*33b1fccfSAndroid Build Coastguard Worker */
385*33b1fccfSAndroid Build Coastguard Worker #define hash_for_each_possible(name, obj, member, key) \
386*33b1fccfSAndroid Build Coastguard Worker hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
387*33b1fccfSAndroid Build Coastguard Worker
388*33b1fccfSAndroid Build Coastguard Worker #ifdef __cplusplus
389*33b1fccfSAndroid Build Coastguard Worker }
390*33b1fccfSAndroid Build Coastguard Worker #endif
391*33b1fccfSAndroid Build Coastguard Worker
392*33b1fccfSAndroid Build Coastguard Worker #endif
393