mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager
synced 2024-10-14 20:18:39 +00:00
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:
parent
72d6062cb0
commit
479de883b3
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue