From 721a4efc0545548a241080b53ab480e34f366240 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Jan 2016 09:25:32 +0100 Subject: buffer: add support for pools using caller data in allocation This should allow using more complex allocators than simple malloc wrappers. --- libavutil/buffer.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'libavutil/buffer.c') 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; -- cgit v1.2.3