[FFmpeg-devel] [PATCH 3/7] Add alias-safe union typedefs
Sun Jan 31 19:01:48 CET 2010
On Sun, 2010-01-31 at 17:25 +0100, Michael Niedermayer wrote:
> On Sun, Jan 31, 2010 at 05:08:37PM +0200, Uoti Urpala wrote:
> > A hard to avoid problem with any union usage with explicit types is that
> > it makes the macro less general. You need to know what types were used
> > to write to the memory. For example if you want to use the read macros
> > on a buffer that you just wrote to using put_bits.h inlined code, then
> > you need to know that the inlined code may have written data using the
> > type uint32_t, and a type matching that must be present in the union. If
> > no such type is present in the union then it's certainly not safe.
> I think you miss here that data would be written as type union uint32_t+X
Well then you must at least make sure writes are done that way before
you can use macros, which is an additional requirement compared to what
the macros are expected to do now.
> where X is the effective type of the array and it would be read as
> union of whatever+X. X being common so no aliassing arises in ISO C
The method cannot rely on only the standard in any case though if you
want to support arbitrary unaligned addresses. I think the only
standard-conforming way to read an int from an unaligned address without
parsing it a char at a time would be to memcpy it to an aligned address
and then read it from there.
More information about the ffmpeg-devel