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