History

libbpf: Fix libbpf hashmap on (I)LP32 architectures

On ILP32, 64-bit result was shifted by value calculated for 32-bit long type and returned value was much outside hashmap capacity. As advised by Andrii Nakryiko, this patch uses different hashing variant for architectures with size_t shorter than long long.

Jakub Bogusz committed 6 years ago

Commit b2f9f151 parent f43cb0d1 file · +8 −4Browse files

Unified · Split

tools/lib/bpf/hashmap.h+8−4
@@ -11,14 +11,18 @@ · 10 unmodified lines
1111 #include <stdbool.h>
1212 #include <stddef.h>
1313 #include <limits.h>
14−#ifndef __WORDSIZE
15−#define __WORDSIZE (__SIZEOF_LONG__ * 8)
16−#endif
1714
1815 static inline size_t hash_bits(size_t h, int bits)
1916 {
2017 /* shuffle bits and return requested number of upper bits */
21− return (h * 11400714819323198485llu) >> (__WORDSIZE - bits);
18+#if (__SIZEOF_SIZE_T__ == __SIZEOF_LONG_LONG__)
19+ /* LP64 case */
20+ return (h * 11400714819323198485llu) >> (__SIZEOF_LONG_LONG__ * 8 - bits);
21+#elif (__SIZEOF_SIZE_T__ <= __SIZEOF_LONG__)
22+ return (h * 2654435769lu) >> (__SIZEOF_LONG__ * 8 - bits);
23+#else
24+# error "Unsupported size_t size"
25+#endif
2226 }
2327
2428 typedef size_t (*hashmap_hash_fn)(const void *key, void *ctx);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t