Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
* Data structure alignment question
@ 2003-03-20  0:36 Marc S. Gibian
  2003-03-20 16:59 ` Brian Haley
  2003-03-20 19:55 ` David Konerding
  0 siblings, 2 replies; 7+ messages in thread
From: Marc S. Gibian @ 2003-03-20  0:36 UTC (permalink / raw)


I am porting a program that runs on a number of other Unix/Linux
platforms to Tru64 5.1, GCC 3.2.2. This program interchanges messages
via sockets with other systems. Thus, the data alignment in
structures/classes is critical. To my surprise, I find that my data is
begin laid out more compactly than on the other platforms. It seems
like there must be a pragma or other mechanism within GCC that would
allow me to instruct the compiler to align the data on long word (32
bit) boundries. If not, I'm going to be stuck adding pad fields to a
non-trival set of structures.

Thanks in advance for your help,
Marc


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

* Re: Data structure alignment question
  2003-03-20  0:36 Data structure alignment question Marc S. Gibian
@ 2003-03-20 16:59 ` Brian Haley
  2003-03-20 19:55 ` David Konerding
  1 sibling, 0 replies; 7+ messages in thread
From: Brian Haley @ 2003-03-20 16:59 UTC (permalink / raw)


Marc S. Gibian wrote:
> I am porting a program that runs on a number of other Unix/Linux
> platforms to Tru64 5.1, GCC 3.2.2. This program interchanges messages
> via sockets with other systems. Thus, the data alignment in
> structures/classes is critical. To my surprise, I find that my data is
> begin laid out more compactly than on the other platforms. It seems
> like there must be a pragma or other mechanism within GCC that would
> allow me to instruct the compiler to align the data on long word (32
> bit) boundries. If not, I'm going to be stuck adding pad fields to a
> non-trival set of structures.

Hi Marc,

It would help if you posted at least one of the data structures
in question.  Also, on Alpha, longword alignment is 64-bits, not
32, maybe that's the problem?  I would also recommend reading
the Programmers Guide, Section 2 on the "Compiling System", it
explains data structure alignment on Tru64 Unix.

http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V51B_HTML/ARH9VDTE/TITLE.HTM

-Brian Haley, Tru64 Unix Networking




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

* Re: Data structure alignment question
  2003-03-20  0:36 Data structure alignment question Marc S. Gibian
  2003-03-20 16:59 ` Brian Haley
@ 2003-03-20 19:55 ` David Konerding
  2003-03-21 20:48   ` Marc S. Gibian
  1 sibling, 1 reply; 7+ messages in thread
From: David Konerding @ 2003-03-20 19:55 UTC (permalink / raw)


In article <8df11a0.0303191636.389f8f0c@posting.google.com>, Marc S. Gibian wrote:
> I am porting a program that runs on a number of other Unix/Linux
> platforms to Tru64 5.1, GCC 3.2.2. This program interchanges messages
> via sockets with other systems. Thus, the data alignment in
> structures/classes is critical. To my surprise, I find that my data is
> begin laid out more compactly than on the other platforms. It seems
> like there must be a pragma or other mechanism within GCC that would
> allow me to instruct the compiler to align the data on long word (32
> bit) boundries. If not, I'm going to be stuck adding pad fields to a
> non-trival set of structures.

Hi,

The "info" pages, which are included with the gcc distribution, have
a page on this, "Variable attributes" which explain how to specify the
alignment and packing of structures.

However, your message seems to suggest that you are writing the binary
data of a structure directly over the network.  Is that correct?  If so,
you should be aware many people consider this a poor solution because
it's not very portable.  I always use some intermediate data encoding
which ensures portable behavior (in my case CORBA, but other data
encoders exist).

Dave


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

* Re: Data structure alignment question
  2003-03-20 19:55 ` David Konerding
@ 2003-03-21 20:48   ` Marc S. Gibian
  2003-03-21 23:21     ` David Konerding
  0 siblings, 1 reply; 7+ messages in thread
From: Marc S. Gibian @ 2003-03-21 20:48 UTC (permalink / raw)


David Konerding <dek@compbio.berkeley.edu> wrote in message news:<slrnb7k7on.qv.dek@wuzzy.berkeley.edu>...
 
> The "info" pages, which are included with the gcc distribution, have
> a page on this, "Variable attributes" which explain how to specify the
> alignment and packing of structures.

I have attempted to follow these instructions and things still just
did not seem to work... see below for more details.
 
> However, your message seems to suggest that you are writing the binary
> data of a structure directly over the network.  Is that correct?  If so,
> you should be aware many people consider this a poor solution because
> it's not very portable.  I always use some intermediate data encoding
> which ensures portable behavior (in my case CORBA, but other data
> encoders exist).

Rather than get into the merits of binary vs. text based protocols,
this is not an open issue for my current effort because I am porting
software that has been around for quite a while, with a significant
installed base. Thus, we need to avoid changing the protocol to
maintain compatibility with the existing installed product.

One example of the structures that I am working with are:

class base {
   long one;
   unsigned short two;
};

class msg : public base {
   char three[size];
};

When used on Tru64, three is offset 6 bytes from the start of the msg
class. For all other platforms this software runs on, three is offset
eight bytes from the start.

I have tried:

class base {
   long one __attribute__ ((aligned(4)));
   unsigned short two __attribute__ ((aligned(4)));
} __attribute__ ((aligned(4)));

class msg : public base {
   char three[size] __attribute__ ((aligned(4)));
} __attribute__ ((aligned(4)));

and get the same result. I have tried just about every permutation I
can think of and continue to get exactly the same result.

I originally thought I would have to pack things on Tru64 since its
natural alignment is 8 byte boundaries. But, the actual results I am
seeing are that these classes are already being packed more tightly
than I want, so that seems the wrong angle to pursue.

The only solution I've found thus far is to manually pad as:

class base {
   long one;
   unsigned short two;
   short pad;
}

This works just fine, though it means I have to manually go through
all of my classes and check their alignment on Tru64, not a trivial
task.

Thanks again for your help with this.

-Marc


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

* Re: Data structure alignment question
  2003-03-21 20:48   ` Marc S. Gibian
@ 2003-03-21 23:21     ` David Konerding
  2003-03-24 19:49       ` Marc S. Gibian
  0 siblings, 1 reply; 7+ messages in thread
From: David Konerding @ 2003-03-21 23:21 UTC (permalink / raw)


In article <8df11a0.0303211248.6d44bd12@posting.google.com>, Marc S. Gibian wrote:
> David Konerding <dek@compbio.berkeley.edu> wrote in message news:<slrnb7k7on.qv.dek@wuzzy.berkeley.edu>...
>  
>> The "info" pages, which are included with the gcc distribution, have
>> a page on this, "Variable attributes" which explain how to specify the
>> alignment and packing of structures.
> 
> I have attempted to follow these instructions and things still just
> did not seem to work... see below for more details.
>  
>> However, your message seems to suggest that you are writing the binary
>> data of a structure directly over the network.  Is that correct?  If so,
>> you should be aware many people consider this a poor solution because
>> it's not very portable.  I always use some intermediate data encoding
>> which ensures portable behavior (in my case CORBA, but other data
>> encoders exist).
> 
> Rather than get into the merits of binary vs. text based protocols,
> this is not an open issue for my current effort because I am porting
> software that has been around for quite a while, with a significant
> installed base. Thus, we need to avoid changing the protocol to
> maintain compatibility with the existing installed product.

I appreciate your need to maintain protocol compatibility.

However, my point was to avoid taking a struct, and writing it over the
network directly.  Attempting to get a struct to have the alignment of
fields you need by using compiler options is iffy.  Even if you don't use
a platform-neutral data encoder like XDR or CORBA, you can achieve layout
of fields in memory without using structs- you have to do the work by
hand, but this is more reliable and portable.  Effectively, you end up
writing a function which "packs" a struct into a binary representation
that is equivalent to your protocol format, and another function which
"unpacks" a struct.  Is source compatibility still a requirement?
You would have to rewrite some core routines to handle this.

Dave


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

* Re: Data structure alignment question
  2003-03-21 23:21     ` David Konerding
@ 2003-03-24 19:49       ` Marc S. Gibian
  2003-04-16  1:05         ` Marc S. Gibian
  0 siblings, 1 reply; 7+ messages in thread
From: Marc S. Gibian @ 2003-03-24 19:49 UTC (permalink / raw)


David Konerding <dek@compbio.berkeley.edu> wrote in message news:<slrnb7n873.5ge.dek@wuzzy.berkeley.edu>...
> > Rather than get into the merits of binary vs. text based protocols,
> > this is not an open issue for my current effort because I am porting
> > software that has been around for quite a while, with a significant
> > installed base. Thus, we need to avoid changing the protocol to
> > maintain compatibility with the existing installed product.
> 
> I appreciate your need to maintain protocol compatibility.
> 
> However, my point was to avoid taking a struct, and writing it over the
> network directly.  Attempting to get a struct to have the alignment of
> fields you need by using compiler options is iffy.  Even if you don't use
> a platform-neutral data encoder like XDR or CORBA, you can achieve layout
> of fields in memory without using structs- you have to do the work by
> hand, but this is more reliable and portable.  Effectively, you end up
> writing a function which "packs" a struct into a binary representation
> that is equivalent to your protocol format, and another function which
> "unpacks" a struct.  Is source compatibility still a requirement?
> You would have to rewrite some core routines to handle this.

Yup... I also need to maintain source compatibility, primarily due to
the large size of the existing code base and the "small" effort being
directed to the current porting effort. Thus far, it appears that my
one padding addition from which my example was drawn has addressed the
major issues. There are probably other cases that have not been
surfaced yet, but that's the nature of any porting activity.

My preference for a distributed application such as this one would
have been to use some kind of middleware to handle the messaging, and
with it all of the encoding and alignment issues. I suspect CORBA
would be overkill, and I am unfamiliar with XDR. I'd have loved to use
a universally available RPC (once that was DCE RPC which was
everywhere and interoperated with Microsoft RPC), but RPC seems out of
vogue now. All of the new XML relatives seem poorly suited to this
particular application since it includes very high volume data
transfer (> 1 million data records could conceivably be getting moved
between the server and client involved), so its text based format
would introduce far more inefficiency than this application could
accept. DRDA, the wire protocol standard that is used for DB2, seems a
reasonable approach, though it would not leave room for application
specific messaging, which IS a requirement. I could see something like
the old ASN.1 based self-describing data encoding, though that takes a
sophisticated parser that probably is not available any longer.

So, while it is not a possibility for my current task to change the
protocol, I do wonder what would be a good engineering choice were I
designing this application from scratch?

In the meantime, anyone have any thoughts on my alignment problem? Or
am I stuck doing by-hand padding/alignment?

Thanks for your thoughts and help,
Marc


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

* Re: Data structure alignment question
  2003-03-24 19:49       ` Marc S. Gibian
@ 2003-04-16  1:05         ` Marc S. Gibian
  0 siblings, 0 replies; 7+ messages in thread
From: Marc S. Gibian @ 2003-04-16  1:05 UTC (permalink / raw)


Interestingly, today I ran into a new occurance of this alignment
problem on a platform that had previously been aligning properly. The
only change that I know of was the the system was reinstalled after a
disk crash and I installed GCC 3.2.2 rather than its much older GCC
2.7.? that it was using before the crash. To make things even more
interesting, this platform is running SCO OpenServer 5.0.5 on a I686
platform, so the alignment issue is happening in this case on a 32-bit
machine. This suggests rather strongly that the problem I've been
having on Tru64 is not a matter of porting to our first 64-bit
platform, but rather an issue with our first (and now second) GCC
3.2.2 platform.


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

end of thread, other threads:[~2003-04-16  1:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-03-20  0:36 Data structure alignment question Marc S. Gibian
2003-03-20 16:59 ` Brian Haley
2003-03-20 19:55 ` David Konerding
2003-03-21 20:48   ` Marc S. Gibian
2003-03-21 23:21     ` David Konerding
2003-03-24 19:49       ` Marc S. Gibian
2003-04-16  1:05         ` Marc S. Gibian

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