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
38 #include "intreadwrite.h"
43 #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
44 #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
45 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
46 #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
47 #define free AV_JOIN(MALLOC_PREFIX, free)
49 void *malloc(size_t size);
50 void *memalign(size_t align, size_t size);
51 int posix_memalign(void **ptr, size_t align, size_t size);
52 void *realloc(void *ptr, size_t size);
55 #endif /* MALLOC_PREFIX */
57 /* You can redefine av_malloc and av_free in your project to use your
58 * memory allocator. You do not need to suppress this file because the
59 * linker will do it automatically. */
61 void *av_malloc(size_t size)
64 #if CONFIG_MEMALIGN_HACK
68 /* let's disallow possible ambiguous cases */
69 if (size > (INT_MAX - 32) || !size)
72 #if CONFIG_MEMALIGN_HACK
73 ptr = malloc(size + 32);
76 diff = ((-(long)ptr - 1) & 31) + 1;
77 ptr = (char *)ptr + diff;
78 ((char *)ptr)[-1] = diff;
79 #elif HAVE_POSIX_MEMALIGN
80 if (posix_memalign(&ptr, 32, size))
82 #elif HAVE_ALIGNED_MALLOC
83 ptr = _aligned_malloc(size, 32);
85 ptr = memalign(32, size);
87 * Indeed, we should align it:
90 * on 32 for 586, PPro - K6-III
91 * on 64 for K7 (maybe for P3 too).
92 * Because L1 and L2 caches are aligned on those values.
93 * But I don't want to code such logic here!
96 * For AVX ASM. SSE / NEON needs only 16.
97 * Why not larger? Because I did not see a difference in benchmarks ...
100 * memalign(64) + 1 3071, 3051, 3032
101 * memalign(64) + 2 3051, 3032, 3041
102 * memalign(64) + 4 2911, 2896, 2915
103 * memalign(64) + 8 2545, 2554, 2550
104 * memalign(64) + 16 2543, 2572, 2563
105 * memalign(64) + 32 2546, 2545, 2571
106 * memalign(64) + 64 2570, 2533, 2558
108 * BTW, malloc seems to do 8-byte alignment by default here.
116 void *av_realloc(void *ptr, size_t size)
118 #if CONFIG_MEMALIGN_HACK
122 /* let's disallow possible ambiguous cases */
123 if (size > (INT_MAX - 16))
126 #if CONFIG_MEMALIGN_HACK
127 //FIXME this isn't aligned correctly, though it probably isn't needed
129 return av_malloc(size);
130 diff = ((char *)ptr)[-1];
131 return (char *)realloc((char *)ptr - diff, size + diff) + diff;
132 #elif HAVE_ALIGNED_MALLOC
133 return _aligned_realloc(ptr, size, 32);
135 return realloc(ptr, size);
139 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
141 if (size <= 0 || nmemb >= INT_MAX / size)
143 return av_realloc(ptr, nmemb * size);
146 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
150 if (size <= 0 || nmemb >= INT_MAX / size)
151 return AVERROR(ENOMEM);
156 ret = av_realloc(*ptrptr, nmemb * size);
159 return AVERROR(ENOMEM);
165 void av_free(void *ptr)
167 #if CONFIG_MEMALIGN_HACK
169 free((char *)ptr - ((char *)ptr)[-1]);
170 #elif HAVE_ALIGNED_MALLOC
177 void av_freep(void *arg)
179 void **ptr = (void **)arg;
184 void *av_mallocz(size_t size)
186 void *ptr = av_malloc(size);
188 memset(ptr, 0, size);
192 char *av_strdup(const char *s)
196 int len = strlen(s) + 1;
197 ptr = av_malloc(len);
204 static void fill16(uint8_t *dst, int len)
206 uint32_t v = AV_RN16(dst - 2);
222 static void fill24(uint8_t *dst, int len)
225 uint32_t v = AV_RB24(dst - 3);
226 uint32_t a = v << 8 | v >> 16;
227 uint32_t b = v << 16 | v >> 8;
228 uint32_t c = v << 24 | v;
230 uint32_t v = AV_RL24(dst - 3);
231 uint32_t a = v | v << 24;
232 uint32_t b = v >> 8 | v << 16;
233 uint32_t c = v >> 16 | v << 8;
262 static void fill32(uint8_t *dst, int len)
264 uint32_t v = AV_RN32(dst - 4);
278 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
280 const uint8_t *src = &dst[-back];
285 memset(dst, *src, cnt);
286 } else if (back == 2) {
288 } else if (back == 3) {
290 } else if (back == 4) {
295 while (cnt > blocklen) {
296 memcpy(dst, src, blocklen);
301 memcpy(dst, src, cnt);
305 AV_COPY32U(dst, src);
306 AV_COPY32U(dst + 4, src + 4);
312 AV_COPY32U(dst, src);
318 AV_COPY16U(dst, src);