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

Michael Niedermayer michaelni at gmx.at
Sun Apr 28 01:07:21 CEST 2013


On Sat, Apr 27, 2013 at 11:26:56PM +0200, Stefano Sabatini wrote:
> On date Monday 2013-04-15 22:38:05 +0200, Stefano Sabatini encoded:
> > On date Sunday 2013-04-14 03:07:54 +0200, Clément Bœsch encoded:
> > > ---
> > >  libavutil/mem.c     | 19 +++++++++++++++++++
> > >  libavutil/mem.h     | 13 +++++++++++++
> > >  libavutil/version.h |  2 +-
> > >  3 files changed, 33 insertions(+), 1 deletion(-)
> [...]
> 
> Updated, with related bonus patch.
> -- 
> FFmpeg = Fierce and Foolish Murdering Patchable Easy Geisha

>  mem.c |   34 +++++++++++++++++++++++++++++++++-
>  mem.h |   19 +++++++++++++++++++
>  2 files changed, 52 insertions(+), 1 deletion(-)
> e273e4a57c642aaf4040cc577bba2050e2e3b6a6  0011-lavu-add-av_dynarray_alloc_elem.patch
> From 7c979633a22ddc4d40533d23a32b7f56b7ae8650 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().
> 
> ---
>  libavutil/mem.c |   34 +++++++++++++++++++++++++++++++++-
>  libavutil/mem.h |   19 +++++++++++++++++++
>  2 files changed, 52 insertions(+), 1 deletion(-)
> 
> diff --git a/libavutil/mem.c b/libavutil/mem.c
> index d091124..5b19114 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
> +
>  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;
>  }

nice idea


> @@ -259,6 +261,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;
> +        *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 544cc94..8dbfb94 100644
> --- a/libavutil/mem.h
> +++ b/libavutil/mem.h
> @@ -204,10 +204,29 @@ 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_add_elem()
>   */
>  void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
>  
>  /**
> + * Add an element of size elem_size to a dynamic array.
> + *
> + * 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 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.
>   */

the rest of av_dynarray_alloc_elem LGTM

[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Concerning the gods, I have no means of knowing whether they exist or not
or of what sort they may be, because of the obscurity of the subject, and
the brevity of human life -- Protagoras
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130428/08e70a3d/attachment.asc>


More information about the ffmpeg-devel mailing list