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 "mem.h"
00037
00038
00039 #undef free
00040 #undef malloc
00041 #undef realloc
00042
00043
00044
00045
00046
00047 void *av_malloc(unsigned int size)
00048 {
00049 void *ptr = NULL;
00050 #if CONFIG_MEMALIGN_HACK
00051 long diff;
00052 #endif
00053
00054
00055 if(size > (INT_MAX-16) )
00056 return NULL;
00057
00058 #if CONFIG_MEMALIGN_HACK
00059 ptr = malloc(size+16);
00060 if(!ptr)
00061 return ptr;
00062 diff= ((-(long)ptr - 1)&15) + 1;
00063 ptr = (char*)ptr + diff;
00064 ((char*)ptr)[-1]= diff;
00065 #elif HAVE_POSIX_MEMALIGN
00066 if (posix_memalign(&ptr,16,size))
00067 ptr = NULL;
00068 #elif HAVE_MEMALIGN
00069 ptr = memalign(16,size);
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 #else
00097 ptr = malloc(size);
00098 #endif
00099 return ptr;
00100 }
00101
00102 void *av_realloc(void *ptr, unsigned int size)
00103 {
00104 #if CONFIG_MEMALIGN_HACK
00105 int diff;
00106 #endif
00107
00108
00109 if(size > (INT_MAX-16) )
00110 return NULL;
00111
00112 #if CONFIG_MEMALIGN_HACK
00113
00114 if(!ptr) return av_malloc(size);
00115 diff= ((char*)ptr)[-1];
00116 ptr= realloc((char*)ptr - diff, size + diff);
00117 if(ptr) ptr = (char*)ptr + diff;
00118 return ptr;
00119 #else
00120 return realloc(ptr, size);
00121 #endif
00122 }
00123
00124 void av_free(void *ptr)
00125 {
00126
00127 if (ptr)
00128 #if CONFIG_MEMALIGN_HACK
00129 free((char*)ptr - ((char*)ptr)[-1]);
00130 #else
00131 free(ptr);
00132 #endif
00133 }
00134
00135 void av_freep(void *arg)
00136 {
00137 void **ptr= (void**)arg;
00138 av_free(*ptr);
00139 *ptr = NULL;
00140 }
00141
00142 void *av_mallocz(unsigned int size)
00143 {
00144 void *ptr = av_malloc(size);
00145 if (ptr)
00146 memset(ptr, 0, size);
00147 return ptr;
00148 }
00149
00150 char *av_strdup(const char *s)
00151 {
00152 char *ptr= NULL;
00153 if(s){
00154 int len = strlen(s) + 1;
00155 ptr = av_malloc(len);
00156 if (ptr)
00157 memcpy(ptr, s, len);
00158 }
00159 return ptr;
00160 }
00161