2012-10-07 13:45:44 +00:00
|
|
|
/*
|
2013-03-08 15:01:00 +00:00
|
|
|
* This file is part of FFmpeg.
|
2012-10-07 13:45:44 +00:00
|
|
|
*
|
2013-03-08 15:01:00 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-10-07 13:45:44 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2013-03-08 15:01:00 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-10-07 13:45:44 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2013-03-08 15:01:00 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-10-07 13:45:44 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2016-07-17 20:24:35 +00:00
|
|
|
#include <stdatomic.h>
|
2012-10-07 13:45:44 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-12-07 00:56:56 +00:00
|
|
|
#include "avassert.h"
|
2012-10-07 13:45:44 +00:00
|
|
|
#include "buffer_internal.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "mem.h"
|
lavu: fix memory leaks by using a mutex instead of atomics
The buffer pool has to atomically add and remove entries from the linked
list of available buffers. This was done by removing the entire list
with a CAS operation, working on it, and then setting it back again
(using a retry-loop in case another thread was doing the same thing).
This could effectively cause memory leaks: while a thread was working on
the buffer list, other threads would allocate new buffers, increasing
the pool's total size. There was no real leak, but since these extra
buffers were not needed, but not free'd either (except when the buffer
pool was destroyed), this had the same effects as a real leak. For some
reason, growth was exponential, and could easily kill the process due
to OOM in real-world uses.
Fix this by using a mutex to protect the list operations. The fancy
way atomics remove the whole list to work on it is not needed anymore,
which also avoids the situation which was causing the leak.
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2014-11-14 12:34:50 +00:00
|
|
|
#include "thread.h"
|
2012-10-07 13:45:44 +00:00
|
|
|
|
2021-09-14 09:29:23 +00:00
|
|
|
static AVBufferRef *buffer_create(AVBuffer *buf, uint8_t *data, size_t size,
|
|
|
|
void (*free)(void *opaque, uint8_t *data),
|
|
|
|
void *opaque, int flags)
|
2012-10-07 13:45:44 +00:00
|
|
|
{
|
|
|
|
AVBufferRef *ref = NULL;
|
|
|
|
|
|
|
|
buf->data = data;
|
|
|
|
buf->size = size;
|
|
|
|
buf->free = free ? free : av_buffer_default_free;
|
|
|
|
buf->opaque = opaque;
|
2016-07-17 20:24:35 +00:00
|
|
|
|
|
|
|
atomic_init(&buf->refcount, 1);
|
2012-10-07 13:45:44 +00:00
|
|
|
|
2020-06-01 14:22:01 +00:00
|
|
|
buf->flags = flags;
|
2012-10-07 13:45:44 +00:00
|
|
|
|
|
|
|
ref = av_mallocz(sizeof(*ref));
|
2021-09-14 09:29:23 +00:00
|
|
|
if (!ref)
|
2012-10-07 13:45:44 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ref->buffer = buf;
|
|
|
|
ref->data = data;
|
|
|
|
ref->size = size;
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
2021-09-14 09:29:23 +00:00
|
|
|
AVBufferRef *av_buffer_create(uint8_t *data, size_t size,
|
|
|
|
void (*free)(void *opaque, uint8_t *data),
|
|
|
|
void *opaque, int flags)
|
|
|
|
{
|
|
|
|
AVBufferRef *ret;
|
|
|
|
AVBuffer *buf = av_mallocz(sizeof(*buf));
|
|
|
|
if (!buf)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = buffer_create(buf, data, size, free, opaque, flags);
|
|
|
|
if (!ret) {
|
|
|
|
av_free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-07 13:45:44 +00:00
|
|
|
void av_buffer_default_free(void *opaque, uint8_t *data)
|
|
|
|
{
|
|
|
|
av_free(data);
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:59:32 +00:00
|
|
|
AVBufferRef *av_buffer_alloc(size_t size)
|
2012-10-07 13:45:44 +00:00
|
|
|
{
|
|
|
|
AVBufferRef *ret = NULL;
|
|
|
|
uint8_t *data = NULL;
|
|
|
|
|
|
|
|
data = av_malloc(size);
|
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
|
|
|
|
if (!ret)
|
|
|
|
av_freep(&data);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:59:32 +00:00
|
|
|
AVBufferRef *av_buffer_allocz(size_t size)
|
2012-10-07 13:45:44 +00:00
|
|
|
{
|
|
|
|
AVBufferRef *ret = av_buffer_alloc(size);
|
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(ret->data, 0, size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-17 03:06:45 +00:00
|
|
|
AVBufferRef *av_buffer_ref(const AVBufferRef *buf)
|
2012-10-07 13:45:44 +00:00
|
|
|
{
|
|
|
|
AVBufferRef *ret = av_mallocz(sizeof(*ret));
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*ret = *buf;
|
|
|
|
|
2016-07-17 20:24:35 +00:00
|
|
|
atomic_fetch_add_explicit(&buf->buffer->refcount, 1, memory_order_relaxed);
|
2012-10-07 13:45:44 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-14 23:40:35 +00:00
|
|
|
static void buffer_replace(AVBufferRef **dst, AVBufferRef **src)
|
2012-10-07 13:45:44 +00:00
|
|
|
{
|
|
|
|
AVBuffer *b;
|
|
|
|
|
2015-01-14 23:40:35 +00:00
|
|
|
b = (*dst)->buffer;
|
|
|
|
|
|
|
|
if (src) {
|
|
|
|
**dst = **src;
|
|
|
|
av_freep(src);
|
|
|
|
} else
|
|
|
|
av_freep(dst);
|
2012-10-07 13:45:44 +00:00
|
|
|
|
2019-12-05 09:02:07 +00:00
|
|
|
if (atomic_fetch_sub_explicit(&b->refcount, 1, memory_order_acq_rel) == 1) {
|
2021-09-14 09:29:23 +00:00
|
|
|
/* 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);
|
2012-10-07 13:45:44 +00:00
|
|
|
b->free(b->opaque, b->data);
|
2021-09-14 09:29:23 +00:00
|
|
|
if (free_avbuffer)
|
|
|
|
av_free(b);
|
2012-10-07 13:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 23:40:35 +00:00
|
|
|
void av_buffer_unref(AVBufferRef **buf)
|
|
|
|
{
|
|
|
|
if (!buf || !*buf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
buffer_replace(buf, NULL);
|
|
|
|
}
|
|
|
|
|
2012-10-07 13:45:44 +00:00
|
|
|
int av_buffer_is_writable(const AVBufferRef *buf)
|
|
|
|
{
|
|
|
|
if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
|
|
|
|
return 0;
|
|
|
|
|
2016-07-17 20:24:35 +00:00
|
|
|
return atomic_load(&buf->buffer->refcount) == 1;
|
2012-10-07 13:45:44 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 15:15:19 +00:00
|
|
|
void *av_buffer_get_opaque(const AVBufferRef *buf)
|
|
|
|
{
|
|
|
|
return buf->buffer->opaque;
|
|
|
|
}
|
|
|
|
|
2013-03-30 18:13:26 +00:00
|
|
|
int av_buffer_get_ref_count(const AVBufferRef *buf)
|
|
|
|
{
|
2017-03-22 16:46:01 +00:00
|
|
|
return atomic_load(&buf->buffer->refcount);
|
2013-03-30 18:13:26 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 13:45:44 +00:00
|
|
|
int av_buffer_make_writable(AVBufferRef **pbuf)
|
|
|
|
{
|
|
|
|
AVBufferRef *newbuf, *buf = *pbuf;
|
|
|
|
|
|
|
|
if (av_buffer_is_writable(buf))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
newbuf = av_buffer_alloc(buf->size);
|
|
|
|
if (!newbuf)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
memcpy(newbuf->data, buf->data, buf->size);
|
2015-01-14 23:42:55 +00:00
|
|
|
|
|
|
|
buffer_replace(pbuf, &newbuf);
|
2012-10-07 13:45:44 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:59:32 +00:00
|
|
|
int av_buffer_realloc(AVBufferRef **pbuf, size_t size)
|
2012-10-07 13:45:44 +00:00
|
|
|
{
|
|
|
|
AVBufferRef *buf = *pbuf;
|
|
|
|
uint8_t *tmp;
|
2020-01-04 18:52:08 +00:00
|
|
|
int ret;
|
2012-10-07 13:45:44 +00:00
|
|
|
|
|
|
|
if (!buf) {
|
|
|
|
/* allocate a new buffer with av_realloc(), so it will be reallocatable
|
|
|
|
* later */
|
|
|
|
uint8_t *data = av_realloc(NULL, size);
|
|
|
|
if (!data)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
|
|
|
|
if (!buf) {
|
|
|
|
av_freep(&data);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:22:01 +00:00
|
|
|
buf->buffer->flags_internal |= BUFFER_FLAG_REALLOCATABLE;
|
2012-10-07 13:45:44 +00:00
|
|
|
*pbuf = buf;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else if (buf->size == size)
|
|
|
|
return 0;
|
|
|
|
|
2020-06-01 14:22:01 +00:00
|
|
|
if (!(buf->buffer->flags_internal & BUFFER_FLAG_REALLOCATABLE) ||
|
2016-08-02 19:16:44 +00:00
|
|
|
!av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {
|
2012-10-07 13:45:44 +00:00
|
|
|
/* cannot realloc, allocate a new reallocable buffer and copy data */
|
|
|
|
AVBufferRef *new = NULL;
|
|
|
|
|
2020-01-04 18:52:08 +00:00
|
|
|
ret = av_buffer_realloc(&new, size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-10-07 13:45:44 +00:00
|
|
|
|
|
|
|
memcpy(new->data, buf->data, FFMIN(size, buf->size));
|
|
|
|
|
2015-01-14 23:42:55 +00:00
|
|
|
buffer_replace(pbuf, &new);
|
2012-10-07 13:45:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = av_realloc(buf->buffer->data, size);
|
|
|
|
if (!tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
buf->buffer->data = buf->data = tmp;
|
|
|
|
buf->buffer->size = buf->size = size;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-20 07:03:13 +00:00
|
|
|
|
2021-09-17 03:06:45 +00:00
|
|
|
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
|
2020-06-05 09:17:49 +00:00
|
|
|
{
|
|
|
|
AVBufferRef *dst = *pdst;
|
|
|
|
AVBufferRef *tmp;
|
|
|
|
|
|
|
|
if (!src) {
|
|
|
|
av_buffer_unref(pdst);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dst && dst->buffer == src->buffer) {
|
|
|
|
/* make sure the data pointers match */
|
|
|
|
dst->data = src->data;
|
|
|
|
dst->size = src->size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = av_buffer_ref(src);
|
|
|
|
if (!tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
av_buffer_unref(pdst);
|
|
|
|
*pdst = tmp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:59:32 +00:00
|
|
|
AVBufferPool *av_buffer_pool_init2(size_t size, void *opaque,
|
|
|
|
AVBufferRef* (*alloc)(void *opaque, size_t size),
|
2016-01-09 08:25:32 +00:00
|
|
|
void (*pool_free)(void *opaque))
|
|
|
|
{
|
|
|
|
AVBufferPool *pool = av_mallocz(sizeof(*pool));
|
|
|
|
if (!pool)
|
|
|
|
return NULL;
|
|
|
|
|
2024-07-11 20:55:31 +00:00
|
|
|
if (ff_mutex_init(&pool->mutex, NULL)) {
|
|
|
|
av_free(pool);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-09 08:25:32 +00:00
|
|
|
|
|
|
|
pool->size = size;
|
|
|
|
pool->opaque = opaque;
|
|
|
|
pool->alloc2 = alloc;
|
2020-06-01 13:25:09 +00:00
|
|
|
pool->alloc = av_buffer_alloc; // fallback
|
2016-01-09 08:25:32 +00:00
|
|
|
pool->pool_free = pool_free;
|
|
|
|
|
2016-07-17 20:24:35 +00:00
|
|
|
atomic_init(&pool->refcount, 1);
|
2016-01-09 08:25:32 +00:00
|
|
|
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:59:32 +00:00
|
|
|
AVBufferPool *av_buffer_pool_init(size_t size, AVBufferRef* (*alloc)(size_t size))
|
2013-01-20 07:03:13 +00:00
|
|
|
{
|
|
|
|
AVBufferPool *pool = av_mallocz(sizeof(*pool));
|
|
|
|
if (!pool)
|
|
|
|
return NULL;
|
|
|
|
|
2024-07-11 20:55:31 +00:00
|
|
|
if (ff_mutex_init(&pool->mutex, NULL)) {
|
|
|
|
av_free(pool);
|
|
|
|
return NULL;
|
|
|
|
}
|
lavu: fix memory leaks by using a mutex instead of atomics
The buffer pool has to atomically add and remove entries from the linked
list of available buffers. This was done by removing the entire list
with a CAS operation, working on it, and then setting it back again
(using a retry-loop in case another thread was doing the same thing).
This could effectively cause memory leaks: while a thread was working on
the buffer list, other threads would allocate new buffers, increasing
the pool's total size. There was no real leak, but since these extra
buffers were not needed, but not free'd either (except when the buffer
pool was destroyed), this had the same effects as a real leak. For some
reason, growth was exponential, and could easily kill the process due
to OOM in real-world uses.
Fix this by using a mutex to protect the list operations. The fancy
way atomics remove the whole list to work on it is not needed anymore,
which also avoids the situation which was causing the leak.
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2014-11-14 12:34:50 +00:00
|
|
|
|
2013-01-20 07:03:13 +00:00
|
|
|
pool->size = size;
|
|
|
|
pool->alloc = alloc ? alloc : av_buffer_alloc;
|
|
|
|
|
2016-07-17 20:24:35 +00:00
|
|
|
atomic_init(&pool->refcount, 1);
|
2013-01-20 07:03:13 +00:00
|
|
|
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
2021-02-20 13:21:47 +00:00
|
|
|
static void buffer_pool_flush(AVBufferPool *pool)
|
2013-01-20 07:03:13 +00:00
|
|
|
{
|
|
|
|
while (pool->pool) {
|
|
|
|
BufferPoolEntry *buf = pool->pool;
|
|
|
|
pool->pool = buf->next;
|
|
|
|
|
|
|
|
buf->free(buf->opaque, buf->data);
|
|
|
|
av_freep(&buf);
|
|
|
|
}
|
2021-02-20 13:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
lavu: fix memory leaks by using a mutex instead of atomics
The buffer pool has to atomically add and remove entries from the linked
list of available buffers. This was done by removing the entire list
with a CAS operation, working on it, and then setting it back again
(using a retry-loop in case another thread was doing the same thing).
This could effectively cause memory leaks: while a thread was working on
the buffer list, other threads would allocate new buffers, increasing
the pool's total size. There was no real leak, but since these extra
buffers were not needed, but not free'd either (except when the buffer
pool was destroyed), this had the same effects as a real leak. For some
reason, growth was exponential, and could easily kill the process due
to OOM in real-world uses.
Fix this by using a mutex to protect the list operations. The fancy
way atomics remove the whole list to work on it is not needed anymore,
which also avoids the situation which was causing the leak.
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2014-11-14 12:34:50 +00:00
|
|
|
ff_mutex_destroy(&pool->mutex);
|
2016-01-09 08:25:32 +00:00
|
|
|
|
|
|
|
if (pool->pool_free)
|
|
|
|
pool->pool_free(pool->opaque);
|
|
|
|
|
2013-01-20 07:03:13 +00:00
|
|
|
av_freep(&pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_buffer_pool_uninit(AVBufferPool **ppool)
|
|
|
|
{
|
|
|
|
AVBufferPool *pool;
|
|
|
|
|
|
|
|
if (!ppool || !*ppool)
|
|
|
|
return;
|
|
|
|
pool = *ppool;
|
|
|
|
*ppool = NULL;
|
|
|
|
|
2021-02-20 13:21:47 +00:00
|
|
|
ff_mutex_lock(&pool->mutex);
|
|
|
|
buffer_pool_flush(pool);
|
|
|
|
ff_mutex_unlock(&pool->mutex);
|
|
|
|
|
2019-12-05 09:02:07 +00:00
|
|
|
if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
|
2013-01-20 07:03:13 +00:00
|
|
|
buffer_pool_free(pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pool_release_buffer(void *opaque, uint8_t *data)
|
|
|
|
{
|
|
|
|
BufferPoolEntry *buf = opaque;
|
|
|
|
AVBufferPool *pool = buf->pool;
|
2013-03-24 00:44:48 +00:00
|
|
|
|
lavu: fix memory leaks by using a mutex instead of atomics
The buffer pool has to atomically add and remove entries from the linked
list of available buffers. This was done by removing the entire list
with a CAS operation, working on it, and then setting it back again
(using a retry-loop in case another thread was doing the same thing).
This could effectively cause memory leaks: while a thread was working on
the buffer list, other threads would allocate new buffers, increasing
the pool's total size. There was no real leak, but since these extra
buffers were not needed, but not free'd either (except when the buffer
pool was destroyed), this had the same effects as a real leak. For some
reason, growth was exponential, and could easily kill the process due
to OOM in real-world uses.
Fix this by using a mutex to protect the list operations. The fancy
way atomics remove the whole list to work on it is not needed anymore,
which also avoids the situation which was causing the leak.
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2014-11-14 12:34:50 +00:00
|
|
|
ff_mutex_lock(&pool->mutex);
|
|
|
|
buf->next = pool->pool;
|
|
|
|
pool->pool = buf;
|
|
|
|
ff_mutex_unlock(&pool->mutex);
|
|
|
|
|
2019-12-05 09:02:07 +00:00
|
|
|
if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
|
2013-01-20 07:03:13 +00:00
|
|
|
buffer_pool_free(pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a new buffer and override its free() callback so that
|
|
|
|
* it is returned to the pool on free */
|
|
|
|
static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
|
|
|
|
{
|
|
|
|
BufferPoolEntry *buf;
|
|
|
|
AVBufferRef *ret;
|
|
|
|
|
2020-06-01 13:25:09 +00:00
|
|
|
av_assert0(pool->alloc || pool->alloc2);
|
|
|
|
|
2016-01-09 08:25:32 +00:00
|
|
|
ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
|
|
|
|
pool->alloc(pool->size);
|
2013-01-20 07:03:13 +00:00
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
buf = av_mallocz(sizeof(*buf));
|
|
|
|
if (!buf) {
|
|
|
|
av_buffer_unref(&ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->data = ret->buffer->data;
|
|
|
|
buf->opaque = ret->buffer->opaque;
|
|
|
|
buf->free = ret->buffer->free;
|
|
|
|
buf->pool = pool;
|
|
|
|
|
|
|
|
ret->buffer->opaque = buf;
|
|
|
|
ret->buffer->free = pool_release_buffer;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
|
|
|
|
{
|
|
|
|
AVBufferRef *ret;
|
|
|
|
BufferPoolEntry *buf;
|
|
|
|
|
lavu: fix memory leaks by using a mutex instead of atomics
The buffer pool has to atomically add and remove entries from the linked
list of available buffers. This was done by removing the entire list
with a CAS operation, working on it, and then setting it back again
(using a retry-loop in case another thread was doing the same thing).
This could effectively cause memory leaks: while a thread was working on
the buffer list, other threads would allocate new buffers, increasing
the pool's total size. There was no real leak, but since these extra
buffers were not needed, but not free'd either (except when the buffer
pool was destroyed), this had the same effects as a real leak. For some
reason, growth was exponential, and could easily kill the process due
to OOM in real-world uses.
Fix this by using a mutex to protect the list operations. The fancy
way atomics remove the whole list to work on it is not needed anymore,
which also avoids the situation which was causing the leak.
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2014-11-14 12:34:50 +00:00
|
|
|
ff_mutex_lock(&pool->mutex);
|
|
|
|
buf = pool->pool;
|
|
|
|
if (buf) {
|
2021-09-14 09:29:23 +00:00
|
|
|
memset(&buf->buffer, 0, sizeof(buf->buffer));
|
|
|
|
ret = buffer_create(&buf->buffer, buf->data, pool->size,
|
|
|
|
pool_release_buffer, buf, 0);
|
lavu: fix memory leaks by using a mutex instead of atomics
The buffer pool has to atomically add and remove entries from the linked
list of available buffers. This was done by removing the entire list
with a CAS operation, working on it, and then setting it back again
(using a retry-loop in case another thread was doing the same thing).
This could effectively cause memory leaks: while a thread was working on
the buffer list, other threads would allocate new buffers, increasing
the pool's total size. There was no real leak, but since these extra
buffers were not needed, but not free'd either (except when the buffer
pool was destroyed), this had the same effects as a real leak. For some
reason, growth was exponential, and could easily kill the process due
to OOM in real-world uses.
Fix this by using a mutex to protect the list operations. The fancy
way atomics remove the whole list to work on it is not needed anymore,
which also avoids the situation which was causing the leak.
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2014-11-14 12:34:50 +00:00
|
|
|
if (ret) {
|
|
|
|
pool->pool = buf->next;
|
|
|
|
buf->next = NULL;
|
2021-09-14 09:29:23 +00:00
|
|
|
buf->buffer.flags_internal |= BUFFER_FLAG_NO_FREE;
|
lavu: fix memory leaks by using a mutex instead of atomics
The buffer pool has to atomically add and remove entries from the linked
list of available buffers. This was done by removing the entire list
with a CAS operation, working on it, and then setting it back again
(using a retry-loop in case another thread was doing the same thing).
This could effectively cause memory leaks: while a thread was working on
the buffer list, other threads would allocate new buffers, increasing
the pool's total size. There was no real leak, but since these extra
buffers were not needed, but not free'd either (except when the buffer
pool was destroyed), this had the same effects as a real leak. For some
reason, growth was exponential, and could easily kill the process due
to OOM in real-world uses.
Fix this by using a mutex to protect the list operations. The fancy
way atomics remove the whole list to work on it is not needed anymore,
which also avoids the situation which was causing the leak.
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2014-11-14 12:34:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = pool_alloc_buffer(pool);
|
2013-01-20 07:03:13 +00:00
|
|
|
}
|
lavu: fix memory leaks by using a mutex instead of atomics
The buffer pool has to atomically add and remove entries from the linked
list of available buffers. This was done by removing the entire list
with a CAS operation, working on it, and then setting it back again
(using a retry-loop in case another thread was doing the same thing).
This could effectively cause memory leaks: while a thread was working on
the buffer list, other threads would allocate new buffers, increasing
the pool's total size. There was no real leak, but since these extra
buffers were not needed, but not free'd either (except when the buffer
pool was destroyed), this had the same effects as a real leak. For some
reason, growth was exponential, and could easily kill the process due
to OOM in real-world uses.
Fix this by using a mutex to protect the list operations. The fancy
way atomics remove the whole list to work on it is not needed anymore,
which also avoids the situation which was causing the leak.
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2014-11-14 12:34:50 +00:00
|
|
|
ff_mutex_unlock(&pool->mutex);
|
|
|
|
|
|
|
|
if (ret)
|
2016-07-17 20:24:35 +00:00
|
|
|
atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
|
2013-01-20 07:03:13 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-12-07 00:56:56 +00:00
|
|
|
|
2021-09-17 03:06:45 +00:00
|
|
|
void *av_buffer_pool_buffer_get_opaque(const AVBufferRef *ref)
|
2019-12-07 00:56:56 +00:00
|
|
|
{
|
|
|
|
BufferPoolEntry *buf = ref->buffer->opaque;
|
|
|
|
av_assert0(buf);
|
|
|
|
return buf->opaque;
|
|
|
|
}
|