[vlc-commits] picture: avoid using void cast

Steve Lhomme git at videolan.org
Mon Nov 18 16:15:33 CET 2019


vlc | branch: master | Steve Lhomme <robux4 at ycbcr.xyz> | Thu Nov 14 10:41:52 2019 +0100| [7811efb7b926c2ac6fda2214328a029d8f3a9a95] | committer: Steve Lhomme

picture: avoid using void cast

Always allocate exactly the type we're going to use, regardless of padding.

We can allocate and use the extended picture_priv_t locally without having to
add an extra field in the regular structure.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=7811efb7b926c2ac6fda2214328a029d8f3a9a95
---

 src/misc/picture.c | 41 +++++++++++++++++++++++++++--------------
 src/misc/picture.h |  2 --
 2 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/src/misc/picture.c b/src/misc/picture.c
index cdde6f9951..ce683d8e13 100644
--- a/src/misc/picture.c
+++ b/src/misc/picture.c
@@ -193,14 +193,9 @@ int picture_Setup( picture_t *p_picture, const video_format_t *restrict fmt )
  *
  *****************************************************************************/
 
-static picture_priv_t *picture_NewPrivate(const video_format_t *restrict p_fmt,
-                                          size_t extra)
+static bool picture_InitPrivate(const video_format_t *restrict p_fmt,
+                                picture_priv_t *priv)
 {
-    /* */
-    picture_priv_t *priv = malloc(sizeof (*priv) + extra);
-    if( unlikely(priv == NULL) )
-        return NULL;
-
     picture_t *p_picture = &priv->picture;
 
     memset( p_picture, 0, sizeof( *p_picture ) );
@@ -210,23 +205,29 @@ static picture_priv_t *picture_NewPrivate(const video_format_t *restrict p_fmt,
     if( picture_Setup( p_picture, p_fmt ) )
     {
         free( p_picture );
-        return NULL;
+        return false;
     }
 
     atomic_init(&p_picture->refs, 1);
     priv->gc.opaque = NULL;
 
-    return priv;
+    return true;
 }
 
 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
 {
     assert(p_resource != NULL);
 
-    picture_priv_t *priv = picture_NewPrivate(p_fmt, 0);
+    picture_priv_t *priv = malloc(sizeof(*priv));
     if (unlikely(priv == NULL))
         return NULL;
 
+    if (!picture_InitPrivate(p_fmt, priv))
+    {
+        free(priv);
+        return NULL;
+    }
+
     picture_t *p_picture = &priv->picture;
 
     p_picture->p_sys = p_resource->p_sys;
@@ -248,12 +249,24 @@ picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_r
 
 #define PICTURE_SW_SIZE_MAX (UINT32_C(1) << 28) /* 256MB: 8K * 8K * 4*/
 
+struct picture_priv_buffer_t {
+    picture_priv_t   priv;
+    picture_buffer_t res;
+};
+
 picture_t *picture_NewFromFormat(const video_format_t *restrict fmt)
 {
-    picture_priv_t *priv = picture_NewPrivate(fmt, sizeof (picture_buffer_t));
-    if (unlikely(priv == NULL))
+    static_assert(offsetof(struct picture_priv_buffer_t, priv)==0,
+                  "misplaced picture_priv_t, destroy won't work");
+
+    struct picture_priv_buffer_t *privbuf = malloc(sizeof(*privbuf));
+    if (unlikely(privbuf == NULL))
         return NULL;
 
+    picture_priv_t *priv = &privbuf->priv;
+    if (!picture_InitPrivate(fmt, priv))
+        goto error;
+
     priv->gc.destroy = picture_DestroyFromFormat;
 
     picture_t *pic = &priv->picture;
@@ -278,7 +291,7 @@ picture_t *picture_NewFromFormat(const video_format_t *restrict fmt)
     if (unlikely(pic_size >= PICTURE_SW_SIZE_MAX))
         goto error;
 
-    picture_buffer_t *res = (void *)priv->extra;
+    picture_buffer_t *res = &privbuf->res;
 
     unsigned char *buf = picture_Allocate(&res->fd, pic_size);
     if (unlikely(buf == NULL))
@@ -298,7 +311,7 @@ picture_t *picture_NewFromFormat(const video_format_t *restrict fmt)
 
     return pic;
 error:
-    free(pic);
+    free(privbuf);
     return NULL;
 }
 
diff --git a/src/misc/picture.h b/src/misc/picture.h
index 1215214d69..a87164ce86 100644
--- a/src/misc/picture.h
+++ b/src/misc/picture.h
@@ -31,8 +31,6 @@ typedef struct
         void (*destroy)(picture_t *);
         void *opaque;
     } gc;
-
-    max_align_t extra[];
 } picture_priv_t;
 
 void *picture_Allocate(int *, size_t);



More information about the vlc-commits mailing list