diff options
author | Nicolas George <george@nsup.org> | 2014-03-08 21:27:00 +0100 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2014-03-22 20:53:36 +0100 |
commit | b0dcf7653064186ce738edb7467157a653157890 (patch) | |
tree | 128dd8437bcb3656864f36c6f0a5a0760414e930 /libavutil/mem.c | |
parent | 7b9a310d0e27c1c67c62289b5578ad1518cd07ac (diff) | |
download | ffmpeg-b0dcf7653064186ce738edb7467157a653157890.tar.gz |
lavu/mem: reimplement the dynarray functions with the macro.
Signed-off-by: Nicolas George <george@nsup.org>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavutil/mem.c')
-rw-r--r-- | libavutil/mem.c | 75 |
1 files 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) |