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