[PATCH] glibcompat: Provide proper override for 'g_hash_table_steal_extended'

Peter Krempa posted 1 patch 1 year, 11 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/50a6a70e7e399e1dff560984afcb5d734047d80c.1653911477.git.pkrempa@redhat.com
src/util/glibcompat.c | 20 ++++++++++++++++++++
src/util/glibcompat.h |  8 ++++++++
src/util/virhash.c    | 11 +++--------
3 files changed, 31 insertions(+), 8 deletions(-)
[PATCH] glibcompat: Provide proper override for 'g_hash_table_steal_extended'
Posted by Peter Krempa 1 year, 11 months ago
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>
---
 src/util/glibcompat.c | 20 ++++++++++++++++++++
 src/util/glibcompat.h |  8 ++++++++
 src/util/virhash.c    | 11 +++--------
 3 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/src/util/glibcompat.c b/src/util/glibcompat.c
index eb6dcc0111..fdc32af5e2 100644
--- a/src/util/glibcompat.c
+++ b/src/util/glibcompat.c
@@ -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)
diff --git a/src/util/glibcompat.h b/src/util/glibcompat.h
index 697687b967..1f3a6f728f 100644
--- a/src/util/glibcompat.h
+++ b/src/util/glibcompat.h
@@ -70,6 +70,14 @@

 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);
diff --git a/src/util/virhash.c b/src/util/virhash.c
index a89b2d662e..c72c248186 100644
--- a/src/util/virhash.c
+++ b/src/util/virhash.c
@@ -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;
 }
-- 
2.35.3
Re: [PATCH] glibcompat: Provide proper override for 'g_hash_table_steal_extended'
Posted by Ján Tomko 1 year, 11 months ago
On a Monday in 2022, Peter Krempa wrote:
>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>
>---
> src/util/glibcompat.c | 20 ++++++++++++++++++++
> src/util/glibcompat.h |  8 ++++++++
> src/util/virhash.c    | 11 +++--------
> 3 files changed, 31 insertions(+), 8 deletions(-)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano