From: alan@nabeth.cxo.dec.com (Alan Rollow - Dr. File System's Home for Wayward Inodes.)
Subject: Re: Funny alignment with structures ?
Date: 6 Dec 1994 19:20:51 GMT [thread overview]
Message-ID: <3c2dij$268@nntpd2.cxo.dec.com> (raw)
In-Reply-To: 3c1slb$qb3@newsstand.cit.cornell.edu
In article <3c1slb$qb3@newsstand.cit.cornell.edu>, olin@cheme.cornell.edu (Steve Thompson) writes:
>
>> [ Example deleted, but it showed a structure having poorly
designed alignment for the requires of most RISC systems. ]
>>Could someone please tell me how I can get around this funny alignment
>>arrangement ?
>
>Count the bytes: the U32 field has two U8 fields and eight U16 fields
>before it. Thats 2*1 + 8*2 = 18 bytes. Now since 18 is not a multiple
>of 4 (the size of a U32), you get padding.
To expand... Most RISC systems, the Alpha being no exception,
require particular alignment of data objects. Bytes can start
on any byte boundary. Shorts must start on a 2 byte boundary,
32 bits in on 4 byte boundaries and longs on 8 byte boundaries.
The compilers enforce this alignment, to ensure that no traps
are required to realign the data at run-time.
You may be able to override some of the realignment enforced by
the compiler, using compiler options (-Zp looks likely for the
OSF C compiler). But the hardware still won't like it and you're
likely to get unaligned access trap messages. You can fix this
by realigning the data by hand if stuck with that particular data
format, but it will use some program time to do it.
I use something like the following:
convert(char *bytes)
{
union {
char bytes[4] ;
int word ;
} tmp ;
tmp.bytes[0] = bytes[0] ;
tmp.bytes[1] = bytes[1] ;
tmp.bytes[2] = bytes[2] ;
tmp.bytes[3] = bytes[3] ;
return tmp.word ;
}
If you know the incoming data alignment, you can adjust the algorithm
accordingly; using two shorts instead of four bytes.
>
>-steve
>/sig
>
>---------------------------------------------------------------------------
>Steve Thompson, System Mangler Internet: thompson@cheme.cornell.edu
--
Alan Rollow alan@nabeth.cxo.dec.com
next prev parent reply other threads:[~1994-12-06 19:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
1994-12-06 5:28 Funny alignment with structures ? Anthony
1994-12-06 14:32 ` Steve Thompson
1994-12-06 19:20 ` Alan Rollow - Dr. File System's Home for Wayward Inodes. [this message]
1994-12-07 6:56 ` Tim Bartley
1994-12-07 23:58 ` Anthony
1994-12-08 1:31 ` Anthony
1994-12-07 14:50 ` Stefan Monnier
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