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_AVX512 ? 64 : (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)
81 if (size > max_alloc_size)
84 #if HAVE_POSIX_MEMALIGN
85 if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
86 if (posix_memalign(&ptr, ALIGN, size))
88 #elif HAVE_ALIGNED_MALLOC
89 ptr = _aligned_malloc(size, ALIGN);
92 ptr = memalign(ALIGN, size);
94 ptr = memalign(size, ALIGN);
97 * Indeed, we should align it:
100 * on 32 for 586, PPro - K6-III
101 * on 64 for K7 (maybe for P3 too).
102 * Because L1 and L2 caches are aligned on those values.
103 * But I don't want to code such logic here!
106 * For AVX ASM. SSE / NEON needs only 16.
107 * Why not larger? Because I did not see a difference in benchmarks ...
109 /* benchmarks with P3
110 * memalign(64) + 1 3071, 3051, 3032
111 * memalign(64) + 2 3051, 3032, 3041
112 * memalign(64) + 4 2911, 2896, 2915
113 * memalign(64) + 8 2545, 2554, 2550
114 * memalign(64) + 16 2543, 2572, 2563
115 * memalign(64) + 32 2546, 2545, 2571
116 * memalign(64) + 64 2570, 2533, 2558
118 * BTW, malloc seems to do 8-byte alignment by default here.
127 #if CONFIG_MEMORY_POISONING
129 memset(ptr, FF_MEMORY_POISON, size);
134 void *av_realloc(void *ptr, size_t size)
137 if (size > max_alloc_size)
140 #if HAVE_ALIGNED_MALLOC
141 ret = _aligned_realloc(ptr, size + !size, ALIGN);
143 ret = realloc(ptr, size + !size);
145 #if CONFIG_MEMORY_POISONING
147 memset(ret, FF_MEMORY_POISON, size);
152 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
157 if (av_size_mult(elsize, nelem, &size)) {
161 r = av_realloc(ptr, size);
167 int av_reallocp(void *ptr, size_t size)
176 memcpy(&val, ptr, sizeof(val));
177 val = av_realloc(val, size);
181 return AVERROR(ENOMEM);
184 memcpy(ptr, &val, sizeof(val));
188 void *av_malloc_array(size_t nmemb, size_t size)
191 if (av_size_mult(nmemb, size, &result) < 0)
193 return av_malloc(result);
196 void *av_mallocz_array(size_t nmemb, size_t size)
199 if (av_size_mult(nmemb, size, &result) < 0)
201 return av_mallocz(result);
204 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
207 if (av_size_mult(nmemb, size, &result) < 0)
209 return av_realloc(ptr, result);
212 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
216 memcpy(&val, ptr, sizeof(val));
217 val = av_realloc_f(val, nmemb, size);
218 memcpy(ptr, &val, sizeof(val));
219 if (!val && nmemb && size)
220 return AVERROR(ENOMEM);
225 void av_free(void *ptr)
227 #if HAVE_ALIGNED_MALLOC
234 void av_freep(void *arg)
238 memcpy(&val, arg, sizeof(val));
239 memcpy(arg, &(void *){ NULL }, sizeof(val));
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)
254 if (av_size_mult(nmemb, size, &result) < 0)
256 return av_mallocz(result);
259 char *av_strdup(const char *s)
263 size_t len = strlen(s) + 1;
264 ptr = av_realloc(NULL, len);
271 char *av_strndup(const char *s, size_t len)
273 char *ret = NULL, *end;
278 end = memchr(s, 0, len);
282 ret = av_realloc(NULL, len + 1);
291 void *av_memdup(const void *p, size_t size)
295 ptr = av_malloc(size);
297 memcpy(ptr, p, size);
302 int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
305 memcpy(&tab, tab_ptr, sizeof(tab));
307 FF_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, {
309 memcpy(tab_ptr, &tab, sizeof(tab));
311 return AVERROR(ENOMEM);
316 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
319 memcpy(&tab, tab_ptr, sizeof(tab));
321 FF_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, {
323 memcpy(tab_ptr, &tab, sizeof(tab));
330 void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
331 const uint8_t *elem_data)
333 uint8_t *tab_elem_data = NULL;
335 FF_DYNARRAY_ADD(INT_MAX, elem_size, *tab_ptr, *nb_ptr, {
336 tab_elem_data = (uint8_t *)*tab_ptr + (*nb_ptr) * elem_size;
338 memcpy(tab_elem_data, elem_data, elem_size);
339 else if (CONFIG_MEMORY_POISONING)
340 memset(tab_elem_data, FF_MEMORY_POISON, elem_size);
345 return tab_elem_data;
348 static void fill16(uint8_t *dst, int len)
350 uint32_t v = AV_RN16(dst - 2);
366 static void fill24(uint8_t *dst, int len)
369 uint32_t v = AV_RB24(dst - 3);
370 uint32_t a = v << 8 | v >> 16;
371 uint32_t b = v << 16 | v >> 8;
372 uint32_t c = v << 24 | v;
374 uint32_t v = AV_RL24(dst - 3);
375 uint32_t a = v | v << 24;
376 uint32_t b = v >> 8 | v << 16;
377 uint32_t c = v >> 16 | v << 8;
406 static void fill32(uint8_t *dst, int len)
408 uint32_t v = AV_RN32(dst - 4);
411 uint64_t v2= v + ((uint64_t)v<<32);
434 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
436 const uint8_t *src = &dst[-back];
441 memset(dst, *src, cnt);
442 } else if (back == 2) {
444 } else if (back == 3) {
446 } else if (back == 4) {
451 while (cnt > blocklen) {
452 memcpy(dst, src, blocklen);
457 memcpy(dst, src, cnt);
461 AV_COPY32U(dst, src);
462 AV_COPY32U(dst + 4, src + 4);
468 AV_COPY32U(dst, src);
474 AV_COPY16U(dst, src);
484 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
486 if (min_size <= *size)
489 if (min_size > max_alloc_size) {
494 min_size = FFMIN(max_alloc_size, FFMAX(min_size + min_size / 16 + 32, min_size));
496 ptr = av_realloc(ptr, min_size);
497 /* we could set this to the unmodified min_size but this is safer
498 * if the user lost the ptr and uses NULL now
508 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
510 ff_fast_malloc(ptr, size, min_size, 0);
513 void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size)
515 ff_fast_malloc(ptr, size, min_size, 1);