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

Reimar Döffinger Reimar.Doeffinger
Thu Dec 7 11:39:02 CET 2006


Hello,
On Wed, Dec 06, 2006 at 09:38:51PM +0100, Michael Niedermayer wrote:
> On Wed, Dec 06, 2006 at 05:34:12PM +0100, Reimar D?ffinger wrote:
[...]
> > -    return realloc(ptr - diff, size + diff) + diff;
> > +    ptr2 = realloc(ptr - diff, size + 16) + diff;
> > +    if(!((int)ptr2&15))
> 
> hmm didnt this cast cause same silly warning with gcc on x86-64 or
> something, anyway long and int casts are mixed not a real issue though

The result of reusing code from other tries ;-)

> > +        return ptr2;
> > +    ptr = ptr2 - diff;
> 
> void * arithmetic

Fixed, but most of the code actually is copy&paste from av_malloc, which
seems to use void * arithmetic, too...

> > +    diff= ((-(long)ptr - 1)&15) + 1;
> > +    ptr += diff;
> > +    memove(ptr, ptr2, size);
> > +    ((char*)ptr)[-1]= diff;
> > +    return ptr;
> 
> 
> the comment says:
>    If size is zero, it is identical to
>  * free(ptr) and NULL is returned.
> 
> this isnt true, though it neither was before your patch in the 
> CONFIG_MEMALIGN_HACK case ...

Fixed.

> except these nitpicks iam fine with the patch

See if it's still fine. Too bad I can't find any good solution for the
non-memalign-hack case.

Greetings,
Reimar D?ffinger
-------------- next part --------------
Index: libavutil/mem.c
===================================================================
--- libavutil/mem.c	(revision 7217)
+++ libavutil/mem.c	(working copy)
@@ -105,6 +105,7 @@
 {
 #ifdef CONFIG_MEMALIGN_HACK
     int diff;
+    char *p = ptr, *p2;
 #endif
 
     /* let's disallow possible ambiguous cases */
@@ -112,10 +113,22 @@
         return NULL;
 
 #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;
+    if(!p) return av_malloc(size);
+    if(!size) {
+      av_free(p);
+      return NULL;
+    }
+    diff = p[-1];
+    p2 = realloc(p - diff, size + 16);
+    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);
 #endif



More information about the ffmpeg-devel mailing list