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