Go to the documentation of this file.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(size_t size)
00065 {
00066 void *ptr = NULL;
00067 #if CONFIG_MEMALIGN_HACK
00068 long diff;
00069 #endif
00070
00071
00072 if(size > (INT_MAX-32) )
00073 return NULL;
00074
00075 #if CONFIG_MEMALIGN_HACK
00076 ptr = malloc(size+32);
00077 if(!ptr)
00078 return ptr;
00079 diff= ((-(long)ptr - 1)&31) + 1;
00080 ptr = (char*)ptr + diff;
00081 ((char*)ptr)[-1]= diff;
00082 #elif HAVE_POSIX_MEMALIGN
00083 if (posix_memalign(&ptr,32,size))
00084 ptr = NULL;
00085 #elif HAVE_MEMALIGN
00086 ptr = memalign(32,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 #else
00112 ptr = malloc(size);
00113 #endif
00114 return ptr;
00115 }
00116
00117 void *av_realloc(void *ptr, size_t size)
00118 {
00119 #if CONFIG_MEMALIGN_HACK
00120 int diff;
00121 #endif
00122
00123
00124 if(size > (INT_MAX-16) )
00125 return NULL;
00126
00127 #if CONFIG_MEMALIGN_HACK
00128
00129 if(!ptr) return av_malloc(size);
00130 diff= ((char*)ptr)[-1];
00131 return (char*)realloc((char*)ptr - diff, size + diff) + diff;
00132 #else
00133 return realloc(ptr, size);
00134 #endif
00135 }
00136
00137 void av_free(void *ptr)
00138 {
00139 #if CONFIG_MEMALIGN_HACK
00140 if (ptr)
00141 free((char*)ptr - ((char*)ptr)[-1]);
00142 #else
00143 free(ptr);
00144 #endif
00145 }
00146
00147 void av_freep(void *arg)
00148 {
00149 void **ptr= (void**)arg;
00150 av_free(*ptr);
00151 *ptr = NULL;
00152 }
00153
00154 void *av_mallocz(size_t size)
00155 {
00156 void *ptr = av_malloc(size);
00157 if (ptr)
00158 memset(ptr, 0, size);
00159 return ptr;
00160 }
00161
00162 char *av_strdup(const char *s)
00163 {
00164 char *ptr= NULL;
00165 if(s){
00166 int len = strlen(s) + 1;
00167 ptr = av_malloc(len);
00168 if (ptr)
00169 memcpy(ptr, s, len);
00170 }
00171 return ptr;
00172 }
00173