Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
From: Michal Jaegermann <michal@ellpspace.math.ualberta.ca>
To: axp-list@redhat.com
Subject: Re: unaligned traps question
Date: Fri, 18 Oct 2002 12:47:52 -0600	[thread overview]
Message-ID: <20021018124752.A3353@ellpspace.math.ualberta.ca> (raw)
In-Reply-To: <02bc01c276ca$4cbcfb00$35de2b09@Viswanadha>; from ram@jtcsv.com on Fri, Oct 18, 2002 at 10:17:55AM -0700

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




  reply	other threads:[~2002-10-18 18:48 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-15  0:50 unaligned traps question Ram Viswanadha
2002-10-15  1:38 ` Michal Jaegermann
2002-10-15  2:03 ` Uncle George
2002-10-15 15:39   ` George France
2002-10-15 16:46     ` Michal Jaegermann
2002-10-15 18:24       ` alvin
2002-10-15 19:33         ` George France
2002-10-15 19:40         ` Falk Hueffner
     [not found]         ` <3DAC6EF2.BC962839@voicenet.com>
2002-10-16 18:15           ` alvin
2002-10-16 19:18             ` George France
2002-10-16 19:45               ` Falk Hueffner
2002-10-16 20:08                 ` George France
2002-10-17 18:37                   ` Falk Hueffner
2002-10-16 20:23             ` Michal Jaegermann
2002-10-16 21:30               ` alvin
2002-10-17  1:47             ` Uncle George
2002-10-15 18:08     ` Uncle George
2002-10-15 19:29       ` George France
2002-10-15 20:10         ` Uncle George
2002-10-15 20:46           ` George France
2002-10-15 15:33 ` George France
2002-10-17 22:17   ` Ram Viswanadha
2002-10-18 15:36     ` Michal Jaegermann
2002-10-18 17:17       ` Ram Viswanadha
2002-10-18 18:47         ` Michal Jaegermann [this message]
     [not found] <673E0A4C-E1DF-11D6-AC80-000393768D2C@mcgillsociety.org>
2002-10-17 15:50 ` George France
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox