#include "mem.h"
#include "thread.h"
-AVBufferRef *av_buffer_create(uint8_t *data, int size,
+AVBufferRef *av_buffer_create(uint8_t *data, buffer_size_t size,
void (*free)(void *opaque, uint8_t *data),
void *opaque, int flags)
{
av_free(data);
}
-AVBufferRef *av_buffer_alloc(int size)
+AVBufferRef *av_buffer_alloc(buffer_size_t size)
{
AVBufferRef *ret = NULL;
uint8_t *data = NULL;
return ret;
}
-AVBufferRef *av_buffer_allocz(int size)
+AVBufferRef *av_buffer_allocz(buffer_size_t size)
{
AVBufferRef *ret = av_buffer_alloc(size);
if (!ret)
return 0;
}
-int av_buffer_realloc(AVBufferRef **pbuf, int size)
+int av_buffer_realloc(AVBufferRef **pbuf, buffer_size_t size)
{
AVBufferRef *buf = *pbuf;
uint8_t *tmp;
+ int ret;
if (!buf) {
/* allocate a new buffer with av_realloc(), so it will be reallocatable
/* cannot realloc, allocate a new reallocable buffer and copy data */
AVBufferRef *new = NULL;
- av_buffer_realloc(&new, size);
- if (!new)
- return AVERROR(ENOMEM);
+ ret = av_buffer_realloc(&new, size);
+ if (ret < 0)
+ return ret;
memcpy(new->data, buf->data, FFMIN(size, buf->size));
return 0;
}
-AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
- AVBufferRef* (*alloc)(void *opaque, int size),
+int av_buffer_replace(AVBufferRef **pdst, AVBufferRef *src)
+{
+ AVBufferRef *dst = *pdst;
+ AVBufferRef *tmp;
+
+ if (!src) {
+ av_buffer_unref(pdst);
+ return 0;
+ }
+
+ if (dst && dst->buffer == src->buffer) {
+ /* make sure the data pointers match */
+ dst->data = src->data;
+ dst->size = src->size;
+ return 0;
+ }
+
+ tmp = av_buffer_ref(src);
+ if (!tmp)
+ return AVERROR(ENOMEM);
+
+ av_buffer_unref(pdst);
+ *pdst = tmp;
+ return 0;
+}
+
+AVBufferPool *av_buffer_pool_init2(buffer_size_t size, void *opaque,
+ AVBufferRef* (*alloc)(void *opaque, buffer_size_t size),
void (*pool_free)(void *opaque))
{
AVBufferPool *pool = av_mallocz(sizeof(*pool));
return pool;
}
-AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
+AVBufferPool *av_buffer_pool_init(buffer_size_t size, AVBufferRef* (*alloc)(buffer_size_t size))
{
AVBufferPool *pool = av_mallocz(sizeof(*pool));
if (!pool)
return pool;
}
-/*
- * This function gets called when the pool has been uninited and
- * all the buffers returned to it.
- */
-static void buffer_pool_free(AVBufferPool *pool)
+static void buffer_pool_flush(AVBufferPool *pool)
{
while (pool->pool) {
BufferPoolEntry *buf = pool->pool;
buf->free(buf->opaque, buf->data);
av_freep(&buf);
}
+}
+
+/*
+ * This function gets called when the pool has been uninited and
+ * all the buffers returned to it.
+ */
+static void buffer_pool_free(AVBufferPool *pool)
+{
+ buffer_pool_flush(pool);
ff_mutex_destroy(&pool->mutex);
if (pool->pool_free)
pool = *ppool;
*ppool = NULL;
+ ff_mutex_lock(&pool->mutex);
+ buffer_pool_flush(pool);
+ ff_mutex_unlock(&pool->mutex);
+
if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
buffer_pool_free(pool);
}