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
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;
45 if (flags & AV_BUFFER_FLAG_READONLY)
46 buf->flags |= BUFFER_FLAG_READONLY;
48 ref = av_mallocz(sizeof(*ref));
61 void av_buffer_default_free(void *opaque, uint8_t *data)
66 AVBufferRef *av_buffer_alloc(int size)
68 AVBufferRef *ret = NULL;
71 data = av_malloc(size);
75 ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
82 AVBufferRef *av_buffer_allocz(int size)
84 AVBufferRef *ret = av_buffer_alloc(size);
88 memset(ret->data, 0, size);
92 AVBufferRef *av_buffer_ref(AVBufferRef *buf)
94 AVBufferRef *ret = av_mallocz(sizeof(*ret));
101 avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 1);
106 static void buffer_replace(AVBufferRef **dst, AVBufferRef **src)
118 if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
119 b->free(b->opaque, b->data);
124 void av_buffer_unref(AVBufferRef **buf)
129 buffer_replace(buf, NULL);
132 int av_buffer_is_writable(const AVBufferRef *buf)
134 if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
137 return avpriv_atomic_int_get(&buf->buffer->refcount) == 1;
140 void *av_buffer_get_opaque(const AVBufferRef *buf)
142 return buf->buffer->opaque;
145 int av_buffer_get_ref_count(const AVBufferRef *buf)
147 return buf->buffer->refcount;
150 int av_buffer_make_writable(AVBufferRef **pbuf)
152 AVBufferRef *newbuf, *buf = *pbuf;
154 if (av_buffer_is_writable(buf))
157 newbuf = av_buffer_alloc(buf->size);
159 return AVERROR(ENOMEM);
161 memcpy(newbuf->data, buf->data, buf->size);
163 buffer_replace(pbuf, &newbuf);
168 int av_buffer_realloc(AVBufferRef **pbuf, int size)
170 AVBufferRef *buf = *pbuf;
174 /* allocate a new buffer with av_realloc(), so it will be reallocatable
176 uint8_t *data = av_realloc(NULL, size);
178 return AVERROR(ENOMEM);
180 buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
183 return AVERROR(ENOMEM);
186 buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
190 } else if (buf->size == size)
193 if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
194 !av_buffer_is_writable(buf)) {
195 /* cannot realloc, allocate a new reallocable buffer and copy data */
196 AVBufferRef *new = NULL;
198 av_buffer_realloc(&new, size);
200 return AVERROR(ENOMEM);
202 memcpy(new->data, buf->data, FFMIN(size, buf->size));
204 buffer_replace(pbuf, &new);
208 tmp = av_realloc(buf->buffer->data, size);
210 return AVERROR(ENOMEM);
212 buf->buffer->data = buf->data = tmp;
213 buf->buffer->size = buf->size = size;
217 AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
219 AVBufferPool *pool = av_mallocz(sizeof(*pool));
223 ff_mutex_init(&pool->mutex, NULL);
226 pool->alloc = alloc ? alloc : av_buffer_alloc;
228 avpriv_atomic_int_set(&pool->refcount, 1);
234 * This function gets called when the pool has been uninited and
235 * all the buffers returned to it.
237 static void buffer_pool_free(AVBufferPool *pool)
240 BufferPoolEntry *buf = pool->pool;
241 pool->pool = buf->next;
243 buf->free(buf->opaque, buf->data);
246 ff_mutex_destroy(&pool->mutex);
250 void av_buffer_pool_uninit(AVBufferPool **ppool)
254 if (!ppool || !*ppool)
259 if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
260 buffer_pool_free(pool);
264 /* remove the whole buffer list from the pool and return it */
265 static BufferPoolEntry *get_pool(AVBufferPool *pool)
267 BufferPoolEntry *cur = *(void * volatile *)&pool->pool, *last = NULL;
269 while (cur != last) {
271 cur = avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, last, NULL);
279 static void add_to_pool(BufferPoolEntry *buf)
282 BufferPoolEntry *cur, *end = buf;
291 while (avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, NULL, buf)) {
292 /* pool is not empty, retrieve it and append it to our list */
293 cur = get_pool(pool);
301 static void pool_release_buffer(void *opaque, uint8_t *data)
303 BufferPoolEntry *buf = opaque;
304 AVBufferPool *pool = buf->pool;
306 if(CONFIG_MEMORY_POISONING)
307 memset(buf->data, FF_MEMORY_POISON, pool->size);
312 ff_mutex_lock(&pool->mutex);
313 buf->next = pool->pool;
315 ff_mutex_unlock(&pool->mutex);
318 if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
319 buffer_pool_free(pool);
322 /* allocate a new buffer and override its free() callback so that
323 * it is returned to the pool on free */
324 static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
326 BufferPoolEntry *buf;
329 ret = pool->alloc(pool->size);
333 buf = av_mallocz(sizeof(*buf));
335 av_buffer_unref(&ret);
339 buf->data = ret->buffer->data;
340 buf->opaque = ret->buffer->opaque;
341 buf->free = ret->buffer->free;
344 ret->buffer->opaque = buf;
345 ret->buffer->free = pool_release_buffer;
348 avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
349 avpriv_atomic_int_add_and_fetch(&pool->nb_allocated, 1);
355 AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
358 BufferPoolEntry *buf;
361 /* check whether the pool is empty */
362 buf = get_pool(pool);
363 if (!buf && pool->refcount <= pool->nb_allocated) {
364 av_log(NULL, AV_LOG_DEBUG, "Pool race dectected, spining to avoid overallocation and eventual OOM\n");
365 while (!buf && avpriv_atomic_int_get(&pool->refcount) <= avpriv_atomic_int_get(&pool->nb_allocated))
366 buf = get_pool(pool);
370 return pool_alloc_buffer(pool);
372 /* keep the first entry, return the rest of the list to the pool */
373 add_to_pool(buf->next);
376 ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
383 ff_mutex_lock(&pool->mutex);
386 ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
389 pool->pool = buf->next;
393 ret = pool_alloc_buffer(pool);
395 ff_mutex_unlock(&pool->mutex);
399 avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);