1 /*
2 * default memory allocator for libavutil
3 * Copyright (c) 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * default memory allocator for libavutil
25 */
26
27 #define _XOPEN_SOURCE 600
28
29 #include "config.h"
30
31 #include <limits.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "avassert.h"
37 #include "avutil.h"
38 #include "common.h"
39 #include "dynarray.h"
40 #include "intreadwrite.h"
41 #include "mem.h"
42
43 #ifdef MALLOC_PREFIX
44
45 #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
46 #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
47 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
48 #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
49 #define free AV_JOIN(MALLOC_PREFIX, free)
50
51 void *malloc(size_t size);
52 void *memalign(size_t align, size_t size);
53 int posix_memalign(void **ptr, size_t align, size_t size);
54 void *realloc(void *ptr, size_t size);
55 void free(void *ptr);
56
57 #endif /* MALLOC_PREFIX */
58
59 #include "mem_internal.h"
60
61 #define ALIGN (HAVE_AVX512 ? 64 : (HAVE_AVX ? 32 : 16))
62
63 /* NOTE: if you want to override these functions with your own
64 * implementations (not recommended) you have to link libav* as
65 * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
66 * Note that this will cost performance. */
67
68 static size_t max_alloc_size= INT_MAX;
69
70 /* Set to 0 to remove the maximum allocation size. */
av_max_alloc(size_t max)71 void av_max_alloc(size_t max){
72 max_alloc_size = max;
73 }
74
av_malloc(size_t size)75 void *av_malloc(size_t size)
76 {
77 void *ptr = NULL;
78
79 /* let's disallow possibly ambiguous cases */
80 if (max_alloc_size && size > (max_alloc_size - 32))
81 return NULL;
82
83 #if HAVE_POSIX_MEMALIGN
84 if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
85 if (posix_memalign(&ptr, ALIGN, size))
86 ptr = NULL;
87 #elif HAVE_ALIGNED_MALLOC
88 ptr = _aligned_malloc(size, ALIGN);
89 #elif HAVE_MEMALIGN
90 #ifndef __DJGPP__
91 ptr = memalign(ALIGN, size);
92 #else
93 ptr = memalign(size, ALIGN);
94 #endif
95 /* Why 64?
96 * Indeed, we should align it:
97 * on 4 for 386
98 * on 16 for 486
99 * on 32 for 586, PPro - K6-III
100 * on 64 for K7 (maybe for P3 too).
101 * Because L1 and L2 caches are aligned on those values.
102 * But I don't want to code such logic here!
103 */
104 /* Why 32?
105 * For AVX ASM. SSE / NEON needs only 16.
106 * Why not larger? Because I did not see a difference in benchmarks ...
107 */
108 /* benchmarks with P3
109 * memalign(64) + 1 3071, 3051, 3032
110 * memalign(64) + 2 3051, 3032, 3041
111 * memalign(64) + 4 2911, 2896, 2915
112 * memalign(64) + 8 2545, 2554, 2550
113 * memalign(64) + 16 2543, 2572, 2563
114 * memalign(64) + 32 2546, 2545, 2571
115 * memalign(64) + 64 2570, 2533, 2558
116 *
117 * BTW, malloc seems to do 8-byte alignment by default here.
118 */
119 #else
120 ptr = malloc(size);
121 #endif
122 if(!ptr && !size) {
123 size = 1;
124 ptr= av_malloc(1);
125 }
126 #if CONFIG_MEMORY_POISONING
127 if (ptr)
128 memset(ptr, FF_MEMORY_POISON, size);
129 #endif
130 return ptr;
131 }
132
av_realloc(void * ptr,size_t size)133 void *av_realloc(void *ptr, size_t size)
134 {
135 /* let's disallow possibly ambiguous cases */
136 if (max_alloc_size && size > (max_alloc_size - 32))
137 return NULL;
138
139 #if HAVE_ALIGNED_MALLOC
140 return _aligned_realloc(ptr, size + !size, ALIGN);
141 #else
142 return realloc(ptr, size + !size);
143 #endif
144 }
145
av_realloc_f(void * ptr,size_t nelem,size_t elsize)146 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
147 {
148 size_t size;
149 void *r;
150
151 if (av_size_mult(elsize, nelem, &size)) {
152 av_free(ptr);
153 return NULL;
154 }
155 r = av_realloc(ptr, size);
156 if (!r)
157 av_free(ptr);
158 return r;
159 }
160
av_reallocp(void * ptr,size_t size)161 int av_reallocp(void *ptr, size_t size)
162 {
163 void *val;
164
165 if (!size) {
166 av_freep(ptr);
167 return 0;
168 }
169
170 memcpy(&val, ptr, sizeof(val));
171 val = av_realloc(val, size);
172
173 if (!val) {
174 av_freep(ptr);
175 return AVERROR(ENOMEM);
176 }
177
178 memcpy(ptr, &val, sizeof(val));
179 return 0;
180 }
181
av_malloc_array(size_t nmemb,size_t size)182 void *av_malloc_array(size_t nmemb, size_t size)
183 {
184 if (!size || nmemb >= INT_MAX / size)
185 return NULL;
186 return av_malloc(nmemb * size);
187 }
188
av_mallocz_array(size_t nmemb,size_t size)189 void *av_mallocz_array(size_t nmemb, size_t size)
190 {
191 if (!size || nmemb >= INT_MAX / size)
192 return NULL;
193 return av_mallocz(nmemb * size);
194 }
195
av_realloc_array(void * ptr,size_t nmemb,size_t size)196 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
197 {
198 if (!size || nmemb >= INT_MAX / size)
199 return NULL;
200 return av_realloc(ptr, nmemb * size);
201 }
202
av_reallocp_array(void * ptr,size_t nmemb,size_t size)203 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
204 {
205 void *val;
206
207 memcpy(&val, ptr, sizeof(val));
208 val = av_realloc_f(val, nmemb, size);
209 memcpy(ptr, &val, sizeof(val));
210 if (!val && nmemb && size)
211 return AVERROR(ENOMEM);
212
213 return 0;
214 }
215
av_free(void * ptr)216 void av_free(void *ptr)
217 {
218 #if HAVE_ALIGNED_MALLOC
219 _aligned_free(ptr);
220 #else
221 free(ptr);
222 #endif
223 }
224
av_freep(void * arg)225 void av_freep(void *arg)
226 {
227 void *val;
228
229 memcpy(&val, arg, sizeof(val));
230 memcpy(arg, &(void *){ NULL }, sizeof(val));
231 av_free(val);
232 }
233
av_mallocz(size_t size)234 void *av_mallocz(size_t size)
235 {
236 void *ptr = av_malloc(size);
237 if (ptr)
238 memset(ptr, 0, size);
239 return ptr;
240 }
241
av_calloc(size_t nmemb,size_t size)242 void *av_calloc(size_t nmemb, size_t size)
243 {
244 if (size <= 0 || nmemb >= INT_MAX / size)
245 return NULL;
246 return av_mallocz(nmemb * size);
247 }
248
av_strdup(const char * s)249 char *av_strdup(const char *s)
250 {
251 char *ptr = NULL;
252 if (s) {
253 size_t len = strlen(s) + 1;
254 ptr = av_realloc(NULL, len);
255 if (ptr)
256 memcpy(ptr, s, len);
257 }
258 return ptr;
259 }
260
av_strndup(const char * s,size_t len)261 char *av_strndup(const char *s, size_t len)
262 {
263 char *ret = NULL, *end;
264
265 if (!s)
266 return NULL;
267
268 end = memchr(s, 0, len);
269 if (end)
270 len = end - s;
271
272 ret = av_realloc(NULL, len + 1);
273 if (!ret)
274 return NULL;
275
276 memcpy(ret, s, len);
277 ret[len] = 0;
278 return ret;
279 }
280
av_memdup(const void * p,size_t size)281 void *av_memdup(const void *p, size_t size)
282 {
283 void *ptr = NULL;
284 if (p) {
285 ptr = av_malloc(size);
286 if (ptr)
287 memcpy(ptr, p, size);
288 }
289 return ptr;
290 }
291
av_dynarray_add_nofree(void * tab_ptr,int * nb_ptr,void * elem)292 int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
293 {
294 void **tab;
295 memcpy(&tab, tab_ptr, sizeof(tab));
296
297 FF_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, {
298 tab[*nb_ptr] = elem;
299 memcpy(tab_ptr, &tab, sizeof(tab));
300 }, {
301 return AVERROR(ENOMEM);
302 });
303 return 0;
304 }
305
av_dynarray_add(void * tab_ptr,int * nb_ptr,void * elem)306 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
307 {
308 void **tab;
309 memcpy(&tab, tab_ptr, sizeof(tab));
310
311 FF_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, {
312 tab[*nb_ptr] = elem;
313 memcpy(tab_ptr, &tab, sizeof(tab));
314 }, {
315 *nb_ptr = 0;
316 av_freep(tab_ptr);
317 });
318 }
319
av_dynarray2_add(void ** tab_ptr,int * nb_ptr,size_t elem_size,const uint8_t * elem_data)320 void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
321 const uint8_t *elem_data)
322 {
323 uint8_t *tab_elem_data = NULL;
324
325 FF_DYNARRAY_ADD(INT_MAX, elem_size, *tab_ptr, *nb_ptr, {
326 tab_elem_data = (uint8_t *)*tab_ptr + (*nb_ptr) * elem_size;
327 if (elem_data)
328 memcpy(tab_elem_data, elem_data, elem_size);
329 else if (CONFIG_MEMORY_POISONING)
330 memset(tab_elem_data, FF_MEMORY_POISON, elem_size);
331 }, {
332 av_freep(tab_ptr);
333 *nb_ptr = 0;
334 });
335 return tab_elem_data;
336 }
337
fill16(uint8_t * dst,int len)338 static void fill16(uint8_t *dst, int len)
339 {
340 uint32_t v = AV_RN16(dst - 2);
341
342 v |= v << 16;
343
344 while (len >= 4) {
345 AV_WN32(dst, v);
346 dst += 4;
347 len -= 4;
348 }
349
350 while (len--) {
351 *dst = dst[-2];
352 dst++;
353 }
354 }
355
fill24(uint8_t * dst,int len)356 static void fill24(uint8_t *dst, int len)
357 {
358 #if HAVE_BIGENDIAN
359 uint32_t v = AV_RB24(dst - 3);
360 uint32_t a = v << 8 | v >> 16;
361 uint32_t b = v << 16 | v >> 8;
362 uint32_t c = v << 24 | v;
363 #else
364 uint32_t v = AV_RL24(dst - 3);
365 uint32_t a = v | v << 24;
366 uint32_t b = v >> 8 | v << 16;
367 uint32_t c = v >> 16 | v << 8;
368 #endif
369
370 while (len >= 12) {
371 AV_WN32(dst, a);
372 AV_WN32(dst + 4, b);
373 AV_WN32(dst + 8, c);
374 dst += 12;
375 len -= 12;
376 }
377
378 if (len >= 4) {
379 AV_WN32(dst, a);
380 dst += 4;
381 len -= 4;
382 }
383
384 if (len >= 4) {
385 AV_WN32(dst, b);
386 dst += 4;
387 len -= 4;
388 }
389
390 while (len--) {
391 *dst = dst[-3];
392 dst++;
393 }
394 }
395
fill32(uint8_t * dst,int len)396 static void fill32(uint8_t *dst, int len)
397 {
398 uint32_t v = AV_RN32(dst - 4);
399
400 #if HAVE_FAST_64BIT
401 uint64_t v2= v + ((uint64_t)v<<32);
402 while (len >= 32) {
403 AV_WN64(dst , v2);
404 AV_WN64(dst+ 8, v2);
405 AV_WN64(dst+16, v2);
406 AV_WN64(dst+24, v2);
407 dst += 32;
408 len -= 32;
409 }
410 #endif
411
412 while (len >= 4) {
413 AV_WN32(dst, v);
414 dst += 4;
415 len -= 4;
416 }
417
418 while (len--) {
419 *dst = dst[-4];
420 dst++;
421 }
422 }
423
av_memcpy_backptr(uint8_t * dst,int back,int cnt)424 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
425 {
426 const uint8_t *src = &dst[-back];
427 if (!back)
428 return;
429
430 if (back == 1) {
431 memset(dst, *src, cnt);
432 } else if (back == 2) {
433 fill16(dst, cnt);
434 } else if (back == 3) {
435 fill24(dst, cnt);
436 } else if (back == 4) {
437 fill32(dst, cnt);
438 } else {
439 if (cnt >= 16) {
440 int blocklen = back;
441 while (cnt > blocklen) {
442 memcpy(dst, src, blocklen);
443 dst += blocklen;
444 cnt -= blocklen;
445 blocklen <<= 1;
446 }
447 memcpy(dst, src, cnt);
448 return;
449 }
450 if (cnt >= 8) {
451 AV_COPY32U(dst, src);
452 AV_COPY32U(dst + 4, src + 4);
453 src += 8;
454 dst += 8;
455 cnt -= 8;
456 }
457 if (cnt >= 4) {
458 AV_COPY32U(dst, src);
459 src += 4;
460 dst += 4;
461 cnt -= 4;
462 }
463 if (cnt >= 2) {
464 AV_COPY16U(dst, src);
465 src += 2;
466 dst += 2;
467 cnt -= 2;
468 }
469 if (cnt)
470 *dst = *src;
471 }
472 }
473
av_fast_realloc(void * ptr,unsigned int * size,size_t min_size)474 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
475 {
476 if (min_size <= *size)
477 return ptr;
478
479 if (min_size > max_alloc_size - 32) {
480 *size = 0;
481 return NULL;
482 }
483
484 min_size = FFMIN(max_alloc_size - 32, FFMAX(min_size + min_size / 16 + 32, min_size));
485
486 ptr = av_realloc(ptr, min_size);
487 /* we could set this to the unmodified min_size but this is safer
488 * if the user lost the ptr and uses NULL now
489 */
490 if (!ptr)
491 min_size = 0;
492
493 *size = min_size;
494
495 return ptr;
496 }
497
av_fast_malloc(void * ptr,unsigned int * size,size_t min_size)498 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
499 {
500 ff_fast_malloc(ptr, size, min_size, 0);
501 }
502
av_fast_mallocz(void * ptr,unsigned int * size,size_t min_size)503 void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size)
504 {
505 ff_fast_malloc(ptr, size, min_size, 1);
506 }
507