[FFmpeg-devel] [PATCH] av_malloc() workaround for QNX platform

Hendrik Leppkes h.leppkes at gmail.com
Thu Feb 7 15:40:42 CET 2013


On Thu, Feb 7, 2013 at 3:31 PM, Mike Gorchak <mike.gorchak.qnx at gmail.com> wrote:
> Hi,
>
> I've attached a patch for QNX which resolves issues of memalign() and
> posix_memalign() functions. I know this looks like a dirty hack, but this
> is the only way to workaround current issues.
>
> By the way I have found a bug in the av_malloc() function when
> CONFIG_MEMALIGN_HACK is used. I've tried it instead on QNX
> instead of posix_memalign()/memaligh() functions. The code looks like:
>
> #if CONFIG_MEMALIGN_HACK
>     ptr = malloc(size + ALIGN);
>     if (!ptr)
>         return ptr;
>     diff              = ((~(long)ptr)&(ALIGN - 1)) + 1;
>     ptr               = (char *)ptr + diff;
>     ((char *)ptr)[-1] = diff;
>
> After malloc() there is needed a check for ptr which can be ALREADY
> aligned. In this case we have not to execute this line: ((char
> *)ptr)[-1] = diff; which can cause a memory trashing. But better to
> allocate (size+2*ALIGN) bytes and then move ptr forward for at least ALIGN
> bytes to make sure that buffer has spare space for writting a 'diff'
> value.
>

I looked through the memalign hack code, and it seems to work as expected.
If ptr returned by malloc is already aligned, diff will equal ALIGN,
and the returned pointer is advanced by ALIGN, and there is plenty
space to write the diff.

Regarding your patch, it indeed looks terribly hacky. Such
inconsistent behavior should maybe simply use the memalign hack and be
done with it?


More information about the ffmpeg-devel mailing list