[vlc-commits] picture: allow overriding the default allocator

Rémi Denis-Courmont git at videolan.org
Sun Oct 28 11:18:20 CET 2018


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Oct 27 18:40:45 2018 +0300| [51d234921297443b5c067f77681203962cc17e01] | committer: Rémi Denis-Courmont

picture: allow overriding the default allocator

This keeps track of the underlying memory allocation, and makes room
for non-anonymous memory allocation.

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

 include/vlc_picture.h |  8 ++++++++
 src/misc/picture.c    | 38 ++++++++++++++++++++++++++++++++------
 src/misc/picture.h    |  3 +++
 3 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/include/vlc_picture.h b/include/vlc_picture.h
index f9d55b3175..6aca13bca0 100644
--- a/include/vlc_picture.h
+++ b/include/vlc_picture.h
@@ -64,6 +64,14 @@ typedef struct picture_context_t
     struct picture_context_t *(*copy)(struct picture_context_t *);
 } picture_context_t;
 
+typedef struct picture_buffer_t
+{
+    int fd;
+    void *base;
+    size_t size;
+    off_t offset;
+} picture_buffer_t;
+
 /**
  * Video picture
  */
diff --git a/src/misc/picture.c b/src/misc/picture.c
index 4cdbf8193a..b373ebd21f 100644
--- a/src/misc/picture.c
+++ b/src/misc/picture.c
@@ -62,10 +62,27 @@ static void picture_DestroyFromResource( picture_t *p_picture )
 /**
  * Destroys a picture allocated with picture_NewFromFormat().
  */
-static void picture_Destroy( picture_t *p_picture )
+static void picture_Destroy(picture_t *pic)
 {
-    aligned_free( p_picture->p[0].p_pixels );
-    free( p_picture );
+    picture_buffer_t *res = pic->p_sys;
+
+    if (res != NULL)
+        picture_Deallocate(res->fd, res->base, res->size);
+    free(pic);
+}
+
+VLC_WEAK void *picture_Allocate(int *restrict fdp, size_t size)
+{
+    assert((size % 16) == 0);
+    *fdp = -1;
+    return aligned_alloc(16, size);
+}
+
+VLC_WEAK void picture_Deallocate(int fd, void *base, size_t size)
+{
+    assert(fd == -1);
+    aligned_free(base);
+    assert((size % 16) == 0);
 }
 
 /*****************************************************************************
@@ -235,15 +252,17 @@ picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_r
 
 picture_t *picture_NewFromFormat(const video_format_t *restrict fmt)
 {
-    picture_priv_t *priv = picture_NewPrivate(fmt, 0);
+    picture_priv_t *priv = picture_NewPrivate(fmt, sizeof (picture_buffer_t));
     if (unlikely(priv == NULL))
         return NULL;
 
     priv->gc.destroy = picture_Destroy;
 
     picture_t *pic = &priv->picture;
-    if (pic->i_planes == 0)
+    if (pic->i_planes == 0) {
+        pic->p_sys = NULL;
         return pic;
+    }
 
     /* Calculate how big the new image should be */
     size_t plane_sizes[PICTURE_PLANE_MAX];
@@ -261,10 +280,17 @@ picture_t *picture_NewFromFormat(const video_format_t *restrict fmt)
     if (unlikely(pic_size >= PICTURE_SW_SIZE_MAX))
         goto error;
 
-    uint8_t *buf = aligned_alloc(16, pic_size);
+    picture_buffer_t *res = (void *)priv->extra;
+
+    unsigned char *buf = picture_Allocate(&res->fd, pic_size);
     if (unlikely(buf == NULL))
         goto error;
 
+    res->base = buf;
+    res->size = pic_size;
+    res->offset = 0;
+    pic->p_sys = res;
+
     /* Fill the p_pixels field for each plane */
     for (int i = 0; i < pic->i_planes; i++)
     {
diff --git a/src/misc/picture.h b/src/misc/picture.h
index a838144a6c..633d2a3abb 100644
--- a/src/misc/picture.h
+++ b/src/misc/picture.h
@@ -35,3 +35,6 @@ typedef struct
 
     max_align_t extra[];
 } picture_priv_t;
+
+void *picture_Allocate(int *, size_t);
+void picture_Deallocate(int, void *, size_t);



More information about the vlc-commits mailing list