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

Stefano Sabatini stefasab at gmail.com
Fri May 10 10:26:13 CEST 2013


On date Sunday 2013-04-28 01:07:21 +0200, Michael Niedermayer encoded:
> 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

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.

I Will push soon if I see no more comments.
-- 
FFmpeg = Fancy and Fierce Magic Practical Enlightened Gorilla
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0004-lavu-add-av_dynarray_alloc_elem.patch
Type: text/x-diff
Size: 4332 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130510/3d9a98e3/attachment.bin>


More information about the ffmpeg-devel mailing list