comp.lang.ada
 help / color / mirror / Atom feed
From: cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!cs.utexa s.edu!swrinde!menudo.uh.edu!nuchat!leeweyr!bill@ucbvax.Berkeley.EDU  (Bill Lee)
Subject: Re: Data Overlays
Date: 18 Aug 93 00:39:40 GMT	[thread overview]
Message-ID: <1993Aug18.003940.2208@leeweyr.sccsi.com> (raw)

In article <1993Aug13.124835.18422@iplmail.orl.mmc.com> rgilbert@orl.mmc.com wr
ites:
>
>Many times I have wanted to create two different types to look at the
>same object.  An example might be a record structure which is to be
>output via some DMA device which expects the data to look like an 
>array of bytes.  Anyway I've come up with two different methods to
>implement this.
>
			.
			.
>
>Method 2:  Just use a representation clause to map one of the objects to the
>           address of the other.
>
>      X : SAMPLE_REC_TYPE;
>      Y : BYTE_ARRAY_TYPE;
>      for Y use at X'address;
>
>   This seems to be much more straight forward and makes no assumptions about
>   the implementation.  But when this method is used a compiler (Telesoft)
>   warning is issued to the affect that the representation clause should not
>   be used to produce overlayed data?
>


O.k., Here's my problem:

I have a system which receives "messages" from a number of other
systems. The messages come via POSIX queues. On any one queue, I
can receive a number of different messages, mostly with record
structures defined by the C code where the message originated.

I do not know a priori what the next message will be. I have to
"receive" the message into a Byte_Array. Once I have the message,
I can access a standard header and extract a message identifier,
which then tells me what kind of message I've just received.
Once I have a message in a Byte_Array, and once I know what kind
of message it is, I can then convert the message to an Ada record
which has the right structure for the message.

I've considered both methods above. Certainly, Unchecked_Conversion
is one alternative. But I have used another method which seems to
work, seems to be portable, and, moreover, SEEMS to be in line with
what the language designers intended. To wit:

Given an Ada type, Messages:

generic
  type Byte_Arrays is private;
function Extract_Message ( A_Message : in Byte_Arrays ) return Messages;

where the body looks like

function Extract_Message ( A_Message : in Byte_Arrays ) return Messages is
  Temp_Message : Messages;
  for Temp_Message use at A_Message'address;
begin
  return Temp_Message;
end Extract_Message;

In many cases, I have actually created a more "Ada-like" message
type, and I do far more than just return the C structure.

I like the "feel" of this method. It uses the representation
specification which allows a structure to be placed "at" an address.
If this usage is not what the language designers had in mind, then
what was the purpose of the rep spec?

Is there any inherent danger or "wrongness" in this technique? And
yes, I have read the "for the purpose of overlaying....erroneous"
section in the LRM. Is this overlaying? Is it erroneous?

Incidently, I use this same technique in bindings where I have to
deal with structures created in C. The C code malloc's the memory
and places structs in it, and then just passes back the C pointer.
I make the assumption (possibly in error in some cases) that a C
pointer and a System.Address are coincident. Then I can calculate
the address in the malloc'ed memory where a specific C structure
exists, and I can assign an Ada record "at" that address in order
to extract the information from it.

This technique evokes no complaints from the DEC compiler, and it
seems to work properly. We're running ULTRIX workstations.

Comments/observations/suggestions (hell, maybe even a flame or two
8-{) would be appreciated.

Regards,

Bill Lee

(Not at work, at home. We're so damned secure there that it's hard
to work!)

bill@leeweyr.sccsi.com

             reply	other threads:[~1993-08-18  0:39 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-08-18  0:39 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!cs.utexa [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-09-01 14:36 Data Overlays David Emery
1993-09-01  4:23 Jim Lonjers
1993-08-31 20:36 dog.ee.lbl.gov!agate!howland.reston.ans.net!math.ohio-state.edu!magnus.ac
1993-08-31  3:51 Jim Lonjers
1993-08-23 14:17 Bob Crispen
1993-08-23 14:11 Alex Blakemore
1993-08-19 13:16 cs.utexas.edu!mars.tsd.arlut.utexas.edu!gardner
1993-08-19  2:18 portal!cup.portal.com!R_Tim_Coslet
1993-08-18 16:40 cis.ohio-state.edu!magnus.acs.ohio-state.edu!csn!news.den.mmc.com!iplmail
1993-08-18 16:27 Charles H. Sampson
1993-08-18 16:04 Charles H. Sampson
1993-08-18 12:55 cis.ohio-state.edu!magnus.acs.ohio-state.edu!csn!news.den.mmc.com!iplmail
1993-08-18 12:50 cis.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!europa.eng.gtefsd.com!fs7.ece.cmu.edu!news.sei.cmu.edu!firth
1993-08-18  2:11 cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!do
1993-08-18  1:53 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.
1993-08-17 15:16 Charles H. Sampson
1993-08-13 17:57 cgl!sgiblab!darwin.sura.net!mlb.semi.harris.com!x102a!scook
1993-08-13 12:48 Bob Gilbert
replies disabled

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