diff options
author | Derek Buitenhuis <derek.buitenhuis@gmail.com> | 2016-02-17 16:07:16 +0000 |
---|---|---|
committer | Derek Buitenhuis <derek.buitenhuis@gmail.com> | 2016-02-17 16:07:16 +0000 |
commit | 26abd5149ebf9602d8036be4c6e72e08c98ea998 (patch) | |
tree | ffc931a2c7a53ff57272601f2a43dca7b799c301 /libavutil | |
parent | ae3c0a9c1f66367586c82e9b48d31bedac3e14aa (diff) | |
parent | 721a4efc0545548a241080b53ab480e34f366240 (diff) | |
download | ffmpeg-26abd5149ebf9602d8036be4c6e72e08c98ea998.tar.gz |
Merge commit '721a4efc0545548a241080b53ab480e34f366240'
* commit '721a4efc0545548a241080b53ab480e34f366240':
buffer: add support for pools using caller data in allocation
Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
Diffstat (limited to 'libavutil')
-rw-r--r-- | libavutil/buffer.c | 27 | ||||
-rw-r--r-- | libavutil/buffer.h | 17 | ||||
-rw-r--r-- | libavutil/buffer_internal.h | 3 | ||||
-rw-r--r-- | libavutil/version.h | 4 |
4 files changed, 48 insertions, 3 deletions
diff --git a/libavutil/buffer.c b/libavutil/buffer.c index bb112c238e..694e116a3c 100644 --- a/libavutil/buffer.c +++ b/libavutil/buffer.c @@ -214,6 +214,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)); @@ -244,6 +264,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); } @@ -326,7 +350,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; diff --git a/libavutil/buffer.h b/libavutil/buffer.h index b4399fd39f..207e971190 100644 --- a/libavutil/buffer.h +++ b/libavutil/buffer.h @@ -249,6 +249,23 @@ typedef struct AVBufferPool AVBufferPool; AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size)); /** + * Allocate and initialize a buffer pool with a more complex allocator. + * + * @param size size of each buffer in this pool + * @param opaque arbitrary user data used by the allocator + * @param alloc a function that will be used to allocate new buffers when the + * pool is empty. + * @param pool_free a function that will be called immediately before the pool + * is freed. I.e. after av_buffer_pool_can_uninit() is called + * by the pool and all the frames are returned to the pool and + * freed. It is intended to uninitialize the user opaque data. + * @return newly created buffer pool on success, NULL on error. + */ +AVBufferPool *av_buffer_pool_init2(int size, void *opaque, + AVBufferRef* (*alloc)(void *opaque, int size), + void (*pool_free)(void *opaque)); + +/** * Mark the pool as being available for freeing. It will actually be freed only * once all the allocated buffers associated with the pool are released. Thus it * is safe to call this function while some of the allocated buffers are still diff --git a/libavutil/buffer_internal.h b/libavutil/buffer_internal.h index e6530485d3..29ce8a643c 100644 --- a/libavutil/buffer_internal.h +++ b/libavutil/buffer_internal.h @@ -90,7 +90,10 @@ struct AVBufferPool { volatile int nb_allocated; int size; + void *opaque; AVBufferRef* (*alloc)(int size); + AVBufferRef* (*alloc2)(void *opaque, int size); + void (*pool_free)(void *opaque); }; #endif /* AVUTIL_BUFFER_INTERNAL_H */ diff --git a/libavutil/version.h b/libavutil/version.h index 0ea8c79467..9a330f994a 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -64,8 +64,8 @@ */ #define LIBAVUTIL_VERSION_MAJOR 55 -#define LIBAVUTIL_VERSION_MINOR 17 -#define LIBAVUTIL_VERSION_MICRO 103 +#define LIBAVUTIL_VERSION_MINOR 18 +#define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ LIBAVUTIL_VERSION_MINOR, \ |