Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* Funny alignment with structures ?
@ 1994-12-06  5:28 Anthony
  1994-12-06 14:32 ` Steve Thompson
  1994-12-07 14:50 ` Stefan Monnier
  0 siblings, 2 replies; 7+ messages in thread
From: Anthony @ 1994-12-06  5:28 UTC (permalink / raw)


In the following data structure:
U8 unsigned 8 bit
U16 unsigned 16 bit
U32 unsigned 32 bit

typedef struct {
	U8	major_version;		/* Version - major */
	U8	minor_version;		/*         - minor */
	U16	flags;			/* Control flags */
	U16	entry_size;		/* Size of entries */
	U16	entry_count;		/* Number of entries */
	U16	next_entry;		/* Next entry number to be filled */
	U16	start_par;		/* PAR of start of buffer data */
	U16	end_par;		/* PAR past end of buffer data */
	U16	next_par;		/* PAR of next entry rel to start of event file */
	U16	next_offset;		/* Offset in PAR of next entry */
	U32 	seqno;		/* Entry sequence number */
	U16	updates;		/* Entry update counter */
	U16	timeh;			/* Time of last update */
	U16	timel;
	U16	date;			/* Date of last update */
	U8	spare[34];		/* Header is 64 bytes */
} MUXEVB_HEADER;

I thought the alignment would be like this:

	16bits	      16bits	    16bits	    16bits
_______________________________________________________________
|major v|minor v|    flags     | entry_size    | entry_count   |
----------------------------------------------------------------
| next_entry    | start_par    | end_par       | next_par      |
----------------------------------------------------------------
| next_offset   |             seqno            | updates       |
----------------------------------------------------------------
 ..........


but I am getting this alignment

	16bits	      16bits	    16bits	    16bits
_______________________________________________________________
|major v|minor v|    flags     | entry_size    | entry_count   |
----------------------------------------------------------------
| next_entry    | start_par    | end_par       | next_par      |
----------------------------------------------------------------
| next_offset   |                    not used                  |
----------------------------------------------------------------
|             seqno            | updates       | .......
--------------------------------------------------
 ..........

Could someone please tell me how I can get around this funny alignment
arrangement ?

Thank you

Anthony Lee				These are my opinions and not SEQEB.
SEQEB					
150 Charlotte Street			
Brisbane
Qld 4000
Australia
voice:+61 7 223 4541
fax:  +61 7 221 7556
email: AL012@svtstu.seqeb.gov.au


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Funny alignment with structures ?
  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.
  1994-12-07 14:50 ` Stefan Monnier
  1 sibling, 1 reply; 7+ messages in thread
From: Steve Thompson @ 1994-12-06 14:32 UTC (permalink / raw)


In article <D0DJ7u.2A4@un.seqeb.gov.au>, al012@svtstu.seqeb.gov.au (Anthony) writes:
>In the following data structure:
>U8 unsigned 8 bit
>U16 unsigned 16 bit
>U32 unsigned 32 bit
>
>typedef struct {
>	U8	major_version;		/* Version - major */
>	U8	minor_version;		/*         - minor */
>	U16	flags;			/* Control flags */
>	U16	entry_size;		/* Size of entries */
>	U16	entry_count;		/* Number of entries */
>	U16	next_entry;		/* Next entry number to be filled */
>	U16	start_par;		/* PAR of start of buffer data */
>	U16	end_par;		/* PAR past end of buffer data */
>	U16	next_par;		/* PAR of next entry rel to start of event file */
>	U16	next_offset;		/* Offset in PAR of next entry */
>	U32 	seqno;		/* Entry sequence number */
>	U16	updates;		/* Entry update counter */
>	U16	timeh;			/* Time of last update */
>	U16	timel;
>	U16	date;			/* Date of last update */
>	U8	spare[34];		/* Header is 64 bytes */
>} MUXEVB_HEADER;
>
>I thought the alignment would be like this:
>[...]
>
>but I am getting this alignment
>[...]
>
>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.

-steve
/sig

---------------------------------------------------------------------------
Steve Thompson, System Mangler   Internet: thompson@cheme.cornell.edu
School of Chemical Engineering   Phone:    (607) 255 5573
Olin Hall, Cornell University    FAX:      (607) 255 9166
Ithaca NY 14853                  "Time is just one damn thing after another"
---------------------------------------------------------------------------


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Funny alignment with structures ?
  1994-12-06 14:32 ` Steve Thompson
@ 1994-12-06 19:20   ` Alan Rollow - Dr. File System's Home for Wayward Inodes.
  1994-12-07  6:56     ` Tim Bartley
  1994-12-07 23:58     ` Anthony
  0 siblings, 2 replies; 7+ messages in thread
From: Alan Rollow - Dr. File System's Home for Wayward Inodes. @ 1994-12-06 19:20 UTC (permalink / raw)



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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Funny alignment with structures ?
  1994-12-06 19:20   ` Alan Rollow - Dr. File System's Home for Wayward Inodes.
@ 1994-12-07  6:56     ` Tim Bartley
  1994-12-07 23:58     ` Anthony
  1 sibling, 0 replies; 7+ messages in thread
From: Tim Bartley @ 1994-12-07  6:56 UTC (permalink / raw)


alan@nabeth.cxo.dec.com writes
> 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 ;
> }
> 

You can also use the following construct:

#pragma member_alignment save
#pragma nomember_alignment
struct {
   [definitions for struct you want no padding for]
};
#pragma member_alignment restore

if you use the -migrate option on the C compiler. This gives you
access to the DEC C compiler (you may need to have the
CMPDEVENH200 subset installed). Check out the cc(1) man page for
more information on the -migrate switch.

The compiler will then generate the appropriate instruction sequences
so that unaligned accessed do not occur. The advantage of this
approach is that you can control the padding on a structure by
structure basis.

Hope this helps,

Tim.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Funny alignment with structures ?
  1994-12-06  5:28 Funny alignment with structures ? Anthony
  1994-12-06 14:32 ` Steve Thompson
@ 1994-12-07 14:50 ` Stefan Monnier
  1 sibling, 0 replies; 7+ messages in thread
From: Stefan Monnier @ 1994-12-07 14:50 UTC (permalink / raw)


In article <D0DJ7u.2A4@un.seqeb.gov.au>,
Anthony <al012@svtstu.seqeb.gov.au> wrote:
] I thought the alignment would be like this:
] 
] 	16bits	      16bits	    16bits	    16bits
] _______________________________________________________________
] |major v|minor v|    flags     | entry_size    | entry_count   |
] ----------------------------------------------------------------
] | next_entry    | start_par    | end_par       | next_par      |
] ----------------------------------------------------------------
] | next_offset   |             seqno            | updates       |
] ----------------------------------------------------------------
]  ..........

here, seqno is not on a 32bit boundary. It's hence incorrectly aligned.

] but I am getting this alignment
] 
] 	16bits	      16bits	    16bits	    16bits
] _______________________________________________________________
] |major v|minor v|    flags     | entry_size    | entry_count   |
] ----------------------------------------------------------------
] | next_entry    | start_par    | end_par       | next_par      |
] ----------------------------------------------------------------
] | next_offset   |                    not used                  |
] ----------------------------------------------------------------
] |             seqno            | updates       | .......
] --------------------------------------------------
]  ..........

That's a surprise, I would expect:

  | next_offset   |  not used    |            seqno              |

Since this is sufficient for seqno to be 32bits aligned. Are you sure
of the alignment you suggested ?

Anyway, you can't really avoid it. Either you move things around so
that the compiler doesn't have to insert blanks, or you split the
seqno field into two 16bits fields. Unless there's a magic compiler
option, but that would be a surprise !


	Stefan
-- 
--------------------------------------------------------------------
-- Suicidez-vous jeune, vous profiterez plus longtemps de la mort --
--------------------------------------------------------------------


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Funny alignment with structures ?
  1994-12-06 19:20   ` Alan Rollow - Dr. File System's Home for Wayward Inodes.
  1994-12-07  6:56     ` Tim Bartley
@ 1994-12-07 23:58     ` Anthony
  1994-12-08  1:31       ` Anthony
  1 sibling, 1 reply; 7+ messages in thread
From: Anthony @ 1994-12-07 23:58 UTC (permalink / raw)


In article <3c2dij$268@nntpd2.cxo.dec.com>, alan@nabeth.cxo.dec.com (Alan Rollow - Dr. File System's Home for Wayward Inodes.) says:

>
>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.
>

Currently, I am using the -Zp1 switch to get my alignment.  The
problem is that I am receiving a block of bits from a remote machine
and therefore I can't  rearrange the order of the components of
my struct.  I would have thought that a -Zp1 would cause the compiler
to generate code that would try and shift the bytes etc. ?



Anthony Lee				These are my opinions and not SEQEB.
SEQEB					
150 Charlotte Street			
Brisbane
Qld 4000
Australia
voice:+61 7 223 4541
fax:  +61 7 221 7556
email: AL012@svtstu.seqeb.gov.au


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Funny alignment with structures ?
  1994-12-07 23:58     ` Anthony
@ 1994-12-08  1:31       ` Anthony
  0 siblings, 0 replies; 7+ messages in thread
From: Anthony @ 1994-12-08  1:31 UTC (permalink / raw)


In article <D0Gt97.GzE@un.seqeb.gov.au>, al012@svtstu.seqeb.gov.au (Anthony) says:
>
>In article <3c2dij$268@nntpd2.cxo.dec.com>, alan@nabeth.cxo.dec.com (Alan Rollow - Dr. File System's Home for Wayward Inodes.) says:
>
>>
>>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.
>>
>
>Currently, I am using the -Zp1 switch to get my alignment.  The
>problem is that I am receiving a block of bits from a remote machine
>and therefore I can't  rearrange the order of the components of
>my struct.  I would have thought that a -Zp1 would cause the compiler
>to generate code that would try and shift the bytes etc. ?

Unfortunately, my -Zp1 switch seems to be causing my program to
crashed :-(.


Anthony Lee				These are my opinions and not SEQEB.
SEQEB					
150 Charlotte Street			
Brisbane
Qld 4000
Australia
voice:+61 7 223 4541
fax:  +61 7 221 7556
email: AL012@svtstu.seqeb.gov.au


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1994-12-08  1:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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.
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

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