libavutil/mem.c
Go to the documentation of this file.
00001 /*
00002  * default memory allocator for libavutil
00003  * Copyright (c) 2002 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #define _XOPEN_SOURCE 600
00028 
00029 #include "config.h"
00030 
00031 #include <limits.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #if HAVE_MALLOC_H
00035 #include <malloc.h>
00036 #endif
00037 
00038 #include "avutil.h"
00039 #include "mem.h"
00040 
00041 /* here we can use OS-dependent allocation functions */
00042 #undef free
00043 #undef malloc
00044 #undef realloc
00045 
00046 #ifdef MALLOC_PREFIX
00047 
00048 #define malloc         AV_JOIN(MALLOC_PREFIX, malloc)
00049 #define memalign       AV_JOIN(MALLOC_PREFIX, memalign)
00050 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
00051 #define realloc        AV_JOIN(MALLOC_PREFIX, realloc)
00052 #define free           AV_JOIN(MALLOC_PREFIX, free)
00053 
00054 void *malloc(size_t size);
00055 void *memalign(size_t align, size_t size);
00056 int   posix_memalign(void **ptr, size_t align, size_t size);
00057 void *realloc(void *ptr, size_t size);
00058 void  free(void *ptr);
00059 
00060 #endif /* MALLOC_PREFIX */
00061 
00062 #define ALIGN (HAVE_AVX ? 32 : 16)
00063 
00064 /* You can redefine av_malloc and av_free in your project to use your
00065    memory allocator. You do not need to suppress this file because the
00066    linker will do it automatically. */
00067 
00068 static size_t max_alloc_size= INT_MAX;
00069 
00070 void av_max_alloc(size_t max){
00071     max_alloc_size = max;
00072 }
00073 
00074 void *av_malloc(size_t size)
00075 {
00076     void *ptr = NULL;
00077 #if CONFIG_MEMALIGN_HACK
00078     long diff;
00079 #endif
00080 
00081     /* let's disallow possible ambiguous cases */
00082     if (size > (max_alloc_size-32))
00083         return NULL;
00084 
00085 #if CONFIG_MEMALIGN_HACK
00086     ptr = malloc(size+ALIGN);
00087     if(!ptr)
00088         return ptr;
00089     diff= ((-(long)ptr - 1)&(ALIGN-1)) + 1;
00090     ptr = (char*)ptr + diff;
00091     ((char*)ptr)[-1]= diff;
00092 #elif HAVE_POSIX_MEMALIGN
00093     if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
00094     if (posix_memalign(&ptr,ALIGN,size))
00095         ptr = NULL;
00096 #elif HAVE_MEMALIGN
00097     ptr = memalign(ALIGN,size);
00098     /* Why 64?
00099        Indeed, we should align it:
00100          on 4 for 386
00101          on 16 for 486
00102          on 32 for 586, PPro - K6-III
00103          on 64 for K7 (maybe for P3 too).
00104        Because L1 and L2 caches are aligned on those values.
00105        But I don't want to code such logic here!
00106      */
00107      /* Why 32?
00108         For AVX ASM. SSE / NEON needs only 16.
00109         Why not larger? Because I did not see a difference in benchmarks ...
00110      */
00111      /* benchmarks with P3
00112         memalign(64)+1          3071,3051,3032
00113         memalign(64)+2          3051,3032,3041
00114         memalign(64)+4          2911,2896,2915
00115         memalign(64)+8          2545,2554,2550
00116         memalign(64)+16         2543,2572,2563
00117         memalign(64)+32         2546,2545,2571
00118         memalign(64)+64         2570,2533,2558
00119 
00120         BTW, malloc seems to do 8-byte alignment by default here.
00121      */
00122 #else
00123     ptr = malloc(size);
00124 #endif
00125     if(!ptr && !size)
00126         ptr= av_malloc(1);
00127     return ptr;
00128 }
00129 
00130 void *av_realloc(void *ptr, size_t size)
00131 {
00132 #if CONFIG_MEMALIGN_HACK
00133     int diff;
00134 #endif
00135 
00136     /* let's disallow possible ambiguous cases */
00137     if (size > (max_alloc_size-32))
00138         return NULL;
00139 
00140 #if CONFIG_MEMALIGN_HACK
00141     //FIXME this isn't aligned correctly, though it probably isn't needed
00142     if(!ptr) return av_malloc(size);
00143     diff= ((char*)ptr)[-1];
00144     ptr= realloc((char*)ptr - diff, size + diff);
00145     if(ptr) ptr = (char*)ptr + diff;
00146     return ptr;
00147 #else
00148     return realloc(ptr, size + !size);
00149 #endif
00150 }
00151 
00152 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
00153 {
00154     size_t size;
00155     void *r;
00156 
00157     if (av_size_mult(elsize, nelem, &size)) {
00158         av_free(ptr);
00159         return NULL;
00160     }
00161     r = av_realloc(ptr, size);
00162     if (!r && size)
00163         av_free(ptr);
00164     return r;
00165 }
00166 
00167 void av_free(void *ptr)
00168 {
00169 #if CONFIG_MEMALIGN_HACK
00170     if (ptr)
00171         free((char*)ptr - ((char*)ptr)[-1]);
00172 #else
00173     free(ptr);
00174 #endif
00175 }
00176 
00177 void av_freep(void *arg)
00178 {
00179     void **ptr= (void**)arg;
00180     av_free(*ptr);
00181     *ptr = NULL;
00182 }
00183 
00184 void *av_mallocz(size_t size)
00185 {
00186     void *ptr = av_malloc(size);
00187     if (ptr)
00188         memset(ptr, 0, size);
00189     return ptr;
00190 }
00191 
00192 void *av_calloc(size_t nmemb, size_t size)
00193 {
00194     if (size <= 0 || nmemb >= INT_MAX / size)
00195         return NULL;
00196     return av_mallocz(nmemb * size);
00197 }
00198 
00199 char *av_strdup(const char *s)
00200 {
00201     char *ptr= NULL;
00202     if(s){
00203         int len = strlen(s) + 1;
00204         ptr = av_malloc(len);
00205         if (ptr)
00206             memcpy(ptr, s, len);
00207     }
00208     return ptr;
00209 }
00210 
00211 /* add one element to a dynamic array */
00212 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
00213 {
00214     /* see similar ffmpeg.c:grow_array() */
00215     int nb, nb_alloc;
00216     intptr_t *tab;
00217 
00218     nb = *nb_ptr;
00219     tab = *(intptr_t**)tab_ptr;
00220     if ((nb & (nb - 1)) == 0) {
00221         if (nb == 0)
00222             nb_alloc = 1;
00223         else
00224             nb_alloc = nb * 2;
00225         tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
00226         *(intptr_t**)tab_ptr = tab;
00227     }
00228     tab[nb++] = (intptr_t)elem;
00229     *nb_ptr = nb;
00230 }