On Sun, Aug 26, 2001 at 02:16:12PM -0400, Zhihui Zhang wrote:
>
> Thanks for your replay. I use gdb to find out that the buffer address is
> not 16-byte aligned. This leads to a question as to how to align a
> statically allocated data structure properly. Using union seems to be able
> to align you on a long boundary (or even long long?), but that is not 16
> byte aligned.
>
> union {
> my_data_structure_t xyz;
> long pad;
> }
>
> The natural alignment seems to work only on primitive data types. If you
> define:
>
> unsigned char sector_buf[512];
>
> It will not always be aligned on a 512 byte boundary, even 16-byte
> alignment is not guaranteed. Is there a way to achieve this?
>
Not in 100% portable manner.
One way to achieve this would be to do in the same way as with
malloc()ed data: Allocate more memory than necessary and then do the
aligning manually.
Eg.
unsigned char unaligned_buf[1023];
unsigned char * aligned_ptr;
...
aligned_ptr = (unsigned char *) (((unsigned long)(unaligned_buf + 511)) & (~511UL));
/* Now aligned_ptr should point to a 512-byte buffer allocated on a
512-byte boundary. */
(This code assumes that an unsigned long can hold a pointer. This is
not necessarily true. I also haven't tested the code so be careful.)
Another way might be to use a gcc-specific extension:
unsigned char buf[512] __attribute__ ((aligned (512)));
This does depend on some support from the linker so it might not work.
(No, I haven't tested this either :-) )
--
<Insert your favourite quote here.>
Erik Trulsson
[EMAIL PROTECTED]
To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message