[FFmpeg-devel] [PATCH] lavu/fifo: add av_fifo_alloc_array function
Lukasz Marek
lukasz.m.luki2 at gmail.com
Sat May 10 08:15:52 CEST 2014
TODO: minor bump and doc/APIchanges update
Allows to alloc fifo buffer by passing
number of elements and size of element.
Signed-off-by: Lukasz Marek <lukasz.m.luki2 at gmail.com>
---
libavutil/fifo.c | 27 +++++++++++++++++++++------
libavutil/fifo.h | 8 ++++++++
2 files changed, 29 insertions(+), 6 deletions(-)
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index e35237c..5c82d09 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 *av_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 av_fifo_alloc_common(buffer, size);
+}
+
+AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
+{
+ void *buffer = av_malloc_array(nmemb, size);
+ return av_fifo_alloc_common(buffer, nmemb * size);
+}
+
void av_fifo_free(AVFifoBuffer *f)
{
if (f) {
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index 66fb48a..f3bdcbc 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -42,6 +42,14 @@ typedef struct AVFifoBuffer {
AVFifoBuffer *av_fifo_alloc(unsigned int size);
/**
+ * Initialize an AVFifoBuffer.
+ * @param nmemb number of elements
+ * @param size size of the single element
+ * @return AVFifoBuffer or NULL in case of memory allocation failure
+ */
+AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
+
+/**
* Free an AVFifoBuffer.
* @param f AVFifoBuffer to free
*/
--
1.9.1
More information about the ffmpeg-devel
mailing list