00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #define _XOPEN_SOURCE 600
00028
00029 #include "config.h"
00030
00031 #include <limits.h>
00032 #include <stdint.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #if HAVE_MALLOC_H
00036 #include <malloc.h>
00037 #endif
00038
00039 #include "avutil.h"
00040 #include "intreadwrite.h"
00041 #include "mem.h"
00042
00043 #ifdef MALLOC_PREFIX
00044
00045 #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
00046 #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
00047 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
00048 #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
00049 #define free AV_JOIN(MALLOC_PREFIX, free)
00050
00051 void *malloc(size_t size);
00052 void *memalign(size_t align, size_t size);
00053 int posix_memalign(void **ptr, size_t align, size_t size);
00054 void *realloc(void *ptr, size_t size);
00055 void free(void *ptr);
00056
00057 #endif
00058
00059 #define ALIGN (HAVE_AVX ? 32 : 16)
00060
00061
00062
00063
00064
00065
00066 static size_t max_alloc_size= INT_MAX;
00067
00068 void av_max_alloc(size_t max){
00069 max_alloc_size = max;
00070 }
00071
00072 void *av_malloc(size_t size)
00073 {
00074 void *ptr = NULL;
00075 #if CONFIG_MEMALIGN_HACK
00076 long diff;
00077 #endif
00078
00079
00080 if (size > (max_alloc_size - 32))
00081 return NULL;
00082
00083 #if CONFIG_MEMALIGN_HACK
00084 ptr = malloc(size + ALIGN);
00085 if (!ptr)
00086 return ptr;
00087 diff = ((~(long)ptr)&(ALIGN - 1)) + 1;
00088 ptr = (char *)ptr + diff;
00089 ((char *)ptr)[-1] = diff;
00090 #elif HAVE_POSIX_MEMALIGN
00091 if (size)
00092 if (posix_memalign(&ptr, ALIGN, size))
00093 ptr = NULL;
00094 #elif HAVE_ALIGNED_MALLOC
00095 ptr = _aligned_malloc(size, ALIGN);
00096 #elif HAVE_MEMALIGN
00097 ptr = memalign(ALIGN, size);
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 #else
00123 ptr = malloc(size);
00124 #endif
00125 if(!ptr && !size) {
00126 size = 1;
00127 ptr= av_malloc(1);
00128 }
00129 #if CONFIG_MEMORY_POISONING
00130 if (ptr)
00131 memset(ptr, 0x2a, size);
00132 #endif
00133 return ptr;
00134 }
00135
00136 void *av_realloc(void *ptr, size_t size)
00137 {
00138 #if CONFIG_MEMALIGN_HACK
00139 int diff;
00140 #endif
00141
00142
00143 if (size > (max_alloc_size - 32))
00144 return NULL;
00145
00146 #if CONFIG_MEMALIGN_HACK
00147
00148 if (!ptr)
00149 return av_malloc(size);
00150 diff = ((char *)ptr)[-1];
00151 ptr = realloc((char *)ptr - diff, size + diff);
00152 if (ptr)
00153 ptr = (char *)ptr + diff;
00154 return ptr;
00155 #elif HAVE_ALIGNED_MALLOC
00156 return _aligned_realloc(ptr, size + !size, ALIGN);
00157 #else
00158 return realloc(ptr, size + !size);
00159 #endif
00160 }
00161
00162 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
00163 {
00164 size_t size;
00165 void *r;
00166
00167 if (av_size_mult(elsize, nelem, &size)) {
00168 av_free(ptr);
00169 return NULL;
00170 }
00171 r = av_realloc(ptr, size);
00172 if (!r && size)
00173 av_free(ptr);
00174 return r;
00175 }
00176
00177 void av_free(void *ptr)
00178 {
00179 #if CONFIG_MEMALIGN_HACK
00180 if (ptr)
00181 free((char *)ptr - ((char *)ptr)[-1]);
00182 #elif HAVE_ALIGNED_MALLOC
00183 _aligned_free(ptr);
00184 #else
00185 free(ptr);
00186 #endif
00187 }
00188
00189 void av_freep(void *arg)
00190 {
00191 void **ptr = (void **)arg;
00192 av_free(*ptr);
00193 *ptr = NULL;
00194 }
00195
00196 void *av_mallocz(size_t size)
00197 {
00198 void *ptr = av_malloc(size);
00199 if (ptr)
00200 memset(ptr, 0, size);
00201 return ptr;
00202 }
00203
00204 void *av_calloc(size_t nmemb, size_t size)
00205 {
00206 if (size <= 0 || nmemb >= INT_MAX / size)
00207 return NULL;
00208 return av_mallocz(nmemb * size);
00209 }
00210
00211 char *av_strdup(const char *s)
00212 {
00213 char *ptr = NULL;
00214 if (s) {
00215 int len = strlen(s) + 1;
00216 ptr = av_malloc(len);
00217 if (ptr)
00218 memcpy(ptr, s, len);
00219 }
00220 return ptr;
00221 }
00222
00223
00224 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
00225 {
00226
00227 int nb, nb_alloc;
00228 intptr_t *tab;
00229
00230 nb = *nb_ptr;
00231 tab = *(intptr_t**)tab_ptr;
00232 if ((nb & (nb - 1)) == 0) {
00233 if (nb == 0)
00234 nb_alloc = 1;
00235 else
00236 nb_alloc = nb * 2;
00237 tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
00238 *(intptr_t**)tab_ptr = tab;
00239 }
00240 tab[nb++] = (intptr_t)elem;
00241 *nb_ptr = nb;
00242 }
00243
00244 static void fill16(uint8_t *dst, int len)
00245 {
00246 uint32_t v = AV_RN16(dst - 2);
00247
00248 v |= v << 16;
00249
00250 while (len >= 4) {
00251 AV_WN32(dst, v);
00252 dst += 4;
00253 len -= 4;
00254 }
00255
00256 while (len--) {
00257 *dst = dst[-2];
00258 dst++;
00259 }
00260 }
00261
00262 static void fill24(uint8_t *dst, int len)
00263 {
00264 #if HAVE_BIGENDIAN
00265 uint32_t v = AV_RB24(dst - 3);
00266 uint32_t a = v << 8 | v >> 16;
00267 uint32_t b = v << 16 | v >> 8;
00268 uint32_t c = v << 24 | v;
00269 #else
00270 uint32_t v = AV_RL24(dst - 3);
00271 uint32_t a = v | v << 24;
00272 uint32_t b = v >> 8 | v << 16;
00273 uint32_t c = v >> 16 | v << 8;
00274 #endif
00275
00276 while (len >= 12) {
00277 AV_WN32(dst, a);
00278 AV_WN32(dst + 4, b);
00279 AV_WN32(dst + 8, c);
00280 dst += 12;
00281 len -= 12;
00282 }
00283
00284 if (len >= 4) {
00285 AV_WN32(dst, a);
00286 dst += 4;
00287 len -= 4;
00288 }
00289
00290 if (len >= 4) {
00291 AV_WN32(dst, b);
00292 dst += 4;
00293 len -= 4;
00294 }
00295
00296 while (len--) {
00297 *dst = dst[-3];
00298 dst++;
00299 }
00300 }
00301
00302 static void fill32(uint8_t *dst, int len)
00303 {
00304 uint32_t v = AV_RN32(dst - 4);
00305
00306 while (len >= 4) {
00307 AV_WN32(dst, v);
00308 dst += 4;
00309 len -= 4;
00310 }
00311
00312 while (len--) {
00313 *dst = dst[-4];
00314 dst++;
00315 }
00316 }
00317
00318 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
00319 {
00320 const uint8_t *src = &dst[-back];
00321 if (back <= 1) {
00322 memset(dst, *src, cnt);
00323 } else if (back == 2) {
00324 fill16(dst, cnt);
00325 } else if (back == 3) {
00326 fill24(dst, cnt);
00327 } else if (back == 4) {
00328 fill32(dst, cnt);
00329 } else {
00330 if (cnt >= 16) {
00331 int blocklen = back;
00332 while (cnt > blocklen) {
00333 memcpy(dst, src, blocklen);
00334 dst += blocklen;
00335 cnt -= blocklen;
00336 blocklen <<= 1;
00337 }
00338 memcpy(dst, src, cnt);
00339 return;
00340 }
00341 if (cnt >= 8) {
00342 AV_COPY32U(dst, src);
00343 AV_COPY32U(dst + 4, src + 4);
00344 src += 8;
00345 dst += 8;
00346 cnt -= 8;
00347 }
00348 if (cnt >= 4) {
00349 AV_COPY32U(dst, src);
00350 src += 4;
00351 dst += 4;
00352 cnt -= 4;
00353 }
00354 if (cnt >= 2) {
00355 AV_COPY16U(dst, src);
00356 src += 2;
00357 dst += 2;
00358 cnt -= 2;
00359 }
00360 if (cnt)
00361 *dst = *src;
00362 }
00363 }
00364