1 /*
2 * Copyright 2021 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef __MPP_HASH_H__
18 #define __MPP_HASH_H__
19
20 #include <stdbool.h>
21
22 #include "rk_type.h"
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #define GOLDEN_RATIO_32 0x61C88647
29 #define GOLDEN_RATIO_64 0x61C8864680B583EBull
30
31 #if __SIZEOF_POINTER__ == 4
32 #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_32
33 #define hash_long(val, bits) hash_32(val, bits)
34 #elif __SIZEOF_POINTER__ == 8
35 #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_64
36 #define hash_long(val, bits) hash_64(val, bits)
37 #else
38 #error __SIZEOF_POINTER__ not 4 or 8
39 #endif
40
41 #define typeof __typeof__
42
43 struct hlist_node {
44 struct hlist_node *next, **pprev;
45 };
46
47 struct hlist_head {
48 struct hlist_node *first;
49 };
50
51 #define HLIST_HEAD_INIT { .first = NULL }
52 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
53 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
54
55 #define LIST_POISON1 ((void *) 0x100)
56 #define LIST_POISON2 ((void *) 0x200)
57
58 #define WRITE_ONCE(var, val) \
59 (*((volatile typeof(val) *)(&(var))) = (val))
60
61 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
62
INIT_HLIST_NODE(struct hlist_node * h)63 static inline void INIT_HLIST_NODE(struct hlist_node *h)
64 {
65 h->next = NULL;
66 h->pprev = NULL;
67 }
68
hlist_unhashed(const struct hlist_node * h)69 static inline int hlist_unhashed(const struct hlist_node *h)
70 {
71 return !h->pprev;
72 }
73
hlist_empty(const struct hlist_head * h)74 static inline int hlist_empty(const struct hlist_head *h)
75 {
76 // return !READ_ONCE(h->first);
77 return !h->first;
78 }
79
__hlist_del(struct hlist_node * n)80 static inline void __hlist_del(struct hlist_node *n)
81 {
82 struct hlist_node *next = n->next;
83 struct hlist_node **pprev = n->pprev;
84
85 WRITE_ONCE(*pprev, next);
86 if (next)
87 next->pprev = pprev;
88 }
89
hlist_del(struct hlist_node * n)90 static inline void hlist_del(struct hlist_node *n)
91 {
92 __hlist_del(n);
93 n->next = (struct hlist_node*)LIST_POISON1;
94 n->pprev = (struct hlist_node**)LIST_POISON2;
95 }
96
hlist_del_init(struct hlist_node * n)97 static inline void hlist_del_init(struct hlist_node *n)
98 {
99 if (!hlist_unhashed(n)) {
100 __hlist_del(n);
101 INIT_HLIST_NODE(n);
102 }
103 }
104
hlist_add_head(struct hlist_node * n,struct hlist_head * h)105 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
106 {
107 struct hlist_node *first = h->first;
108 n->next = first;
109 if (first)
110 first->pprev = &n->next;
111 WRITE_ONCE(h->first, n);
112 n->pprev = &h->first;
113 }
114
hlist_add_before(struct hlist_node * n,struct hlist_node * next)115 static inline void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
116 {
117 n->pprev = next->pprev;
118 n->next = next;
119 next->pprev = &n->next;
120 WRITE_ONCE(*(n->pprev), n);
121 }
122
hlist_add_behind(struct hlist_node * n,struct hlist_node * prev)123 static inline void hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
124 {
125 n->next = prev->next;
126 WRITE_ONCE(prev->next, n);
127 n->pprev = &prev->next;
128
129 if (n->next)
130 n->next->pprev = &n->next;
131 }
132
hlist_add_fake(struct hlist_node * n)133 static inline void hlist_add_fake(struct hlist_node *n)
134 {
135 n->pprev = &n->next;
136 }
137
hlist_fake(struct hlist_node * h)138 static inline int hlist_fake(struct hlist_node *h)
139 {
140 return h->pprev == &h->next;
141 }
142
143 static inline int
hlist_is_singular_node(struct hlist_node * n,struct hlist_head * h)144 hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
145 {
146 return !n->next && n->pprev == &h->first;
147 }
148
hlist_move_list(struct hlist_head * old,struct hlist_head * _new)149 static inline void hlist_move_list(struct hlist_head *old,
150 struct hlist_head *_new)
151 {
152 _new->first = old->first;
153 if (_new->first)
154 _new->first->pprev = &_new->first;
155 old->first = NULL;
156 }
157
158 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
159
160 #define hlist_for_each(pos, head) \
161 for (pos = (head)->first; pos ; pos = pos->next)
162
163 #define hlist_for_each_safe(pos, n, head) \
164 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
165 pos = n)
166
167 #define hlist_entry_safe(ptr, type, member) \
168 ({ typeof(ptr) ____ptr = (ptr); \
169 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
170 })
171
172 #define hlist_for_each_entry(pos, head, member) \
173 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
174 pos; \
175 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
176
177 #define hlist_for_each_entry_continue(pos, member) \
178 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
179 pos; \
180 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
181
182 #define hlist_for_each_entry_from(pos, member) \
183 for (; pos; \
184 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
185
186 #define hlist_for_each_entry_safe(pos, n, head, member) \
187 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
188 pos && ({ n = pos->member.next; 1; }); \
189 pos = hlist_entry_safe(n, typeof(*pos), member))
190
191 #define DEFINE_HASHTABLE(name, bits) \
192 struct hlist_head name[1 << (bits)] = \
193 { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
194
195 #define DECLARE_HASHTABLE(name, bits) \
196 struct hlist_head name[1 << (bits)]
197
198 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
199
200 #define ilog2(n) \
201 ( \
202 (n) & (1ULL << 63) ? 63 : \
203 (n) & (1ULL << 62) ? 62 : \
204 (n) & (1ULL << 61) ? 61 : \
205 (n) & (1ULL << 60) ? 60 : \
206 (n) & (1ULL << 59) ? 59 : \
207 (n) & (1ULL << 58) ? 58 : \
208 (n) & (1ULL << 57) ? 57 : \
209 (n) & (1ULL << 56) ? 56 : \
210 (n) & (1ULL << 55) ? 55 : \
211 (n) & (1ULL << 54) ? 54 : \
212 (n) & (1ULL << 53) ? 53 : \
213 (n) & (1ULL << 52) ? 52 : \
214 (n) & (1ULL << 51) ? 51 : \
215 (n) & (1ULL << 50) ? 50 : \
216 (n) & (1ULL << 49) ? 49 : \
217 (n) & (1ULL << 48) ? 48 : \
218 (n) & (1ULL << 47) ? 47 : \
219 (n) & (1ULL << 46) ? 46 : \
220 (n) & (1ULL << 45) ? 45 : \
221 (n) & (1ULL << 44) ? 44 : \
222 (n) & (1ULL << 43) ? 43 : \
223 (n) & (1ULL << 42) ? 42 : \
224 (n) & (1ULL << 41) ? 41 : \
225 (n) & (1ULL << 40) ? 40 : \
226 (n) & (1ULL << 39) ? 39 : \
227 (n) & (1ULL << 38) ? 38 : \
228 (n) & (1ULL << 37) ? 37 : \
229 (n) & (1ULL << 36) ? 36 : \
230 (n) & (1ULL << 35) ? 35 : \
231 (n) & (1ULL << 34) ? 34 : \
232 (n) & (1ULL << 33) ? 33 : \
233 (n) & (1ULL << 32) ? 32 : \
234 (n) & (1ULL << 31) ? 31 : \
235 (n) & (1ULL << 30) ? 30 : \
236 (n) & (1ULL << 29) ? 29 : \
237 (n) & (1ULL << 28) ? 28 : \
238 (n) & (1ULL << 27) ? 27 : \
239 (n) & (1ULL << 26) ? 26 : \
240 (n) & (1ULL << 25) ? 25 : \
241 (n) & (1ULL << 24) ? 24 : \
242 (n) & (1ULL << 23) ? 23 : \
243 (n) & (1ULL << 22) ? 22 : \
244 (n) & (1ULL << 21) ? 21 : \
245 (n) & (1ULL << 20) ? 20 : \
246 (n) & (1ULL << 19) ? 19 : \
247 (n) & (1ULL << 18) ? 18 : \
248 (n) & (1ULL << 17) ? 17 : \
249 (n) & (1ULL << 16) ? 16 : \
250 (n) & (1ULL << 15) ? 15 : \
251 (n) & (1ULL << 14) ? 14 : \
252 (n) & (1ULL << 13) ? 13 : \
253 (n) & (1ULL << 12) ? 12 : \
254 (n) & (1ULL << 11) ? 11 : \
255 (n) & (1ULL << 10) ? 10 : \
256 (n) & (1ULL << 9) ? 9 : \
257 (n) & (1ULL << 8) ? 8 : \
258 (n) & (1ULL << 7) ? 7 : \
259 (n) & (1ULL << 6) ? 6 : \
260 (n) & (1ULL << 5) ? 5 : \
261 (n) & (1ULL << 4) ? 4 : \
262 (n) & (1ULL << 3) ? 3 : \
263 (n) & (1ULL << 2) ? 2 : \
264 (n) & (1ULL << 1) ? 1 : 0 \
265 )
266
267 #define HASH_SIZE(name) (ARRAY_SIZE(name))
268 #define HASH_BITS(name) ilog2(HASH_SIZE(name))
269
270 /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
271 #define hash_min(val, bits) \
272 (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
273
274 #define hash_add(hashtable, node, key) \
275 hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
276
277 /**
278 * hash_empty - check whether a hashtable is empty
279 * @hashtable: hashtable to check
280 *
281 * This has to be a macro since HASH_BITS() will not work on pointers since
282 * it calculates the size during preprocessing.
283 */
284 #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
285
286 /**
287 * hash_for_each - iterate over a hashtable
288 * @name: hashtable to iterate
289 * @bkt: integer to use as bucket loop cursor
290 * @obj: the type * to use as a loop cursor for each entry
291 * @member: the name of the hlist_node within the struct
292 */
293 #define hash_for_each(name, bkt, obj, member) \
294 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name); \
295 (bkt)++) \
296 hlist_for_each_entry(obj, &name[bkt], member)
297
298 /**
299 * hash_for_each_safe - iterate over a hashtable safe against removal of
300 * hash entry
301 * @name: hashtable to iterate
302 * @bkt: integer to use as bucket loop cursor
303 * @tmp: a &struct used for temporary storage
304 * @obj: the type * to use as a loop cursor for each entry
305 * @member: the name of the hlist_node within the struct
306 */
307 #define hash_for_each_safe(name, bkt, tmp, obj, member) \
308 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name); \
309 (bkt)++) \
310 hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
311
312 #define hash_for_each_possible(name, obj, member, key) \
313 hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
314
hash_32(RK_U32 val,unsigned int bits)315 static inline RK_U32 hash_32(RK_U32 val, unsigned int bits)
316 {
317 /* On some cpus multiply is faster, on others gcc will do shifts */
318 RK_U32 hash = val * GOLDEN_RATIO_32;
319
320 /* High bits are more random, so use them. */
321 return hash >> (32 - bits);
322 }
323
__hash_32(RK_U32 val)324 static inline RK_U32 __hash_32(RK_U32 val)
325 {
326 return val * GOLDEN_RATIO_32;
327 }
328
hash_64(RK_U64 val,unsigned int bits)329 static inline RK_U32 hash_64(RK_U64 val, unsigned int bits)
330 {
331 #if __SIZEOF_POINTER__ == 8
332 /* 64x64-bit multiply is efficient on all 64-bit processors */
333 return val * GOLDEN_RATIO_64 >> (64 - bits);
334 #else
335 /* Hash 64 bits using only 32x32-bit multiply. */
336 return hash_32((RK_U32)val ^ ((val >> 32) * GOLDEN_RATIO_32), bits);
337 #endif
338 }
339
hash_ptr(const void * ptr,unsigned int bits)340 static inline RK_U32 hash_ptr(const void *ptr, unsigned int bits)
341 {
342 return hash_long((unsigned long)ptr, bits);
343 }
344
345 /* This really should be called fold32_ptr; it does no hashing to speak of. */
hash32_ptr(const void * ptr)346 static inline RK_U32 hash32_ptr(const void *ptr)
347 {
348 unsigned long val = (unsigned long)ptr;
349
350 #if __SIZEOF_POINTER__ == 8
351 val ^= (val >> 32);
352 #endif
353 return (RK_U32)val;
354 }
355
356 /**
357 * hash_hashed - check whether an object is in any hashtable
358 * @node: the &struct hlist_node of the object to be checked
359 */
hash_hashed(struct hlist_node * node)360 static inline bool hash_hashed(struct hlist_node *node)
361 {
362 return !hlist_unhashed(node);
363 }
364
__hash_empty(struct hlist_head * ht,unsigned int sz)365 static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
366 {
367 unsigned int i;
368
369 for (i = 0; i < sz; i++)
370 if (!hlist_empty(&ht[i]))
371 return false;
372
373 return true;
374 }
375
376 /**
377 * hash_del - remove an object from a hashtable
378 * @node: &struct hlist_node of the object to remove
379 */
hash_del(struct hlist_node * node)380 static inline void hash_del(struct hlist_node *node)
381 {
382 hlist_del_init(node);
383 }
384
385 #ifdef __cplusplus
386 }
387 #endif
388
389 #endif /*__MPP_HASH_H__*/
390