summaryrefslogtreecommitdiff
path: root/libavutil/buffer.c
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2016-01-09 09:25:32 +0100
committerAnton Khirnov <anton@khirnov.net>2016-02-14 21:24:39 +0100
commit721a4efc0545548a241080b53ab480e34f366240 (patch)
tree18a42b31590ac34907c2f76d77b92078c2f0f656 /libavutil/buffer.c
parentc084d6d2cfb570b10d8784eb20cc696dfb7c5605 (diff)
downloadffmpeg-721a4efc0545548a241080b53ab480e34f366240.tar.gz
buffer: add support for pools using caller data in allocation
This should allow using more complex allocators than simple malloc wrappers.
Diffstat (limited to 'libavutil/buffer.c')
-rw-r--r--libavutil/buffer.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/libavutil/buffer.c b/libavutil/buffer.c
index 1bc4a93f38..6681002345 100644
--- a/libavutil/buffer.c
+++ b/libavutil/buffer.c
@@ -194,6 +194,26 @@ int av_buffer_realloc(AVBufferRef **pbuf, int size)
return 0;
}
+AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
+ AVBufferRef* (*alloc)(void *opaque, int size),
+ void (*pool_free)(void *opaque))
+{
+ AVBufferPool *pool = av_mallocz(sizeof(*pool));
+ if (!pool)
+ return NULL;
+
+ ff_mutex_init(&pool->mutex, NULL);
+
+ pool->size = size;
+ pool->opaque = opaque;
+ pool->alloc2 = alloc;
+ pool->pool_free = pool_free;
+
+ avpriv_atomic_int_set(&pool->refcount, 1);
+
+ return pool;
+}
+
AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
{
AVBufferPool *pool = av_mallocz(sizeof(*pool));
@@ -224,6 +244,10 @@ static void buffer_pool_free(AVBufferPool *pool)
av_freep(&buf);
}
ff_mutex_destroy(&pool->mutex);
+
+ if (pool->pool_free)
+ pool->pool_free(pool->opaque);
+
av_freep(&pool);
}
@@ -261,7 +285,8 @@ static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
BufferPoolEntry *buf;
AVBufferRef *ret;
- ret = pool->alloc(pool->size);
+ ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
+ pool->alloc(pool->size);
if (!ret)
return NULL;