Commit b0dcf765 authored by Nicolas George's avatar Nicolas George Committed by Michael Niedermayer

lavu/mem: reimplement the dynarray functions with the macro.

Signed-off-by: 's avatarNicolas George <george@nsup.org>
Signed-off-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parent 7b9a310d
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment