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 #include "mem_internal.h"
64 #define ALIGN (HAVE_AVX ? 32 : 16)
66 /* NOTE: if you want to override these functions with your own
67 * implementations (not recommended) you have to link libav* as
68 * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
69 * Note that this will cost performance. */
71 static size_t max_alloc_size= INT_MAX;
73 void av_max_alloc(size_t max){
77 void *av_malloc(size_t size)
80 #if CONFIG_MEMALIGN_HACK
84 /* let's disallow possibly ambiguous cases */
85 if (size > (max_alloc_size - 32))
88 #if CONFIG_MEMALIGN_HACK
89 ptr = malloc(size + ALIGN);
92 diff = ((~(long)ptr)&(ALIGN - 1)) + 1;
93 ptr = (char *)ptr + diff;
94 ((char *)ptr)[-1] = diff;
95 #elif HAVE_POSIX_MEMALIGN
96 if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
97 if (posix_memalign(&ptr, ALIGN, size))
99 #elif HAVE_ALIGNED_MALLOC
100 ptr = _aligned_malloc(size, ALIGN);
103 ptr = memalign(ALIGN, size);
105 ptr = memalign(size, ALIGN);
108 * Indeed, we should align it:
111 * on 32 for 586, PPro - K6-III
112 * on 64 for K7 (maybe for P3 too).
113 * Because L1 and L2 caches are aligned on those values.
114 * But I don't want to code such logic here!
117 * For AVX ASM. SSE / NEON needs only 16.
118 * Why not larger? Because I did not see a difference in benchmarks ...
120 /* benchmarks with P3
121 * memalign(64) + 1 3071, 3051, 3032
122 * memalign(64) + 2 3051, 3032, 3041
123 * memalign(64) + 4 2911, 2896, 2915
124 * memalign(64) + 8 2545, 2554, 2550
125 * memalign(64) + 16 2543, 2572, 2563
126 * memalign(64) + 32 2546, 2545, 2571
127 * memalign(64) + 64 2570, 2533, 2558
129 * BTW, malloc seems to do 8-byte alignment by default here.
138 #if CONFIG_MEMORY_POISONING
140 memset(ptr, FF_MEMORY_POISON, size);
145 void *av_realloc(void *ptr, size_t size)
147 #if CONFIG_MEMALIGN_HACK
151 /* let's disallow possibly ambiguous cases */
152 if (size > (max_alloc_size - 32))
155 #if CONFIG_MEMALIGN_HACK
156 //FIXME this isn't aligned correctly, though it probably isn't needed
158 return av_malloc(size);
159 diff = ((char *)ptr)[-1];
160 av_assert0(diff>0 && diff<=ALIGN);
161 ptr = realloc((char *)ptr - diff, size + diff);
163 ptr = (char *)ptr + diff;
165 #elif HAVE_ALIGNED_MALLOC
166 return _aligned_realloc(ptr, size + !size, ALIGN);
168 return realloc(ptr, size + !size);
172 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
177 if (av_size_mult(elsize, nelem, &size)) {
181 r = av_realloc(ptr, size);
187 int av_reallocp(void *ptr, size_t size)
196 memcpy(&val, ptr, sizeof(val));
197 val = av_realloc(val, size);
201 return AVERROR(ENOMEM);
204 memcpy(ptr, &val, sizeof(val));
208 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
210 if (!size || nmemb >= INT_MAX / size)
212 return av_realloc(ptr, nmemb * size);
215 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
219 memcpy(&val, ptr, sizeof(val));
220 val = av_realloc_f(val, nmemb, size);
221 memcpy(ptr, &val, sizeof(val));
222 if (!val && nmemb && size)
223 return AVERROR(ENOMEM);
228 void av_free(void *ptr)
230 #if CONFIG_MEMALIGN_HACK
232 int v= ((char *)ptr)[-1];
233 av_assert0(v>0 && v<=ALIGN);
234 free((char *)ptr - v);
236 #elif HAVE_ALIGNED_MALLOC
243 void av_freep(void *arg)
247 memcpy(&val, arg, sizeof(val));
248 memcpy(arg, &(void *){ NULL }, sizeof(val));
252 void *av_mallocz(size_t size)
254 void *ptr = av_malloc(size);
256 memset(ptr, 0, size);
260 void *av_calloc(size_t nmemb, size_t size)
262 if (size <= 0 || nmemb >= INT_MAX / size)
264 return av_mallocz(nmemb * size);
267 char *av_strdup(const char *s)
271 size_t len = strlen(s) + 1;
272 ptr = av_realloc(NULL, len);
279 char *av_strndup(const char *s, size_t len)
281 char *ret = NULL, *end;
286 end = memchr(s, 0, len);
290 ret = av_realloc(NULL, len + 1);
299 void *av_memdup(const void *p, size_t size)
303 ptr = av_malloc(size);
305 memcpy(ptr, p, size);
310 int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
313 memcpy(&tab, tab_ptr, sizeof(tab));
315 AV_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, {
317 memcpy(tab_ptr, &tab, sizeof(tab));
319 return AVERROR(ENOMEM);
324 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
327 memcpy(&tab, tab_ptr, sizeof(tab));
329 AV_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, {
331 memcpy(tab_ptr, &tab, sizeof(tab));
338 void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
339 const uint8_t *elem_data)
341 uint8_t *tab_elem_data = NULL;
343 AV_DYNARRAY_ADD(INT_MAX, elem_size, *tab_ptr, *nb_ptr, {
344 tab_elem_data = (uint8_t *)*tab_ptr + (*nb_ptr) * elem_size;
346 memcpy(tab_elem_data, elem_data, elem_size);
347 else if (CONFIG_MEMORY_POISONING)
348 memset(tab_elem_data, FF_MEMORY_POISON, elem_size);
353 return tab_elem_data;
356 static void fill16(uint8_t *dst, int len)
358 uint32_t v = AV_RN16(dst - 2);
374 static void fill24(uint8_t *dst, int len)
377 uint32_t v = AV_RB24(dst - 3);
378 uint32_t a = v << 8 | v >> 16;
379 uint32_t b = v << 16 | v >> 8;
380 uint32_t c = v << 24 | v;
382 uint32_t v = AV_RL24(dst - 3);
383 uint32_t a = v | v << 24;
384 uint32_t b = v >> 8 | v << 16;
385 uint32_t c = v >> 16 | v << 8;
414 static void fill32(uint8_t *dst, int len)
416 uint32_t v = AV_RN32(dst - 4);
430 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
432 const uint8_t *src = &dst[-back];
437 memset(dst, *src, cnt);
438 } else if (back == 2) {
440 } else if (back == 3) {
442 } else if (back == 4) {
447 while (cnt > blocklen) {
448 memcpy(dst, src, blocklen);
453 memcpy(dst, src, cnt);
457 AV_COPY32U(dst, src);
458 AV_COPY32U(dst + 4, src + 4);
464 AV_COPY32U(dst, src);
470 AV_COPY16U(dst, src);
480 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
482 if (min_size < *size)
485 min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
487 ptr = av_realloc(ptr, min_size);
488 /* we could set this to the unmodified min_size but this is safer
489 * if the user lost the ptr and uses NULL now
499 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
501 ff_fast_malloc(ptr, size, min_size, 0);
504 void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size)
506 ff_fast_malloc(ptr, size, min_size, 1);