00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "config.h"
00028
00029 #include <limits.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #if HAVE_MALLOC_H
00033 #include <malloc.h>
00034 #endif
00035
00036 #include "avutil.h"
00037 #include "mem.h"
00038
00039
00040 #undef free
00041 #undef malloc
00042 #undef realloc
00043
00044 #ifdef MALLOC_PREFIX
00045
00046 #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
00047 #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
00048 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
00049 #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
00050 #define free AV_JOIN(MALLOC_PREFIX, free)
00051
00052 void *malloc(size_t size);
00053 void *memalign(size_t align, size_t size);
00054 int posix_memalign(void **ptr, size_t align, size_t size);
00055 void *realloc(void *ptr, size_t size);
00056 void free(void *ptr);
00057
00058 #endif
00059
00060
00061
00062
00063
00064 void *av_malloc(unsigned int size)
00065 {
00066 void *ptr = NULL;
00067 #if CONFIG_MEMALIGN_HACK
00068 long diff;
00069 #endif
00070
00071
00072 if(size > (INT_MAX-16) )
00073 return NULL;
00074
00075 #if CONFIG_MEMALIGN_HACK
00076 ptr = malloc(size+16);
00077 if(!ptr)
00078 return ptr;
00079 diff= ((-(long)ptr - 1)&15) + 1;
00080 ptr = (char*)ptr + diff;
00081 ((char*)ptr)[-1]= diff;
00082 #elif HAVE_POSIX_MEMALIGN
00083 if (posix_memalign(&ptr,16,size))
00084 ptr = NULL;
00085 #elif HAVE_MEMALIGN
00086 ptr = memalign(16,size);
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 #else
00114 ptr = malloc(size);
00115 #endif
00116 return ptr;
00117 }
00118
00119 void *av_realloc(void *ptr, unsigned int size)
00120 {
00121 #if CONFIG_MEMALIGN_HACK
00122 int diff;
00123 #endif
00124
00125
00126 if(size > (INT_MAX-16) )
00127 return NULL;
00128
00129 #if CONFIG_MEMALIGN_HACK
00130
00131 if(!ptr) return av_malloc(size);
00132 diff= ((char*)ptr)[-1];
00133 return (char*)realloc((char*)ptr - diff, size + diff) + diff;
00134 #else
00135 return realloc(ptr, size);
00136 #endif
00137 }
00138
00139 void av_free(void *ptr)
00140 {
00141
00142 if (ptr)
00143 #if CONFIG_MEMALIGN_HACK
00144 free((char*)ptr - ((char*)ptr)[-1]);
00145 #else
00146 free(ptr);
00147 #endif
00148 }
00149
00150 void av_freep(void *arg)
00151 {
00152 void **ptr= (void**)arg;
00153 av_free(*ptr);
00154 *ptr = NULL;
00155 }
00156
00157 void *av_mallocz(unsigned int size)
00158 {
00159 void *ptr = av_malloc(size);
00160 if (ptr)
00161 memset(ptr, 0, size);
00162 return ptr;
00163 }
00164
00165 char *av_strdup(const char *s)
00166 {
00167 char *ptr= NULL;
00168 if(s){
00169 int len = strlen(s) + 1;
00170 ptr = av_malloc(len);
00171 if (ptr)
00172 memcpy(ptr, s, len);
00173 }
00174 return ptr;
00175 }
00176