]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/mem.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavutil / mem.c
index 965daa93c846dcd8edbadc22ea03b5f75b86003f..87c2008a27d2808399e2861d669fca949d33282d 100644 (file)
@@ -24,6 +24,8 @@
  * default memory allocator for libavutil
  */
 
+#define _XOPEN_SOURCE 600
+
 #include "config.h"
 
 #include <limits.h>
@@ -57,10 +59,14 @@ void  free(void *ptr);
 
 #endif /* MALLOC_PREFIX */
 
+#define ALIGN (HAVE_AVX ? 32 : 16)
+
 /* You can redefine av_malloc and av_free in your project to use your
    memory allocator. You do not need to suppress this file because the
    linker will do it automatically. */
 
+#define MAX_MALLOC_SIZE INT_MAX
+
 void *av_malloc(size_t size)
 {
     void *ptr = NULL;
@@ -69,23 +75,22 @@ void *av_malloc(size_t size)
 #endif
 
     /* let's disallow possible ambiguous cases */
-    if(size > (INT_MAX-32) )
+    if (size > (MAX_MALLOC_SIZE-32))
         return NULL;
-    else if(!size)
-        size= 1;
 
 #if CONFIG_MEMALIGN_HACK
-    ptr = malloc(size+32);
+    ptr = malloc(size+ALIGN);
     if(!ptr)
         return ptr;
-    diff= ((-(long)ptr - 1)&31) + 1;
+    diff= ((-(long)ptr - 1)&(ALIGN-1)) + 1;
     ptr = (char*)ptr + diff;
     ((char*)ptr)[-1]= diff;
 #elif HAVE_POSIX_MEMALIGN
-    if (posix_memalign(&ptr,32,size))
+    if (size) //OSX on SDK 10.6 has a broken posix_memalign implementation
+    if (posix_memalign(&ptr,ALIGN,size))
         ptr = NULL;
 #elif HAVE_MEMALIGN
-    ptr = memalign(32,size);
+    ptr = memalign(ALIGN,size);
     /* Why 64?
        Indeed, we should align it:
          on 4 for 386
@@ -113,6 +118,8 @@ void *av_malloc(size_t size)
 #else
     ptr = malloc(size);
 #endif
+    if(!ptr && !size)
+        ptr= av_malloc(1);
     return ptr;
 }
 
@@ -123,7 +130,7 @@ void *av_realloc(void *ptr, size_t size)
 #endif
 
     /* let's disallow possible ambiguous cases */
-    if(size > (INT_MAX-16) )
+    if (size > (MAX_MALLOC_SIZE-16))
         return NULL;
 
 #if CONFIG_MEMALIGN_HACK
@@ -132,7 +139,7 @@ void *av_realloc(void *ptr, size_t size)
     diff= ((char*)ptr)[-1];
     return (char*)realloc((char*)ptr - diff, size + diff) + diff;
 #else
-    return realloc(ptr, size);
+    return realloc(ptr, size + !size);
 #endif
 }