comp.lang.ada
 help / color / mirror / Atom feed
From: Martin <martin.dowie@btopenworld.com>
Subject: Re: Cross-platform issues
Date: Tue, 8 Dec 2009 01:02:11 -0800 (PST)
Date: 2009-12-08T01:02:11-08:00	[thread overview]
Message-ID: <1767f89d-083c-49ff-a9d5-3d56ef05cd9a@e27g2000yqd.googlegroups.com> (raw)
In-Reply-To: hfk6s0$vn7$1@aioe.org

On Dec 8, 12:33 am, tmo...@acm.org wrote:
> > closely as possible to the one that inspired it.  The original
> > emits trace data with things like field length info in 4-byte
> > integers, for example. I suppose that using a derived type to
> > ensure that in my implementation those fields conform with the
> > original is one way to do it, but is that accepted practice, or
> > is there a more usual one?
>
>   Representation clauses and pragmas let you specify machine
> characteristics, for instance that a particular field is 4 bytes long.
> Types, and derived types, generally have to do with the logical nature of
> the data
> eg
> type Field_Lengths is range 1 .. 64;
> type Water_Temperatures is range 32 .. 212;
> type Car_Mileages is range 0 .. 400_000;
> etc
> An item of any of those types would fit in a four byte field, even
> though the first two types could fit in a single byte.  In the absence
> of a representation clause, the compiler is allowed to choose whatever
> it likes.  If you want something *represented* the same on two different
> machines, or via two different compilers, use representation clauses.

...but not necessarily on the types that you use in the application -
that /can/ have a enormous performance hit. The compiler usually does
a great job of picking the most optimal size for a type that it can.

What you might then consider is deriving types with rep-clauses to,
well, represent the structure you need to be portable (e.g. if it
needs to go 'over a wire').

-- A type perhaps used a lot in an application
type Internal is record
   I : Integer;
   B : Boolean;
   F : Float;
end record;

-- A required external representation but one that might have a large
performance hit
type External is new Internal;
for External use record
   I at 0 range 0 .. 31;
   B at 1 range 0 ..  0;
   F at 2 range 0 .. 31;
end record;

procedure Write_To_Memory (My_Appliaction_Record : Internal) is
   My_Memory_Mapped_Record : Extenal;
   for My_Memory_Mapped_Record'Address use <address>;
begin
   My_Memory_Mapped_Record := External (My_Application_RecorD);
end;

And the compiler fills in the mapping. Note the reverse can be done as
well. This technique work especially well with 'wholey' enums, e.g.

-- Internal 'logical' representaion
type Internal is (Alpha, Beta, Gamma);

-- Required for an external interface
type External in new Internal;
for External ise (Alpha => 1, Beta => 2, Gamma => 4);

If the enumeration is being used to index into arrays a lot then you
will see a big performance hit if you had added the rep-clause to the
Internal type.

Cheers
-- Martin



  reply	other threads:[~2009-12-08  9:02 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-05 21:23 Cross-platform issues Leslie
2009-12-06 17:13 ` John B. Matthews
2009-12-06 23:39   ` Leslie
2009-12-07  6:03     ` Per Sandberg
2009-12-07 21:05     ` sjw
2009-12-08  0:33     ` Randy Brukardt
2009-12-08  0:33     ` tmoran
2009-12-08  9:02       ` Martin [this message]
2009-12-08 19:46 ` Leslie
replies disabled

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