#include "buffer_internal.h"
#include "common.h"
#include "mem.h"
+ #include "thread.h"
+
++#define USE_ATOMICS !(HAVE_PTHREADS || HAVE_W32THREADS)
+
AVBufferRef *av_buffer_create(uint8_t *data, int size,
void (*free)(void *opaque, uint8_t *data),
void *opaque, int flags)
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);
}
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;
}
AVBufferRef *ret;
BufferPoolEntry *buf;
- avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
++#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) {
+ 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);
+ }
+ ff_mutex_unlock(&pool->mutex);
++#endif
+
+ if (ret)
+ avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
return ret;
}