[Ffmpeg-devel] [RFC] another attempt at memalign realloc
Reimar Döffinger
Reimar.Doeffinger
Thu Dec 7 19:05:55 CET 2006
Hello,
On Thu, Dec 07, 2006 at 01:09:51PM +0100, Reimar D?ffinger wrote:
> On Thu, Dec 07, 2006 at 12:55:47PM +0100, Michael Niedermayer wrote:
> > > Too bad I can't find any good solution for the
> > > non-memalign-hack case.
> >
> > p= realloc(p, size);
> > if(!((long)p&15))
> > return p;
> > p2= av_malloc(size);
> > if(p2)
> > memcpy(p2, p, size);
> > av_free(p);
> > return p2;
> >
> > ?
> >
> > btw, this one with a small modification should work in the memalign-hack
> > case too i think, would simplify the code though it would be slower in the
> > memalign-hack case ...
>
> Yes, I was thinking about that, and probably it is fine for ffmpeg, but
> it does not meet this part of realloc:
> "If realloc() fails the original block is left untouched".
> If we do it this change, at least we must free p always and say
> "If av_realloc() fails the original block is freed".
> Unless of course I miss something *g*
Actually, there is a rather obvious solution.
Disadvantage: av_realloc without MEMALIGN_HACK now _always_ has the time
and space costs of av_malloc.
Greetings,
Reimar D?ffinger
-------------- next part --------------
Index: libavutil/mem.c
===================================================================
--- libavutil/mem.c (revision 7217)
+++ libavutil/mem.c (working copy)
@@ -105,19 +105,40 @@
{
#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);
+ 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))
+ return ptr;
+ memcpy(p2, ptr, size);
+ free(ptr);
+ return p2;
#endif
}
More information about the ffmpeg-devel
mailing list