Re: [PATCH] Optimize generic get_unaligned / put_unalignedimplementations.

From: Marcel Holtmann
Date: Thu Feb 15 2007 - 03:36:03 EST


Hi Andrew,

> > +#define get_unaligned(ptr) \
> > +({ \
> > + const struct { \
> > + union { \
> > + const int __un_foo[0]; \
> > + const __typeof__(*(ptr)) __un; \
> > + } __un __attribute__ ((packed)); \
> > + } * const __gu_p = (void *) (ptr); \
> > + \
> > + __gu_p->__un.__un; \
> > })
>
> Can someone please tell us how this magic works? (And it does appear to
> work).
>
> It seems to assuming that the compiler will assume that members of packed
> structures can have arbitrary alignment, even if that alignment is obvious.
>
> Which makes sense, but I'd like to see chapter-and-verse from the spec or
> from the gcc docs so we can rely upon it working on all architectures and
> compilers from now until ever more.

I am far away from having any knowledge about the GCC internals and the
reason why this code works, but I've been told the generic way of
handling unaligned access is this:

#define get_unaligned(ptr) \
({ \
struct __attribute__((packed)) { \
typeof(*(ptr)) __v; \
} *__p = (void *) (ptr); \
__p->__v; \
})

#define put_unaligned(val, ptr) \
do { \
struct __attribute__((packed)) { \
typeof(*(ptr)) __v; \
} *__p = (void *) (ptr); \
__p->__v = (val); \
} while(0)

Actually I am using this code in the Bluetooth userspace library for
over two years now without any complaints.

Regards

Marcel


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/