2 * default memory allocator for libavutil
3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
24 * default memory allocator for libavutil
27 #define _XOPEN_SOURCE 600
42 #include "intreadwrite.h"
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)
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);
59 #endif /* MALLOC_PREFIX */
61 #define ALIGN (HAVE_AVX ? 32 : 16)
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. */
68 static size_t max_alloc_size= INT_MAX;
70 void av_max_alloc(size_t max){
74 void *av_malloc(size_t size)
77 #if CONFIG_MEMALIGN_HACK
81 /* let's disallow possibly ambiguous cases */
82 if (size > (max_alloc_size - 32))
85 #if CONFIG_MEMALIGN_HACK
86 ptr = malloc(size + ALIGN);
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))
96 #elif HAVE_ALIGNED_MALLOC
97 ptr = _aligned_malloc(size, ALIGN);
100 ptr = memalign(ALIGN, size);
102 ptr = memalign(size, ALIGN);
105 * Indeed, we should align it:
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!
114 * For AVX ASM. SSE / NEON needs only 16.
115 * Why not larger? Because I did not see a difference in benchmarks ...
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
126 * BTW, malloc seems to do 8-byte alignment by default here.
135 #if CONFIG_MEMORY_POISONING
137 memset(ptr, FF_MEMORY_POISON, size);
142 void *av_realloc(void *ptr, size_t size)
144 #if CONFIG_MEMALIGN_HACK
148 /* let's disallow possibly ambiguous cases */
149 if (size > (max_alloc_size - 32))
152 #if CONFIG_MEMALIGN_HACK
153 //FIXME this isn't aligned correctly, though it probably isn't needed
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);
160 ptr = (char *)ptr + diff;
162 #elif HAVE_ALIGNED_MALLOC
163 return _aligned_realloc(ptr, size + !size, ALIGN);
165 return realloc(ptr, size + !size);
169 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
174 if (av_size_mult(elsize, nelem, &size)) {
178 r = av_realloc(ptr, size);
184 int av_reallocp(void *ptr, size_t size)
193 ret = av_realloc(*ptrptr, size);
197 return AVERROR(ENOMEM);
204 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
206 if (!size || nmemb >= INT_MAX / size)
208 return av_realloc(ptr, nmemb * size);
211 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
214 *ptrptr = av_realloc_f(*ptrptr, nmemb, size);
215 if (!*ptrptr && nmemb && size)
216 return AVERROR(ENOMEM);
220 void av_free(void *ptr)
222 #if CONFIG_MEMALIGN_HACK
224 int v= ((char *)ptr)[-1];
225 av_assert0(v>0 && v<=ALIGN);
226 free((char *)ptr - v);
228 #elif HAVE_ALIGNED_MALLOC
235 void av_freep(void *arg)
237 void **ptr = (void **)arg;
242 void *av_mallocz(size_t size)
244 void *ptr = av_malloc(size);
246 memset(ptr, 0, size);
250 void *av_calloc(size_t nmemb, size_t size)
252 if (size <= 0 || nmemb >= INT_MAX / size)
254 return av_mallocz(nmemb * size);
257 char *av_strdup(const char *s)
261 int len = strlen(s) + 1;
262 ptr = av_realloc(NULL, len);
269 void *av_memdup(const void *p, size_t size)
273 ptr = av_malloc(size);
275 memcpy(ptr, p, size);
280 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
282 /* see similar ffmpeg.c:grow_array() */
287 tab = *(intptr_t**)tab_ptr;
288 if ((nb & (nb - 1)) == 0) {
292 if (nb > INT_MAX / (2 * sizeof(intptr_t)))
296 tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
299 *(intptr_t**)tab_ptr = tab;
301 tab[nb++] = (intptr_t)elem;
310 void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
311 const uint8_t *elem_data)
313 int nb = *nb_ptr, nb_alloc;
314 uint8_t *tab = *tab_ptr, *tab_elem_data;
316 if ((nb & (nb - 1)) == 0) {
320 if (nb > INT_MAX / (2 * elem_size))
324 tab = av_realloc(tab, nb_alloc * elem_size);
330 tab_elem_data = tab + nb*elem_size;
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;
343 static void fill16(uint8_t *dst, int len)
345 uint32_t v = AV_RN16(dst - 2);
361 static void fill24(uint8_t *dst, int len)
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;
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;
401 static void fill32(uint8_t *dst, int len)
403 uint32_t v = AV_RN32(dst - 4);
417 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
419 const uint8_t *src = &dst[-back];
424 memset(dst, *src, cnt);
425 } else if (back == 2) {
427 } else if (back == 3) {
429 } else if (back == 4) {
434 while (cnt > blocklen) {
435 memcpy(dst, src, blocklen);
440 memcpy(dst, src, cnt);
444 AV_COPY32U(dst, src);
445 AV_COPY32U(dst + 4, src + 4);
451 AV_COPY32U(dst, src);
457 AV_COPY16U(dst, src);
467 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
469 if (min_size < *size)
472 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
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
486 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
489 if (min_size < *size)
491 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
493 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
500 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
502 ff_fast_malloc(ptr, size, min_size, 0);