yading@11: /* yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include yading@11: #include yading@11: yading@11: #include "atomic.h" yading@11: #include "buffer_internal.h" yading@11: #include "common.h" yading@11: #include "mem.h" yading@11: yading@11: AVBufferRef *av_buffer_create(uint8_t *data, int size, yading@11: void (*free)(void *opaque, uint8_t *data), yading@11: void *opaque, int flags) yading@11: { yading@11: AVBufferRef *ref = NULL; yading@11: AVBuffer *buf = NULL; yading@11: yading@11: buf = av_mallocz(sizeof(*buf)); yading@11: if (!buf) yading@11: return NULL; yading@11: yading@11: buf->data = data; yading@11: buf->size = size; yading@11: buf->free = free ? free : av_buffer_default_free; yading@11: buf->opaque = opaque; yading@11: buf->refcount = 1; yading@11: yading@11: if (flags & AV_BUFFER_FLAG_READONLY) yading@11: buf->flags |= BUFFER_FLAG_READONLY; yading@11: yading@11: ref = av_mallocz(sizeof(*ref)); yading@11: if (!ref) { yading@11: av_freep(&buf); yading@11: return NULL; yading@11: } yading@11: yading@11: ref->buffer = buf; yading@11: ref->data = data; yading@11: ref->size = size; yading@11: yading@11: return ref; yading@11: } yading@11: yading@11: void av_buffer_default_free(void *opaque, uint8_t *data) yading@11: { yading@11: av_free(data); yading@11: } yading@11: yading@11: AVBufferRef *av_buffer_alloc(int size) yading@11: { yading@11: AVBufferRef *ret = NULL; yading@11: uint8_t *data = NULL; yading@11: yading@11: data = av_malloc(size); yading@11: if (!data) yading@11: return NULL; yading@11: yading@11: ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0); yading@11: if (!ret) yading@11: av_freep(&data); yading@11: yading@11: return ret; yading@11: } yading@11: yading@11: AVBufferRef *av_buffer_allocz(int size) yading@11: { yading@11: AVBufferRef *ret = av_buffer_alloc(size); yading@11: if (!ret) yading@11: return NULL; yading@11: yading@11: memset(ret->data, 0, size); yading@11: return ret; yading@11: } yading@11: yading@11: AVBufferRef *av_buffer_ref(AVBufferRef *buf) yading@11: { yading@11: AVBufferRef *ret = av_mallocz(sizeof(*ret)); yading@11: yading@11: if (!ret) yading@11: return NULL; yading@11: yading@11: *ret = *buf; yading@11: yading@11: avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 1); yading@11: yading@11: return ret; yading@11: } yading@11: yading@11: void av_buffer_unref(AVBufferRef **buf) yading@11: { yading@11: AVBuffer *b; yading@11: yading@11: if (!buf || !*buf) yading@11: return; yading@11: b = (*buf)->buffer; yading@11: av_freep(buf); yading@11: yading@11: if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) { yading@11: b->free(b->opaque, b->data); yading@11: av_freep(&b); yading@11: } yading@11: } yading@11: yading@11: int av_buffer_is_writable(const AVBufferRef *buf) yading@11: { yading@11: if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY) yading@11: return 0; yading@11: yading@11: return avpriv_atomic_int_get(&buf->buffer->refcount) == 1; yading@11: } yading@11: yading@11: void *av_buffer_get_opaque(const AVBufferRef *buf) yading@11: { yading@11: return buf->buffer->opaque; yading@11: } yading@11: yading@11: int av_buffer_get_ref_count(const AVBufferRef *buf) yading@11: { yading@11: return buf->buffer->refcount; yading@11: } yading@11: yading@11: int av_buffer_make_writable(AVBufferRef **pbuf) yading@11: { yading@11: AVBufferRef *newbuf, *buf = *pbuf; yading@11: yading@11: if (av_buffer_is_writable(buf)) yading@11: return 0; yading@11: yading@11: newbuf = av_buffer_alloc(buf->size); yading@11: if (!newbuf) yading@11: return AVERROR(ENOMEM); yading@11: yading@11: memcpy(newbuf->data, buf->data, buf->size); yading@11: av_buffer_unref(pbuf); yading@11: *pbuf = newbuf; yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: int av_buffer_realloc(AVBufferRef **pbuf, int size) yading@11: { yading@11: AVBufferRef *buf = *pbuf; yading@11: uint8_t *tmp; yading@11: yading@11: if (!buf) { yading@11: /* allocate a new buffer with av_realloc(), so it will be reallocatable yading@11: * later */ yading@11: uint8_t *data = av_realloc(NULL, size); yading@11: if (!data) yading@11: return AVERROR(ENOMEM); yading@11: yading@11: buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0); yading@11: if (!buf) { yading@11: av_freep(&data); yading@11: return AVERROR(ENOMEM); yading@11: } yading@11: yading@11: buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE; yading@11: *pbuf = buf; yading@11: yading@11: return 0; yading@11: } else if (buf->size == size) yading@11: return 0; yading@11: yading@11: if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) || yading@11: !av_buffer_is_writable(buf)) { yading@11: /* cannot realloc, allocate a new reallocable buffer and copy data */ yading@11: AVBufferRef *new = NULL; yading@11: yading@11: av_buffer_realloc(&new, size); yading@11: if (!new) yading@11: return AVERROR(ENOMEM); yading@11: yading@11: memcpy(new->data, buf->data, FFMIN(size, buf->size)); yading@11: yading@11: av_buffer_unref(pbuf); yading@11: *pbuf = new; yading@11: return 0; yading@11: } yading@11: yading@11: tmp = av_realloc(buf->buffer->data, size); yading@11: if (!tmp) yading@11: return AVERROR(ENOMEM); yading@11: yading@11: buf->buffer->data = buf->data = tmp; yading@11: buf->buffer->size = buf->size = size; yading@11: return 0; yading@11: } yading@11: yading@11: AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size)) yading@11: { yading@11: AVBufferPool *pool = av_mallocz(sizeof(*pool)); yading@11: if (!pool) yading@11: return NULL; yading@11: yading@11: pool->size = size; yading@11: pool->alloc = alloc ? alloc : av_buffer_alloc; yading@11: yading@11: avpriv_atomic_int_set(&pool->refcount, 1); yading@11: yading@11: return pool; yading@11: } yading@11: yading@11: /* yading@11: * This function gets called when the pool has been uninited and yading@11: * all the buffers returned to it. yading@11: */ yading@11: static void buffer_pool_free(AVBufferPool *pool) yading@11: { yading@11: while (pool->pool) { yading@11: BufferPoolEntry *buf = pool->pool; yading@11: pool->pool = buf->next; yading@11: yading@11: buf->free(buf->opaque, buf->data); yading@11: av_freep(&buf); yading@11: } yading@11: av_freep(&pool); yading@11: } yading@11: yading@11: void av_buffer_pool_uninit(AVBufferPool **ppool) yading@11: { yading@11: AVBufferPool *pool; yading@11: yading@11: if (!ppool || !*ppool) yading@11: return; yading@11: pool = *ppool; yading@11: *ppool = NULL; yading@11: yading@11: if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1)) yading@11: buffer_pool_free(pool); yading@11: } yading@11: yading@11: /* remove the whole buffer list from the pool and return it */ yading@11: static BufferPoolEntry *get_pool(AVBufferPool *pool) yading@11: { yading@11: BufferPoolEntry *cur = *(void * volatile *)&pool->pool, *last = NULL; yading@11: yading@11: while (cur != last) { yading@11: last = cur; yading@11: cur = avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, last, NULL); yading@11: if (!cur) yading@11: return NULL; yading@11: } yading@11: yading@11: return cur; yading@11: } yading@11: yading@11: static void add_to_pool(BufferPoolEntry *buf) yading@11: { yading@11: AVBufferPool *pool; yading@11: BufferPoolEntry *cur, *end = buf; yading@11: yading@11: if (!buf) yading@11: return; yading@11: pool = buf->pool; yading@11: yading@11: while (end->next) yading@11: end = end->next; yading@11: yading@11: while (avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, NULL, buf)) { yading@11: /* pool is not empty, retrieve it and append it to our list */ yading@11: cur = get_pool(pool); yading@11: end->next = cur; yading@11: while (end->next) yading@11: end = end->next; yading@11: } yading@11: } yading@11: yading@11: static void pool_release_buffer(void *opaque, uint8_t *data) yading@11: { yading@11: BufferPoolEntry *buf = opaque; yading@11: AVBufferPool *pool = buf->pool; yading@11: yading@11: if(CONFIG_MEMORY_POISONING) yading@11: memset(buf->data, 0x2a, pool->size); yading@11: yading@11: add_to_pool(buf); yading@11: if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1)) yading@11: buffer_pool_free(pool); yading@11: } yading@11: yading@11: /* allocate a new buffer and override its free() callback so that yading@11: * it is returned to the pool on free */ yading@11: static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool) yading@11: { yading@11: BufferPoolEntry *buf; yading@11: AVBufferRef *ret; yading@11: yading@11: ret = pool->alloc(pool->size); yading@11: if (!ret) yading@11: return NULL; yading@11: yading@11: buf = av_mallocz(sizeof(*buf)); yading@11: if (!buf) { yading@11: av_buffer_unref(&ret); yading@11: return NULL; yading@11: } yading@11: yading@11: buf->data = ret->buffer->data; yading@11: buf->opaque = ret->buffer->opaque; yading@11: buf->free = ret->buffer->free; yading@11: buf->pool = pool; yading@11: yading@11: ret->buffer->opaque = buf; yading@11: ret->buffer->free = pool_release_buffer; yading@11: yading@11: avpriv_atomic_int_add_and_fetch(&pool->refcount, 1); yading@11: avpriv_atomic_int_add_and_fetch(&pool->nb_allocated, 1); yading@11: yading@11: return ret; yading@11: } yading@11: yading@11: AVBufferRef *av_buffer_pool_get(AVBufferPool *pool) yading@11: { yading@11: AVBufferRef *ret; yading@11: BufferPoolEntry *buf; yading@11: yading@11: /* check whether the pool is empty */ yading@11: buf = get_pool(pool); yading@11: if (!buf && pool->refcount <= pool->nb_allocated) { yading@11: av_log(NULL, AV_LOG_DEBUG, "Pool race dectected, spining to avoid overallocation and eventual OOM\n"); yading@11: while (!buf && avpriv_atomic_int_get(&pool->refcount) <= avpriv_atomic_int_get(&pool->nb_allocated)) yading@11: buf = get_pool(pool); yading@11: } yading@11: yading@11: if (!buf) yading@11: return pool_alloc_buffer(pool); yading@11: yading@11: /* keep the first entry, return the rest of the list to the pool */ yading@11: add_to_pool(buf->next); yading@11: buf->next = NULL; yading@11: yading@11: ret = av_buffer_create(buf->data, pool->size, pool_release_buffer, yading@11: buf, 0); yading@11: if (!ret) { yading@11: add_to_pool(buf); yading@11: return NULL; yading@11: } yading@11: avpriv_atomic_int_add_and_fetch(&pool->refcount, 1); yading@11: yading@11: return ret; yading@11: }