comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Record Elements and Endian Conversion
Date: Thu, 10 Jan 2013 20:47:57 +0200
Date: 2013-01-10T20:47:57+02:00	[thread overview]
Message-ID: <al8gmuFlnueU1@mid.individual.net> (raw)
In-Reply-To: <fbb1f71f-67c0-4452-896a-35ceca29365e@googlegroups.com>

On 13-01-10 19:31 , awdorrin wrote:
> I am migrating legacy code written for the PowerPC platform to X86,
> and have been running into Endian issues throughout the code.
> 
> In the latest issue I've discovered, a section of code in which a
> record has been defined that has 111 components of various data
> types.
> 
> Is there any mechanism within Ada that would provide a way to iterate
> across all components of a record, perhaps providing their data
> types?

One way, sort of, is to use stream I/O. The default (compiler-defined)
'Read, 'Write etc. procedures for the record type traverse the record
components in declaration order and call the 'Read or 'Write, etc.,
procedure for the component type, on the component.

If you override the 'Read etc. for the component types, but use the
default 'Read for the record type, the result is rather like an
iteration over the components that calls your 'Read, etc., and then you
can do what you like with the component values.

> Problem I have is that this record is used (via a MEMCPY call) to
> populate a buffer used to transmit an message via Ethernet.

Argh.

> While I
> have adjusted the record's representation clause to put the data
> fields of the record into the correct bit location, those fields
> larger than a byte are of the wrong Endianness within the buffer.

I would either swap bytes in the components, using for example bindings
to the libc functions htonl, htons, or build the buffer using stream
output, as above, with the 'Write procedure for the component types
written to produce network order. (Since stream 'Write does not depend
on the bit-wise representation of the record, you can remove the rep
spec for the record type if you use the 'Write method.)

I believe some new Gnat versions have a Gnat-specific attribute to set
the endianness of record representations -- and not only the
bit-numbering order -- which makes the compiler actually swap endianness
when a record component is assigned, if the component has the non-native
endianness.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
      .      @       .



  parent reply	other threads:[~2013-01-10 18:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-10 17:31 Record Elements and Endian Conversion awdorrin
2013-01-10 18:32 ` Dmitry A. Kazakov
2013-01-10 18:47 ` Niklas Holsti [this message]
2013-01-10 19:40 ` awdorrin
2013-01-11 14:44 ` Marc C
replies disabled

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