]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
glibcompat: Provide proper override for 'g_hash_table_steal_extended'
authorPeter Krempa <pkrempa@redhat.com>
Wed, 4 May 2022 15:23:56 +0000 (17:23 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 1 Jun 2022 10:27:10 +0000 (12:27 +0200)
We've emulated the function in virHashSteal, with a note pointing to use
the proper version. Move the code to glibcomapt.c and make it such that
builds using newer glib already use the new function.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/util/glibcompat.c
src/util/glibcompat.h
src/util/virhash.c

index eb6dcc0111b9bb968a6e45a659cc45f31d297df0..fdc32af5e2b3464deb72b684383b58370abcf94b 100644 (file)
@@ -64,6 +64,7 @@
 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 
 #undef g_canonicalize_filename
+#undef g_hash_table_steal_extended
 #undef g_fsync
 #undef g_strdup_printf
 #undef g_strdup_vprintf
@@ -173,6 +174,25 @@ vir_g_canonicalize_filename(const gchar *filename,
 }
 
 
+gboolean
+vir_g_hash_table_steal_extended(GHashTable *hash_table,
+                                gconstpointer lookup_key,
+                                gpointer *stolen_key,
+                                gpointer *stolen_value)
+{
+#if GLIB_CHECK_VERSION(2, 58, 0)
+    return g_hash_table_steal_extended(hash_table, lookup_key, stolen_key, stolen_value);
+#else /* ! GLIB_CHECK_VERSION(2, 58, 0) */
+    if (!(g_hash_table_lookup_extended(hash_table, lookup_key, stolen_key, stolen_value)))
+        return FALSE;
+
+    g_hash_table_steal(hash_table, lookup_key);
+
+    return TRUE;
+#endif /* ! GLIB_CHECK_VERSION(2, 58, 0) */
+}
+
+
 /* Drop when min glib >= 2.63.0 */
 gint
 vir_g_fsync(gint fd)
index 697687b967f8416e6d7b3a4ae57520576b97851b..1f3a6f728faf74f73b4ab71243a5abe85b376140 100644 (file)
 
 gchar * vir_g_canonicalize_filename(const gchar *filename,
                                     const gchar *relative_to);
+
+gboolean
+vir_g_hash_table_steal_extended(GHashTable *hash_table,
+                                gconstpointer lookup_key,
+                                gpointer *stolen_key,
+                                gpointer *stolen_value);
+#define g_hash_table_steal_extended vir_g_hash_table_steal_extended
+
 gint vir_g_fsync(gint fd);
 char *vir_g_strdup_printf(const char *msg, ...)
     G_GNUC_PRINTF(1, 2);
index a89b2d662e93416a5b40b92cacfd1e49282bae13..c72c2481867b09e701126c81286a7f5fdf7d9805 100644 (file)
@@ -242,24 +242,19 @@ virHashHasEntry(GHashTable *table,
  * Find the userdata specified by @name
  * and remove it from the hash without freeing it.
  *
- * Deprecated: consider using g_hash_table_steal_extended once we upgrade to
- * glib 2.58
+ * Deprecated: consider using g_hash_table_steal_extended instead
  *
  * Returns a pointer to the userdata
  */
 void *virHashSteal(GHashTable *table, const char *name)
 {
-    g_autofree void *orig_name = NULL;
+    g_autofree void *orig_name = NULL; /* the original key needs to be freed */
     void *val = NULL;
 
     if (!table || !name)
         return NULL;
 
-    /* we can replace this by g_hash_table_steal_extended with glib 2.58 */
-    if (!(g_hash_table_lookup_extended(table, name, &orig_name, &val)))
-        return NULL;
-
-    g_hash_table_steal(table, name);
+    g_hash_table_steal_extended(table, name, &orig_name, &val);
 
     return val;
 }