diff mbox series

[FFmpeg-devel] avutil/buffer: free all pooled buffers immediately after uninitializing the pool

Message ID 20210220132147.772-1-jamrial@gmail.com
State Accepted
Commit 45a29029761297535296ddf66bcdeca3086a468d
Headers show
Series [FFmpeg-devel] avutil/buffer: free all pooled buffers immediately after uninitializing the pool
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

James Almer Feb. 20, 2021, 1:21 p.m. UTC
No buffer will be fetched from the pool after it's uninitialized, so there's
no benefit from waiting until every single buffer has been returned to it
before freeing them all.
This should free some memory in certain scenarios, which can be beneficial in
low memory systems.

Based on a patch by Jonas Karlman.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavutil/buffer.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

Comments

Anton Khirnov Feb. 24, 2021, 8:23 a.m. UTC | #1
Quoting James Almer (2021-02-20 14:21:47)
> No buffer will be fetched from the pool after it's uninitialized, so there's
> no benefit from waiting until every single buffer has been returned to it
> before freeing them all.
> This should free some memory in certain scenarios, which can be beneficial in
> low memory systems.
> 
> Based on a patch by Jonas Karlman.
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavutil/buffer.c | 19 ++++++++++++++-----
>  1 file changed, 14 insertions(+), 5 deletions(-)

Looks ok.
James Almer Feb. 24, 2021, 1:47 p.m. UTC | #2
On 2/24/2021 5:23 AM, Anton Khirnov wrote:
> Quoting James Almer (2021-02-20 14:21:47)
>> No buffer will be fetched from the pool after it's uninitialized, so there's
>> no benefit from waiting until every single buffer has been returned to it
>> before freeing them all.
>> This should free some memory in certain scenarios, which can be beneficial in
>> low memory systems.
>>
>> Based on a patch by Jonas Karlman.
>>
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>>   libavutil/buffer.c | 19 ++++++++++++++-----
>>   1 file changed, 14 insertions(+), 5 deletions(-)
> 
> Looks ok.

Pushed, thanks.
diff mbox series

Patch

diff --git a/libavutil/buffer.c b/libavutil/buffer.c
index d67b4bbdaf..3204f11b68 100644
--- a/libavutil/buffer.c
+++ b/libavutil/buffer.c
@@ -279,11 +279,7 @@  AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
     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;
@@ -292,6 +288,15 @@  static void buffer_pool_free(AVBufferPool *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)
@@ -309,6 +314,10 @@  void av_buffer_pool_uninit(AVBufferPool **ppool)
     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);
 }