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
41 #include "intreadwrite.h"
46 #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
47 #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
48 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
49 #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
50 #define free AV_JOIN(MALLOC_PREFIX, free)
52 void *malloc(size_t size);
53 void *memalign(size_t align, size_t size);
54 int posix_memalign(void **ptr, size_t align, size_t size);
55 void *realloc(void *ptr, size_t size);
58 #endif /* MALLOC_PREFIX */
60 #define ALIGN (HAVE_AVX ? 32 : 16)
62 /* NOTE: if you want to override these functions with your own
63 * implementations (not recommended) you have to link libav* as
64 * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
65 * Note that this will cost performance. */
67 static size_t max_alloc_size= INT_MAX;
69 void av_max_alloc(size_t max){
73 void *av_malloc(size_t size)
76 #if CONFIG_MEMALIGN_HACK
80 /* let's disallow possible ambiguous cases */
81 if (size > (max_alloc_size - 32))
84 #if CONFIG_MEMALIGN_HACK
85 ptr = malloc(size + ALIGN);
88 diff = ((~(long)ptr)&(ALIGN - 1)) + 1;
89 ptr = (char *)ptr + diff;
90 ((char *)ptr)[-1] = diff;
91 #elif HAVE_POSIX_MEMALIGN
92 if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
93 if (posix_memalign(&ptr, ALIGN, size))
95 #elif HAVE_ALIGNED_MALLOC
96 ptr = _aligned_malloc(size, ALIGN);
99 ptr = memalign(ALIGN, size);
101 ptr = memalign(size, ALIGN);
104 * Indeed, we should align it:
107 * on 32 for 586, PPro - K6-III
108 * on 64 for K7 (maybe for P3 too).
109 * Because L1 and L2 caches are aligned on those values.
110 * But I don't want to code such logic here!
113 * For AVX ASM. SSE / NEON needs only 16.
114 * Why not larger? Because I did not see a difference in benchmarks ...
116 /* benchmarks with P3
117 * memalign(64) + 1 3071, 3051, 3032
118 * memalign(64) + 2 3051, 3032, 3041
119 * memalign(64) + 4 2911, 2896, 2915
120 * memalign(64) + 8 2545, 2554, 2550
121 * memalign(64) + 16 2543, 2572, 2563
122 * memalign(64) + 32 2546, 2545, 2571
123 * memalign(64) + 64 2570, 2533, 2558
125 * BTW, malloc seems to do 8-byte alignment by default here.
134 #if CONFIG_MEMORY_POISONING
136 memset(ptr, FF_MEMORY_POISON, size);
141 void *av_realloc(void *ptr, size_t size)
143 #if CONFIG_MEMALIGN_HACK
147 /* let's disallow possible ambiguous cases */
148 if (size > (max_alloc_size - 32))
151 #if CONFIG_MEMALIGN_HACK
152 //FIXME this isn't aligned correctly, though it probably isn't needed
154 return av_malloc(size);
155 diff = ((char *)ptr)[-1];
156 av_assert0(diff>0 && diff<=ALIGN);
157 ptr = realloc((char *)ptr - diff, size + diff);
159 ptr = (char *)ptr + diff;
161 #elif HAVE_ALIGNED_MALLOC
162 return _aligned_realloc(ptr, size + !size, ALIGN);
164 return realloc(ptr, size + !size);
168 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
173 if (av_size_mult(elsize, nelem, &size)) {
177 r = av_realloc(ptr, size);
183 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
185 if (size <= 0 || nmemb >= INT_MAX / size)
187 return av_realloc(ptr, nmemb * size);
190 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
193 *ptrptr = av_realloc_f(*ptrptr, nmemb, size);
194 if (!*ptrptr && nmemb && size)
195 return AVERROR(ENOMEM);
199 void av_free(void *ptr)
201 #if CONFIG_MEMALIGN_HACK
203 int v= ((char *)ptr)[-1];
204 av_assert0(v>0 && v<=ALIGN);
205 free((char *)ptr - v);
207 #elif HAVE_ALIGNED_MALLOC
214 void av_freep(void *arg)
216 void **ptr = (void **)arg;
221 void *av_mallocz(size_t size)
223 void *ptr = av_malloc(size);
225 memset(ptr, 0, size);
229 void *av_calloc(size_t nmemb, size_t size)
231 if (size <= 0 || nmemb >= INT_MAX / size)
233 return av_mallocz(nmemb * size);
236 char *av_strdup(const char *s)
240 int len = strlen(s) + 1;
241 ptr = av_malloc(len);
248 void *av_memdup(const void *p, size_t size)
252 ptr = av_malloc(size);
254 memcpy(ptr, p, size);
259 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
261 /* see similar ffmpeg.c:grow_array() */
266 tab = *(intptr_t**)tab_ptr;
267 if ((nb & (nb - 1)) == 0) {
271 if (nb > INT_MAX / (2 * sizeof(intptr_t)))
275 tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
278 *(intptr_t**)tab_ptr = tab;
280 tab[nb++] = (intptr_t)elem;
289 void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
290 const uint8_t *elem_data)
292 int nb = *nb_ptr, nb_alloc;
293 uint8_t *tab = *tab_ptr, *tab_elem_data;
295 if ((nb & (nb - 1)) == 0) {
299 if (nb > INT_MAX / (2 * elem_size))
303 tab = av_realloc(tab, nb_alloc * elem_size);
309 tab_elem_data = tab + nb*elem_size;
311 memcpy(tab_elem_data, elem_data, elem_size);
312 else if (CONFIG_MEMORY_POISONING)
313 memset(tab_elem_data, FF_MEMORY_POISON, elem_size);
314 return tab_elem_data;
322 static void fill16(uint8_t *dst, int len)
324 uint32_t v = AV_RN16(dst - 2);
340 static void fill24(uint8_t *dst, int len)
343 uint32_t v = AV_RB24(dst - 3);
344 uint32_t a = v << 8 | v >> 16;
345 uint32_t b = v << 16 | v >> 8;
346 uint32_t c = v << 24 | v;
348 uint32_t v = AV_RL24(dst - 3);
349 uint32_t a = v | v << 24;
350 uint32_t b = v >> 8 | v << 16;
351 uint32_t c = v >> 16 | v << 8;
380 static void fill32(uint8_t *dst, int len)
382 uint32_t v = AV_RN32(dst - 4);
396 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
398 const uint8_t *src = &dst[-back];
403 memset(dst, *src, cnt);
404 } else if (back == 2) {
406 } else if (back == 3) {
408 } else if (back == 4) {
413 while (cnt > blocklen) {
414 memcpy(dst, src, blocklen);
419 memcpy(dst, src, cnt);
423 AV_COPY32U(dst, src);
424 AV_COPY32U(dst + 4, src + 4);
430 AV_COPY32U(dst, src);
436 AV_COPY16U(dst, src);