[Spice-devel,v2,1/2] display-channel: reuse function to set video codecs

Submitted by Victor Toso on Dec. 2, 2016, 5:58 p.m.

Details

Message ID 20161202175802.7687-1-victortoso@redhat.com
State Accepted
Commit 82165c10eb50cc931647203e08e2e026037894e7
Headers show
Series "Series without cover letter" ( rev: 2 ) in Spice

Not browsing as part of any series.

Commit Message

Victor Toso Dec. 2, 2016, 5:58 p.m.
From: Victor Toso <me@victortoso.com>

Including g_clear_pointer() in glib-compat.h and using it to avoid
warnings in odd situations.

Signed-off-by: Victor Toso <victortoso@redhat.com>
---
 server/display-channel.c |  8 +++-----
 server/glib-compat.h     | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)

Patch hide | download patch | download mbox

diff --git a/server/display-channel.c b/server/display-channel.c
index 52f0a3d..061a99d 100644
--- a/server/display-channel.c
+++ b/server/display-channel.c
@@ -21,6 +21,7 @@ 
 #include <common/sw_canvas.h>
 
 #include "display-channel-private.h"
+#include "glib-compat.h"
 
 G_DEFINE_TYPE(DisplayChannel, display_channel, TYPE_COMMON_GRAPHICS_CHANNEL)
 
@@ -62,10 +63,7 @@  display_channel_set_property(GObject *object,
             self->priv->n_surfaces = g_value_get_uint(value);
             break;
         case PROP_VIDEO_CODECS:
-            if (self->priv->video_codecs) {
-                g_array_unref(self->priv->video_codecs);
-            }
-            self->priv->video_codecs = g_array_ref(g_value_get_boxed(value));
+            display_channel_set_video_codecs(self, g_value_get_boxed(value));
             break;
         default:
             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
@@ -211,7 +209,7 @@  void display_channel_set_video_codecs(DisplayChannel *display, GArray *video_cod
 {
     spice_return_if_fail(display);
 
-    g_array_unref(display->priv->video_codecs);
+    g_clear_pointer(&display->priv->video_codecs, g_array_unref);
     display->priv->video_codecs = g_array_ref(video_codecs);
 }
 
diff --git a/server/glib-compat.h b/server/glib-compat.h
index 8a27afe..50a0783 100644
--- a/server/glib-compat.h
+++ b/server/glib-compat.h
@@ -20,6 +20,25 @@ 
 
 #include <glib.h>
 
+#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 */                                              \
+    gpointer *_pp = (gpointer *) (pp);                                         \
+    gpointer _p;                                                               \
+    /* This assignment is needed to avoid a gcc warning */                     \
+    GDestroyNotify _destroy = (GDestroyNotify) (destroy);                      \
+                                                                               \
+    _p = *_pp;                                                                 \
+    if (_p)                                                                    \
+      {                                                                        \
+        *_pp = NULL;                                                           \
+        _destroy (_p);                                                         \
+      }                                                                        \
+  } G_STMT_END
+#endif
+
 #if !GLIB_CHECK_VERSION(2,32,0)
 static inline void
 g_queue_free_full(GQueue *queue, GDestroyNotify free_func)

Comments

> From: Victor Toso <me@victortoso.com>
> 
> Including g_clear_pointer() in glib-compat.h and using it to avoid
> warnings in odd situations.
> 
> Signed-off-by: Victor Toso <victortoso@redhat.com>

Acked

> ---
>  server/display-channel.c |  8 +++-----
>  server/glib-compat.h     | 19 +++++++++++++++++++
>  2 files changed, 22 insertions(+), 5 deletions(-)
> 
> diff --git a/server/display-channel.c b/server/display-channel.c
> index 52f0a3d..061a99d 100644
> --- a/server/display-channel.c
> +++ b/server/display-channel.c
> @@ -21,6 +21,7 @@
>  #include <common/sw_canvas.h>
>  
>  #include "display-channel-private.h"
> +#include "glib-compat.h"
>  
>  G_DEFINE_TYPE(DisplayChannel, display_channel, TYPE_COMMON_GRAPHICS_CHANNEL)
>  
> @@ -62,10 +63,7 @@ display_channel_set_property(GObject *object,
>              self->priv->n_surfaces = g_value_get_uint(value);
>              break;
>          case PROP_VIDEO_CODECS:
> -            if (self->priv->video_codecs) {
> -                g_array_unref(self->priv->video_codecs);
> -            }
> -            self->priv->video_codecs =
> g_array_ref(g_value_get_boxed(value));
> +            display_channel_set_video_codecs(self,
> g_value_get_boxed(value));
>              break;
>          default:
>              G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
> @@ -211,7 +209,7 @@ void display_channel_set_video_codecs(DisplayChannel
> *display, GArray *video_cod
>  {
>      spice_return_if_fail(display);
>  
> -    g_array_unref(display->priv->video_codecs);
> +    g_clear_pointer(&display->priv->video_codecs, g_array_unref);
>      display->priv->video_codecs = g_array_ref(video_codecs);
>  }
>  
> diff --git a/server/glib-compat.h b/server/glib-compat.h
> index 8a27afe..50a0783 100644
> --- a/server/glib-compat.h
> +++ b/server/glib-compat.h
> @@ -20,6 +20,25 @@
>  
>  #include <glib.h>
>  
> +#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 */
> \
> +    gpointer *_pp = (gpointer *) (pp);
> \
> +    gpointer _p;
> \
> +    /* This assignment is needed to avoid a gcc warning */
> \
> +    GDestroyNotify _destroy = (GDestroyNotify) (destroy);
> \
> +
> \
> +    _p = *_pp;
> \
> +    if (_p)
> \
> +      {
> \
> +        *_pp = NULL;
> \
> +        _destroy (_p);
> \
> +      }
> \
> +  } G_STMT_END
> +#endif
> +
>  #if !GLIB_CHECK_VERSION(2,32,0)
>  static inline void
>  g_queue_free_full(GQueue *queue, GDestroyNotify free_func)

Frediano