From d17953b287e0fa9d61c9aada7e36c03403b2fd69 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Thu, 4 Aug 2022 10:16:51 +0200 Subject: lavu/buffer: convert to AVRefcount WIP --- libavutil/buffer.c | 22 ++++++++++++---------- libavutil/buffer_internal.h | 5 ++++- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/libavutil/buffer.c b/libavutil/buffer.c index 54590be566..6612183094 100644 --- a/libavutil/buffer.c +++ b/libavutil/buffer.c @@ -24,6 +24,7 @@ #include "buffer_internal.h" #include "common.h" #include "mem.h" +#include "refcount.h" #include "thread.h" static AVBufferRef *buffer_create(AVBuffer *buf, uint8_t *data, size_t size, @@ -37,7 +38,7 @@ static AVBufferRef *buffer_create(AVBuffer *buf, uint8_t *data, size_t size, buf->free = free ? free : av_buffer_default_free; buf->opaque = opaque; - atomic_init(&buf->refcount, 1); + av_refcount_init(&buf->rc, NULL, NULL); buf->flags = flags; @@ -109,7 +110,7 @@ AVBufferRef *av_buffer_ref(const AVBufferRef *buf) *ret = *buf; - atomic_fetch_add_explicit(&buf->buffer->refcount, 1, memory_order_relaxed); + av_refcount_inc(&buf->buffer->rc); return ret; } @@ -126,7 +127,7 @@ static void buffer_replace(AVBufferRef **dst, AVBufferRef **src) } else av_freep(dst); - if (atomic_fetch_sub_explicit(&b->refcount, 1, memory_order_acq_rel) == 1) { + if (!av_refcount_dec(&bc->rc)) { /* b->free below might already free the structure containing *b, * so we have to read the flag now to avoid use-after-free. */ int free_avbuffer = !(b->flags_internal & BUFFER_FLAG_NO_FREE); @@ -149,7 +150,7 @@ int av_buffer_is_writable(const AVBufferRef *buf) if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY) return 0; - return atomic_load(&buf->buffer->refcount) == 1; + return av_refcount_exclusive(&buf->buffer->rc); } void *av_buffer_get_opaque(const AVBufferRef *buf) @@ -159,7 +160,8 @@ void *av_buffer_get_opaque(const AVBufferRef *buf) int av_buffer_get_ref_count(const AVBufferRef *buf) { - return atomic_load(&buf->buffer->refcount); + //return atomic_load(&buf->buffer->refcount); + return 1; } int av_buffer_make_writable(AVBufferRef **pbuf) @@ -272,7 +274,7 @@ AVBufferPool *av_buffer_pool_init2(size_t size, void *opaque, pool->alloc = av_buffer_alloc; // fallback pool->pool_free = pool_free; - atomic_init(&pool->refcount, 1); + av_refcount_init(&pool->rc, NULL, NULL); return pool; } @@ -288,7 +290,7 @@ AVBufferPool *av_buffer_pool_init(size_t size, AVBufferRef* (*alloc)(size_t size pool->size = size; pool->alloc = alloc ? alloc : av_buffer_alloc; - atomic_init(&pool->refcount, 1); + av_refcount_init(&pool->rc, NULL, NULL); return pool; } @@ -332,7 +334,7 @@ void av_buffer_pool_uninit(AVBufferPool **ppool) buffer_pool_flush(pool); ff_mutex_unlock(&pool->mutex); - if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1) + if (av_refcount_dec(&pool->rc) == 1) buffer_pool_free(pool); } @@ -349,7 +351,7 @@ static void pool_release_buffer(void *opaque, uint8_t *data) pool->pool = buf; ff_mutex_unlock(&pool->mutex); - if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1) + if (!av_refcount_dec(&pool->rc)) buffer_pool_free(pool); } @@ -406,7 +408,7 @@ AVBufferRef *av_buffer_pool_get(AVBufferPool *pool) ff_mutex_unlock(&pool->mutex); if (ret) - atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed); + av_refcount_inc(&pool->rc); return ret; } diff --git a/libavutil/buffer_internal.h b/libavutil/buffer_internal.h index adb916aaa2..29119b47ab 100644 --- a/libavutil/buffer_internal.h +++ b/libavutil/buffer_internal.h @@ -23,6 +23,7 @@ #include #include "buffer.h" +#include "refcount.h" #include "thread.h" /** @@ -39,6 +40,8 @@ struct AVBuffer { uint8_t *data; /**< data described by this buffer */ size_t size; /**< size of data in bytes */ + AVRefcount rc; + /** * number of existing AVBufferRef instances referring to this buffer */ @@ -98,7 +101,7 @@ struct AVBufferPool { * buffers have been released, then it's safe to free the pool and all * the buffers in it. */ - atomic_uint refcount; + AVRefcount rc; size_t size; void *opaque; -- cgit v1.2.3