diff options
author | Thomas Haller <thaller@redhat.com> | 2020-09-15 19:22:01 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-09-18 15:26:37 +0200 |
commit | 479de883b3f5eed5bd9a37c057d6a7090bfe1630 (patch) | |
tree | 477a0d93902a82feb3371db826908eb9844d8013 | |
parent | 72d6062cb0d5d65e4d91416d826c73e13354c13d (diff) | |
download | NetworkManager-479de883b3f5eed5bd9a37c057d6a7090bfe1630.tar.gz |
shared: add m_g_variant_singleton_u_0()
We anyway cache our variants for the properties of NMDBusObject instances.
If such a variant is well known to be always the same, there is no need
to allocate a new instance every time. In particular, because GVariant
is an immutable and ref counted type.
Add a singleton getter for a "u" variant with numeric value 0.
-rw-r--r-- | shared/nm-glib-aux/nm-shared-utils.c | 28 | ||||
-rw-r--r-- | shared/nm-glib-aux/nm-shared-utils.h | 2 |
2 files changed, 30 insertions, 0 deletions
diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c index 242719ffed..c5c9576fae 100644 --- a/shared/nm-glib-aux/nm-shared-utils.c +++ b/shared/nm-glib-aux/nm-shared-utils.c @@ -405,6 +405,34 @@ nm_utils_gbytes_to_variant_ay (GBytes *bytes) /*****************************************************************************/ +#define _variant_singleton_get(create_variant) \ + ({ \ + static GVariant *_singleton = NULL; \ + GVariant *_v; \ + \ + again: \ + _v = g_atomic_pointer_get (&_singleton); \ + if (G_UNLIKELY (!_v)) { \ + _v = (create_variant); \ + nm_assert (_v); \ + nm_assert (g_variant_is_floating (_v)); \ + g_variant_ref_sink (_v); \ + if (!g_atomic_pointer_compare_and_exchange (&_singleton, NULL, _v)) { \ + g_variant_unref (_v); \ + goto again; \ + } \ + } \ + _v; \ + }) + +GVariant * +nm_g_variant_singleton_u_0 (void) +{ + return _variant_singleton_get (g_variant_new_uint32 (0)); +} + +/*****************************************************************************/ + /* Convert a hash table with "char *" keys and values to an "a{ss}" GVariant. * The keys will be sorted asciibetically. * Returns a floating reference. diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h index e413531fd8..f09c1580cd 100644 --- a/shared/nm-glib-aux/nm-shared-utils.h +++ b/shared/nm-glib-aux/nm-shared-utils.h @@ -1249,6 +1249,8 @@ char *nm_utils_str_utf8safe_unescape_cp (const char *str, NMUtilsStrUtf8SafeFlag char *nm_utils_str_utf8safe_escape_take (char *str, NMUtilsStrUtf8SafeFlags flags); +GVariant *nm_g_variant_singleton_u_0 (void); + static inline void nm_g_variant_unref_floating (GVariant *var) { |