2 * This file is part of FFmpeg.
4 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <stdatomic.h>
24 #include "buffer_internal.h"
29 AVBufferRef *av_buffer_create(uint8_t *data, buffer_size_t size,
30 void (*free)(void *opaque, uint8_t *data),
31 void *opaque, int flags)
33 AVBufferRef *ref = NULL;
36 buf = av_mallocz(sizeof(*buf));
42 buf->free = free ? free : av_buffer_default_free;
45 atomic_init(&buf->refcount, 1);
49 ref = av_mallocz(sizeof(*ref));
62 void av_buffer_default_free(void *opaque, uint8_t *data)
67 AVBufferRef *av_buffer_alloc(buffer_size_t 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(buffer_size_t 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 static void buffer_replace(AVBufferRef **dst, AVBufferRef **src)
119 if (atomic_fetch_sub_explicit(&b->refcount, 1, memory_order_acq_rel) == 1) {
120 b->free(b->opaque, b->data);
125 void av_buffer_unref(AVBufferRef **buf)
130 buffer_replace(buf, NULL);
133 int av_buffer_is_writable(const AVBufferRef *buf)
135 if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
138 return atomic_load(&buf->buffer->refcount) == 1;
141 void *av_buffer_get_opaque(const AVBufferRef *buf)
143 return buf->buffer->opaque;
146 int av_buffer_get_ref_count(const AVBufferRef *buf)
148 return atomic_load(&buf->buffer->refcount);
151 int av_buffer_make_writable(AVBufferRef **pbuf)
153 AVBufferRef *newbuf, *buf = *pbuf;
155 if (av_buffer_is_writable(buf))
158 newbuf = av_buffer_alloc(buf->size);
160 return AVERROR(ENOMEM);
162 memcpy(newbuf->data, buf->data, buf->size);
164 buffer_replace(pbuf, &newbuf);
169 int av_buffer_realloc(AVBufferRef **pbuf, buffer_size_t size)
171 AVBufferRef *buf = *pbuf;
176 /* allocate a new buffer with av_realloc(), so it will be reallocatable
178 uint8_t *data = av_realloc(NULL, size);
180 return AVERROR(ENOMEM);
182 buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
185 return AVERROR(ENOMEM);
188 buf->buffer->flags_internal |= BUFFER_FLAG_REALLOCATABLE;
192 } else if (buf->size == size)
195 if (!(buf->buffer->flags_internal & BUFFER_FLAG_REALLOCATABLE) ||
196 !av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {
197 /* cannot realloc, allocate a new reallocable buffer and copy data */
198 AVBufferRef *new = NULL;
200 ret = av_buffer_realloc(&new, size);
204 memcpy(new->data, buf->data, FFMIN(size, buf->size));
206 buffer_replace(pbuf, &new);
210 tmp = av_realloc(buf->buffer->data, size);
212 return AVERROR(ENOMEM);
214 buf->buffer->data = buf->data = tmp;
215 buf->buffer->size = buf->size = size;
219 int av_buffer_replace(AVBufferRef **pdst, AVBufferRef *src)
221 AVBufferRef *dst = *pdst;
225 av_buffer_unref(pdst);
229 if (dst && dst->buffer == src->buffer) {
230 /* make sure the data pointers match */
231 dst->data = src->data;
232 dst->size = src->size;
236 tmp = av_buffer_ref(src);
238 return AVERROR(ENOMEM);
240 av_buffer_unref(pdst);
245 AVBufferPool *av_buffer_pool_init2(buffer_size_t size, void *opaque,
246 AVBufferRef* (*alloc)(void *opaque, buffer_size_t size),
247 void (*pool_free)(void *opaque))
249 AVBufferPool *pool = av_mallocz(sizeof(*pool));
253 ff_mutex_init(&pool->mutex, NULL);
256 pool->opaque = opaque;
257 pool->alloc2 = alloc;
258 pool->alloc = av_buffer_alloc; // fallback
259 pool->pool_free = pool_free;
261 atomic_init(&pool->refcount, 1);
266 AVBufferPool *av_buffer_pool_init(buffer_size_t size, AVBufferRef* (*alloc)(buffer_size_t size))
268 AVBufferPool *pool = av_mallocz(sizeof(*pool));
272 ff_mutex_init(&pool->mutex, NULL);
275 pool->alloc = alloc ? alloc : av_buffer_alloc;
277 atomic_init(&pool->refcount, 1);
282 static void buffer_pool_flush(AVBufferPool *pool)
285 BufferPoolEntry *buf = pool->pool;
286 pool->pool = buf->next;
288 buf->free(buf->opaque, buf->data);
294 * This function gets called when the pool has been uninited and
295 * all the buffers returned to it.
297 static void buffer_pool_free(AVBufferPool *pool)
299 buffer_pool_flush(pool);
300 ff_mutex_destroy(&pool->mutex);
303 pool->pool_free(pool->opaque);
308 void av_buffer_pool_uninit(AVBufferPool **ppool)
312 if (!ppool || !*ppool)
317 ff_mutex_lock(&pool->mutex);
318 buffer_pool_flush(pool);
319 ff_mutex_unlock(&pool->mutex);
321 if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
322 buffer_pool_free(pool);
325 static void pool_release_buffer(void *opaque, uint8_t *data)
327 BufferPoolEntry *buf = opaque;
328 AVBufferPool *pool = buf->pool;
330 if(CONFIG_MEMORY_POISONING)
331 memset(buf->data, FF_MEMORY_POISON, pool->size);
333 ff_mutex_lock(&pool->mutex);
334 buf->next = pool->pool;
336 ff_mutex_unlock(&pool->mutex);
338 if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
339 buffer_pool_free(pool);
342 /* allocate a new buffer and override its free() callback so that
343 * it is returned to the pool on free */
344 static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
346 BufferPoolEntry *buf;
349 av_assert0(pool->alloc || pool->alloc2);
351 ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
352 pool->alloc(pool->size);
356 buf = av_mallocz(sizeof(*buf));
358 av_buffer_unref(&ret);
362 buf->data = ret->buffer->data;
363 buf->opaque = ret->buffer->opaque;
364 buf->free = ret->buffer->free;
367 ret->buffer->opaque = buf;
368 ret->buffer->free = pool_release_buffer;
373 AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
376 BufferPoolEntry *buf;
378 ff_mutex_lock(&pool->mutex);
381 ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
384 pool->pool = buf->next;
388 ret = pool_alloc_buffer(pool);
390 ff_mutex_unlock(&pool->mutex);
393 atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
398 void *av_buffer_pool_buffer_get_opaque(AVBufferRef *ref)
400 BufferPoolEntry *buf = ref->buffer->opaque;