comp.lang.ada
 help / color / mirror / Atom feed
From: Mats Karlssohn <mats@mida.se>
Subject: Re: records containing variable length arrays [long]
Date: Fri, 08 Jun 2001 14:32:55 +0200
Date: 2001-06-08T12:31:12+00:00	[thread overview]
Message-ID: <3B20C5F7.94DC72A1@mida.se> (raw)
In-Reply-To: 3B1E8BB9.42BF95D2@mida.se

I will try to address two posts with the same reply here...

Jeffrey Carter wrote:
%<
> In that case you can access it simply. We'll call what you get from C
> Data_Address and Data_Length. Then you can say
> 
> Raw_Data : constant Buffer (1 .. Data_Length);
> -- or (7 .. Data_Length + 6), if you prefer
> pragma Import (Ada, Raw_Data); -- Prevent any initialization of Raw_Data
> for Raw_Data'Address use Data_Address;
> 
> You can then Translate this to an appropriately constrained record type.
> Since you do not need to modify it, you can make it constant, which
> should avoid copying the data.
>
> If you want to live dangerously, you can avoid the buffer representation
> and simply apply the address clause to the record object.

Stephen Leake wrote:
%<
> Do you really need to declare the message as a single type? That is,
> do you really need to pass objects of that type around? Or can you get
> by with reading the message from a stream, and building a set of
> internal objects that other packages access in a controlled way?

Last night i came up with a partitial solution that seems promising.
I realised that I will need to pass pointers to the messages around
to different routines.

I ended up declaring the record in the way that was discussed earlier.
Then I declare an access type to the record and use an instance of
System.Address_To_Access_Conversions to convert the C pointer to an
Ada pointer. I'll probably wrap another layer around the buffer
manager and let that glue perform the address to access conversion.

Right now this seems to work but I have a layout problem in another
similar (but not identical) record type. See below for a new question.

Stephen Leake wrote:
> The point being that there are many reasonable communications
> protocols that simply cannot be expressed as an Ada record, and this
> seems to be one of them. The correct approach is to encode the
> protocol as a procedure that reads from a stream, not as an Ada record
> type.

Well, I guess that could be done, but I really do not want to copy
the data around. I gets dumped into shared (actually reflected)
memory by another CPU and in some cases the records can be quite
big and arriving fast.


Now, for the new (but related) question.

Given this declaration and representation:

   type Message(Response_Length : Byte; Output_Length : Byte) is
      record
         Magic           : Word;
         Operation       : Word;
         Status          : Word;
         Response_Data   : Buffer(1..Response_Length);
         Output_Data     : Buffer(1..Output_Length);
         CRC             : Word;
      end record;

   for Message use
      record
          Magic           at 0 range 0..15;
          Operation       at 2 range 0..15;
          Status          at 4 range 0..15;
          Response_Length at 6 range 0.. 7;
          Output_Length   at 7 range 0.. 7;
      end record;

   pragma Pack(Message);

Is there a way to explicitly put at least the CRC element into the
representation clause ? My experiments haven't gotten me anywhere
on this part.

-- 
Mats Karlssohn, developer                         mailto:mats@mida.se  
Mida Systemutveckling AB                          http://www.mida.se
Box 64, S-732 22 ARBOGA, SWEDEN
Phone: +46-(0)589-89808   Fax: +46-(0)589-89809



  parent reply	other threads:[~2001-06-08 12:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-06 19:59 records containing variable length arrays [long] Mats Karlssohn
2001-06-06 23:06 ` Jeffrey Carter
2001-06-07 11:44   ` Mats Karlssohn
2001-06-08  2:10     ` Jeffrey Carter
2001-06-07 21:38 ` Stephen Leake
2001-06-08 12:32 ` Mats Karlssohn [this message]
2001-06-08 16:42   ` Jeffrey Carter
2001-06-08 22:28     ` Jeffrey Carter
2001-06-19 11:43     ` Mats Karlssohn
replies disabled

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