• 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                                                                                                \
51     {                                                                                                                  \
52         .first = NULL                                                                                                  \
53     }
54 #define HLIST_HEAD(name) struct hlist_head name = {.first = NULL}
55 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
56 
57 #define LIST_POISON1 ((void *)0x100)
58 #define LIST_POISON2 ((void *)0x200)
59 
60 #define WRITE_ONCE(var, val) (*((volatile typeof(val) *)(&(var))) = (val))
61 
62 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
63 
INIT_HLIST_NODE(struct hlist_node * h)64 static inline void INIT_HLIST_NODE(struct hlist_node *h)
65 {
66     h->next = NULL;
67     h->pprev = NULL;
68 }
69 
hlist_unhashed(const struct hlist_node * h)70 static inline int hlist_unhashed(const struct hlist_node *h)
71 {
72     return !h->pprev;
73 }
74 
hlist_empty(const struct hlist_head * h)75 static inline int hlist_empty(const struct hlist_head *h)
76 {
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 }
90 
hlist_del(struct hlist_node * n)91 static inline void hlist_del(struct hlist_node *n)
92 {
93     _hlist_del(n);
94     n->next = (struct hlist_node *)LIST_POISON1;
95     n->pprev = (struct hlist_node **)LIST_POISON2;
96 }
97 
hlist_del_init(struct hlist_node * n)98 static inline void hlist_del_init(struct hlist_node *n)
99 {
100     if (!hlist_unhashed(n)) {
101         _hlist_del(n);
102         INIT_HLIST_NODE(n);
103     }
104 }
105 
hlist_add_head(struct hlist_node * n,struct hlist_head * h)106 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
107 {
108     struct hlist_node *first = h->first;
109     n->next = first;
110     if (first) {
111         first->pprev = &n->next;
112     }
113     WRITE_ONCE(h->first, n);
114     n->pprev = &h->first;
115 }
116 
hlist_add_before(struct hlist_node * n,struct hlist_node * next)117 static inline void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
118 {
119     n->pprev = next->pprev;
120     n->next = next;
121     next->pprev = &n->next;
122     WRITE_ONCE(*(n->pprev), n);
123 }
124 
hlist_add_behind(struct hlist_node * n,struct hlist_node * prev)125 static inline void hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
126 {
127     n->next = prev->next;
128     WRITE_ONCE(prev->next, n);
129     n->pprev = &prev->next;
130     if (n->next) {
131         n->next->pprev = &n->next;
132     }
133 }
134 
hlist_add_fake(struct hlist_node * n)135 static inline void hlist_add_fake(struct hlist_node *n)
136 {
137     n->pprev = &n->next;
138 }
139 
hlist_fake(struct hlist_node * h)140 static inline int hlist_fake(struct hlist_node *h)
141 {
142     return h->pprev == &h->next;
143 }
144 
hlist_is_singular_node(struct hlist_node * n,struct hlist_head * h)145 static inline int hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
146 {
147     return !n->next && n->pprev == &h->first;
148 }
149 
hlist_move_list(struct hlist_head * old,struct hlist_head * _new)150 static inline void hlist_move_list(struct hlist_head *old, 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) for ((pos) = (head)->first; (pos); pos = (pos)->next)
162 
163 #define hlist_for_each_safe(pos, n, head)                                                                              \
164     for ((pos) = (head)->first; (pos) && ( {                                                                           \
165                                     n = (pos)->next;                                                                   \
166                                     1;                                                                                 \
167                                 });                                                                                    \
168          pos = n)
169 
170 #define hlist_entry_safe(ptr, type, member)                                                                            \
171     ( {                                                                                                                \
172         typeof(ptr) ____ptr = (ptr);                                                                                   \
173         ____ptr ? hlist_entry(____ptr, type, member) : NULL;                                                           \
174     })
175 
176 #define hlist_for_each_entry(pos, head, member)                                                                        \
177     do {                                                                                                               \
178         for ((pos) = hlist_entry_safe((head)->first, typeof(*(pos)), member); pos;                                     \
179              pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))                                       \
180     } while (0)
181 
182 #define hlist_for_each_entry_continue(pos, member)                                                                     \
183     do {                                                                                                               \
184         for ((pos) = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); pos;                                \
185              pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))                                       \
186     } while (0)
187 
188 #define hlist_for_each_entry_from(pos, member)                                                                         \
189     do {                                                                                                               \
190         for (; pos; 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); (pos) && ( {                             \
196                                                                                   n = (pos)->member.next;              \
197                                                                                   1;                                   \
198                                                                               });                                      \
199              (pos) = hlist_entry_safe(n, typeof(*(pos)), member))                                                      \
200     } while (0)
201 
202 #define DEFINE_HASHTABLE(name, bits)                                                                                   \
203     struct hlist_head name[1 << (bits)] = {[0 ...((1 << (bits)) - 1)] = HLIST_HEAD_INIT}
204 
205 #define DECLARE_HASHTABLE(name, bits) struct hlist_head name[1 << (bits)]
206 
207 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
208 
209 #define ilog2(n)                                                                                                       \
210     ((n) & (1ULL << 63)   ? 63                                                                                         \
211      : (n) & (1ULL << 62) ? 62                                                                                         \
212      : (n) & (1ULL << 61) ? 61                                                                                         \
213      : (n) & (1ULL << 60) ? 60                                                                                         \
214      : (n) & (1ULL << 59) ? 59                                                                                         \
215      : (n) & (1ULL << 58) ? 58                                                                                         \
216      : (n) & (1ULL << 57) ? 57                                                                                         \
217      : (n) & (1ULL << 56) ? 56                                                                                         \
218      : (n) & (1ULL << 55) ? 55                                                                                         \
219      : (n) & (1ULL << 54) ? 54                                                                                         \
220      : (n) & (1ULL << 53) ? 53                                                                                         \
221      : (n) & (1ULL << 52) ? 52                                                                                         \
222      : (n) & (1ULL << 51) ? 51                                                                                         \
223      : (n) & (1ULL << 50) ? 50                                                                                         \
224      : (n) & (1ULL << 49) ? 49                                                                                         \
225      : (n) & (1ULL << 48) ? 48                                                                                         \
226      : (n) & (1ULL << 47) ? 47                                                                                         \
227      : (n) & (1ULL << 46) ? 46                                                                                         \
228      : (n) & (1ULL << 45) ? 45                                                                                         \
229      : (n) & (1ULL << 44) ? 44                                                                                         \
230      : (n) & (1ULL << 43) ? 43                                                                                         \
231      : (n) & (1ULL << 42) ? 42                                                                                         \
232      : (n) & (1ULL << 41) ? 41                                                                                         \
233      : (n) & (1ULL << 40) ? 40                                                                                         \
234      : (n) & (1ULL << 39) ? 39                                                                                         \
235      : (n) & (1ULL << 38) ? 38                                                                                         \
236      : (n) & (1ULL << 37) ? 37                                                                                         \
237      : (n) & (1ULL << 36) ? 36                                                                                         \
238      : (n) & (1ULL << 35) ? 35                                                                                         \
239      : (n) & (1ULL << 34) ? 34                                                                                         \
240      : (n) & (1ULL << 33) ? 33                                                                                         \
241      : (n) & (1ULL << 32) ? 32                                                                                         \
242      : (n) & (1ULL << 31) ? 31                                                                                         \
243      : (n) & (1ULL << 30) ? 30                                                                                         \
244      : (n) & (1ULL << 29) ? 29                                                                                         \
245      : (n) & (1ULL << 28) ? 28                                                                                         \
246      : (n) & (1ULL << 27) ? 27                                                                                         \
247      : (n) & (1ULL << 26) ? 26                                                                                         \
248      : (n) & (1ULL << 25) ? 25                                                                                         \
249      : (n) & (1ULL << 24) ? 24                                                                                         \
250      : (n) & (1ULL << 23) ? 23                                                                                         \
251      : (n) & (1ULL << 22) ? 22                                                                                         \
252      : (n) & (1ULL << 21) ? 21                                                                                         \
253      : (n) & (1ULL << 20) ? 20                                                                                         \
254      : (n) & (1ULL << 19) ? 19                                                                                         \
255      : (n) & (1ULL << 18) ? 18                                                                                         \
256      : (n) & (1ULL << 17) ? 17                                                                                         \
257      : (n) & (1ULL << 16) ? 16                                                                                         \
258      : (n) & (1ULL << 15) ? 15                                                                                         \
259      : (n) & (1ULL << 14) ? 14                                                                                         \
260      : (n) & (1ULL << 13) ? 13                                                                                         \
261      : (n) & (1ULL << 12) ? 12                                                                                         \
262      : (n) & (1ULL << 11) ? 11                                                                                         \
263      : (n) & (1ULL << 10) ? 10                                                                                         \
264      : (n) & (1ULL << 9)  ? 9                                                                                          \
265      : (n) & (1ULL << 8)  ? 8                                                                                          \
266      : (n) & (1ULL << 7)  ? 7                                                                                          \
267      : (n) & (1ULL << 6)  ? 6                                                                                          \
268      : (n) & (1ULL << 5)  ? 5                                                                                          \
269      : (n) & (1ULL << 4)  ? 4                                                                                          \
270      : (n) & (1ULL << 3)  ? 3                                                                                          \
271      : (n) & (1ULL << 2)  ? 2                                                                                          \
272      : (n) & (1ULL << 1)  ? 1                                                                                          \
273                           : 0)
274 
275 #define HASH_SIZE(name) (ARRAY_SIZE(name))
276 #define HASH_BITS(name) ilog2(HASH_SIZE(name))
277 
278 /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
279 #define hash_min(val, bits) (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
280 
281 #define hash_add(hashtable, node, key) hlist_add_head(node, &(hashtable)[hash_min(key, HASH_BITS(hashtable))])
282 
283 /**
284  * hash_empty - check whether a hashtable is empty
285  * @hashtable: hashtable to check
286  *
287  * This has to be a macro since HASH_BITS() will not work on pointers since
288  * it calculates the size during preprocessing.
289  */
290 #define hash_empty(hashtable) _hash_empty(hashtable, HASH_SIZE(hashtable))
291 
292 /**
293  * hash_for_each - iterate over a hashtable
294  * @name: hashtable to iterate
295  * @bkt: integer to use as bucket loop cursor
296  * @obj: the type * to use as a loop cursor for each entry
297  * @member: the name of the hlist_node within the struct
298  */
299 #define hash_for_each(name, bkt, obj, member)                                                                          \
300     for ((bkt) = 0, (obj) = NULL; (obj) == NULL && (bkt) < HASH_SIZE(name); (bkt)++)                                   \
301     hlist_for_each_entry((obj), &(name)[bkt], member)
302 
303 /**
304  * hash_for_each_safe - iterate over a hashtable safe against removal of
305  * hash entry
306  * @name: hashtable to iterate
307  * @bkt: integer to use as bucket loop cursor
308  * @tmp: a &struct used for temporary storage
309  * @obj: the type * to use as a loop cursor for each entry
310  * @member: the name of the hlist_node within the struct
311  */
312 #define hash_for_each_safe(name, bkt, tmp, obj, member)                                                                \
313     for ((bkt) = 0, (obj) = NULL; (obj) == NULL && (bkt) < HASH_SIZE(name); (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(unsigned int val,unsigned int bits)319 static inline unsigned int hash_32(unsigned int val, unsigned int bits)
320 {
321     /* On some cpus multiply is faster, on others gcc will do shifts */
322     unsigned int hash = val * GOLDEN_RATIO_32;
323 
324     /* High bits are more random, so use them. */
325     return hash >> (32 - bits); // (32 - bits)
326 }
327 
_hash_32(unsigned int val)328 static inline unsigned int _hash_32(unsigned int val)
329 {
330     return val * GOLDEN_RATIO_32;
331 }
332 
hash_64(RK_U64 val,unsigned int bits)333 static inline unsigned int hash_64(RK_U64 val, unsigned int bits)
334 {
335 #if __SIZEOF_POINTER__ == 8
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((unsigned int)val ^ ((val >> 32) * GOLDEN_RATIO_32), bits);
341 #endif
342 }
343 
hash_ptr(const void * ptr,unsigned int bits)344 static inline unsigned int 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 unsigned int hash32_ptr(const void *ptr)
351 {
352     unsigned long val = (unsigned long)ptr;
353 
354 #if __SIZEOF_POINTER__ == 8
355     val ^= (val >> 32); // (val >> 32)
356 #endif
357     return (unsigned int)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
395