comp.lang.ada
 help / color / mirror / Atom feed
From: Robert Dewar <robert_dewar@my-deja.com>
Subject: Re: Overlay allowability
Date: 2000/05/04
Date: 2000-05-04T00:00:00+00:00	[thread overview]
Message-ID: <8es7n2$7lk$1@nnrp1.deja.com> (raw)
In-Reply-To: 3910514D.13BF2DE1@Raytheon.com

In article <3910514D.13BF2DE1@Raytheon.com>,
  "Samuel T. Harris" <samuel_t_harris@Raytheon.com> wrote:

> Many times the need for such overlays is a continual need
> throughout a section of code. Several calls to
> unchecked_conversion
> is simply to slow and does present a consistency problem
> associated with having two separate objects instead of two
> overlayed objects. This consistency problem can be exploited
> in a tasking environment and cause unpredictable results.

I think that's a potentially misleading response.
It refers to an obvious
misuse of unchecked conversion. The proper way to do this UC
is to convert between the access types *ONCE* and then reference
the view you want through the appropriate access type. This will
typically give pretty much identical code to the use of an
address overlay, so there is no efficiency issue.

> So, when performance and consistency are the requirements,
> make an overlay. If one simply needs to jam one kind of data
> into another, use unchecked_conversion.

This advice is to be dubious, because as above
it is based on the wrong approach to using unchecked conversion.
In fact, in practice in Ada 95, there is likely very little
difference between the following:

   type a is ...
   type b is ...

   AV : aliased a;
   BV : aliased b;
   for BV'Address use AV'Address;
   -- your compiler may make you define a constant for this
   -- address making this a bit less convenient to use.

and

   type a is ...
   type a_Access is access all a;
   type b is ...
   type b_Access is access all b;

   function To_b_Access is new UC (a_Access, b_Access);

   AV : aliased A;
   BVA : constant b_Access := To_b_Access (AV'Access);
   BV : B renames BVA.all;

In practice, if the types are records or arrays, you can
probably shorten this to:

   BV : constant b_Access := To_B_Access (AV'Access);

and use automatic dereferencing.

Which to prefer?

The use of UC is to me clearer, and has the advantage of the
loud "with Unchecked_Conversion" in the context clause
indicating a unit that is suspect and potentially unsafe.

The danger of the address overlay is that it is far too easy
to sneak this in. I have even seen things like:

   x : integer;
   b : integer;
   for b'address use x'address;

to achieve the effect of a simple renaming, and that clearly
represents running amok with address overlays :-)

I think I preferred the Ada 83 formulation.



Sent via Deja.com http://www.deja.com/
Before you buy.




  parent reply	other threads:[~2000-05-04  0:00 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-05-01  0:00 Overlay allowability Marc A. Criley
2000-05-01  0:00 ` Ted Dennison
2000-05-03  0:00   ` Samuel T. Harris
2000-05-03  0:00     ` Robert A Duff
2000-05-03  0:00     ` Ted Dennison
2000-05-04  0:00     ` Robert Dewar [this message]
2000-05-08  0:00       ` Samuel T. Harris
2000-05-08  0:00         ` Robert Dewar
2000-05-09  0:00           ` Samuel T. Harris
2000-05-09  0:00             ` Ted Dennison
2000-05-10  0:00               ` Marc A. Criley
2000-05-11  0:00                 ` tmoran
2000-05-12  0:00                   ` tmoran
2000-05-01  0:00 ` Tucker Taft
2000-05-01  0:00   ` mark_biggar
2000-05-01  0:00   ` Keith Thompson
2000-05-08  0:00     ` Tucker Taft
2000-05-03  0:00   ` Robert I. Eachus
2000-05-01  0:00 ` tmoran
2000-05-02  0:00 ` Robert I. Eachus
2000-05-03  0:00   ` Marc A. Criley
replies disabled

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