From a87231abbc9db56208561a969c17aee7c81b2cb2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 30 Apr 2015 15:15:34 +0200 Subject: [PATCH] glib-compat: add nm_g_hash_table_replace() compat function The newer version of g_hash_table_replace() is useful, because it saves an additional hash table lookup. (cherry picked from commit 2bea70e75031bad941f0e9562bd2293f72b0e277) --- include/nm-glib-compat.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/nm-glib-compat.h b/include/nm-glib-compat.h index 1dc939feac..c5674ea9e7 100644 --- a/include/nm-glib-compat.h +++ b/include/nm-glib-compat.h @@ -163,4 +163,21 @@ q_n##_quark (void) \ } #endif + +static inline gboolean +nm_g_hash_table_replace (GHashTable *hash, gpointer key, gpointer value) +{ + /* glib 2.40 added a return value indicating whether the key already existed + * (910191597a6c2e5d5d460e9ce9efb4f47d9cc63c). */ +#if GLIB_CHECK_VERSION(2, 40, 0) + return g_hash_table_replace (hash, key, value); +#else + gboolean contained = g_hash_table_contains (hash, key); + + g_hash_table_replace (hash, key, value); + return !contained; +#endif +} + + #endif /* __NM_GLIB_COMPAT_H__ */