From 56dd585146288070c98a9429ae85dc3551940a4b Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Thu, 4 Aug 2022 08:40:57 +0200 Subject: [PATCH] avcodec/refstruct: Allow checking for exclusive ownership This is the analog of av_buffer_is_writable(); it will be used in the next commit. Signed-off-by: Andreas Rheinhardt --- libavcodec/refstruct.c | 16 ++++++++++++++++ libavcodec/refstruct.h | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/libavcodec/refstruct.c b/libavcodec/refstruct.c index 75323e47a5..9a6575f987 100644 --- a/libavcodec/refstruct.c +++ b/libavcodec/refstruct.c @@ -71,6 +71,13 @@ static RefCount *get_refcount(void *obj) return ref; } +static const RefCount *cget_refcount(const void *obj) +{ + const RefCount *ref = (const RefCount*)((const char*)obj - REFCOUNT_OFFSET); + ff_assert(ref->cookie == REFSTRUCT_COOKIE); + return ref; +} + static void *get_userdata(void *buf) { return (char*)buf + REFCOUNT_OFFSET; @@ -164,3 +171,12 @@ void ff_refstruct_replace(void *dstp, const void *src) memcpy(dstp, &dst, sizeof(dst)); } } + +int ff_refstruct_exclusive(const void *obj) +{ + const RefCount *ref = cget_refcount(obj); + /* Casting const away here is safe, because it is a load. + * It is necessary because atomic_load_explicit() does not + * accept const atomics in C11 (see also N1807). */ + return atomic_load_explicit((atomic_uintptr_t*)&ref->refcount, memory_order_acquire) == 1; +} diff --git a/libavcodec/refstruct.h b/libavcodec/refstruct.h index 0086717c17..ee6936d77a 100644 --- a/libavcodec/refstruct.h +++ b/libavcodec/refstruct.h @@ -142,4 +142,13 @@ const void *ff_refstruct_ref_c(const void *obj); */ void ff_refstruct_replace(void *dstp, const void *src); +/** + * Check whether the reference count of an object managed + * via this API is 1. + * + * @param obj A pointer to an object managed via this API. + * @return 1 if the reference count of obj is 1; 0 otherwise. + */ +int ff_refstruct_exclusive(const void *obj); + #endif /* AVCODEC_REFSTRUCT_H */