From b0dcf7653064186ce738edb7467157a653157890 Mon Sep 17 00:00:00 2001 From: Nicolas George Date: Sat, 8 Mar 2014 21:27:00 +0100 Subject: [PATCH] lavu/mem: reimplement the dynarray functions with the macro. Signed-off-by: Nicolas George Signed-off-by: Michael Niedermayer --- libavutil/mem.c | 75 +++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 53 deletions(-) diff --git a/libavutil/mem.c b/libavutil/mem.c index 10b0137a7e..7206ddcea4 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -39,6 +39,7 @@ #include "avassert.h" #include "avutil.h" #include "common.h" +#include "dynarray.h" #include "intreadwrite.h" #include "mem.h" @@ -279,65 +280,33 @@ void *av_memdup(const void *p, size_t size) void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem) { - /* see similar ffmpeg.c:grow_array() */ - int nb, nb_alloc; - intptr_t *tab; - - nb = *nb_ptr; - tab = *(intptr_t**)tab_ptr; - if ((nb & (nb - 1)) == 0) { - if (nb == 0) { - nb_alloc = 1; - } else { - if (nb > INT_MAX / (2 * sizeof(intptr_t))) - goto fail; - nb_alloc = nb * 2; - } - tab = av_realloc(tab, nb_alloc * sizeof(intptr_t)); - if (!tab) - goto fail; - *(intptr_t**)tab_ptr = tab; - } - tab[nb++] = (intptr_t)elem; - *nb_ptr = nb; - return; - -fail: - av_freep(tab_ptr); - *nb_ptr = 0; + intptr_t *tab = *(intptr_t**)tab_ptr; + + AV_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, { + tab[*nb_ptr] = (intptr_t)elem; + *(intptr_t **)tab_ptr = tab; + }, { + *nb_ptr = 0; + av_freep(tab_ptr); + }); } void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data) { - int nb = *nb_ptr, nb_alloc; - uint8_t *tab = *tab_ptr, *tab_elem_data; - - 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; - tab_elem_data = tab + nb*elem_size; - if (elem_data) - memcpy(tab_elem_data, elem_data, elem_size); - else if (CONFIG_MEMORY_POISONING) - memset(tab_elem_data, FF_MEMORY_POISON, elem_size); + uint8_t *tab_elem_data = NULL; + + AV_DYNARRAY_ADD(INT_MAX, elem_size, *tab_ptr, *nb_ptr, { + tab_elem_data = (uint8_t *)*tab_ptr + (*nb_ptr) * elem_size; + if (elem_data) + memcpy(tab_elem_data, elem_data, elem_size); + else if (CONFIG_MEMORY_POISONING) + memset(tab_elem_data, FF_MEMORY_POISON, elem_size); + }, { + av_freep(tab_ptr); + *nb_ptr = 0; + }); return tab_elem_data; - -fail: - av_freep(tab_ptr); - *nb_ptr = 0; - return NULL; } static void fill16(uint8_t *dst, int len)