From 7336e39f3cd2833786b0ebcca5604e13d2f86500 Mon Sep 17 00:00:00 2001 From: Lukasz Marek Date: Sat, 10 May 2014 08:06:53 +0200 Subject: lavu/fifo: add av_fifo_alloc_array function Allows to alloc fifo buffer by passing number of elements and size of element. Signed-off-by: Lukasz Marek --- libavutil/fifo.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'libavutil/fifo.c') diff --git a/libavutil/fifo.c b/libavutil/fifo.c index 09ffa4fd26..77391ee7f2 100644 --- a/libavutil/fifo.c +++ b/libavutil/fifo.c @@ -24,19 +24,34 @@ #include "common.h" #include "fifo.h" -AVFifoBuffer *av_fifo_alloc(unsigned int size) +static AVFifoBuffer *fifo_alloc_common(void *buffer, size_t size) { - AVFifoBuffer *f = av_mallocz(sizeof(AVFifoBuffer)); - if (!f) + AVFifoBuffer *f; + if (!buffer) + return NULL; + f = av_mallocz(sizeof(AVFifoBuffer)); + if (!f) { + av_free(buffer); return NULL; - f->buffer = av_malloc(size); + } + f->buffer = buffer; f->end = f->buffer + size; av_fifo_reset(f); - if (!f->buffer) - av_freep(&f); return f; } +AVFifoBuffer *av_fifo_alloc(unsigned int size) +{ + void *buffer = av_malloc(size); + return fifo_alloc_common(buffer, size); +} + +AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size) +{ + void *buffer = av_malloc_array(nmemb, size); + return fifo_alloc_common(buffer, nmemb * size); +} + void av_fifo_free(AVFifoBuffer *f) { if (f) { -- cgit v1.2.3