diff options
author | Simon McVittie <smcv@collabora.com> | 2019-05-01 19:57:18 +0100 |
---|---|---|
committer | Simon McVittie <smcv@collabora.com> | 2019-11-11 18:32:06 +0000 |
commit | 46ba6b4187ea7e70caeef4bee144a932c6bf84ca (patch) | |
tree | 0a03dcd1ba7a8ad6975024a571a84764f21037ce /glnx-backports.h | |
parent | 9d0711fe1d6db58b4e24a6c92113930fecafd43e (diff) | |
download | libglnx-46ba6b4187ea7e70caeef4bee144a932c6bf84ca.tar.gz |
backports: Add a backport of g_clear_pointer()
This will be necessary if targeting GLib versions older than 2.34,
such as GLib 2.32 in Ubuntu 12.04 and the Steam Runtime.
Signed-off-by: Simon McVittie <smcv@collabora.com>
Diffstat (limited to 'glnx-backports.h')
-rw-r--r-- | glnx-backports.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/glnx-backports.h b/glnx-backports.h index cd853cc..b6d93c0 100644 --- a/glnx-backports.h +++ b/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 |