This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Inquiry on Alignment Requirements
- From: Andrew Haley <aph at redhat dot com>
- To: Patrick Rutkowski <rutski89 at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Mon, 03 Aug 2009 12:16:43 +0100
- Subject: Re: Inquiry on Alignment Requirements
- References: <4A762854.3010501@gmail.com>
Patrick Rutkowski wrote:
> I'm in the process of writing a memory allocator for a project of
> mine; needless to say, GCC is one of the targeted compilers.
>
> I find myself having to think about what memory alignment rules to
> guarantee for pointers returned from my allocator. I know that some
> subset of instructions on some architectures work much faster when
> operating on properly aligned data. Furthermore, I know that other
> such instructions flat out fail to work if operating on improperly
> aligned data.
>
> The question is: does GCC use such instructions? I don't want to
> return poorly aligned data from my alloc() function and have GCC
> try to use an aligned-only instruction it, resulting in things
> exploding at runtime.
>
> __alignof__ seems like it might be of some help:
> http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Alignment.html#Alignment
> But unfortunately it doesn't tell me what the _strictest_ alignment
> rule is, which is what I need to know to write a generic memory
> allocator.
>
> Does GCC document or otherwise make known what minimum alignment it
> expects on each platform?
Yes. There's a target macro, BIGGEST_ALIGNMENT:
-- Macro: BIGGEST_ALIGNMENT
Biggest alignment that any data type can require on this machine,
in bits. Note that this is not the biggest alignment that is
supported, just the biggest alignment that, when violated, may
cause a fault.
I think you'll have to look in the gcc source for this: I'm not sure
gcc exports it. Also, I'm not sure if BIGGEST_ALIGNMENT is going to be
big enough for vector types.
Andrew.