diff mbox series

[FFmpeg-devel,21/42] avcodec/refstruct: Allow to always return zeroed pool entries

Message ID AS8P250MB0744E2F4748DFD32EEC0C8A38FFAA@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM
State New
Headers show
Series New API for reference counting and ThreadFrames | expand

Commit Message

Andreas Rheinhardt Sept. 19, 2023, 7:57 p.m. UTC
This is in preparation for the following commit.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/refstruct.c | 10 ++++++++++
 libavcodec/refstruct.h |  8 ++++++++
 2 files changed, 18 insertions(+)

Comments

Anton Khirnov Oct. 12, 2023, 12:45 p.m. UTC | #1
Quoting Andreas Rheinhardt (2023-09-19 21:57:13)
> diff --git a/libavcodec/refstruct.h b/libavcodec/refstruct.h
> Furthermore, it also makes the pool behave as if the
> FF_REFSTRUCT_POOL_FLAG_NO_ZEROING flag had been provided.

What does this imply for the caller?
Andreas Rheinhardt Oct. 12, 2023, 1:25 p.m. UTC | #2
Anton Khirnov:
> Quoting Andreas Rheinhardt (2023-09-19 21:57:13)
>> diff --git a/libavcodec/refstruct.h b/libavcodec/refstruct.h
>> Furthermore, it also makes the pool behave as if the
>> FF_REFSTRUCT_POOL_FLAG_NO_ZEROING flag had been provided.
> 
> What does this imply for the caller?
> 

It means that if you have an init callback, that the object has not been
zeroed before it is given to you. So the init function would probably be
only useful for logging. If you don't have an init callback at all, you
won't notice the difference.
The rationale for this behaviour is that it makes no sense to zero
initially if it is zeroed generically before every use and I want to
spare the user to set the FF_REFSTRUCT_POOL_FLAG_NO_ZEROING flag (which
could be easily forgotten).

- Andreas
Anton Khirnov Oct. 12, 2023, 1:56 p.m. UTC | #3
Quoting Andreas Rheinhardt (2023-10-12 15:25:14)
> Anton Khirnov:
> > Quoting Andreas Rheinhardt (2023-09-19 21:57:13)
> >> diff --git a/libavcodec/refstruct.h b/libavcodec/refstruct.h
> >> Furthermore, it also makes the pool behave as if the
> >> FF_REFSTRUCT_POOL_FLAG_NO_ZEROING flag had been provided.
> > 
> > What does this imply for the caller?
> > 
> 
> It means that if you have an init callback, that the object has not been
> zeroed before it is given to you. So the init function would probably be
> only useful for logging. If you don't have an init callback at all, you
> won't notice the difference.
> The rationale for this behaviour is that it makes no sense to zero
> initially if it is zeroed generically before every use and I want to
> spare the user to set the FF_REFSTRUCT_POOL_FLAG_NO_ZEROING flag (which
> could be easily forgotten).

Can you mention this in the docs then?
diff mbox series

Patch

diff --git a/libavcodec/refstruct.c b/libavcodec/refstruct.c
index 7539b7942e..f8d040874d 100644
--- a/libavcodec/refstruct.c
+++ b/libavcodec/refstruct.c
@@ -262,6 +262,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;
@@ -334,6 +338,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 = pthread_mutex_init(&pool->mutex, NULL);
diff --git a/libavcodec/refstruct.h b/libavcodec/refstruct.h
index e2fe45e77d..ce3830977f 100644
--- a/libavcodec/refstruct.h
+++ b/libavcodec/refstruct.h
@@ -207,6 +207,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, 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)