summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-07-02 16:13:38 +0200
committerThomas Haller <thaller@redhat.com>2019-07-08 13:57:19 +0200
commit9a28b42f18a1d4139462f4e3c6a8b378736a434f (patch)
treef06bcc4e4a8c23e40518b2c424e763d95418154c
parentfb4823c8aefd1e07624efb3e67fb9c39c048e465 (diff)
downloadNetworkManager-9a28b42f18a1d4139462f4e3c6a8b378736a434f.tar.gz
shared: add nm_pdirect_hash()/nm_pdirect_equal()
This follows a pointer to a pointer and compares them. In a sense it's like nm_pstr_*(), which follow a pointer to a string. However, these functions use direct pointer comparison. The purpose is when you hash a key that has as first field a pointer value (and then compare them by pointer equality).
-rw-r--r--shared/nm-glib-aux/nm-hash-utils.c22
-rw-r--r--shared/nm-glib-aux/nm-hash-utils.h9
2 files changed, 31 insertions, 0 deletions
diff --git a/shared/nm-glib-aux/nm-hash-utils.c b/shared/nm-glib-aux/nm-hash-utils.c
index b2ae75e3fc..a6158269d4 100644
--- a/shared/nm-glib-aux/nm-hash-utils.c
+++ b/shared/nm-glib-aux/nm-hash-utils.c
@@ -199,3 +199,25 @@ nm_pstr_equal (gconstpointer a, gconstpointer b)
&& s2
&& nm_streq0 (*s1, *s2));
}
+
+guint
+nm_pdirect_hash (gconstpointer p)
+{
+ const void *const*s = p;
+
+ if (!s)
+ return nm_hash_static (1852748873u);
+ return nm_direct_hash (*s);
+}
+
+gboolean
+nm_pdirect_equal (gconstpointer a, gconstpointer b)
+{
+ const void *const*s1 = a;
+ const void *const*s2 = b;
+
+ return (s1 == s2)
+ || ( s1
+ && s2
+ && *s1 == *s2);
+}
diff --git a/shared/nm-glib-aux/nm-hash-utils.h b/shared/nm-glib-aux/nm-hash-utils.h
index 472cfec8cc..f13e0b6d56 100644
--- a/shared/nm-glib-aux/nm-hash-utils.h
+++ b/shared/nm-glib-aux/nm-hash-utils.h
@@ -290,6 +290,15 @@ gboolean nm_pstr_equal (gconstpointer a, gconstpointer b);
/*****************************************************************************/
+/* this hashes/compares the pointer value that we point to. Basically,
+ * (((const void *const*) a) == ((const void *const*) b)). */
+
+guint nm_pdirect_hash (gconstpointer p);
+
+gboolean nm_pdirect_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