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.
This commit is contained in:
Thomas Haller 2020-09-15 19:22:01 +02:00
parent 72d6062cb0
commit 479de883b3
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 30 additions and 0 deletions

View file

@ -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.

View file

@ -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)
{