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
43 #include "intreadwrite.h"
48 #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
49 #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
50 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
51 #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
52 #define free AV_JOIN(MALLOC_PREFIX, free)
54 void *malloc(size_t size);
55 void *memalign(size_t align, size_t size);
56 int posix_memalign(void **ptr, size_t align, size_t size);
57 void *realloc(void *ptr, size_t size);
60 #endif /* MALLOC_PREFIX */
62 #define ALIGN (HAVE_AVX ? 32 : 16)
64 /* NOTE: if you want to override these functions with your own
65 * implementations (not recommended) you have to link libav* as
66 * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
67 * Note that this will cost performance. */
69 static size_t max_alloc_size= INT_MAX;
71 void av_max_alloc(size_t max){
75 void *av_malloc(size_t size)
78 #if CONFIG_MEMALIGN_HACK
82 /* let's disallow possibly ambiguous cases */
83 if (size > (max_alloc_size - 32))
86 #if CONFIG_MEMALIGN_HACK
87 ptr = malloc(size + ALIGN);
90 diff = ((~(long)ptr)&(ALIGN - 1)) + 1;
91 ptr = (char *)ptr + diff;
92 ((char *)ptr)[-1] = diff;
93 #elif HAVE_POSIX_MEMALIGN
94 if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
95 if (posix_memalign(&ptr, ALIGN, size))
97 #elif HAVE_ALIGNED_MALLOC
98 ptr = _aligned_malloc(size, ALIGN);
101 ptr = memalign(ALIGN, size);
103 ptr = memalign(size, ALIGN);
106 * Indeed, we should align it:
109 * on 32 for 586, PPro - K6-III
110 * on 64 for K7 (maybe for P3 too).
111 * Because L1 and L2 caches are aligned on those values.
112 * But I don't want to code such logic here!
115 * For AVX ASM. SSE / NEON needs only 16.
116 * Why not larger? Because I did not see a difference in benchmarks ...
118 /* benchmarks with P3
119 * memalign(64) + 1 3071, 3051, 3032
120 * memalign(64) + 2 3051, 3032, 3041
121 * memalign(64) + 4 2911, 2896, 2915
122 * memalign(64) + 8 2545, 2554, 2550
123 * memalign(64) + 16 2543, 2572, 2563
124 * memalign(64) + 32 2546, 2545, 2571
125 * memalign(64) + 64 2570, 2533, 2558
127 * BTW, malloc seems to do 8-byte alignment by default here.
136 #if CONFIG_MEMORY_POISONING
138 memset(ptr, FF_MEMORY_POISON, size);
143 void *av_realloc(void *ptr, size_t size)
145 #if CONFIG_MEMALIGN_HACK
149 /* let's disallow possibly ambiguous cases */
150 if (size > (max_alloc_size - 32))
153 #if CONFIG_MEMALIGN_HACK
154 //FIXME this isn't aligned correctly, though it probably isn't needed
156 return av_malloc(size);
157 diff = ((char *)ptr)[-1];
158 av_assert0(diff>0 && diff<=ALIGN);
159 ptr = realloc((char *)ptr - diff, size + diff);
161 ptr = (char *)ptr + diff;
163 #elif HAVE_ALIGNED_MALLOC
164 return _aligned_realloc(ptr, size + !size, ALIGN);
166 return realloc(ptr, size + !size);
170 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
175 if (av_size_mult(elsize, nelem, &size)) {
179 r = av_realloc(ptr, size);
185 int av_reallocp(void *ptr, size_t size)
194 ret = av_realloc(*ptrptr, size);
198 return AVERROR(ENOMEM);
205 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
207 if (!size || nmemb >= INT_MAX / size)
209 return av_realloc(ptr, nmemb * size);
212 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
215 *ptrptr = av_realloc_f(*ptrptr, nmemb, size);
216 if (!*ptrptr && nmemb && size)
217 return AVERROR(ENOMEM);
221 void av_free(void *ptr)
223 #if CONFIG_MEMALIGN_HACK
225 int v= ((char *)ptr)[-1];
226 av_assert0(v>0 && v<=ALIGN);
227 free((char *)ptr - v);
229 #elif HAVE_ALIGNED_MALLOC
236 void av_freep(void *arg)
238 void **ptr = (void **)arg;
243 void *av_mallocz(size_t size)
245 void *ptr = av_malloc(size);
247 memset(ptr, 0, size);
251 void *av_calloc(size_t nmemb, size_t size)
253 if (size <= 0 || nmemb >= INT_MAX / size)
255 return av_mallocz(nmemb * size);
258 char *av_strdup(const char *s)
262 int len = strlen(s) + 1;
263 ptr = av_realloc(NULL, len);
270 void *av_memdup(const void *p, size_t size)
274 ptr = av_malloc(size);
276 memcpy(ptr, p, size);
281 int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
283 void **tab = *(void ***)tab_ptr;
285 AV_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, {
287 *(void ***)tab_ptr = tab;
289 return AVERROR(ENOMEM);
294 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
296 void **tab = *(void ***)tab_ptr;
298 AV_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, {
300 *(void ***)tab_ptr = tab;
307 void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
308 const uint8_t *elem_data)
310 uint8_t *tab_elem_data = NULL;
312 AV_DYNARRAY_ADD(INT_MAX, elem_size, *tab_ptr, *nb_ptr, {
313 tab_elem_data = (uint8_t *)*tab_ptr + (*nb_ptr) * elem_size;
315 memcpy(tab_elem_data, elem_data, elem_size);
316 else if (CONFIG_MEMORY_POISONING)
317 memset(tab_elem_data, FF_MEMORY_POISON, elem_size);
322 return tab_elem_data;
325 static void fill16(uint8_t *dst, int len)
327 uint32_t v = AV_RN16(dst - 2);
343 static void fill24(uint8_t *dst, int len)
346 uint32_t v = AV_RB24(dst - 3);
347 uint32_t a = v << 8 | v >> 16;
348 uint32_t b = v << 16 | v >> 8;
349 uint32_t c = v << 24 | v;
351 uint32_t v = AV_RL24(dst - 3);
352 uint32_t a = v | v << 24;
353 uint32_t b = v >> 8 | v << 16;
354 uint32_t c = v >> 16 | v << 8;
383 static void fill32(uint8_t *dst, int len)
385 uint32_t v = AV_RN32(dst - 4);
399 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
401 const uint8_t *src = &dst[-back];
406 memset(dst, *src, cnt);
407 } else if (back == 2) {
409 } else if (back == 3) {
411 } else if (back == 4) {
416 while (cnt > blocklen) {
417 memcpy(dst, src, blocklen);
422 memcpy(dst, src, cnt);
426 AV_COPY32U(dst, src);
427 AV_COPY32U(dst + 4, src + 4);
433 AV_COPY32U(dst, src);
439 AV_COPY16U(dst, src);
449 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
451 if (min_size < *size)
454 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
456 ptr = av_realloc(ptr, min_size);
457 /* we could set this to the unmodified min_size but this is safer
458 * if the user lost the ptr and uses NULL now
468 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
471 if (min_size < *size)
473 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
475 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
482 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
484 ff_fast_malloc(ptr, size, min_size, 0);