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-10 12:43:06 +0200
commit7f75a1b5f500127fbecebc4921a3f7d1f8546565 (patch)
tree5ca9f817e2a9f65cb8b542c6389407fbbc0508ab
parentbf1cadbdc77030b41ba3b98ab59c6461ac9766d4 (diff)
downloadNetworkManager-7f75a1b5f500127fbecebc4921a3f7d1f8546565.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