libs: use g_clear_object() wherever applicable.
authorJavier Jardón <jjardon@gnome.org>
Sat, 30 Jun 2012 20:34:15 +0000 (05:34 +0900)
committerGwenole Beauchesne <gwenole.beauchesne@intel.com>
Thu, 19 Jul 2012 13:19:17 +0000 (15:19 +0200)
This is a preferred thread-safe version. Also add an inline version of
g_clear_object() if compiling with glib < 2.28.

Signed-off-by: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
gst-libs/gst/vaapi/glibcompat.h
gst-libs/gst/vaapi/gstvaapicontext.c
gst-libs/gst/vaapi/gstvaapidisplay.c
gst-libs/gst/vaapi/gstvaapiobject.c
gst-libs/gst/vaapi/gstvaapisubpicture.c
gst-libs/gst/vaapi/gstvaapisurface.c
gst-libs/gst/vaapi/gstvaapisurfaceproxy.c
gst-libs/gst/vaapi/gstvaapivideobuffer.c
gst-libs/gst/vaapi/gstvaapivideoconverter_glx.c
gst-libs/gst/vaapi/gstvaapivideopool.c

index 93a3903..30a8563 100644 (file)
@@ -23,6 +23,7 @@
 #define GLIB_COMPAT_H
 
 #include <glib.h>
+#include <glib-object.h>
 
 #if !GLIB_CHECK_VERSION(2,27,2)
 static inline void
@@ -33,6 +34,24 @@ g_list_free_full(GList *list, GDestroyNotify free_func)
 }
 #endif
 
+#if !GLIB_CHECK_VERSION(2,28,0)
+static inline void
+g_clear_object_inline(volatile GObject **object_ptr)
+{
+    gpointer * const ptr = (gpointer)object_ptr;
+    gpointer old;
+
+    do {
+        old = g_atomic_pointer_get(ptr);
+    } while G_UNLIKELY(!g_atomic_pointer_compare_and_exchange(ptr, old, NULL));
+
+    if (old)
+        g_object_unref(old);
+}
+#undef  g_clear_object
+#define g_clear_object(obj) g_clear_object_inline((volatile GObject **)(obj))
+#endif
+
 #if GLIB_CHECK_VERSION(2,31,2)
 #define GStaticMutex                    GMutex
 #undef  g_static_mutex_init
index 9b1bf1a..52e81e4 100644 (file)
@@ -158,10 +158,7 @@ gst_vaapi_context_destroy_surfaces(GstVaapiContext *context)
         priv->surfaces = NULL;
     }
 
-    if (priv->surfaces_pool) {
-        g_object_unref(priv->surfaces_pool);
-        priv->surfaces_pool = NULL;
-    }
+    g_clear_object(&priv->surfaces_pool);
 }
 
 static void
index 01e19fc..e113f71 100644 (file)
@@ -352,10 +352,7 @@ gst_vaapi_display_destroy(GstVaapiDisplay *display)
             klass->close_display(display);
     }
 
-    if (priv->parent) {
-        g_object_unref(priv->parent);
-        priv->parent = NULL;
-    }
+    g_clear_object(&priv->parent);
 
     if (g_display_cache) {
         gst_vaapi_display_cache_remove(get_display_cache(), display);
@@ -408,8 +405,7 @@ gst_vaapi_display_create(GstVaapiDisplay *display)
         info.va_display
     );
     if (cached_info) {
-        if (priv->parent)
-            g_object_unref(priv->parent);
+        g_clear_object(&priv->parent);
         priv->parent = g_object_ref(cached_info->display);
     }
 
index ec8286e..ad3d139 100644 (file)
@@ -71,10 +71,7 @@ gst_vaapi_object_finalize(GObject *object)
 
     priv->id = GST_VAAPI_ID_NONE;
 
-    if (priv->display) {
-        g_object_unref(priv->display);
-        priv->display = NULL;
-    }
+    g_clear_object(&priv->display);
 
     G_OBJECT_CLASS(gst_vaapi_object_parent_class)->finalize(object);
 }
index 11a94f1..cfd7fd3 100644 (file)
@@ -79,10 +79,7 @@ gst_vaapi_subpicture_destroy(GstVaapiSubpicture *subpicture)
         GST_VAAPI_OBJECT_ID(subpicture) = VA_INVALID_ID;
     }
 
-    if (priv->image) {
-        g_object_unref(priv->image);
-        priv->image = NULL;
-    }
+    g_clear_object(&priv->image);
 }
 
 static gboolean
index 65c4b28..b471de7 100644 (file)
@@ -444,10 +444,7 @@ gst_vaapi_surface_set_parent_context(
 
     priv = surface->priv;
 
-    if (priv->parent_context) {
-        g_object_unref(priv->parent_context);
-        priv->parent_context = NULL;
-    }
+    g_clear_object(&priv->parent_context);
 
     if (context)
         priv->parent_context = g_object_ref(context);
index 3c2f229..aa4103e 100644 (file)
@@ -260,10 +260,7 @@ gst_vaapi_surface_proxy_set_context(
 
     priv = proxy->priv;
 
-    if (priv->context) {
-        g_object_unref(priv->context);
-        priv->context = NULL;
-    }
+    g_clear_object(&priv->context);
 
     if (context)
         priv->context = g_object_ref(context);
index 81176ff..7d80e50 100644 (file)
@@ -58,10 +58,7 @@ set_display(GstVaapiVideoBuffer *buffer, GstVaapiDisplay *display)
 {
     GstVaapiVideoBufferPrivate * const priv = buffer->priv;
 
-    if (priv->display) {
-        g_object_unref(priv->display);
-        priv->display = NULL;
-    }
+    g_clear_object(&priv->display);
 
     if (display)
         priv->display = g_object_ref(display);
@@ -94,10 +91,7 @@ gst_vaapi_video_buffer_destroy_image(GstVaapiVideoBuffer *buffer)
         priv->image = NULL;
     }
 
-    if (priv->image_pool) {
-        g_object_unref(priv->image_pool);
-        priv->image_pool = NULL;
-    }
+    g_clear_object(&priv->image_pool);
 }
 
 static void
@@ -105,10 +99,7 @@ gst_vaapi_video_buffer_destroy_surface(GstVaapiVideoBuffer *buffer)
 {
     GstVaapiVideoBufferPrivate * const priv = buffer->priv;
 
-    if (priv->proxy) {
-        g_object_unref(priv->proxy);
-        priv->proxy = NULL;
-    }
+    g_clear_object(&priv->proxy);
 
     if (priv->surface) {
         if (priv->surface_pool)
@@ -118,10 +109,7 @@ gst_vaapi_video_buffer_destroy_surface(GstVaapiVideoBuffer *buffer)
         priv->surface = NULL;
     }
 
-    if (priv->surface_pool) {
-        g_object_unref(priv->surface_pool);
-        priv->surface_pool = NULL;
-    }
+    g_clear_object(&priv->surface_pool);
 
     if (priv->buffer) {
         gst_buffer_unref(priv->buffer);
index 106b8ea..0ea968f 100644 (file)
@@ -44,10 +44,7 @@ gst_vaapi_video_converter_glx_dispose(GObject *object)
     GstVaapiVideoConverterGLXPrivate *priv =
       GST_VAAPI_VIDEO_CONVERTER_GLX (object)->priv;
 
-    if (priv->texture)
-      g_object_unref (priv->texture);
-
-    priv->texture = NULL;
+    g_clear_object(&priv->texture);
 
     G_OBJECT_CLASS (gst_vaapi_video_converter_glx_parent_class)->dispose (object);
 }
index e106483..ea24bef 100644 (file)
@@ -95,10 +95,7 @@ gst_vaapi_video_pool_destroy(GstVaapiVideoPool *pool)
         priv->caps = NULL;
     }
 
-    if (priv->display) {
-        g_object_unref(priv->display);
-        priv->display = NULL;
-    }
+    g_clear_object(&priv->display);
 }
 
 static void