summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-01-08 14:40:16 +0100
committerThomas Haller <thaller@redhat.com>2018-01-09 14:24:54 +0100
commit901aa0315b592bc5f897b5abce05a5eed2ee3cad (patch)
tree2077be5c9b8d8e9940e27e2c20a1707b7a51c34f
parent2aad517b0b3b29615d1afdc907107a24cca6e1f2 (diff)
downloadNetworkManager-901aa0315b592bc5f897b5abce05a5eed2ee3cad.tar.gz
shared: add nm_cmp_int2ptr_p_with_data() helper
A cmp() implementation, for sorting an array with pointers, where each pointer is an inteter according to GPOINTER_TO_INT(). That cames for example handy, if you have a GHashTable with keys GINT_TO_POINTER(). Then you get the list of keys via g_hash_table_get_keys_as_array() and want to sort them.
-rw-r--r--shared/nm-utils/nm-macros-internal.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index 4ee59f195e..852a87df5b 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -1049,6 +1049,25 @@ nm_cmp_uint32_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_d
return 0;
}
+static inline int
+nm_cmp_int2ptr_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data)
+{
+ /* p_a and p_b are two pointers to a pointer, where the pointer is
+ * interpreted as a integer using GPOINTER_TO_INT().
+ *
+ * That is the case of a hash-table that uses GINT_TO_POINTER() to
+ * convert integers as pointers, and the resulting keys-as-array
+ * array. */
+ const int a = GPOINTER_TO_INT (*((gconstpointer *) p_a));
+ const int b = GPOINTER_TO_INT (*((gconstpointer *) p_b));
+
+ if (a < b)
+ return -1;
+ if (a > b)
+ return 1;
+ return 0;
+}
+
/*****************************************************************************/
/* Taken from systemd's UNIQ_T and UNIQ macros. */