From 573b02f7d7451e89009f5cb9c2eb28dac85ffe50 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 20 Mar 2020 22:47:40 +0100 Subject: [PATCH] shared: add nm_pgbytes_hash()/nm_pgbytes_equal() For hashing of a pointer to a GBytes*. This is useful if your key is a GBytes array, and the first field in your to be hashed struct. --- shared/nm-glib-aux/nm-hash-utils.c | 22 ++++++++++++++++++++++ shared/nm-glib-aux/nm-hash-utils.h | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/shared/nm-glib-aux/nm-hash-utils.c b/shared/nm-glib-aux/nm-hash-utils.c index cd51bbaf63..dcf662053a 100644 --- a/shared/nm-glib-aux/nm-hash-utils.c +++ b/shared/nm-glib-aux/nm-hash-utils.c @@ -257,3 +257,25 @@ nm_ppdirect_equal (gconstpointer a, gconstpointer b) return **s1 == **s2; } + +/*****************************************************************************/ + +guint +nm_pgbytes_hash (gconstpointer p) +{ + GBytes *const*ptr = p; + gconstpointer arr; + gsize len; + + arr = g_bytes_get_data (*ptr, &len); + return nm_hash_mem (1470631313u, arr, len); +} + +gboolean +nm_pgbytes_equal (gconstpointer a, gconstpointer b) +{ + GBytes *const*ptr_a = a; + GBytes *const*ptr_b = b; + + return g_bytes_equal (*ptr_a, *ptr_b); +} diff --git a/shared/nm-glib-aux/nm-hash-utils.h b/shared/nm-glib-aux/nm-hash-utils.h index 1f29413bd9..9879dc477b 100644 --- a/shared/nm-glib-aux/nm-hash-utils.h +++ b/shared/nm-glib-aux/nm-hash-utils.h @@ -314,6 +314,11 @@ gboolean nm_ppdirect_equal (gconstpointer a, gconstpointer b); /*****************************************************************************/ +guint nm_pgbytes_hash (gconstpointer p); +gboolean nm_pgbytes_equal (gconstpointer a, gconstpointer b); + +/*****************************************************************************/ + #define NM_HASH_OBFUSCATE_PTR_FMT "%016" G_GINT64_MODIFIER "x" /* sometimes we want to log a pointer directly, for providing context/information about