New function: g_clear_object()

By analogy to g_clear_error, takes a pass-by-reference GObject reference
and, if non-%NULL, unrefs it and sets it equal to %NULL.

Bug #620263.
This commit is contained in:
Ryan Lortie
2010-11-08 16:42:32 -05:00
parent 78bc8bec4f
commit 1a1fc130ec
7 changed files with 93 additions and 1 deletions

View File

@@ -562,6 +562,21 @@ G_STMT_START { \
#define G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec) \
G_OBJECT_WARN_INVALID_PSPEC ((object), "property", (property_id), (pspec))
void g_clear_object (volatile GObject **object_ptr);
#define g_clear_object(object_ptr) \
G_STMT_START { \
/* Only one access, please */ \
gpointer *_p = (gpointer) (object_ptr); \
gpointer _o; \
\
do \
_o = g_atomic_pointer_get (_p); \
while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (_p, _o, NULL));\
\
if (_o) \
g_object_unref (_o); \
} G_STMT_END
G_END_DECLS
#endif /* __G_OBJECT_H__ */