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
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 void av_buffer_unref(AVBufferRef **buf)
115 if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
116 b->free(b->opaque, b->data);
121 int av_buffer_is_writable(const AVBufferRef *buf)
123 if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
126 return avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 0) == 1;
129 int av_buffer_make_writable(AVBufferRef **pbuf)
131 AVBufferRef *newbuf, *buf = *pbuf;
133 if (av_buffer_is_writable(buf))
136 newbuf = av_buffer_alloc(buf->size);
138 return AVERROR(ENOMEM);
140 memcpy(newbuf->data, buf->data, buf->size);
141 av_buffer_unref(pbuf);
147 int av_buffer_realloc(AVBufferRef **pbuf, int size)
149 AVBufferRef *buf = *pbuf;
153 /* allocate a new buffer with av_realloc(), so it will be reallocatable
155 uint8_t *data = av_realloc(NULL, size);
157 return AVERROR(ENOMEM);
159 buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
162 return AVERROR(ENOMEM);
165 buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
169 } else if (buf->size == size)
172 if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
173 !av_buffer_is_writable(buf)) {
174 /* cannot realloc, allocate a new reallocable buffer and copy data */
175 AVBufferRef *new = NULL;
177 av_buffer_realloc(&new, size);
179 return AVERROR(ENOMEM);
181 memcpy(new->data, buf->data, FFMIN(size, buf->size));
183 av_buffer_unref(pbuf);
188 tmp = av_realloc(buf->buffer->data, size);
190 return AVERROR(ENOMEM);
192 buf->buffer->data = buf->data = tmp;
193 buf->buffer->size = buf->size = size;
197 AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
198 AVBufferRef* (*alloc)(void *opaque, int size),
199 void (*pool_free)(void *opaque))
201 AVBufferPool *pool = av_mallocz(sizeof(*pool));
205 ff_mutex_init(&pool->mutex, NULL);
208 pool->opaque = opaque;
209 pool->alloc2 = alloc;
210 pool->pool_free = pool_free;
212 avpriv_atomic_int_set(&pool->refcount, 1);
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);
249 pool->pool_free(pool->opaque);
254 void av_buffer_pool_uninit(AVBufferPool **ppool)
258 if (!ppool || !*ppool)
263 if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
264 buffer_pool_free(pool);
267 static void pool_release_buffer(void *opaque, uint8_t *data)
269 BufferPoolEntry *buf = opaque;
270 AVBufferPool *pool = buf->pool;
272 ff_mutex_lock(&pool->mutex);
273 buf->next = pool->pool;
275 ff_mutex_unlock(&pool->mutex);
277 if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
278 buffer_pool_free(pool);
281 /* allocate a new buffer and override its free() callback so that
282 * it is returned to the pool on free */
283 static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
285 BufferPoolEntry *buf;
288 ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
289 pool->alloc(pool->size);
293 buf = av_mallocz(sizeof(*buf));
295 av_buffer_unref(&ret);
299 buf->data = ret->buffer->data;
300 buf->opaque = ret->buffer->opaque;
301 buf->free = ret->buffer->free;
304 ret->buffer->opaque = buf;
305 ret->buffer->free = pool_release_buffer;
310 AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
313 BufferPoolEntry *buf;
315 ff_mutex_lock(&pool->mutex);
318 ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
321 pool->pool = buf->next;
325 ret = pool_alloc_buffer(pool);
327 ff_mutex_unlock(&pool->mutex);
330 avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);