2 * default memory allocator for libavutil
3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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
39 #include "intreadwrite.h"
44 #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
45 #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
46 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
47 #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
48 #define free AV_JOIN(MALLOC_PREFIX, free)
50 void *malloc(size_t size);
51 void *memalign(size_t align, size_t size);
52 int posix_memalign(void **ptr, size_t align, size_t size);
53 void *realloc(void *ptr, size_t size);
56 #endif /* MALLOC_PREFIX */
58 /* You can redefine av_malloc and av_free in your project to use your
59 * memory allocator. You do not need to suppress this file because the
60 * linker will do it automatically. */
62 void *av_malloc(size_t size)
65 #if CONFIG_MEMALIGN_HACK
69 /* let's disallow possibly ambiguous cases */
70 if (size > (INT_MAX - 32) || !size)
73 #if CONFIG_MEMALIGN_HACK
74 ptr = malloc(size + 32);
77 diff = ((-(long)ptr - 1) & 31) + 1;
78 ptr = (char *)ptr + diff;
79 ((char *)ptr)[-1] = diff;
80 #elif HAVE_POSIX_MEMALIGN
81 if (posix_memalign(&ptr, 32, size))
83 #elif HAVE_ALIGNED_MALLOC
84 ptr = _aligned_malloc(size, 32);
86 ptr = memalign(32, size);
88 * Indeed, we should align it:
91 * on 32 for 586, PPro - K6-III
92 * on 64 for K7 (maybe for P3 too).
93 * Because L1 and L2 caches are aligned on those values.
94 * But I don't want to code such logic here!
97 * For AVX ASM. SSE / NEON needs only 16.
98 * Why not larger? Because I did not see a difference in benchmarks ...
100 /* benchmarks with P3
101 * memalign(64) + 1 3071, 3051, 3032
102 * memalign(64) + 2 3051, 3032, 3041
103 * memalign(64) + 4 2911, 2896, 2915
104 * memalign(64) + 8 2545, 2554, 2550
105 * memalign(64) + 16 2543, 2572, 2563
106 * memalign(64) + 32 2546, 2545, 2571
107 * memalign(64) + 64 2570, 2533, 2558
109 * BTW, malloc seems to do 8-byte alignment by default here.
117 void *av_realloc(void *ptr, size_t size)
119 #if CONFIG_MEMALIGN_HACK
123 /* let's disallow possibly ambiguous cases */
124 if (size > (INT_MAX - 16))
127 #if CONFIG_MEMALIGN_HACK
128 //FIXME this isn't aligned correctly, though it probably isn't needed
130 return av_malloc(size);
131 diff = ((char *)ptr)[-1];
132 return (char *)realloc((char *)ptr - diff, size + diff) + diff;
133 #elif HAVE_ALIGNED_MALLOC
134 return _aligned_realloc(ptr, size, 32);
136 return realloc(ptr, size);
140 int av_reallocp(void *ptr, size_t size)
149 ret = av_realloc(*ptrptr, size);
153 return AVERROR(ENOMEM);
160 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
162 if (!size || nmemb >= INT_MAX / size)
164 return av_realloc(ptr, nmemb * size);
167 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
171 if (!size || nmemb >= INT_MAX / size)
172 return AVERROR(ENOMEM);
177 ret = av_realloc(*ptrptr, nmemb * size);
180 return AVERROR(ENOMEM);
186 void av_free(void *ptr)
188 #if CONFIG_MEMALIGN_HACK
190 free((char *)ptr - ((char *)ptr)[-1]);
191 #elif HAVE_ALIGNED_MALLOC
198 void av_freep(void *arg)
200 void **ptr = (void **)arg;
205 void *av_mallocz(size_t size)
207 void *ptr = av_malloc(size);
209 memset(ptr, 0, size);
213 char *av_strdup(const char *s)
217 int len = strlen(s) + 1;
218 ptr = av_realloc(NULL, len);
225 char *av_strndup(const char *s, size_t len)
227 char *ret = NULL, *end;
232 end = memchr(s, 0, len);
236 ret = av_realloc(NULL, len + 1);
245 static void fill16(uint8_t *dst, int len)
247 uint32_t v = AV_RN16(dst - 2);
263 static void fill24(uint8_t *dst, int len)
266 uint32_t v = AV_RB24(dst - 3);
267 uint32_t a = v << 8 | v >> 16;
268 uint32_t b = v << 16 | v >> 8;
269 uint32_t c = v << 24 | v;
271 uint32_t v = AV_RL24(dst - 3);
272 uint32_t a = v | v << 24;
273 uint32_t b = v >> 8 | v << 16;
274 uint32_t c = v >> 16 | v << 8;
303 static void fill32(uint8_t *dst, int len)
305 uint32_t v = AV_RN32(dst - 4);
319 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
321 const uint8_t *src = &dst[-back];
326 memset(dst, *src, cnt);
327 } else if (back == 2) {
329 } else if (back == 3) {
331 } else if (back == 4) {
336 while (cnt > blocklen) {
337 memcpy(dst, src, blocklen);
342 memcpy(dst, src, cnt);
346 AV_COPY32U(dst, src);
347 AV_COPY32U(dst + 4, src + 4);
353 AV_COPY32U(dst, src);
359 AV_COPY16U(dst, src);
369 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
371 if (min_size < *size)
374 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
376 ptr = av_realloc(ptr, min_size);
377 /* we could set this to the unmodified min_size but this is safer
378 * if the user lost the ptr and uses NULL now
388 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
391 if (min_size < *size)
393 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
395 *p = av_malloc(min_size);