Commit 7d9c71e1 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: Extract generic string hashing function for reuse

Calculating a hash of zero-terminated string is a common need when using
hashmap, so extract it for reuse.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200926011357.2366158-5-andriin@fb.com
parent 192f5a1f
...@@ -90,14 +90,7 @@ struct btf_dump { ...@@ -90,14 +90,7 @@ struct btf_dump {
static size_t str_hash_fn(const void *key, void *ctx) static size_t str_hash_fn(const void *key, void *ctx)
{ {
const char *s = key; return str_hash(key);
size_t h = 0;
while (*s) {
h = h * 31 + *s;
s++;
}
return h;
} }
static bool str_equal_fn(const void *a, const void *b, void *ctx) static bool str_equal_fn(const void *a, const void *b, void *ctx)
......
...@@ -25,6 +25,18 @@ static inline size_t hash_bits(size_t h, int bits) ...@@ -25,6 +25,18 @@ static inline size_t hash_bits(size_t h, int bits)
#endif #endif
} }
/* generic C-string hashing function */
static inline size_t str_hash(const char *s)
{
size_t h = 0;
while (*s) {
h = h * 31 + *s;
s++;
}
return h;
}
typedef size_t (*hashmap_hash_fn)(const void *key, void *ctx); typedef size_t (*hashmap_hash_fn)(const void *key, void *ctx);
typedef bool (*hashmap_equal_fn)(const void *key1, const void *key2, void *ctx); typedef bool (*hashmap_equal_fn)(const void *key1, const void *key2, void *ctx);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment