Commit 08f41d80 authored by Philip Withnall's avatar Philip Withnall

Merge branch 'type-safe-g-clear-pointer-1425' into 'master'

gmem.h: Use __typeof__() in the g_clear_pointer() macro

Closes #1425

See merge request GNOME/glib!165
parents c182cd68 0da62659
...@@ -110,6 +110,16 @@ gpointer g_try_realloc_n (gpointer mem, ...@@ -110,6 +110,16 @@ gpointer g_try_realloc_n (gpointer mem,
gsize n_blocks, gsize n_blocks,
gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT; gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) && !defined(__cplusplus) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
#define g_clear_pointer(pp, destroy) \
G_STMT_START { \
G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
__typeof__(*(pp)) _ptr = *(pp); \
*(pp) = NULL; \
if (_ptr) \
destroy (_ptr); \
} G_STMT_END
#else /* __GNUC__ */
#define g_clear_pointer(pp, destroy) \ #define g_clear_pointer(pp, destroy) \
G_STMT_START { \ G_STMT_START { \
G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \ G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
...@@ -127,6 +137,7 @@ gpointer g_try_realloc_n (gpointer mem, ...@@ -127,6 +137,7 @@ gpointer g_try_realloc_n (gpointer mem,
_destroy (_p); \ _destroy (_p); \
} \ } \
} G_STMT_END } G_STMT_END
#endif /* __GNUC__ */
/** /**
* g_steal_pointer: * g_steal_pointer:
......
...@@ -3378,7 +3378,7 @@ g_object_unref (gpointer _object) ...@@ -3378,7 +3378,7 @@ g_object_unref (gpointer _object)
**/ **/
#undef g_clear_object #undef g_clear_object
void void
g_clear_object (volatile GObject **object_ptr) g_clear_object (GObject **object_ptr)
{ {
g_clear_pointer (object_ptr, g_object_unref); g_clear_pointer (object_ptr, g_object_unref);
} }
......
...@@ -668,7 +668,7 @@ G_STMT_START { \ ...@@ -668,7 +668,7 @@ G_STMT_START { \
G_OBJECT_WARN_INVALID_PSPEC ((object), "property", (property_id), (pspec)) G_OBJECT_WARN_INVALID_PSPEC ((object), "property", (property_id), (pspec))
GLIB_AVAILABLE_IN_ALL GLIB_AVAILABLE_IN_ALL
void g_clear_object (volatile GObject **object_ptr); void g_clear_object (GObject **object_ptr);
#define g_clear_object(object_ptr) g_clear_pointer ((object_ptr), g_object_unref) #define g_clear_object(object_ptr) g_clear_pointer ((object_ptr), g_object_unref)
/** /**
......
...@@ -132,7 +132,7 @@ test_clear (void) ...@@ -132,7 +132,7 @@ test_clear (void)
static void static void
test_clear_function (void) test_clear_function (void)
{ {
volatile GObject *o = NULL; GObject *o = NULL;
GObject *tmp; GObject *tmp;
(g_clear_object) (&o); (g_clear_object) (&o);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment