FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
buffer.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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.
8  *
9  * FFmpeg 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.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdint.h>
20 #include <string.h>
21 
22 #include "atomic.h"
23 #include "buffer_internal.h"
24 #include "common.h"
25 #include "mem.h"
26 #include "thread.h"
27 
29  void (*free)(void *opaque, uint8_t *data),
30  void *opaque, int flags)
31 {
32  AVBufferRef *ref = NULL;
33  AVBuffer *buf = NULL;
34 
35  buf = av_mallocz(sizeof(*buf));
36  if (!buf)
37  return NULL;
38 
39  buf->data = data;
40  buf->size = size;
41  buf->free = free ? free : av_buffer_default_free;
42  buf->opaque = opaque;
43  buf->refcount = 1;
44 
45  if (flags & AV_BUFFER_FLAG_READONLY)
47 
48  ref = av_mallocz(sizeof(*ref));
49  if (!ref) {
50  av_freep(&buf);
51  return NULL;
52  }
53 
54  ref->buffer = buf;
55  ref->data = data;
56  ref->size = size;
57 
58  return ref;
59 }
60 
61 void av_buffer_default_free(void *opaque, uint8_t *data)
62 {
63  av_free(data);
64 }
65 
67 {
68  AVBufferRef *ret = NULL;
69  uint8_t *data = NULL;
70 
71  data = av_malloc(size);
72  if (!data)
73  return NULL;
74 
75  ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
76  if (!ret)
77  av_freep(&data);
78 
79  return ret;
80 }
81 
83 {
84  AVBufferRef *ret = av_buffer_alloc(size);
85  if (!ret)
86  return NULL;
87 
88  memset(ret->data, 0, size);
89  return ret;
90 }
91 
93 {
94  AVBufferRef *ret = av_mallocz(sizeof(*ret));
95 
96  if (!ret)
97  return NULL;
98 
99  *ret = *buf;
100 
102 
103  return ret;
104 }
105 
107 {
108  AVBuffer *b;
109 
110  b = (*dst)->buffer;
111 
112  if (src) {
113  **dst = **src;
114  av_freep(src);
115  } else
116  av_freep(dst);
117 
119  b->free(b->opaque, b->data);
120  av_freep(&b);
121  }
122 }
123 
125 {
126  if (!buf || !*buf)
127  return;
128 
129  buffer_replace(buf, NULL);
130 }
131 
133 {
135  return 0;
136 
137  return avpriv_atomic_int_get(&buf->buffer->refcount) == 1;
138 }
139 
141 {
142  return buf->buffer->opaque;
143 }
144 
146 {
147  return buf->buffer->refcount;
148 }
149 
151 {
152  AVBufferRef *newbuf, *buf = *pbuf;
153 
154  if (av_buffer_is_writable(buf))
155  return 0;
156 
157  newbuf = av_buffer_alloc(buf->size);
158  if (!newbuf)
159  return AVERROR(ENOMEM);
160 
161  memcpy(newbuf->data, buf->data, buf->size);
162 
163  buffer_replace(pbuf, &newbuf);
164 
165  return 0;
166 }
167 
168 int av_buffer_realloc(AVBufferRef **pbuf, int size)
169 {
170  AVBufferRef *buf = *pbuf;
171  uint8_t *tmp;
172 
173  if (!buf) {
174  /* allocate a new buffer with av_realloc(), so it will be reallocatable
175  * later */
176  uint8_t *data = av_realloc(NULL, size);
177  if (!data)
178  return AVERROR(ENOMEM);
179 
180  buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
181  if (!buf) {
182  av_freep(&data);
183  return AVERROR(ENOMEM);
184  }
185 
187  *pbuf = buf;
188 
189  return 0;
190  } else if (buf->size == size)
191  return 0;
192 
193  if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
194  !av_buffer_is_writable(buf)) {
195  /* cannot realloc, allocate a new reallocable buffer and copy data */
196  AVBufferRef *new = NULL;
197 
198  av_buffer_realloc(&new, size);
199  if (!new)
200  return AVERROR(ENOMEM);
201 
202  memcpy(new->data, buf->data, FFMIN(size, buf->size));
203 
204  buffer_replace(pbuf, &new);
205  return 0;
206  }
207 
208  tmp = av_realloc(buf->buffer->data, size);
209  if (!tmp)
210  return AVERROR(ENOMEM);
211 
212  buf->buffer->data = buf->data = tmp;
213  buf->buffer->size = buf->size = size;
214  return 0;
215 }
216 
217 AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
218  AVBufferRef* (*alloc)(void *opaque, int size),
219  void (*pool_free)(void *opaque))
220 {
221  AVBufferPool *pool = av_mallocz(sizeof(*pool));
222  if (!pool)
223  return NULL;
224 
225  ff_mutex_init(&pool->mutex, NULL);
226 
227  pool->size = size;
228  pool->opaque = opaque;
229  pool->alloc2 = alloc;
230  pool->pool_free = pool_free;
231 
232  avpriv_atomic_int_set(&pool->refcount, 1);
233 
234  return pool;
235 }
236 
237 AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
238 {
239  AVBufferPool *pool = av_mallocz(sizeof(*pool));
240  if (!pool)
241  return NULL;
242 
243  ff_mutex_init(&pool->mutex, NULL);
244 
245  pool->size = size;
246  pool->alloc = alloc ? alloc : av_buffer_alloc;
247 
248  avpriv_atomic_int_set(&pool->refcount, 1);
249 
250  return pool;
251 }
252 
253 /*
254  * This function gets called when the pool has been uninited and
255  * all the buffers returned to it.
256  */
257 static void buffer_pool_free(AVBufferPool *pool)
258 {
259  while (pool->pool) {
260  BufferPoolEntry *buf = pool->pool;
261  pool->pool = buf->next;
262 
263  buf->free(buf->opaque, buf->data);
264  av_freep(&buf);
265  }
266  ff_mutex_destroy(&pool->mutex);
267 
268  if (pool->pool_free)
269  pool->pool_free(pool->opaque);
270 
271  av_freep(&pool);
272 }
273 
275 {
276  AVBufferPool *pool;
277 
278  if (!ppool || !*ppool)
279  return;
280  pool = *ppool;
281  *ppool = NULL;
282 
283  if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
284  buffer_pool_free(pool);
285 }
286 
287 #if USE_ATOMICS
288 /* remove the whole buffer list from the pool and return it */
289 static BufferPoolEntry *get_pool(AVBufferPool *pool)
290 {
291  BufferPoolEntry *cur = *(void * volatile *)&pool->pool, *last = NULL;
292 
293  while (cur != last) {
294  last = cur;
295  cur = avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, last, NULL);
296  if (!cur)
297  return NULL;
298  }
299 
300  return cur;
301 }
302 
303 static void add_to_pool(BufferPoolEntry *buf)
304 {
305  AVBufferPool *pool;
306  BufferPoolEntry *cur, *end = buf;
307 
308  if (!buf)
309  return;
310  pool = buf->pool;
311 
312  while (end->next)
313  end = end->next;
314 
315  while (avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, NULL, buf)) {
316  /* pool is not empty, retrieve it and append it to our list */
317  cur = get_pool(pool);
318  end->next = cur;
319  while (end->next)
320  end = end->next;
321  }
322 }
323 #endif
324 
325 static void pool_release_buffer(void *opaque, uint8_t *data)
326 {
327  BufferPoolEntry *buf = opaque;
328  AVBufferPool *pool = buf->pool;
329 
330  if(CONFIG_MEMORY_POISONING)
331  memset(buf->data, FF_MEMORY_POISON, pool->size);
332 
333 #if USE_ATOMICS
334  add_to_pool(buf);
335 #else
336  ff_mutex_lock(&pool->mutex);
337  buf->next = pool->pool;
338  pool->pool = buf;
339  ff_mutex_unlock(&pool->mutex);
340 #endif
341 
342  if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
343  buffer_pool_free(pool);
344 }
345 
346 /* allocate a new buffer and override its free() callback so that
347  * it is returned to the pool on free */
349 {
351  AVBufferRef *ret;
352 
353  ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
354  pool->alloc(pool->size);
355  if (!ret)
356  return NULL;
357 
358  buf = av_mallocz(sizeof(*buf));
359  if (!buf) {
360  av_buffer_unref(&ret);
361  return NULL;
362  }
363 
364  buf->data = ret->buffer->data;
365  buf->opaque = ret->buffer->opaque;
366  buf->free = ret->buffer->free;
367  buf->pool = pool;
368 
369  ret->buffer->opaque = buf;
371 
372 #if USE_ATOMICS
375 #endif
376 
377  return ret;
378 }
379 
381 {
382  AVBufferRef *ret;
384 
385 #if USE_ATOMICS
386  /* check whether the pool is empty */
387  buf = get_pool(pool);
388  if (!buf && pool->refcount <= pool->nb_allocated) {
389  av_log(NULL, AV_LOG_DEBUG, "Pool race dectected, spining to avoid overallocation and eventual OOM\n");
390  while (!buf && avpriv_atomic_int_get(&pool->refcount) <= avpriv_atomic_int_get(&pool->nb_allocated))
391  buf = get_pool(pool);
392  }
393 
394  if (!buf)
395  return pool_alloc_buffer(pool);
396 
397  /* keep the first entry, return the rest of the list to the pool */
398  add_to_pool(buf->next);
399  buf->next = NULL;
400 
401  ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
402  buf, 0);
403  if (!ret) {
404  add_to_pool(buf);
405  return NULL;
406  }
407 #else
408  ff_mutex_lock(&pool->mutex);
409  buf = pool->pool;
410  if (buf) {
411  ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
412  buf, 0);
413  if (ret) {
414  pool->pool = buf->next;
415  buf->next = NULL;
416  }
417  } else {
418  ret = pool_alloc_buffer(pool);
419  }
420  ff_mutex_unlock(&pool->mutex);
421 #endif
422 
423  if (ret)
425 
426  return ret;
427 }
AVBufferRef *(* alloc2)(void *opaque, int size)
void(* free)(void *opaque, uint8_t *data)
a callback for freeing the data
int av_buffer_make_writable(AVBufferRef **pbuf)
Create a writable reference from a given buffer reference, avoiding data copy if possible.
Definition: buffer.c:150
#define NULL
Definition: coverity.c:32
#define avpriv_atomic_int_add_and_fetch
Definition: atomic_gcc.h:50
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:124
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
memory handling functions
#define ff_mutex_unlock(mutex)
Definition: thread.h:155
#define FF_MEMORY_POISON
Definition: internal.h:85
const char * b
Definition: vf_curves.c:109
#define BUFFER_FLAG_READONLY
The buffer is always treated as read-only.
struct BufferPoolEntry * next
volatile int refcount
number of existing AVBufferRef instances referring to this buffer
static AVBufferRef * pool_alloc_buffer(AVBufferPool *pool)
Definition: buffer.c:348
uint8_t * data
data described by this buffer
uint8_t
#define av_malloc(s)
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
#define avpriv_atomic_int_set
Definition: atomic_gcc.h:39
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:113
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
The buffer pool.
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
Definition: buffer.c:61
void(* pool_free)(void *opaque)
#define avpriv_atomic_ptr_cas
Definition: atomic_gcc.h:60
#define AVERROR(e)
Definition: error.h:43
#define avpriv_atomic_int_get
Definition: atomic_gcc.h:28
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
int av_buffer_realloc(AVBufferRef **pbuf, int size)
Reallocate a given buffer.
Definition: buffer.c:168
#define ff_mutex_destroy(mutex)
Definition: thread.h:156
#define FFMIN(a, b)
Definition: common.h:96
int av_buffer_is_writable(const AVBufferRef *buf)
Definition: buffer.c:132
#define src
Definition: vp9dsp.c:530
#define ff_mutex_lock(mutex)
Definition: thread.h:154
void(* free)(void *opaque, uint8_t *data)
AVBufferPool * av_buffer_pool_init2(int size, void *opaque, AVBufferRef *(*alloc)(void *opaque, int size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition: buffer.c:217
int flags
A combination of BUFFER_FLAG_*.
#define ff_mutex_init(mutex, attr)
Definition: thread.h:153
static void pool_release_buffer(void *opaque, uint8_t *data)
Definition: buffer.c:325
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:66
volatile int nb_allocated
uint8_t * data
The data buffer.
Definition: buffer.h:89
int av_buffer_get_ref_count(const AVBufferRef *buf)
Definition: buffer.c:145
void * buf
Definition: avisynth_c.h:553
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:82
AVBuffer * buffer
Definition: buffer.h:82
void * av_buffer_get_opaque(const AVBufferRef *buf)
Definition: buffer.c:140
static void buffer_pool_free(AVBufferPool *pool)
Definition: buffer.c:257
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:274
static void buffer_replace(AVBufferRef **dst, AVBufferRef **src)
Definition: buffer.c:106
A reference counted buffer type.
int size
Size of data in bytes.
Definition: buffer.h:93
static int flags
Definition: cpu.c:47
volatile int refcount
void * opaque
an opaque pointer, to be used by the freeing callback
A reference to a data buffer.
Definition: buffer.h:81
common internal and external API header
AVBufferPool * pool
AVBufferPool * av_buffer_pool_init(int size, AVBufferRef *(*alloc)(int size))
Allocate and initialize a buffer pool.
Definition: buffer.c:237
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:145
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
#define av_free(p)
static uint8_t tmp[8]
Definition: des.c:38
int size
size of data in bytes
BufferPoolEntry * pool
#define av_freep(p)
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:380
#define BUFFER_FLAG_REALLOCATABLE
The buffer was av_realloc()ed, so it is reallocatable.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
AVBufferRef *(* alloc)(int size)