Re: [Bluez-devel] Alignment issue

David Woodhouse
2005-01-20T14:30:53Z

[-- Attachment #1: Type: text/plain, Size: 965 bytes --]

On Wed, 2004-08-11 at 09:13 +0200, Marcel Holtmann wrote:
> > The attached patch should fix the problem (it does as far as I can tell).
> > The compiler probably thinks both arguments to memcpy() are aligned and so
> > makes an optimization which breaks the copy (because the destination is not
> > actually aligned). Perhaps this is a compiler bug... I'm using gcc 3.3.3.
> > After googling and reading some discussion on the topic it seems that it's
> > probably not a compiler bug. I'll leave that for someone else to decide. :)
>
> after checking some more resources, you may be right and the compiler
> optimizes here where it should not. I found a comment about gcc and
> using its __builtin_memcpy. Try to replace the memcpy with memmove and
> see if this works.

That isn't guaranteed to work either. This is the patch I've been
testing in the Fedora RPM since August. It lets the compiler know what's
happening so it can generate optimal code.

--
dwmw2

[-- Attachment #2: bluez-libs-2.10-align.patch --]
[-- Type: text/x-patch, Size: 1359 bytes --]

--- bluez-libs-2.10/include/bluetooth.h.align 2004-08-16 20:30:12.998705139 +0100
+++ bluez-libs-2.10/include/bluetooth.h 2004-08-16 20:33:14.933543394 +0100
@@ -88,17 +88,21 @@
#endif

/* Bluetooth unaligned access */
-#if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) || defined(__s390__) || defined(__cris__)
-#define bt_get_unaligned(ptr) (*(ptr))
-#define bt_put_unaligned(val, ptr) ((void)( *(ptr) = (val) ))
-#else
-#define bt_get_unaligned(ptr) \
- ({ __typeof__(*(ptr)) __tmp; memmove(&__tmp, (ptr), sizeof(*(ptr))); __tmp; })
-#define bt_put_unaligned(val, ptr) \
- ({ __typeof__(*(ptr)) __tmp = (val); \
- memmove((ptr), &__tmp, sizeof(*(ptr))); \
- (void)0; })
-#endif
+#define bt_get_unaligned(ptr) \
+({ \
+ struct __attribute__((packed)) { \
+ typeof(*(ptr)) __v; \
+ } *__p = (ptr); \
+ __p->__v; \
+})
+
+#define bt_put_unaligned(val, ptr) \
+do { \
+ struct __attribute__((packed)) { \
+ typeof(*(ptr)) __v; \
+ } *__p = (ptr); \
+ __p->__v = (val); \
+} while(0)

/* BD Address */
typedef struct {