[Ffmpeg-devel] [RFC] another attempt at memalign realloc

Reimar Döffinger Reimar.Doeffinger
Thu Dec 7 20:53:28 CET 2006


Hello,
On Thu, Dec 07, 2006 at 08:38:47PM +0100, Michael Niedermayer wrote:
> > 3) alignment preserved over realloc
> 
> well i can live without aligned realloc() ...

Ok, then IMO leave it as it is. I attached another version with some
bugs fixed for reference if we should ever need it.
In that case we might just add a av_aligned_realloc or some such, where
being slow would not be a problem.

Greetings,
Reimar D?ffinger
-------------- next part --------------
Index: libavutil/mem.c
===================================================================
--- libavutil/mem.c	(revision 7250)
+++ libavutil/mem.c	(working copy)
@@ -105,19 +105,43 @@
 {
 #ifdef CONFIG_MEMALIGN_HACK
     int diff;
+    char *p = ptr;
 #endif
+    char *p2;
 
     /* let's disallow possible ambiguous cases */
     if(size > (INT_MAX-16) )
         return NULL;
+    if(!size) {
+      av_free(ptr);
+      return NULL;
+    }
+    if(!ptr) return av_malloc(size);
 
 #ifdef CONFIG_MEMALIGN_HACK
-    //FIXME this isn't aligned correctly, though it probably isn't needed
-    if(!ptr) return av_malloc(size);
-    diff= ((char*)ptr)[-1];
-    return realloc(ptr - diff, size + diff) + diff;
+    diff = p[-1];
+    p2 = realloc(p - diff, size + 16);
+    if (!p2) return NULL;
+    p2 += diff;
+    if(!((long)p2&15))
+        return p2;
+    p = p2 - diff;
+    diff = ((-(long)p - 1)&15) + 1;
+    p += diff;
+    memmove(p, p2, size);
+    p[-1] = diff;
+    return p;
 #else
-    return realloc(ptr, size);
+    p2 = av_malloc(size);
+    if (!p2) return NULL;
+    ptr = realloc(ptr, size);
+    if(!((long)ptr&15)) {
+        av_free(p2);
+        return ptr;
+    }
+    memcpy(p2, ptr, size);
+    free(ptr);
+    return p2;
 #endif
 }
 



More information about the ffmpeg-devel mailing list