[Spice-devel] [PATCH spice-gtk 06/25] glib-compat: add g_clear_pointer

Marc-André Lureau marcandre.lureau at gmail.com
Thu Jul 12 15:29:03 PDT 2012


A helpful macro from glib 2.34
---
 gtk/glib-compat.h |   20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/gtk/glib-compat.h b/gtk/glib-compat.h
index bd8f3de..aa750b2 100644
--- a/gtk/glib-compat.h
+++ b/gtk/glib-compat.h
@@ -106,4 +106,24 @@ GType spice_main_context_get_type (void) G_GNUC_CONST;
 # define G_SIGNAL_DEPRECATED (1 << 9)
 #endif
 
+#ifndef g_clear_pointer
+#define g_clear_pointer(pp, destroy) \
+  G_STMT_START {                                                               \
+    G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer));                       \
+    /* Only one access, please */                                              \
+    gpointer *_pp = (gpointer *) (pp);                                         \
+    gpointer _p;                                                               \
+    /* This assignment is needed to avoid a gcc warning */                     \
+    GDestroyNotify _destroy = (GDestroyNotify) (destroy);                      \
+                                                                               \
+    (void) (0 ? (gpointer) *(pp) : 0);                                         \
+    do                                                                         \
+      _p = g_atomic_pointer_get (_pp);                                         \
+    while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (_pp, _p, NULL)); \
+                                                                               \
+    if (_p)                                                                    \
+      _destroy (_p);                                                           \
+  } G_STMT_END
+#endif
+
 #endif /* GLIB_COMPAT_H */
-- 
1.7.10.4



More information about the Spice-devel mailing list