2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <stdatomic.h>
23 #include "buffer_internal.h"
28 AVBufferRef *av_buffer_create(uint8_t *data, int size,
29 void (*free)(void *opaque, uint8_t *data),
30 void *opaque, int flags)
32 AVBufferRef *ref = NULL;
35 buf = av_mallocz(sizeof(*buf));
41 buf->free = free ? free : av_buffer_default_free;
44 atomic_init(&buf->refcount, 1);
46 if (flags & AV_BUFFER_FLAG_READONLY)
47 buf->flags |= BUFFER_FLAG_READONLY;
49 ref = av_mallocz(sizeof(*ref));
62 void av_buffer_default_free(void *opaque, uint8_t *data)
67 AVBufferRef *av_buffer_alloc(int size)
69 AVBufferRef *ret = NULL;
72 data = av_malloc(size);
76 ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
83 AVBufferRef *av_buffer_allocz(int size)
85 AVBufferRef *ret = av_buffer_alloc(size);
89 memset(ret->data, 0, size);
93 AVBufferRef *av_buffer_ref(AVBufferRef *buf)
95 AVBufferRef *ret = av_mallocz(sizeof(*ret));
102 atomic_fetch_add_explicit(&buf->buffer->refcount, 1, memory_order_relaxed);
107 void av_buffer_unref(AVBufferRef **buf)
116 if (atomic_fetch_add_explicit(&b->refcount, -1, memory_order_acq_rel) == 1) {
117 b->free(b->opaque, b->data);
122 int av_buffer_is_writable(const AVBufferRef *buf)
124 if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
127 return atomic_load(&buf->buffer->refcount) == 1;
130 int av_buffer_make_writable(AVBufferRef **pbuf)
132 AVBufferRef *newbuf, *buf = *pbuf;
134 if (av_buffer_is_writable(buf))
137 newbuf = av_buffer_alloc(buf->size);
139 return AVERROR(ENOMEM);
141 memcpy(newbuf->data, buf->data, buf->size);
142 av_buffer_unref(pbuf);
148 int av_buffer_realloc(AVBufferRef **pbuf, int size)
150 AVBufferRef *buf = *pbuf;
154 /* allocate a new buffer with av_realloc(), so it will be reallocatable
156 uint8_t *data = av_realloc(NULL, size);
158 return AVERROR(ENOMEM);
160 buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
163 return AVERROR(ENOMEM);
166 buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
170 } else if (buf->size == size)
173 if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
174 !av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {
175 /* cannot realloc, allocate a new reallocable buffer and copy data */
176 AVBufferRef *new = NULL;
178 av_buffer_realloc(&new, size);
180 return AVERROR(ENOMEM);
182 memcpy(new->data, buf->data, FFMIN(size, buf->size));
184 av_buffer_unref(pbuf);
189 tmp = av_realloc(buf->buffer->data, size);
191 return AVERROR(ENOMEM);
193 buf->buffer->data = buf->data = tmp;
194 buf->buffer->size = buf->size = size;
198 AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
199 AVBufferRef* (*alloc)(void *opaque, int size),
200 void (*pool_free)(void *opaque))
202 AVBufferPool *pool = av_mallocz(sizeof(*pool));
206 ff_mutex_init(&pool->mutex, NULL);
209 pool->opaque = opaque;
210 pool->alloc2 = alloc;
211 pool->pool_free = pool_free;
213 atomic_init(&pool->refcount, 1);
218 AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
220 AVBufferPool *pool = av_mallocz(sizeof(*pool));
224 ff_mutex_init(&pool->mutex, NULL);
227 pool->alloc = alloc ? alloc : av_buffer_alloc;
229 atomic_init(&pool->refcount, 1);
235 * This function gets called when the pool has been uninited and
236 * all the buffers returned to it.
238 static void buffer_pool_free(AVBufferPool *pool)
241 BufferPoolEntry *buf = pool->pool;
242 pool->pool = buf->next;
244 buf->free(buf->opaque, buf->data);
247 ff_mutex_destroy(&pool->mutex);
250 pool->pool_free(pool->opaque);
255 void av_buffer_pool_uninit(AVBufferPool **ppool)
259 if (!ppool || !*ppool)
264 if (atomic_fetch_add_explicit(&pool->refcount, -1, memory_order_acq_rel) == 1)
265 buffer_pool_free(pool);
268 static void pool_release_buffer(void *opaque, uint8_t *data)
270 BufferPoolEntry *buf = opaque;
271 AVBufferPool *pool = buf->pool;
273 ff_mutex_lock(&pool->mutex);
274 buf->next = pool->pool;
276 ff_mutex_unlock(&pool->mutex);
278 if (atomic_fetch_add_explicit(&pool->refcount, -1, memory_order_acq_rel) == 1)
279 buffer_pool_free(pool);
282 /* allocate a new buffer and override its free() callback so that
283 * it is returned to the pool on free */
284 static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
286 BufferPoolEntry *buf;
289 ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
290 pool->alloc(pool->size);
294 buf = av_mallocz(sizeof(*buf));
296 av_buffer_unref(&ret);
300 buf->data = ret->buffer->data;
301 buf->opaque = ret->buffer->opaque;
302 buf->free = ret->buffer->free;
305 ret->buffer->opaque = buf;
306 ret->buffer->free = pool_release_buffer;
311 AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
314 BufferPoolEntry *buf;
316 ff_mutex_lock(&pool->mutex);
319 ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
322 pool->pool = buf->next;
326 ret = pool_alloc_buffer(pool);
328 ff_mutex_unlock(&pool->mutex);
331 atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);