libavutil/mem.c
d01fe86d
 /*
cea8f6f3
  * default memory allocator for libavutil
406792e7
  * Copyright (c) 2002 Fabrice Bellard
d01fe86d
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
d01fe86d
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
d01fe86d
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
d01fe86d
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d01fe86d
  */
115329f1
 
983e3246
 /**
ba87f080
  * @file
89c9ff50
  * default memory allocator for libavutil
983e3246
  */
115329f1
 
518cdff8
 #define _XOPEN_SOURCE 600
 
dfcb6b56
 #include "config.h"
8e1e6f31
 
dfcb6b56
 #include <limits.h>
1f91cdce
 #include <stdlib.h>
dfcb6b56
 #include <string.h>
b250f9c6
 #if HAVE_MALLOC_H
d01fe86d
 #include <malloc.h>
 #endif
 
4ae40685
 #include "avutil.h"
77652a6a
 #include "mem.h"
 
89c9ff50
 /* here we can use OS-dependent allocation functions */
1e60e933
 #undef free
 #undef malloc
 #undef realloc
 
4ae40685
 #ifdef MALLOC_PREFIX
 
 #define malloc         AV_JOIN(MALLOC_PREFIX, malloc)
 #define memalign       AV_JOIN(MALLOC_PREFIX, memalign)
 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
 #define realloc        AV_JOIN(MALLOC_PREFIX, realloc)
 #define free           AV_JOIN(MALLOC_PREFIX, free)
 
 void *malloc(size_t size);
 void *memalign(size_t align, size_t size);
 int   posix_memalign(void **ptr, size_t align, size_t size);
 void *realloc(void *ptr, size_t size);
 void  free(void *ptr);
 
 #endif /* MALLOC_PREFIX */
 
08675bb3
 #define ALIGN (HAVE_AVX ? 32 : 16)
 
1a65d50e
 /* NOTE: if you want to override these functions with your own
  * implementations (not recommended) you have to link libav* as
  * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
  * Note that this will cost performance. */
d01fe86d
 
5a8e9942
 static size_t max_alloc_size= INT_MAX;
 
 void av_max_alloc(size_t max){
     max_alloc_size = max;
 }
6b4c0be5
 
490a022d
 void *av_malloc(size_t size)
d01fe86d
 {
1f91cdce
     void *ptr = NULL;
b250f9c6
 #if CONFIG_MEMALIGN_HACK
ed96aeea
     long diff;
88730be6
 #endif
0ecca7a4
 
7d77d5f6
     /* let's disallow possible ambiguous cases */
5a8e9942
     if (size > (max_alloc_size-32))
0ecca7a4
         return NULL;
115329f1
 
b250f9c6
 #if CONFIG_MEMALIGN_HACK
08675bb3
     ptr = malloc(size+ALIGN);
a9493601
     if(!ptr)
         return ptr;
08675bb3
     diff= ((-(long)ptr - 1)&(ALIGN-1)) + 1;
90d30570
     ptr = (char*)ptr + diff;
da9b170c
     ((char*)ptr)[-1]= diff;
b250f9c6
 #elif HAVE_POSIX_MEMALIGN
bed04e2b
     if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
08675bb3
     if (posix_memalign(&ptr,ALIGN,size))
a90de11d
         ptr = NULL;
be1a839c
 #elif HAVE_ALIGNED_MALLOC
cabbd271
     ptr = _aligned_malloc(size, ALIGN);
b250f9c6
 #elif HAVE_MEMALIGN
08675bb3
     ptr = memalign(ALIGN,size);
115329f1
     /* Why 64?
d01fe86d
        Indeed, we should align it:
          on 4 for 386
          on 16 for 486
89c9ff50
          on 32 for 586, PPro - K6-III
bb270c08
          on 64 for K7 (maybe for P3 too).
d01fe86d
        Because L1 and L2 caches are aligned on those values.
        But I don't want to code such logic here!
      */
13dfce3d
      /* Why 32?
         For AVX ASM. SSE / NEON needs only 16.
2cab6401
         Why not larger? Because I did not see a difference in benchmarks ...
8f2b21a8
      */
89c9ff50
      /* benchmarks with P3
bb270c08
         memalign(64)+1          3071,3051,3032
         memalign(64)+2          3051,3032,3041
         memalign(64)+4          2911,2896,2915
         memalign(64)+8          2545,2554,2550
         memalign(64)+16         2543,2572,2563
         memalign(64)+32         2546,2545,2571
         memalign(64)+64         2570,2533,2558
115329f1
 
89c9ff50
         BTW, malloc seems to do 8-byte alignment by default here.
8f2b21a8
      */
d01fe86d
 #else
     ptr = malloc(size);
 #endif
7c84e7d3
     if(!ptr && !size) {
         size = 1;
c8981edd
         ptr= av_malloc(1);
7c84e7d3
     }
 #if CONFIG_MEMORY_POISONING
     if (ptr)
         memset(ptr, 0x2a, size);
 #endif
d01fe86d
     return ptr;
 }
 
490a022d
 void *av_realloc(void *ptr, size_t size)
8e1e6f31
 {
b250f9c6
 #if CONFIG_MEMALIGN_HACK
0a7c36af
     int diff;
 #endif
88730be6
 
7d77d5f6
     /* let's disallow possible ambiguous cases */
5a8e9942
     if (size > (max_alloc_size-32))
0ecca7a4
         return NULL;
 
b250f9c6
 #if CONFIG_MEMALIGN_HACK
0a7c36af
     //FIXME this isn't aligned correctly, though it probably isn't needed
     if(!ptr) return av_malloc(size);
     diff= ((char*)ptr)[-1];
fc119278
     ptr= realloc((char*)ptr - diff, size + diff);
     if(ptr) ptr = (char*)ptr + diff;
     return ptr;
be1a839c
 #elif HAVE_ALIGNED_MALLOC
cabbd271
     return _aligned_realloc(ptr, size + !size, ALIGN);
0a7c36af
 #else
91ff05f6
     return realloc(ptr, size + !size);
da9b170c
 #endif
b7a22d84
 }
 
5cd754bc
 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
 {
     size_t size;
     void *r;
 
     if (av_size_mult(elsize, nelem, &size)) {
         av_free(ptr);
         return NULL;
     }
     r = av_realloc(ptr, size);
     if (!r && size)
         av_free(ptr);
     return r;
 }
 
d01fe86d
 void av_free(void *ptr)
 {
b250f9c6
 #if CONFIG_MEMALIGN_HACK
0c8eb72e
     if (ptr)
90d30570
         free((char*)ptr - ((char*)ptr)[-1]);
be1a839c
 #elif HAVE_ALIGNED_MALLOC
     _aligned_free(ptr);
da9b170c
 #else
0c8eb72e
     free(ptr);
da9b170c
 #endif
d01fe86d
 }
 
79e47000
 void av_freep(void *arg)
 {
     void **ptr= (void**)arg;
     av_free(*ptr);
     *ptr = NULL;
 }
 
490a022d
 void *av_mallocz(size_t size)
79e47000
 {
11362767
     void *ptr = av_malloc(size);
79e47000
     if (ptr)
         memset(ptr, 0, size);
     return ptr;
 }
 
ccecab4a
 void *av_calloc(size_t nmemb, size_t size)
 {
     if (size <= 0 || nmemb >= INT_MAX / size)
         return NULL;
     return av_mallocz(nmemb * size);
 }
 
79e47000
 char *av_strdup(const char *s)
 {
fdf35f26
     char *ptr= NULL;
     if(s){
19757f61
         int len = strlen(s) + 1;
         ptr = av_malloc(len);
         if (ptr)
             memcpy(ptr, s, len);
fdf35f26
     }
79e47000
     return ptr;
 }
 
35ceaa73
 /* add one element to a dynamic array */
 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
 {
     /* see similar ffmpeg.c:grow_array() */
     int nb, nb_alloc;
     intptr_t *tab;
 
     nb = *nb_ptr;
     tab = *(intptr_t**)tab_ptr;
     if ((nb & (nb - 1)) == 0) {
         if (nb == 0)
             nb_alloc = 1;
         else
             nb_alloc = nb * 2;
         tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
         *(intptr_t**)tab_ptr = tab;
     }
     tab[nb++] = (intptr_t)elem;
     *nb_ptr = nb;
 }
c065255b