diff options
author | Stefano Sabatini <stefasab@gmail.com> | 2013-04-14 03:07:54 +0200 |
---|---|---|
committer | Stefano Sabatini <stefasab@gmail.com> | 2013-05-13 13:52:31 +0200 |
commit | e3984166a49f708d255eecce44824f77f160781e (patch) | |
tree | 7918a1ec6f65ecef1a36509679371db661349710 /libavutil/mem.c | |
parent | 84be80698227366d970e045001e4b59e4f99f0a1 (diff) | |
download | ffmpeg-e3984166a49f708d255eecce44824f77f160781e.tar.gz |
lavu/mem: add av_dynarray2_add()
Based on a patch by Clément Bœsch.
See thread:
From: Clément Bœsch <ubitux@gmail.com>
Subject: [FFmpeg-devel] [PATCH 1/5] lavu: add av_dynarray_alloc_elem().
Date: Sun, 14 Apr 2013 03:07:54 +0200
Diffstat (limited to 'libavutil/mem.c')
-rw-r--r-- | libavutil/mem.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/libavutil/mem.c b/libavutil/mem.c index 9b22609d6f..66502eb467 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -270,6 +270,39 @@ fail: *nb_ptr = 0; } +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); + return tab_elem_data; + +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); |