libavutil/buffer.c
8e401dbe
 /*
36099df5
  * This file is part of FFmpeg.
8e401dbe
  *
36099df5
  * FFmpeg is free software; you can redistribute it and/or
8e401dbe
  * 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.
  *
36099df5
  * FFmpeg is distributed in the hope that it will be useful,
8e401dbe
  * 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
36099df5
  * License along with FFmpeg; if not, write to the Free Software
8e401dbe
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
27079a42
 #include <stdatomic.h>
8e401dbe
 #include <stdint.h>
 #include <string.h>
 
16685114
 #include "avassert.h"
8e401dbe
 #include "buffer_internal.h"
 #include "common.h"
 #include "mem.h"
fbd6c97f
 #include "thread.h"
8e401dbe
 
 AVBufferRef *av_buffer_create(uint8_t *data, int size,
                               void (*free)(void *opaque, uint8_t *data),
                               void *opaque, int flags)
 {
     AVBufferRef *ref = NULL;
     AVBuffer    *buf = NULL;
 
     buf = av_mallocz(sizeof(*buf));
     if (!buf)
         return NULL;
 
     buf->data     = data;
     buf->size     = size;
     buf->free     = free ? free : av_buffer_default_free;
     buf->opaque   = opaque;
27079a42
 
     atomic_init(&buf->refcount, 1);
8e401dbe
 
b6c8444e
     buf->flags = flags;
8e401dbe
 
     ref = av_mallocz(sizeof(*ref));
     if (!ref) {
         av_freep(&buf);
         return NULL;
     }
 
     ref->buffer = buf;
     ref->data   = data;
     ref->size   = size;
 
     return ref;
 }
 
 void av_buffer_default_free(void *opaque, uint8_t *data)
 {
     av_free(data);
 }
 
 AVBufferRef *av_buffer_alloc(int size)
 {
     AVBufferRef *ret = NULL;
     uint8_t    *data = NULL;
 
     data = av_malloc(size);
     if (!data)
         return NULL;
 
     ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
     if (!ret)
         av_freep(&data);
 
     return ret;
 }
 
 AVBufferRef *av_buffer_allocz(int size)
 {
     AVBufferRef *ret = av_buffer_alloc(size);
     if (!ret)
         return NULL;
 
     memset(ret->data, 0, size);
     return ret;
 }
 
 AVBufferRef *av_buffer_ref(AVBufferRef *buf)
 {
     AVBufferRef *ret = av_mallocz(sizeof(*ret));
 
     if (!ret)
         return NULL;
 
     *ret = *buf;
 
27079a42
     atomic_fetch_add_explicit(&buf->buffer->refcount, 1, memory_order_relaxed);
8e401dbe
 
     return ret;
 }
 
b4f5da26
 static void buffer_replace(AVBufferRef **dst, AVBufferRef **src)
8e401dbe
 {
     AVBuffer *b;
 
b4f5da26
     b = (*dst)->buffer;
 
     if (src) {
         **dst = **src;
         av_freep(src);
     } else
         av_freep(dst);
8e401dbe
 
71fd7207
     if (atomic_fetch_sub_explicit(&b->refcount, 1, memory_order_acq_rel) == 1) {
8e401dbe
         b->free(b->opaque, b->data);
         av_freep(&b);
     }
 }
 
b4f5da26
 void av_buffer_unref(AVBufferRef **buf)
 {
     if (!buf || !*buf)
         return;
 
     buffer_replace(buf, NULL);
 }
 
8e401dbe
 int av_buffer_is_writable(const AVBufferRef *buf)
 {
     if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
         return 0;
 
27079a42
     return atomic_load(&buf->buffer->refcount) == 1;
8e401dbe
 }
 
c81d2fa9
 void *av_buffer_get_opaque(const AVBufferRef *buf)
 {
     return buf->buffer->opaque;
 }
 
dc92464a
 int av_buffer_get_ref_count(const AVBufferRef *buf)
 {
443e9692
     return atomic_load(&buf->buffer->refcount);
dc92464a
 }
 
8e401dbe
 int av_buffer_make_writable(AVBufferRef **pbuf)
 {
     AVBufferRef *newbuf, *buf = *pbuf;
 
     if (av_buffer_is_writable(buf))
         return 0;
 
     newbuf = av_buffer_alloc(buf->size);
     if (!newbuf)
         return AVERROR(ENOMEM);
 
     memcpy(newbuf->data, buf->data, buf->size);
26d81b57
 
     buffer_replace(pbuf, &newbuf);
8e401dbe
 
     return 0;
 }
 
 int av_buffer_realloc(AVBufferRef **pbuf, int size)
 {
     AVBufferRef *buf = *pbuf;
     uint8_t *tmp;
 
     if (!buf) {
         /* allocate a new buffer with av_realloc(), so it will be reallocatable
          * later */
         uint8_t *data = av_realloc(NULL, size);
         if (!data)
             return AVERROR(ENOMEM);
 
         buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
         if (!buf) {
             av_freep(&data);
             return AVERROR(ENOMEM);
         }
 
b6c8444e
         buf->buffer->flags_internal |= BUFFER_FLAG_REALLOCATABLE;
8e401dbe
         *pbuf = buf;
 
         return 0;
     } else if (buf->size == size)
         return 0;
 
b6c8444e
     if (!(buf->buffer->flags_internal & BUFFER_FLAG_REALLOCATABLE) ||
24a36256
         !av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {
8e401dbe
         /* cannot realloc, allocate a new reallocable buffer and copy data */
         AVBufferRef *new = NULL;
 
         av_buffer_realloc(&new, size);
         if (!new)
             return AVERROR(ENOMEM);
 
         memcpy(new->data, buf->data, FFMIN(size, buf->size));
 
35fad1e9
         buffer_replace(pbuf, &new);
8e401dbe
         return 0;
     }
 
     tmp = av_realloc(buf->buffer->data, size);
     if (!tmp)
         return AVERROR(ENOMEM);
 
     buf->buffer->data = buf->data = tmp;
     buf->buffer->size = buf->size = size;
     return 0;
 }
1cec0624
 
721a4efc
 AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
                                    AVBufferRef* (*alloc)(void *opaque, int size),
                                    void (*pool_free)(void *opaque))
 {
     AVBufferPool *pool = av_mallocz(sizeof(*pool));
     if (!pool)
         return NULL;
 
     ff_mutex_init(&pool->mutex, NULL);
 
     pool->size      = size;
     pool->opaque    = opaque;
     pool->alloc2    = alloc;
ec39c227
     pool->alloc     = av_buffer_alloc; // fallback
721a4efc
     pool->pool_free = pool_free;
 
27079a42
     atomic_init(&pool->refcount, 1);
721a4efc
 
     return pool;
 }
 
1cec0624
 AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
 {
     AVBufferPool *pool = av_mallocz(sizeof(*pool));
     if (!pool)
         return NULL;
 
fbd6c97f
     ff_mutex_init(&pool->mutex, NULL);
 
1cec0624
     pool->size     = size;
     pool->alloc    = alloc ? alloc : av_buffer_alloc;
 
27079a42
     atomic_init(&pool->refcount, 1);
1cec0624
 
     return pool;
 }
 
 /*
  * This function gets called when the pool has been uninited and
  * all the buffers returned to it.
  */
 static void buffer_pool_free(AVBufferPool *pool)
 {
     while (pool->pool) {
         BufferPoolEntry *buf = pool->pool;
         pool->pool = buf->next;
 
         buf->free(buf->opaque, buf->data);
         av_freep(&buf);
     }
fbd6c97f
     ff_mutex_destroy(&pool->mutex);
721a4efc
 
     if (pool->pool_free)
         pool->pool_free(pool->opaque);
 
1cec0624
     av_freep(&pool);
 }
 
 void av_buffer_pool_uninit(AVBufferPool **ppool)
 {
     AVBufferPool *pool;
 
     if (!ppool || !*ppool)
         return;
     pool   = *ppool;
     *ppool = NULL;
 
71fd7207
     if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
1cec0624
         buffer_pool_free(pool);
 }
 
 static void pool_release_buffer(void *opaque, uint8_t *data)
 {
     BufferPoolEntry *buf = opaque;
     AVBufferPool *pool = buf->pool;
058c0029
 
     if(CONFIG_MEMORY_POISONING)
84be8069
         memset(buf->data, FF_MEMORY_POISON, pool->size);
058c0029
 
fbd6c97f
     ff_mutex_lock(&pool->mutex);
     buf->next = pool->pool;
     pool->pool = buf;
     ff_mutex_unlock(&pool->mutex);
 
71fd7207
     if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
1cec0624
         buffer_pool_free(pool);
 }
 
 /* allocate a new buffer and override its free() callback so that
  * it is returned to the pool on free */
 static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
 {
     BufferPoolEntry *buf;
     AVBufferRef     *ret;
 
ec39c227
     av_assert0(pool->alloc || pool->alloc2);
 
721a4efc
     ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
                          pool->alloc(pool->size);
1cec0624
     if (!ret)
         return NULL;
 
     buf = av_mallocz(sizeof(*buf));
     if (!buf) {
         av_buffer_unref(&ret);
         return NULL;
     }
 
     buf->data   = ret->buffer->data;
     buf->opaque = ret->buffer->opaque;
     buf->free   = ret->buffer->free;
     buf->pool   = pool;
 
     ret->buffer->opaque = buf;
     ret->buffer->free   = pool_release_buffer;
 
     return ret;
 }
 
 AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
 {
     AVBufferRef *ret;
     BufferPoolEntry *buf;
 
fbd6c97f
     ff_mutex_lock(&pool->mutex);
     buf = pool->pool;
     if (buf) {
         ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
                                buf, 0);
         if (ret) {
             pool->pool = buf->next;
             buf->next = NULL;
         }
     } else {
         ret = pool_alloc_buffer(pool);
1cec0624
     }
fbd6c97f
     ff_mutex_unlock(&pool->mutex);
 
     if (ret)
27079a42
         atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
1cec0624
 
     return ret;
 }
16685114
 
 void *av_buffer_pool_buffer_get_opaque(AVBufferRef *ref)
 {
     BufferPoolEntry *buf = ref->buffer->opaque;
     av_assert0(buf);
     return buf->opaque;
 }