aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavutil / mem.c
blobfd0ffd988c925da59d64a2a356c5ea0006ed212e
1 /*
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
22 /**
23 * @file
24 * default memory allocator for libavutil
27 #include "config.h"
29 #include <limits.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #if HAVE_MALLOC_H
34 #include <malloc.h>
35 #endif
37 #include "avutil.h"
38 #include "common.h"
39 #include "intreadwrite.h"
40 #include "mem.h"
42 #ifdef MALLOC_PREFIX
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);
54 void free(void *ptr);
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)
64 void *ptr = NULL;
66 /* let's disallow possibly ambiguous cases */
67 if (size > (INT_MAX - 32) || !size)
68 return NULL;
70 #if HAVE_POSIX_MEMALIGN
71 if (posix_memalign(&ptr, 32, size))
72 ptr = NULL;
73 #elif HAVE_ALIGNED_MALLOC
74 ptr = _aligned_malloc(size, 32);
75 #elif HAVE_MEMALIGN
76 ptr = memalign(32, size);
77 /* Why 64?
78 * Indeed, we should align it:
79 * on 4 for 386
80 * on 16 for 486
81 * on 32 for 586, PPro - K6-III
82 * on 64 for K7 (maybe for P3 too).
83 * Because L1 and L2 caches are aligned on those values.
84 * But I don't want to code such logic here!
86 /* Why 32?
87 * For AVX ASM. SSE / NEON needs only 16.
88 * Why not larger? Because I did not see a difference in benchmarks ...
90 /* benchmarks with P3
91 * memalign(64) + 1 3071, 3051, 3032
92 * memalign(64) + 2 3051, 3032, 3041
93 * memalign(64) + 4 2911, 2896, 2915
94 * memalign(64) + 8 2545, 2554, 2550
95 * memalign(64) + 16 2543, 2572, 2563
96 * memalign(64) + 32 2546, 2545, 2571
97 * memalign(64) + 64 2570, 2533, 2558
99 * BTW, malloc seems to do 8-byte alignment by default here.
101 #else
102 ptr = malloc(size);
103 #endif
104 return ptr;
107 void *av_realloc(void *ptr, size_t size)
109 /* let's disallow possibly ambiguous cases */
110 if (size > (INT_MAX - 16))
111 return NULL;
113 #if HAVE_ALIGNED_MALLOC
114 return _aligned_realloc(ptr, size, 32);
115 #else
116 return realloc(ptr, size);
117 #endif
120 int av_reallocp(void *ptr, size_t size)
122 void *val;
124 if (!size) {
125 av_freep(ptr);
126 return 0;
129 memcpy(&val, ptr, sizeof(val));
130 val = av_realloc(val, size);
132 if (!val) {
133 av_freep(ptr);
134 return AVERROR(ENOMEM);
137 memcpy(ptr, &val, sizeof(val));
138 return 0;
141 void *av_malloc_array(size_t nmemb, size_t size)
143 if (!size || nmemb >= INT_MAX / size)
144 return NULL;
145 return av_malloc(nmemb * size);
148 void *av_mallocz_array(size_t nmemb, size_t size)
150 if (!size || nmemb >= INT_MAX / size)
151 return NULL;
152 return av_mallocz(nmemb * size);
155 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
157 if (!size || nmemb >= INT_MAX / size)
158 return NULL;
159 return av_realloc(ptr, nmemb * size);
162 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
164 void *val;
166 if (!size || nmemb >= INT_MAX / size)
167 return AVERROR(ENOMEM);
168 if (!nmemb) {
169 av_freep(ptr);
170 return 0;
173 memcpy(&val, ptr, sizeof(val));
174 val = av_realloc(val, nmemb * size);
175 if (!val) {
176 av_freep(ptr);
177 return AVERROR(ENOMEM);
180 memcpy(ptr, &val, sizeof(val));
181 return 0;
184 void av_free(void *ptr)
186 #if HAVE_ALIGNED_MALLOC
187 _aligned_free(ptr);
188 #else
189 free(ptr);
190 #endif
193 void av_freep(void *arg)
195 void *val;
197 memcpy(&val, arg, sizeof(val));
198 memcpy(arg, &(void *){ NULL }, sizeof(val));
199 av_free(val);
202 void *av_mallocz(size_t size)
204 void *ptr = av_malloc(size);
205 if (ptr)
206 memset(ptr, 0, size);
207 return ptr;
210 char *av_strdup(const char *s)
212 char *ptr = NULL;
213 if (s) {
214 int len = strlen(s) + 1;
215 ptr = av_realloc(NULL, len);
216 if (ptr)
217 memcpy(ptr, s, len);
219 return ptr;
222 char *av_strndup(const char *s, size_t len)
224 char *ret = NULL, *end;
226 if (!s)
227 return NULL;
229 end = memchr(s, 0, len);
230 if (end)
231 len = end - s;
233 ret = av_realloc(NULL, len + 1);
234 if (!ret)
235 return NULL;
237 memcpy(ret, s, len);
238 ret[len] = 0;
239 return ret;
242 static void fill16(uint8_t *dst, int len)
244 uint32_t v = AV_RN16(dst - 2);
246 v |= v << 16;
248 while (len >= 4) {
249 AV_WN32(dst, v);
250 dst += 4;
251 len -= 4;
254 while (len--) {
255 *dst = dst[-2];
256 dst++;
260 static void fill24(uint8_t *dst, int len)
262 #if HAVE_BIGENDIAN
263 uint32_t v = AV_RB24(dst - 3);
264 uint32_t a = v << 8 | v >> 16;
265 uint32_t b = v << 16 | v >> 8;
266 uint32_t c = v << 24 | v;
267 #else
268 uint32_t v = AV_RL24(dst - 3);
269 uint32_t a = v | v << 24;
270 uint32_t b = v >> 8 | v << 16;
271 uint32_t c = v >> 16 | v << 8;
272 #endif
274 while (len >= 12) {
275 AV_WN32(dst, a);
276 AV_WN32(dst + 4, b);
277 AV_WN32(dst + 8, c);
278 dst += 12;
279 len -= 12;
282 if (len >= 4) {
283 AV_WN32(dst, a);
284 dst += 4;
285 len -= 4;
288 if (len >= 4) {
289 AV_WN32(dst, b);
290 dst += 4;
291 len -= 4;
294 while (len--) {
295 *dst = dst[-3];
296 dst++;
300 static void fill32(uint8_t *dst, int len)
302 uint32_t v = AV_RN32(dst - 4);
304 while (len >= 4) {
305 AV_WN32(dst, v);
306 dst += 4;
307 len -= 4;
310 while (len--) {
311 *dst = dst[-4];
312 dst++;
316 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
318 const uint8_t *src = &dst[-back];
319 if (!back)
320 return;
322 if (back == 1) {
323 memset(dst, *src, cnt);
324 } else if (back == 2) {
325 fill16(dst, cnt);
326 } else if (back == 3) {
327 fill24(dst, cnt);
328 } else if (back == 4) {
329 fill32(dst, cnt);
330 } else {
331 if (cnt >= 16) {
332 int blocklen = back;
333 while (cnt > blocklen) {
334 memcpy(dst, src, blocklen);
335 dst += blocklen;
336 cnt -= blocklen;
337 blocklen <<= 1;
339 memcpy(dst, src, cnt);
340 return;
342 if (cnt >= 8) {
343 AV_COPY32U(dst, src);
344 AV_COPY32U(dst + 4, src + 4);
345 src += 8;
346 dst += 8;
347 cnt -= 8;
349 if (cnt >= 4) {
350 AV_COPY32U(dst, src);
351 src += 4;
352 dst += 4;
353 cnt -= 4;
355 if (cnt >= 2) {
356 AV_COPY16U(dst, src);
357 src += 2;
358 dst += 2;
359 cnt -= 2;
361 if (cnt)
362 *dst = *src;
366 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
368 if (min_size < *size)
369 return ptr;
371 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
373 ptr = av_realloc(ptr, min_size);
374 /* we could set this to the unmodified min_size but this is safer
375 * if the user lost the ptr and uses NULL now
377 if (!ptr)
378 min_size = 0;
380 *size = min_size;
382 return ptr;
385 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
387 void **p = ptr;
388 if (min_size < *size)
389 return;
390 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
391 av_free(*p);
392 *p = av_malloc(min_size);
393 if (!*p)
394 min_size = 0;
395 *size = min_size;