diff options
Diffstat (limited to 'libavutil/buffer.c')
-rw-r--r-- | libavutil/buffer.c | 122 |
1 files changed, 108 insertions, 14 deletions
diff --git a/libavutil/buffer.c b/libavutil/buffer.c index 6681002345..694e116a3c 100644 --- a/libavutil/buffer.c +++ b/libavutil/buffer.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -103,14 +103,17 @@ AVBufferRef *av_buffer_ref(AVBufferRef *buf) return ret; } -void av_buffer_unref(AVBufferRef **buf) +static void buffer_replace(AVBufferRef **dst, AVBufferRef **src) { AVBuffer *b; - if (!buf || !*buf) - return; - b = (*buf)->buffer; - av_freep(buf); + b = (*dst)->buffer; + + if (src) { + **dst = **src; + av_freep(src); + } else + av_freep(dst); if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) { b->free(b->opaque, b->data); @@ -118,12 +121,30 @@ void av_buffer_unref(AVBufferRef **buf) } } +void av_buffer_unref(AVBufferRef **buf) +{ + if (!buf || !*buf) + return; + + buffer_replace(buf, NULL); +} + int av_buffer_is_writable(const AVBufferRef *buf) { if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY) return 0; - return avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 0) == 1; + return avpriv_atomic_int_get(&buf->buffer->refcount) == 1; +} + +void *av_buffer_get_opaque(const AVBufferRef *buf) +{ + return buf->buffer->opaque; +} + +int av_buffer_get_ref_count(const AVBufferRef *buf) +{ + return buf->buffer->refcount; } int av_buffer_make_writable(AVBufferRef **pbuf) @@ -138,8 +159,8 @@ int av_buffer_make_writable(AVBufferRef **pbuf) return AVERROR(ENOMEM); memcpy(newbuf->data, buf->data, buf->size); - av_buffer_unref(pbuf); - *pbuf = newbuf; + + buffer_replace(pbuf, &newbuf); return 0; } @@ -180,8 +201,7 @@ int av_buffer_realloc(AVBufferRef **pbuf, int size) memcpy(new->data, buf->data, FFMIN(size, buf->size)); - av_buffer_unref(pbuf); - *pbuf = new; + buffer_replace(pbuf, &new); return 0; } @@ -264,15 +284,60 @@ void av_buffer_pool_uninit(AVBufferPool **ppool) buffer_pool_free(pool); } +#if USE_ATOMICS +/* remove the whole buffer list from the pool and return it */ +static BufferPoolEntry *get_pool(AVBufferPool *pool) +{ + BufferPoolEntry *cur = *(void * volatile *)&pool->pool, *last = NULL; + + while (cur != last) { + last = cur; + cur = avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, last, NULL); + if (!cur) + return NULL; + } + + return cur; +} + +static void add_to_pool(BufferPoolEntry *buf) +{ + AVBufferPool *pool; + BufferPoolEntry *cur, *end = buf; + + if (!buf) + return; + pool = buf->pool; + + while (end->next) + end = end->next; + + while (avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, NULL, buf)) { + /* pool is not empty, retrieve it and append it to our list */ + cur = get_pool(pool); + end->next = cur; + while (end->next) + end = end->next; + } +} +#endif + static void pool_release_buffer(void *opaque, uint8_t *data) { BufferPoolEntry *buf = opaque; AVBufferPool *pool = buf->pool; + if(CONFIG_MEMORY_POISONING) + memset(buf->data, FF_MEMORY_POISON, pool->size); + +#if USE_ATOMICS + add_to_pool(buf); +#else ff_mutex_lock(&pool->mutex); buf->next = pool->pool; pool->pool = buf; ff_mutex_unlock(&pool->mutex); +#endif if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1)) buffer_pool_free(pool); @@ -304,6 +369,11 @@ static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool) ret->buffer->opaque = buf; ret->buffer->free = pool_release_buffer; +#if USE_ATOMICS + avpriv_atomic_int_add_and_fetch(&pool->refcount, 1); + avpriv_atomic_int_add_and_fetch(&pool->nb_allocated, 1); +#endif + return ret; } @@ -312,6 +382,29 @@ AVBufferRef *av_buffer_pool_get(AVBufferPool *pool) AVBufferRef *ret; BufferPoolEntry *buf; +#if USE_ATOMICS + /* check whether the pool is empty */ + buf = get_pool(pool); + if (!buf && pool->refcount <= pool->nb_allocated) { + av_log(NULL, AV_LOG_DEBUG, "Pool race dectected, spining to avoid overallocation and eventual OOM\n"); + while (!buf && avpriv_atomic_int_get(&pool->refcount) <= avpriv_atomic_int_get(&pool->nb_allocated)) + buf = get_pool(pool); + } + + if (!buf) + return pool_alloc_buffer(pool); + + /* keep the first entry, return the rest of the list to the pool */ + add_to_pool(buf->next); + buf->next = NULL; + + ret = av_buffer_create(buf->data, pool->size, pool_release_buffer, + buf, 0); + if (!ret) { + add_to_pool(buf); + return NULL; + } +#else ff_mutex_lock(&pool->mutex); buf = pool->pool; if (buf) { @@ -325,6 +418,7 @@ AVBufferRef *av_buffer_pool_get(AVBufferPool *pool) ret = pool_alloc_buffer(pool); } ff_mutex_unlock(&pool->mutex); +#endif if (ret) avpriv_atomic_int_add_and_fetch(&pool->refcount, 1); |