]> git.sesse.net Git - ffmpeg/blob - libavutil/mem.c
Merge commit '513d849bb605d3d862da1ada709bd2ca1ac68f58'
[ffmpeg] / libavutil / mem.c
1 /*
2  * default memory allocator for libavutil
3  * Copyright (c) 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * default memory allocator for libavutil
25  */
26
27 #define _XOPEN_SOURCE 600
28
29 #include "config.h"
30
31 #include <limits.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #if HAVE_MALLOC_H
36 #include <malloc.h>
37 #endif
38
39 #include "avassert.h"
40 #include "avutil.h"
41 #include "common.h"
42 #include "intreadwrite.h"
43 #include "mem.h"
44
45 #ifdef MALLOC_PREFIX
46
47 #define malloc         AV_JOIN(MALLOC_PREFIX, malloc)
48 #define memalign       AV_JOIN(MALLOC_PREFIX, memalign)
49 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
50 #define realloc        AV_JOIN(MALLOC_PREFIX, realloc)
51 #define free           AV_JOIN(MALLOC_PREFIX, free)
52
53 void *malloc(size_t size);
54 void *memalign(size_t align, size_t size);
55 int   posix_memalign(void **ptr, size_t align, size_t size);
56 void *realloc(void *ptr, size_t size);
57 void  free(void *ptr);
58
59 #endif /* MALLOC_PREFIX */
60
61 #define ALIGN (HAVE_AVX ? 32 : 16)
62
63 /* NOTE: if you want to override these functions with your own
64  * implementations (not recommended) you have to link libav* as
65  * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
66  * Note that this will cost performance. */
67
68 static size_t max_alloc_size= INT_MAX;
69
70 void av_max_alloc(size_t max){
71     max_alloc_size = max;
72 }
73
74 void *av_malloc(size_t size)
75 {
76     void *ptr = NULL;
77 #if CONFIG_MEMALIGN_HACK
78     long diff;
79 #endif
80
81     /* let's disallow possibly ambiguous cases */
82     if (size > (max_alloc_size - 32))
83         return NULL;
84
85 #if CONFIG_MEMALIGN_HACK
86     ptr = malloc(size + ALIGN);
87     if (!ptr)
88         return ptr;
89     diff              = ((~(long)ptr)&(ALIGN - 1)) + 1;
90     ptr               = (char *)ptr + diff;
91     ((char *)ptr)[-1] = diff;
92 #elif HAVE_POSIX_MEMALIGN
93     if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
94     if (posix_memalign(&ptr, ALIGN, size))
95         ptr = NULL;
96 #elif HAVE_ALIGNED_MALLOC
97     ptr = _aligned_malloc(size, ALIGN);
98 #elif HAVE_MEMALIGN
99 #ifndef __DJGPP__
100     ptr = memalign(ALIGN, size);
101 #else
102     ptr = memalign(size, ALIGN);
103 #endif
104     /* Why 64?
105      * Indeed, we should align it:
106      *   on  4 for 386
107      *   on 16 for 486
108      *   on 32 for 586, PPro - K6-III
109      *   on 64 for K7 (maybe for P3 too).
110      * Because L1 and L2 caches are aligned on those values.
111      * But I don't want to code such logic here!
112      */
113     /* Why 32?
114      * For AVX ASM. SSE / NEON needs only 16.
115      * Why not larger? Because I did not see a difference in benchmarks ...
116      */
117     /* benchmarks with P3
118      * memalign(64) + 1          3071, 3051, 3032
119      * memalign(64) + 2          3051, 3032, 3041
120      * memalign(64) + 4          2911, 2896, 2915
121      * memalign(64) + 8          2545, 2554, 2550
122      * memalign(64) + 16         2543, 2572, 2563
123      * memalign(64) + 32         2546, 2545, 2571
124      * memalign(64) + 64         2570, 2533, 2558
125      *
126      * BTW, malloc seems to do 8-byte alignment by default here.
127      */
128 #else
129     ptr = malloc(size);
130 #endif
131     if(!ptr && !size) {
132         size = 1;
133         ptr= av_malloc(1);
134     }
135 #if CONFIG_MEMORY_POISONING
136     if (ptr)
137         memset(ptr, FF_MEMORY_POISON, size);
138 #endif
139     return ptr;
140 }
141
142 void *av_realloc(void *ptr, size_t size)
143 {
144 #if CONFIG_MEMALIGN_HACK
145     int diff;
146 #endif
147
148     /* let's disallow possibly ambiguous cases */
149     if (size > (max_alloc_size - 32))
150         return NULL;
151
152 #if CONFIG_MEMALIGN_HACK
153     //FIXME this isn't aligned correctly, though it probably isn't needed
154     if (!ptr)
155         return av_malloc(size);
156     diff = ((char *)ptr)[-1];
157     av_assert0(diff>0 && diff<=ALIGN);
158     ptr = realloc((char *)ptr - diff, size + diff);
159     if (ptr)
160         ptr = (char *)ptr + diff;
161     return ptr;
162 #elif HAVE_ALIGNED_MALLOC
163     return _aligned_realloc(ptr, size + !size, ALIGN);
164 #else
165     return realloc(ptr, size + !size);
166 #endif
167 }
168
169 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
170 {
171     size_t size;
172     void *r;
173
174     if (av_size_mult(elsize, nelem, &size)) {
175         av_free(ptr);
176         return NULL;
177     }
178     r = av_realloc(ptr, size);
179     if (!r && size)
180         av_free(ptr);
181     return r;
182 }
183
184 int av_reallocp(void *ptr, size_t size)
185 {
186     void **ptrptr = ptr;
187     void *ret;
188
189     if (!size) {
190         av_freep(ptr);
191         return 0;
192     }
193     ret = av_realloc(*ptrptr, size);
194
195     if (!ret) {
196         av_freep(ptr);
197         return AVERROR(ENOMEM);
198     }
199
200     *ptrptr = ret;
201     return 0;
202 }
203
204 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
205 {
206     if (!size || nmemb >= INT_MAX / size)
207         return NULL;
208     return av_realloc(ptr, nmemb * size);
209 }
210
211 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
212 {
213     void **ptrptr = ptr;
214     *ptrptr = av_realloc_f(*ptrptr, nmemb, size);
215     if (!*ptrptr && nmemb && size)
216         return AVERROR(ENOMEM);
217     return 0;
218 }
219
220 void av_free(void *ptr)
221 {
222 #if CONFIG_MEMALIGN_HACK
223     if (ptr) {
224         int v= ((char *)ptr)[-1];
225         av_assert0(v>0 && v<=ALIGN);
226         free((char *)ptr - v);
227     }
228 #elif HAVE_ALIGNED_MALLOC
229     _aligned_free(ptr);
230 #else
231     free(ptr);
232 #endif
233 }
234
235 void av_freep(void *arg)
236 {
237     void **ptr = (void **)arg;
238     av_free(*ptr);
239     *ptr = NULL;
240 }
241
242 void *av_mallocz(size_t size)
243 {
244     void *ptr = av_malloc(size);
245     if (ptr)
246         memset(ptr, 0, size);
247     return ptr;
248 }
249
250 void *av_calloc(size_t nmemb, size_t size)
251 {
252     if (size <= 0 || nmemb >= INT_MAX / size)
253         return NULL;
254     return av_mallocz(nmemb * size);
255 }
256
257 char *av_strdup(const char *s)
258 {
259     char *ptr = NULL;
260     if (s) {
261         int len = strlen(s) + 1;
262         ptr = av_realloc(NULL, len);
263         if (ptr)
264             memcpy(ptr, s, len);
265     }
266     return ptr;
267 }
268
269 void *av_memdup(const void *p, size_t size)
270 {
271     void *ptr = NULL;
272     if (p) {
273         ptr = av_malloc(size);
274         if (ptr)
275             memcpy(ptr, p, size);
276     }
277     return ptr;
278 }
279
280 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
281 {
282     /* see similar ffmpeg.c:grow_array() */
283     int nb, nb_alloc;
284     intptr_t *tab;
285
286     nb = *nb_ptr;
287     tab = *(intptr_t**)tab_ptr;
288     if ((nb & (nb - 1)) == 0) {
289         if (nb == 0) {
290             nb_alloc = 1;
291         } else {
292             if (nb > INT_MAX / (2 * sizeof(intptr_t)))
293                 goto fail;
294             nb_alloc = nb * 2;
295         }
296         tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
297         if (!tab)
298             goto fail;
299         *(intptr_t**)tab_ptr = tab;
300     }
301     tab[nb++] = (intptr_t)elem;
302     *nb_ptr = nb;
303     return;
304
305 fail:
306     av_freep(tab_ptr);
307     *nb_ptr = 0;
308 }
309
310 void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
311                        const uint8_t *elem_data)
312 {
313     int nb = *nb_ptr, nb_alloc;
314     uint8_t *tab = *tab_ptr, *tab_elem_data;
315
316     if ((nb & (nb - 1)) == 0) {
317         if (nb == 0) {
318             nb_alloc = 1;
319         } else {
320             if (nb > INT_MAX / (2 * elem_size))
321                 goto fail;
322             nb_alloc = nb * 2;
323         }
324         tab = av_realloc(tab, nb_alloc * elem_size);
325         if (!tab)
326             goto fail;
327         *tab_ptr = tab;
328     }
329     *nb_ptr = nb + 1;
330     tab_elem_data = tab + nb*elem_size;
331     if (elem_data)
332         memcpy(tab_elem_data, elem_data, elem_size);
333     else if (CONFIG_MEMORY_POISONING)
334         memset(tab_elem_data, FF_MEMORY_POISON, elem_size);
335     return tab_elem_data;
336
337 fail:
338     av_freep(tab_ptr);
339     *nb_ptr = 0;
340     return NULL;
341 }
342
343 static void fill16(uint8_t *dst, int len)
344 {
345     uint32_t v = AV_RN16(dst - 2);
346
347     v |= v << 16;
348
349     while (len >= 4) {
350         AV_WN32(dst, v);
351         dst += 4;
352         len -= 4;
353     }
354
355     while (len--) {
356         *dst = dst[-2];
357         dst++;
358     }
359 }
360
361 static void fill24(uint8_t *dst, int len)
362 {
363 #if HAVE_BIGENDIAN
364     uint32_t v = AV_RB24(dst - 3);
365     uint32_t a = v << 8  | v >> 16;
366     uint32_t b = v << 16 | v >> 8;
367     uint32_t c = v << 24 | v;
368 #else
369     uint32_t v = AV_RL24(dst - 3);
370     uint32_t a = v       | v << 24;
371     uint32_t b = v >> 8  | v << 16;
372     uint32_t c = v >> 16 | v << 8;
373 #endif
374
375     while (len >= 12) {
376         AV_WN32(dst,     a);
377         AV_WN32(dst + 4, b);
378         AV_WN32(dst + 8, c);
379         dst += 12;
380         len -= 12;
381     }
382
383     if (len >= 4) {
384         AV_WN32(dst, a);
385         dst += 4;
386         len -= 4;
387     }
388
389     if (len >= 4) {
390         AV_WN32(dst, b);
391         dst += 4;
392         len -= 4;
393     }
394
395     while (len--) {
396         *dst = dst[-3];
397         dst++;
398     }
399 }
400
401 static void fill32(uint8_t *dst, int len)
402 {
403     uint32_t v = AV_RN32(dst - 4);
404
405     while (len >= 4) {
406         AV_WN32(dst, v);
407         dst += 4;
408         len -= 4;
409     }
410
411     while (len--) {
412         *dst = dst[-4];
413         dst++;
414     }
415 }
416
417 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
418 {
419     const uint8_t *src = &dst[-back];
420     if (!back)
421         return;
422
423     if (back == 1) {
424         memset(dst, *src, cnt);
425     } else if (back == 2) {
426         fill16(dst, cnt);
427     } else if (back == 3) {
428         fill24(dst, cnt);
429     } else if (back == 4) {
430         fill32(dst, cnt);
431     } else {
432         if (cnt >= 16) {
433             int blocklen = back;
434             while (cnt > blocklen) {
435                 memcpy(dst, src, blocklen);
436                 dst       += blocklen;
437                 cnt       -= blocklen;
438                 blocklen <<= 1;
439             }
440             memcpy(dst, src, cnt);
441             return;
442         }
443         if (cnt >= 8) {
444             AV_COPY32U(dst,     src);
445             AV_COPY32U(dst + 4, src + 4);
446             src += 8;
447             dst += 8;
448             cnt -= 8;
449         }
450         if (cnt >= 4) {
451             AV_COPY32U(dst, src);
452             src += 4;
453             dst += 4;
454             cnt -= 4;
455         }
456         if (cnt >= 2) {
457             AV_COPY16U(dst, src);
458             src += 2;
459             dst += 2;
460             cnt -= 2;
461         }
462         if (cnt)
463             *dst = *src;
464     }
465 }
466
467 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
468 {
469     if (min_size < *size)
470         return ptr;
471
472     min_size = FFMAX(17 * min_size / 16 + 32, min_size);
473
474     ptr = av_realloc(ptr, min_size);
475     /* we could set this to the unmodified min_size but this is safer
476      * if the user lost the ptr and uses NULL now
477      */
478     if (!ptr)
479         min_size = 0;
480
481     *size = min_size;
482
483     return ptr;
484 }
485
486 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
487 {
488     void **p = ptr;
489     if (min_size < *size)
490         return 0;
491     min_size = FFMAX(17 * min_size / 16 + 32, min_size);
492     av_free(*p);
493     *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
494     if (!*p)
495         min_size = 0;
496     *size = min_size;
497     return 1;
498 }
499
500 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
501 {
502     ff_fast_malloc(ptr, size, min_size, 0);
503 }
504