From: Pierre Ossman <drzeus-list@drzeus.cx>
To: Luciano Rocha <strange@nsk.no-ip.org>
Cc: Daniel Drake <dsd@gentoo.org>,
linux-kernel@vger.kernel.org, davem@davemloft.net,
kune@deine-taler.de, johannes@sipsolutions.net
Subject: Re: [RFC] Documentation about unaligned memory access
Date: Sat, 24 Nov 2007 17:19:31 +0100 [thread overview]
Message-ID: <20071124171931.699aa1ff@poseidon.drzeus.cx> (raw)
In-Reply-To: <20071124155052.GA15440@bit.office.eurotux.com>
On Sat, 24 Nov 2007 15:50:52 +0000
Luciano Rocha <strange@nsk.no-ip.org> wrote:
>
> Dumb memcpy (while (len--) { *d++ = *s++ }) will have alignment problems
> in any case. Intelligent ones, like the one provided in glibc, first copy
> bytes till output is aligned (C file) *or* size is a multiple (i686 asm file)
> of word size, and then it copies word-by-word.
>
> Linux's x86_64 memcpy does the opposite, copies 64bit words, and then
> copies the last bytes.
>
> So, in effect, as long as no packed structures are used, memcpy should
> be safer on *int, etc., than *char, as the compiler ensures
> word-alignment.
>
It most certainly does not. gcc will assume that an int* has int alignment. memcpy() is a builtin, which gcc can translate to pretty much anything. And C specifies that a pointer to foo, will point to a real object of type foo, so gcc can't be blamed for the unsafe typecasts. I have tested this the hard way, so this is not just speculation.
E.g., we have the following struct:
struct foo
{
u8 a[4];
u32 b;
};
This struct will have a size of 8 bytes and an alignment of 4 bytes (caused by the member b). Now take the following code:
void copy_foo(struct foo *dst, struct foo *src)
{
*dst = *src;
}
On a platform that supports 64-bit loads and stores (e.g. AVR32, where I got hit by this), this will generate:
LD r1, (src)
ST r1, (dst)
Now if I replace that with:
void copy_foo(struct foo *dst, struct foo *src)
{
memcpy(dst, src, sizeof(struct foo));
}
then it will generate the same code. So I cannot use copy_foo() to transfer a struct foo either out of, or into a packet buffer.
In other words, memcpy() does _not_ save you from alignment issues. If you cast from char* or void* to something else, you better be damn sure the alignment is correct because gcc will assume it is.
Rgds
--
-- Pierre Ossman
Linux kernel, MMC maintainer http://www.kernel.org
PulseAudio, core developer http://pulseaudio.org
rdesktop, core developer http://www.rdesktop.org
next prev parent reply other threads:[~2007-11-24 16:19 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-23 0:15 Daniel Drake
2007-11-23 0:27 ` Avuton Olrich
2007-11-23 1:29 ` Alan Cox
2007-11-23 3:04 ` Kyle Moffett
2007-11-23 6:18 ` dean gaudet
2007-11-23 9:46 ` Arne Georg Gleditsch
2007-11-26 14:50 ` dean gaudet
2007-11-23 1:29 ` David Miller
2007-11-23 10:06 ` Jan Engelhardt
2007-11-23 11:43 ` Heikki Orsila
2007-11-25 11:16 ` Geert Uytterhoeven
2007-11-25 11:24 ` Heikki Orsila
2007-11-26 0:48 ` Alan Cox
2007-11-26 17:12 ` Ben Dooks
2007-11-27 7:51 ` Kumar Gala
2007-11-23 22:02 ` Vadim Lobanov
2007-11-23 22:52 ` Dmitri Vorobiev
2007-11-24 13:34 ` Pierre Ossman
2007-11-24 15:50 ` Luciano Rocha
2007-11-24 16:19 ` Pierre Ossman [this message]
2007-11-24 17:22 ` Luciano Rocha
2007-11-24 17:35 ` Pierre Ossman
2007-11-24 18:28 ` Luciano Rocha
2007-11-24 17:53 ` Haavard Skinnemoen
2007-11-25 8:27 ` Denys Vlasenko
2007-11-25 14:13 ` Olaf Titz
2007-11-26 9:14 ` DM
2007-11-26 14:47 ` Johannes Berg
2007-11-27 0:40 ` Arnaldo Carvalho de Melo
2007-11-26 14:51 ` Johannes Berg
2007-11-30 8:18 ` Jörn Engel
[not found] <fa.U+CIv4JClOmn6KppLkEOSk7RW0Y@ifi.uio.no>
2007-11-23 1:24 ` Robert Hancock
2007-11-23 2:07 ` Andi Kleen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20071124171931.699aa1ff@poseidon.drzeus.cx \
--to=drzeus-list@drzeus.cx \
--cc=davem@davemloft.net \
--cc=dsd@gentoo.org \
--cc=johannes@sipsolutions.net \
--cc=kune@deine-taler.de \
--cc=linux-kernel@vger.kernel.org \
--cc=strange@nsk.no-ip.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome