comp.lang.ada
 help / color / mirror / Atom feed
From: cis.ohio-state.edu!math.ohio-state.edu!magnus.acs.ohio-state.edu!csn!news .den.mmc.com!iplmail!alcyone!rgilbert@ucbvax.Berkeley.EDU  (Bob Gilbert)
Subject: Data Overlays
Date: 13 Aug 93 12:48:35 GMT	[thread overview]
Message-ID: <1993Aug13.124835.18422@iplmail.orl.mmc.com> (raw)

Thanks to those who replied to my earlier post about exceptions occuring
in the declarative region.

Now I've got another one:

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 1 :  Use an access type of one of the types and assign the address
            (using unchecked conversion) of the object declare of the
            other type.

      type SAMPLE_REC_TYPE is
        record                                      -- Size is
          Word_1  : INTEGER;                           -- 32 +
          Word_2  : SOME_32_BIT_TYPE;                  -- 32 +
          Word_3  : SOME_OTHER_32_BIT_TYPE;            -- 32 +
          Word_4  : MORE_32_BIT_STUFF;                 -- 32 +
          Word_5  : A_16_BIT_TYPE;                     -- 16 +
          Word_6  : A_16_BIT_TYPE;                     -- 16 =
        end record;                                    -- 160 bits

      for SAMPLE_REC_TYPE'size use 160;   -- Guarantees known size

      ---------------------------------------------
      -- Declare array type of appropiate size to -
      -- map to above record definition.          -
      ---------------------------------------------     
      Sample_Rec_Words : constant := SAMPLE_REC_TYPE'size / 8;

      type BYTE_ARRAY_TYPE is array (1 .. Sample_Rec_Words) of BYTE;
      pragma Pack(BYTE_ARRAY_TYPE);

      type INT_ARRAY_PTR is access BYTE_ARRAY_TYPE;

      ---------------------------------------------
      -- Declare Unchecked_Conversion function to -
      -- permit assigning the address of X to Y.  -
      --         Y := X'address;                  -
      ---------------------------------------------
      function To_Ptr is new Unchecked_Conversion(ADDRESS, INT_ARRAY_PTR);

      ------------------------------------
      -- Declare object of record type   -
      -- and access object of array type -
      ------------------------------------
      X : SAMPLE_REC_TYPE;
      Y : INT_ARRAY_PTR := To_Ptr(X'address);     -- Don't use 'new'

   I have used this method on several occasions and it works just fine.  Howeve
r,
   it does seem a little complicated and makes the assumption that an object
   of an access type is implemented using the address of the object.

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?

Question:  Which of the above methods is prefered?  Is there a better way?

                           Bob

             reply	other threads:[~1993-08-13 12:48 UTC|newest]

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

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