From 572dc4c0025b031da600236a38831b10449c465f Mon Sep 17 00:00:00 2001 From: Simon McVittie <smcv@collabora.com> Date: Wed, 1 May 2019 19:57:18 +0100 Subject: [PATCH] backports: Add a backport of g_clear_pointer() Signed-off-by: Simon McVittie <smcv@collabora.com> Forwarded: https://gitlab.gnome.org/GNOME/libglnx/merge_requests/7 --- subprojects/libglnx/glnx-backports.h | 20 ++++++++++++++++++++ subprojects/libglnx/glnx-fdio.c | 1 + 2 files changed, 21 insertions(+) diff --git a/subprojects/libglnx/glnx-backports.h b/subprojects/libglnx/glnx-backports.h index cd853cca2..b6d93c05f 100644 --- a/subprojects/libglnx/glnx-backports.h +++ b/subprojects/libglnx/glnx-backports.h @@ -27,6 +27,26 @@ G_BEGIN_DECLS +#if !GLIB_CHECK_VERSION(2, 34, 0) +#define g_clear_pointer(pp, destroy) \ + G_STMT_START { \ + G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \ + /* Only one access, please; work around type aliasing */ \ + union { char *in; gpointer *out; } _pp; \ + gpointer _p; \ + /* This assignment is needed to avoid a gcc warning */ \ + GDestroyNotify _destroy = (GDestroyNotify) (destroy); \ + \ + _pp.in = (char *) (pp); \ + _p = *_pp.out; \ + if (_p) \ + { \ + *_pp.out = NULL; \ + _destroy (_p); \ + } \ + } G_STMT_END +#endif + #if !GLIB_CHECK_VERSION(2, 44, 0) #define g_strv_contains glnx_strv_contains diff --git a/subprojects/libglnx/glnx-fdio.c b/subprojects/libglnx/glnx-fdio.c index 7b734ff6f..288cc0235 100644 --- a/subprojects/libglnx/glnx-fdio.c +++ b/subprojects/libglnx/glnx-fdio.c @@ -37,6 +37,7 @@ #include <glnx-errors.h> #include <glnx-xattrs.h> #include <glnx-backport-autoptr.h> +#include <glnx-backports.h> #include <glnx-local-alloc.h> #include <glnx-missing.h> -- GitLab