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