From owner-freebsd-hackers Sun Aug 26 11:39: 2 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from maila.telia.com (maila.telia.com [194.22.194.231]) by hub.freebsd.org (Postfix) with ESMTP id AC8FE37B401 for ; Sun, 26 Aug 2001 11:38:56 -0700 (PDT) (envelope-from ertr1013@student.uu.se) Received: from d1o913.telia.com (d1o913.telia.com [195.252.44.241]) by maila.telia.com (8.11.2/8.11.0) with ESMTP id f7QIctQ27762 for ; Sun, 26 Aug 2001 20:38:55 +0200 (CEST) Received: from ertr1013.student.uu.se (h185n2fls20o913.telia.com [212.181.163.185]) by d1o913.telia.com (8.8.8/8.8.8) with SMTP id UAA12521 for ; Sun, 26 Aug 2001 20:38:53 +0200 (CEST) Received: (qmail 80231 invoked by uid 1001); 26 Aug 2001 18:38:39 -0000 Date: Sun, 26 Aug 2001 20:38:38 +0200 From: Erik Trulsson To: Zhihui Zhang Cc: freebsd-hackers@freebsd.org Subject: Re: ata0-master: non aligned DMA transfer attempted Message-ID: <20010826203838.A62752@student.uu.se> Mail-Followup-To: Zhihui Zhang , freebsd-hackers@freebsd.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.3.21i Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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 :-) ) -- Erik Trulsson ertr1013@student.uu.se To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message