aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf/hashmap.h
diff options
context:
space:
mode:
authorAndrii Nakryiko <andriin@fb.com>2020-09-25 18:13:52 -0700
committerAlexei Starovoitov <ast@kernel.org>2020-09-28 17:27:31 -0700
commit7d9c71e10baa3496d95226aa3bba668f7533ec70 (patch)
treeec6765ac143da67147fabf28281deee6d8a9050d /tools/lib/bpf/hashmap.h
parentlibbpf: Generalize common logic for managing dynamically-sized arrays (diff)
downloadlinux-dev-7d9c71e10baa3496d95226aa3bba668f7533ec70.tar.xz
linux-dev-7d9c71e10baa3496d95226aa3bba668f7533ec70.zip
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: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20200926011357.2366158-5-andriin@fb.com
Diffstat (limited to 'tools/lib/bpf/hashmap.h')
-rw-r--r--tools/lib/bpf/hashmap.h12
1 files changed, 12 insertions, 0 deletions
diff --git a/tools/lib/bpf/hashmap.h b/tools/lib/bpf/hashmap.h
index e0af36b0e5d8..d9b385fe808c 100644
--- a/tools/lib/bpf/hashmap.h
+++ b/tools/lib/bpf/hashmap.h
@@ -25,6 +25,18 @@ static inline size_t hash_bits(size_t h, int bits)
#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 bool (*hashmap_equal_fn)(const void *key1, const void *key2, void *ctx);