Date: Sun, 26 Aug 2001 20:38:38 +0200 From: Erik Trulsson <ertr1013@student.uu.se> To: Zhihui Zhang <zzhang@cs.binghamton.edu> Cc: freebsd-hackers@freebsd.org Subject: Re: ata0-master: non aligned DMA transfer attempted Message-ID: <20010826203838.A62752@student.uu.se> In-Reply-To: <Pine.SOL.4.21.0108261415301.9351-100000@opal> References: <Pine.SOL.4.21.0108261415301.9351-100000@opal>
next in thread | previous in thread | raw e-mail | index | archive | help
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 ertr1013@student.uu.se To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010826203838.A62752>