From 32664c72a5a61e1b2a156436d9132cbbc928b9c0 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 27 Apr 2020 08:02:40 +0200 Subject: shared: add nm_gbytes_get_empty() singleton getter --- shared/nm-glib-aux/nm-shared-utils.c | 18 ++++++++++++++++++ shared/nm-glib-aux/nm-shared-utils.h | 2 ++ 2 files changed, 20 insertions(+) diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c index 61e3e26716..5dd099a7c9 100644 --- a/shared/nm-glib-aux/nm-shared-utils.c +++ b/shared/nm-glib-aux/nm-shared-utils.c @@ -397,6 +397,24 @@ truncate: /*****************************************************************************/ +GBytes * +nm_gbytes_get_empty (void) +{ + static GBytes *bytes = NULL; + GBytes *b; + +again: + b = g_atomic_pointer_get (&bytes); + if (G_UNLIKELY (!b)) { + b = g_bytes_new_static ("", 0); + if (!g_atomic_pointer_compare_and_exchange (&bytes, NULL, b)) { + g_bytes_unref (b); + goto again; + } + } + return b; +} + /** * nm_utils_gbytes_equals: * @bytes: (allow-none): a #GBytes array to compare. Note that diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h index 9200f80fdf..d5990c2d54 100644 --- a/shared/nm-glib-aux/nm-shared-utils.h +++ b/shared/nm-glib-aux/nm-shared-utils.h @@ -389,6 +389,8 @@ nm_utils_is_separator (const char c) /*****************************************************************************/ +GBytes *nm_gbytes_get_empty (void); + static inline gboolean nm_gbytes_equal0 (GBytes *a, GBytes *b) { -- cgit v1.2.1