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
40 #include "intreadwrite.h"
45 #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
46 #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
47 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
48 #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
49 #define free AV_JOIN(MALLOC_PREFIX, free)
51 void *malloc(size_t size);
52 void *memalign(size_t align, size_t size);
53 int posix_memalign(void **ptr, size_t align, size_t size);
54 void *realloc(void *ptr, size_t size);
57 #endif /* MALLOC_PREFIX */
59 #define ALIGN (HAVE_AVX ? 32 : 16)
61 /* NOTE: if you want to override these functions with your own
62 * implementations (not recommended) you have to link libav* as
63 * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
64 * Note that this will cost performance. */
66 static size_t max_alloc_size= INT_MAX;
68 void av_max_alloc(size_t max){
72 void *av_malloc(size_t size)
75 #if CONFIG_MEMALIGN_HACK
79 /* let's disallow possible ambiguous cases */
80 if (size > (max_alloc_size - 32))
83 #if CONFIG_MEMALIGN_HACK
84 ptr = malloc(size + ALIGN);
87 diff = ((~(long)ptr)&(ALIGN - 1)) + 1;
88 ptr = (char *)ptr + diff;
89 ((char *)ptr)[-1] = diff;
90 #elif HAVE_POSIX_MEMALIGN
91 if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
92 if (posix_memalign(&ptr, ALIGN, size))
94 #elif HAVE_ALIGNED_MALLOC
95 ptr = _aligned_malloc(size, ALIGN);
97 ptr = memalign(ALIGN, size);
99 * Indeed, we should align it:
102 * on 32 for 586, PPro - K6-III
103 * on 64 for K7 (maybe for P3 too).
104 * Because L1 and L2 caches are aligned on those values.
105 * But I don't want to code such logic here!
108 * For AVX ASM. SSE / NEON needs only 16.
109 * Why not larger? Because I did not see a difference in benchmarks ...
111 /* benchmarks with P3
112 * memalign(64) + 1 3071, 3051, 3032
113 * memalign(64) + 2 3051, 3032, 3041
114 * memalign(64) + 4 2911, 2896, 2915
115 * memalign(64) + 8 2545, 2554, 2550
116 * memalign(64) + 16 2543, 2572, 2563
117 * memalign(64) + 32 2546, 2545, 2571
118 * memalign(64) + 64 2570, 2533, 2558
120 * BTW, malloc seems to do 8-byte alignment by default here.
129 #if CONFIG_MEMORY_POISONING
131 memset(ptr, 0x2a, size);
136 void *av_realloc(void *ptr, size_t size)
138 #if CONFIG_MEMALIGN_HACK
142 /* let's disallow possible ambiguous cases */
143 if (size > (max_alloc_size - 32))
146 #if CONFIG_MEMALIGN_HACK
147 //FIXME this isn't aligned correctly, though it probably isn't needed
149 return av_malloc(size);
150 diff = ((char *)ptr)[-1];
151 ptr = realloc((char *)ptr - diff, size + diff);
153 ptr = (char *)ptr + diff;
155 #elif HAVE_ALIGNED_MALLOC
156 return _aligned_realloc(ptr, size + !size, ALIGN);
158 return realloc(ptr, size + !size);
162 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
167 if (av_size_mult(elsize, nelem, &size)) {
171 r = av_realloc(ptr, size);
177 void av_free(void *ptr)
179 #if CONFIG_MEMALIGN_HACK
181 free((char *)ptr - ((char *)ptr)[-1]);
182 #elif HAVE_ALIGNED_MALLOC
189 void av_freep(void *arg)
191 void **ptr = (void **)arg;
196 void *av_mallocz(size_t size)
198 void *ptr = av_malloc(size);
200 memset(ptr, 0, size);
204 void *av_calloc(size_t nmemb, size_t size)
206 if (size <= 0 || nmemb >= INT_MAX / size)
208 return av_mallocz(nmemb * size);
211 char *av_strdup(const char *s)
215 int len = strlen(s) + 1;
216 ptr = av_malloc(len);
223 /* add one element to a dynamic array */
224 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
226 /* see similar ffmpeg.c:grow_array() */
231 tab = *(intptr_t**)tab_ptr;
232 if ((nb & (nb - 1)) == 0) {
237 tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
238 *(intptr_t**)tab_ptr = tab;
240 tab[nb++] = (intptr_t)elem;
244 static void fill16(uint8_t *dst, int len)
246 uint32_t v = AV_RN16(dst - 2);
262 static void fill24(uint8_t *dst, int len)
265 uint32_t v = AV_RB24(dst - 3);
266 uint32_t a = v << 8 | v >> 16;
267 uint32_t b = v << 16 | v >> 8;
268 uint32_t c = v << 24 | v;
270 uint32_t v = AV_RL24(dst - 3);
271 uint32_t a = v | v << 24;
272 uint32_t b = v >> 8 | v << 16;
273 uint32_t c = v >> 16 | v << 8;
302 static void fill32(uint8_t *dst, int len)
304 uint32_t v = AV_RN32(dst - 4);
318 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
320 const uint8_t *src = &dst[-back];
322 memset(dst, *src, cnt);
323 } else if (back == 2) {
325 } else if (back == 3) {
327 } else if (back == 4) {
332 while (cnt > blocklen) {
333 memcpy(dst, src, blocklen);
338 memcpy(dst, src, cnt);
342 AV_COPY32U(dst, src);
343 AV_COPY32U(dst + 4, src + 4);
349 AV_COPY32U(dst, src);
355 AV_COPY16U(dst, src);