[FFmpeg-devel] [PATCH] lavu/fifo: add alloc functions

Lukasz Marek lukasz.m.luki2 at gmail.com
Tue May 6 23:53:05 CEST 2014


TODO: minor bump and doc/APIchanges update

Allows to alloc fifo with predefined buffer or by
passing number of elements and size of element.

Signed-off-by: Lukasz Marek <lukasz.m.luki2 at gmail.com>
---
 libavutil/fifo.c | 31 ++++++++++++++++++++++++++-----
 libavutil/fifo.h | 19 ++++++++++++++++++-
 2 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index fabf35e..f1571e8 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -24,16 +24,37 @@
 #include "common.h"
 #include "fifo.h"
 
-AVFifoBuffer *av_fifo_alloc(unsigned int size)
+AVFifoBuffer *av_fifo_alloc_buffer(void *buffer, size_t size)
 {
-    AVFifoBuffer *f = av_mallocz(sizeof(AVFifoBuffer));
+    AVFifoBuffer *f;
+    if (!buffer)
+        return NULL;
+    f = av_mallocz(sizeof(AVFifoBuffer));
     if (!f)
         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)
+{
+    AVFifoBuffer *f;
+    void *buffer = av_malloc(size);
+    f = av_fifo_alloc_buffer(buffer, size);
+    if (!f)
+        av_free(buffer);
+    return f;
+}
+
+AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
+{
+    AVFifoBuffer *f;
+    void *buffer = av_mallocz_array(nmemb, size);
+    f = av_fifo_alloc_buffer(buffer, nmemb * size);
+    if (!f)
+        av_free(buffer);
     return f;
 }
 
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index 66fb48a..6a6f7de 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -36,12 +36,29 @@ typedef struct AVFifoBuffer {
 
 /**
  * Initialize an AVFifoBuffer.
- * @param size of FIFO
+ * @param size of FIFO in bytes
  * @return AVFifoBuffer or NULL in case of memory allocation failure
  */
 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);
+
+/**
+ * Initialize an AVFifoBuffer.
+ * @param buffer existing buffer
+ * @param size   size of the buffer
+ * @return AVFifoBuffer or NULL in case of memory allocation failure
+ * @note buffer is not freed in case of failure
+ */
+AVFifoBuffer *av_fifo_alloc_buffer(void *buffer, size_t size);
+
+/**
  * Free an AVFifoBuffer.
  * @param f AVFifoBuffer to free
  */
-- 
1.9.1



More information about the ffmpeg-devel mailing list