]> git.sesse.net Git - ffmpeg/blob - libavutil/buffer.c
Merge commit 'fb25d99b0a5e21fb8cc184c7a9d3736387778266'
[ffmpeg] / libavutil / buffer.c
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
28 AVBufferRef *av_buffer_create(uint8_t *data, int size,
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)
46         buf->flags |= 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
66 AVBufferRef *av_buffer_alloc(int size)
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
82 AVBufferRef *av_buffer_allocz(int size)
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
92 AVBufferRef *av_buffer_ref(AVBufferRef *buf)
93 {
94     AVBufferRef *ret = av_mallocz(sizeof(*ret));
95
96     if (!ret)
97         return NULL;
98
99     *ret = *buf;
100
101     avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 1);
102
103     return ret;
104 }
105
106 static void buffer_replace(AVBufferRef **dst, AVBufferRef **src)
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
118     if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
119         b->free(b->opaque, b->data);
120         av_freep(&b);
121     }
122 }
123
124 void av_buffer_unref(AVBufferRef **buf)
125 {
126     if (!buf || !*buf)
127         return;
128
129     buffer_replace(buf, NULL);
130 }
131
132 int av_buffer_is_writable(const AVBufferRef *buf)
133 {
134     if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
135         return 0;
136
137     return avpriv_atomic_int_get(&buf->buffer->refcount) == 1;
138 }
139
140 void *av_buffer_get_opaque(const AVBufferRef *buf)
141 {
142     return buf->buffer->opaque;
143 }
144
145 int av_buffer_get_ref_count(const AVBufferRef *buf)
146 {
147     return buf->buffer->refcount;
148 }
149
150 int av_buffer_make_writable(AVBufferRef **pbuf)
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
186         buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
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_init(int size, AVBufferRef* (*alloc)(int size))
218 {
219     AVBufferPool *pool = av_mallocz(sizeof(*pool));
220     if (!pool)
221         return NULL;
222
223     ff_mutex_init(&pool->mutex, NULL);
224
225     pool->size     = size;
226     pool->alloc    = alloc ? alloc : av_buffer_alloc;
227
228     avpriv_atomic_int_set(&pool->refcount, 1);
229
230     return pool;
231 }
232
233 /*
234  * This function gets called when the pool has been uninited and
235  * all the buffers returned to it.
236  */
237 static void buffer_pool_free(AVBufferPool *pool)
238 {
239     while (pool->pool) {
240         BufferPoolEntry *buf = pool->pool;
241         pool->pool = buf->next;
242
243         buf->free(buf->opaque, buf->data);
244         av_freep(&buf);
245     }
246     ff_mutex_destroy(&pool->mutex);
247     av_freep(&pool);
248 }
249
250 void av_buffer_pool_uninit(AVBufferPool **ppool)
251 {
252     AVBufferPool *pool;
253
254     if (!ppool || !*ppool)
255         return;
256     pool   = *ppool;
257     *ppool = NULL;
258
259     if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
260         buffer_pool_free(pool);
261 }
262
263 #if USE_ATOMICS
264 /* remove the whole buffer list from the pool and return it */
265 static BufferPoolEntry *get_pool(AVBufferPool *pool)
266 {
267     BufferPoolEntry *cur = *(void * volatile *)&pool->pool, *last = NULL;
268
269     while (cur != last) {
270         last = cur;
271         cur = avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, last, NULL);
272         if (!cur)
273             return NULL;
274     }
275
276     return cur;
277 }
278
279 static void add_to_pool(BufferPoolEntry *buf)
280 {
281     AVBufferPool *pool;
282     BufferPoolEntry *cur, *end = buf;
283
284     if (!buf)
285         return;
286     pool = buf->pool;
287
288     while (end->next)
289         end = end->next;
290
291     while (avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, NULL, buf)) {
292         /* pool is not empty, retrieve it and append it to our list */
293         cur = get_pool(pool);
294         end->next = cur;
295         while (end->next)
296             end = end->next;
297     }
298 }
299 #endif
300
301 static void pool_release_buffer(void *opaque, uint8_t *data)
302 {
303     BufferPoolEntry *buf = opaque;
304     AVBufferPool *pool = buf->pool;
305
306     if(CONFIG_MEMORY_POISONING)
307         memset(buf->data, FF_MEMORY_POISON, pool->size);
308
309 #if USE_ATOMICS
310     add_to_pool(buf);
311 #else
312     ff_mutex_lock(&pool->mutex);
313     buf->next = pool->pool;
314     pool->pool = buf;
315     ff_mutex_unlock(&pool->mutex);
316 #endif
317
318     if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
319         buffer_pool_free(pool);
320 }
321
322 /* allocate a new buffer and override its free() callback so that
323  * it is returned to the pool on free */
324 static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
325 {
326     BufferPoolEntry *buf;
327     AVBufferRef     *ret;
328
329     ret = pool->alloc(pool->size);
330     if (!ret)
331         return NULL;
332
333     buf = av_mallocz(sizeof(*buf));
334     if (!buf) {
335         av_buffer_unref(&ret);
336         return NULL;
337     }
338
339     buf->data   = ret->buffer->data;
340     buf->opaque = ret->buffer->opaque;
341     buf->free   = ret->buffer->free;
342     buf->pool   = pool;
343
344     ret->buffer->opaque = buf;
345     ret->buffer->free   = pool_release_buffer;
346
347 #if USE_ATOMICS
348     avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
349     avpriv_atomic_int_add_and_fetch(&pool->nb_allocated, 1);
350 #endif
351
352     return ret;
353 }
354
355 AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
356 {
357     AVBufferRef *ret;
358     BufferPoolEntry *buf;
359
360 #if USE_ATOMICS
361     /* check whether the pool is empty */
362     buf = get_pool(pool);
363     if (!buf && pool->refcount <= pool->nb_allocated) {
364         av_log(NULL, AV_LOG_DEBUG, "Pool race dectected, spining to avoid overallocation and eventual OOM\n");
365         while (!buf && avpriv_atomic_int_get(&pool->refcount) <= avpriv_atomic_int_get(&pool->nb_allocated))
366             buf = get_pool(pool);
367     }
368
369     if (!buf)
370         return pool_alloc_buffer(pool);
371
372     /* keep the first entry, return the rest of the list to the pool */
373     add_to_pool(buf->next);
374     buf->next = NULL;
375
376     ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
377                            buf, 0);
378     if (!ret) {
379         add_to_pool(buf);
380         return NULL;
381     }
382 #else
383     ff_mutex_lock(&pool->mutex);
384     buf = pool->pool;
385     if (buf) {
386         ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
387                                buf, 0);
388         if (ret) {
389             pool->pool = buf->next;
390             buf->next = NULL;
391         }
392     } else {
393         ret = pool_alloc_buffer(pool);
394     }
395     ff_mutex_unlock(&pool->mutex);
396 #endif
397
398     if (ret)
399         avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
400
401     return ret;
402 }