From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from int-mx1.corp.redhat.com (int-mx1.corp.redhat.com [172.16.52.254]) by listman.redhat.com (Postfix) with ESMTP id 7255341E4F for ; Fri, 18 Oct 2002 14:48:01 -0400 (EDT) Received: (from mail@localhost) by int-mx1.corp.redhat.com (8.11.6/8.11.6) id g9IIm1B03265 for axp-list@listman.redhat.com; Fri, 18 Oct 2002 14:48:01 -0400 Received: from mx1.redhat.com (mx1.redhat.com [172.16.48.31]) by int-mx1.corp.redhat.com (8.11.6/8.11.6) with SMTP id g9IIm1f03258 for ; Fri, 18 Oct 2002 14:48:01 -0400 Received: from ellpspace.math.ualberta.ca (ellpspace.math.ualberta.ca [129.128.207.67]) by mx1.redhat.com (8.11.6/8.11.6) with SMTP id g9IIR1w17541 for ; Fri, 18 Oct 2002 14:27:01 -0400 Received: (from michal@localhost) by ellpspace.math.ualberta.ca (8.11.6/8.11.6) id g9IIlqg03431 for axp-list@redhat.com; Fri, 18 Oct 2002 12:47:52 -0600 Date: Fri, 18 Oct 2002 12:47:52 -0600 From: Michal Jaegermann To: axp-list@redhat.com Subject: Re: unaligned traps question Message-ID: <20021018124752.A3353@ellpspace.math.ualberta.ca> References: <01da01c273e4$d5c57320$35de2b09@Viswanadha> <02101511334803.01593@shadowfax.middleearth> <01d001c2762a$ecdbffa0$35de2b09@Viswanadha> <20021018093652.A2475@ellpspace.math.ualberta.ca> <02bc01c276ca$4cbcfb00$35de2b09@Viswanadha> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <02bc01c276ca$4cbcfb00$35de2b09@Viswanadha>; from ram@jtcsv.com on Fri, Oct 18, 2002 at 10:17:55AM -0700 X-Loop: axp-list@redhat.com Sender: axp-list-admin@redhat.com Errors-To: axp-list-admin@redhat.com X-BeenThere: axp-list@redhat.com X-Mailman-Version: 2.0.1 Precedence: bulk Reply-To: axp-list@redhat.com List-Help: List-Post: List-Subscribe: , List-Id: Linux and Red Hat on Alpha processors List-Unsubscribe: , List-Archive: On Fri, Oct 18, 2002 at 10:17:55AM -0700, Ram Viswanadha wrote: > How about the using the below code, we have tested it on all platforms that > we have ported to and it works, even on platforms like the mainframes. > > /** > * This should align the memory properly on any machine. > * This is very useful for the safeClone functions. > */ > typedef union { > long t1; > double t2; > void *t3; > } UAlignedMemory; I would expect this to work in any reasonable situation. I am not 100% sure without study of relevant documents if Standard _guarantees_ what you expect (anyone can clarify?) but I would be really surprised if that would fail somewhere. > > /** > * Get the amount of bytes that a pointer is off by from > * the previous aligned pointer > */ > #define U_ALIGNMENT_OFFSET(ptr) (((size_t)ptr) & (sizeof(UAlignedMemory) - 1)) ??? Are you relying here that sizeof(UAlignedMemory) is a power of two? How you can be sure of that? This is an implementation detail. > /** > * Get the amount of bytes to add to a pointer > * in order to get the next aligned address > */ > #define U_ALIGNMENT_OFFSET_UP(ptr) (sizeof(UAlignedMemory) - U_ALIGNMENT_OFFSET(ptr)) I am not sure what all of this is about. If you have ptr *UAlignedMemory; then '&ptr[5];', assuming that you "own" the referenced memory, will make a compiler to compute properly that address by itself. The same if you will use pointers to some of union members. Second-guessing pointer arithmetic is often a source of nasty, and non-obvious on the first look, bugs. More debugging fun if you will hide behind macros. :-) Michal