[FFmpeg-devel] [PATCH 1/5] lavu: add av_dynarray_alloc_elem().

Clément Bœsch ubitux at gmail.com
Fri May 10 18:16:59 CEST 2013


On Fri, May 10, 2013 at 10:26:13AM +0200, Stefano Sabatini wrote:
[...]
> Updated. If someone has an idea about a better name
> (av_dynarray_alloc_elem() doesn't match very well with
> av_dynarray_add()), please tell.
> 

Well, they don't work on the same type of array, they are in a sense
"incompatible", so I think the names are even too close.

> I Will push soon if I see no more comments.
> -- 
> FFmpeg = Fancy and Fierce Magic Practical Enlightened Gorilla

> From 87653c03bfef4364bdf6e1d77a88c321d9e26699 Mon Sep 17 00:00:00 2001
> From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
> Date: Sun, 14 Apr 2013 03:07:54 +0200
> Subject: [PATCH] lavu: add av_dynarray_alloc_elem()
> MIME-Version: 1.0
> Content-Type: text/plain; charset=UTF-8
> Content-Transfer-Encoding: 8bit
> 
> With some modifications by Stefano Sabatini.
> 
> See thread:
> From: Clément Bœsch <ubitux at gmail.com>
> Subject: [FFmpeg-devel] [PATCH 1/5] lavu: add av_dynarray_alloc_elem().
> Date: Sun, 14 Apr 2013 03:07:54 +0200

Feel free to take authorship on the commit and put me in Signed-off-by.

Thanks for working on this.

> ---
>  doc/APIchanges      |    3 +++
>  libavutil/mem.c     |   34 +++++++++++++++++++++++++++++++++-
>  libavutil/mem.h     |   22 ++++++++++++++++++++++
>  libavutil/version.h |    2 +-
>  4 files changed, 59 insertions(+), 2 deletions(-)
> 
> diff --git a/doc/APIchanges b/doc/APIchanges
> index b023a6f..f47c870 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -15,6 +15,9 @@ libavutil:     2012-10-22
>  
>  API changes, most recent first:
>  
> +2013-05-08 - xxxxxxx - lavu 52.31.100 - mem.h
> +  Add av_dynarray_alloc_elem().
> +

Don't forget to update the date.

>  2013-04-19 - xxxxxxx - lavc 55.4.100
>    Add AV_CODEC_PROP_TEXT_SUB property for text based subtitles codec.
>  
> diff --git a/libavutil/mem.c b/libavutil/mem.c
> index 03bf2c8..aeeafdb 100644
> --- a/libavutil/mem.c
> +++ b/libavutil/mem.c
> @@ -70,6 +70,8 @@ void av_max_alloc(size_t max){
>      max_alloc_size = max;
>  }
>  
> +#define MEMORY_POISON 0x2a
> +

Note: could be in a common header (I think it's being used in other places).

>  void *av_malloc(size_t size)
>  {
>      void *ptr = NULL;
> @@ -133,7 +135,7 @@ void *av_malloc(size_t size)
>      }
>  #if CONFIG_MEMORY_POISONING
>      if (ptr)
> -        memset(ptr, 0x2a, size);
> +        memset(ptr, MEMORY_POISON, size);
>  #endif
>      return ptr;
>  }
> @@ -270,6 +272,36 @@ fail:
>      *nb_ptr = 0;
>  }
>  
> +void *av_dynarray_alloc_elem(void **tab_ptr, int *nb_ptr, size_t elem_size)
> +{
> +    int nb = *nb_ptr, nb_alloc;
> +    uint8_t *tab = *tab_ptr;
> +
> +    if ((nb & (nb - 1)) == 0) {
> +        if (nb == 0) {
> +            nb_alloc = 1;
> +        } else {
> +            if (nb > INT_MAX / (2 * elem_size))
> +                goto fail;
> +            nb_alloc = nb * 2;
> +        }
> +        tab = av_realloc(tab, nb_alloc * elem_size);
> +        if (!tab)
> +            goto fail;

I guess the check in av_realloc_f() wasn't enough? (sorry I didn't follow
the thread)

> +        *tab_ptr = tab;
> +    }
> +    *nb_ptr = nb + 1;
> +#if CONFIG_MEMORY_POISONING
> +    memset(tab + nb*elem_size, MEMORY_POISON, elem_size);
> +#endif
> +    return tab + nb*elem_size;
> +
> +fail:
> +    av_freep(tab_ptr);
> +    *nb_ptr = 0;
> +    return NULL;
> +}
> +
>  static void fill16(uint8_t *dst, int len)
>  {
>      uint32_t v = AV_RN16(dst - 2);
> diff --git a/libavutil/mem.h b/libavutil/mem.h
> index 58c26b1..14dc307 100644
> --- a/libavutil/mem.h
> +++ b/libavutil/mem.h
> @@ -215,10 +215,32 @@ void av_freep(void *ptr);
>   * @param tab_ptr pointer to the array to grow
>   * @param nb_ptr  pointer to the number of elements in the array
>   * @param elem    element to add
> + * @see av_dynarray_alloc_elem()
>   */
>  void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
>  
>  /**
> + * Add an element of size elem_size to a dynamic array.
> + *
> + * The array is reallocated when its size reaches powers of 2.
> + * Therefore, the amortized cost of adding an element is constant.
> + *
> + * In case of success, the pointer to the array is updated in order to
> + * point to the new grown array, and the number pointed to by nb_ptr
> + * is incremented.
> + * In case of failure, the array is freed, *tab_ptr is set to NULL and
> + * *nb_ptr is set to 0.
> + *
> + * @param tab_ptr   pointer to the array to grow
> + * @param nb_ptr    pointer to the number of elements in the array
> + * @param elem_size size in bytes of the elements in the array
> + * @return          pointer to the elem_size allocated space at the end of the
> + *                  array, or NULL in case of memory error
> + * @see av_dynarray_add()
> + */
> +void *av_dynarray_alloc_elem(void **tab_ptr, int *nb_ptr, size_t elem_size);
> +
> +/**
>   * Multiply two size_t values checking for overflow.
>   * @return  0 if success, AVERROR(EINVAL) if overflow.
>   */
> diff --git a/libavutil/version.h b/libavutil/version.h
> index 4d87631..f9a7040 100644
> --- a/libavutil/version.h
> +++ b/libavutil/version.h
> @@ -75,7 +75,7 @@
>   */
>  
>  #define LIBAVUTIL_VERSION_MAJOR  52
> -#define LIBAVUTIL_VERSION_MINOR  30
> +#define LIBAVUTIL_VERSION_MINOR  31
>  #define LIBAVUTIL_VERSION_MICRO 100
>  
>  #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

LGTM otherwise.

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130510/5bb5b6bc/attachment.asc>


More information about the ffmpeg-devel mailing list