[FFmpeg-cvslog] avcodec/refstruct: Allow to always return zeroed pool entries

Andreas Rheinhardt git at videolan.org
Wed Nov 1 22:02:46 EET 2023


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Sat Aug  6 13:04:10 2022 +0200| [090d9956fd092023e63f61a37bde36bba55d0258] | committer: Andreas Rheinhardt

avcodec/refstruct: Allow to always return zeroed pool entries

This is in preparation for the following commit.

Reviewed-by: Anton Khirnov <anton at khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=090d9956fd092023e63f61a37bde36bba55d0258
---

 libavcodec/refstruct.c | 10 ++++++++++
 libavcodec/refstruct.h |  8 ++++++++
 2 files changed, 18 insertions(+)

diff --git a/libavcodec/refstruct.c b/libavcodec/refstruct.c
index 5603031ec3..861f847dd7 100644
--- a/libavcodec/refstruct.c
+++ b/libavcodec/refstruct.c
@@ -285,6 +285,10 @@ static int refstruct_pool_get_ext(void *datap, FFRefStructPool *pool)
         }
     }
     atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
+
+    if (pool->pool_flags & FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME)
+        memset(ret, 0, pool->size);
+
     memcpy(datap, &ret, sizeof(ret));
 
     return 0;
@@ -357,6 +361,12 @@ FFRefStructPool *ff_refstruct_pool_alloc_ext_c(size_t size, unsigned flags,
         flags &= ~FF_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR;
     pool->pool_flags    = flags;
 
+    if (flags & FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME) {
+        // We will zero the buffer before every use, so zeroing
+        // upon allocating the buffer is unnecessary.
+        pool->entry_flags |= FF_REFSTRUCT_FLAG_NO_ZEROING;
+    }
+
     atomic_init(&pool->refcount, 1);
 
     err = ff_mutex_init(&pool->mutex, NULL);
diff --git a/libavcodec/refstruct.h b/libavcodec/refstruct.h
index defdc06d52..9a22e5dca7 100644
--- a/libavcodec/refstruct.h
+++ b/libavcodec/refstruct.h
@@ -211,6 +211,14 @@ typedef struct FFRefStructPool FFRefStructPool;
  * the callbacks applied earlier (init_cb potentially followed by reset_cb).
  */
 #define FF_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR                    (1 << 17)
+/**
+ * If this flag is set, the entries will be zeroed before
+ * being returned to the user (after the init or reset callbacks
+ * have been called (if provided)). Furthermore, to avoid zeroing twice
+ * it also makes the pool behave as if the FF_REFSTRUCT_POOL_FLAG_NO_ZEROING
+ * flag had been provided.
+ */
+#define FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME                       (1 << 18)
 
 /**
  * Equivalent to ff_refstruct_pool_alloc(size, flags, NULL, NULL, NULL, NULL, NULL)



More information about the ffmpeg-cvslog mailing list