comp.lang.ada
 help / color / mirror / Atom feed
From: "björn lundin" <b.f.lundin@gmail.com>
Subject: Re: 'Size of an object
Date: Tue, 11 Sep 2012 09:28:34 -0700 (PDT)
Date: 2012-09-11T09:28:34-07:00	[thread overview]
Message-ID: <e9b174f6-27aa-4108-a1fd-ced4ee85387f@googlegroups.com> (raw)
In-Reply-To: <97ff13ad-308a-416f-a300-6c7d9da3d71b@googlegroups.com>

>                         -- thanks, Adam
Den tisdagen den 11:e september 2012 kl. 17:52:03 UTC+2 skrev Adam Beneschan:
>In a larger procedure,
> what might you use 'Size on a local variable for; if you wouldn't ever
> use it on a local variable, then what kinds of *objects* (not types or
> subtypes) might you apply 'Size to, and what would you do with the value?

We have a system that depending on equipment at site, has somewhere between 10 and 60
background processes, or daemons.
depending on platform the ipc mechanism is different, shared mem +semaphors, named pipes, and
memory mapped files are the one we use for AIX, Linux and windows.

This is on ppc and intel. but only 32 bit.
but was on vax/alpha once as well

All implemtations are based on sending a record of data as an array of bytes,
converted with unchecked_conversion. 

when converting, and in message envelope, it is nice to have the size of the byte array.
since it varies betwen different records.

example

spec:
  generic
    Identity : Identity_Type;
    type Data_Type is private;
  package Generic_io is
    procedure Send (Receiver   : in  Process_Type;  Data       : in  Data_Type);
    function  Unpack (Message  : in  Message_Type) return Data_Type;
    function  Pack (Data       : in  Data_Type) return Message_Type;
  end Generic_io;
-----------------------------------------------------------------

a body looks like
Note the use of Data_Length in the body, which comes from Data_Type'Size/System.Storage_Unit


  package body Generic_io is
    Data_Length    : constant Natural := Natural (Data_Type'Size/System.Storage_Unit);
    subtype Byte_Array_Message is Byte_Array (1..Data_Length);
    subtype Byte_Array_2 is Byte_Array (1..2);
    ----------------------------------------------------
    function  Pack (Data : in  Data_Type) return Message_Type is
      function Data_To_Byte_Array is new
             Unchecked_Conversion (Data_Type, Byte_Array_Message);
      Message : Message_Type;
    begin
      if (Data'Size > (MAX_DATA_LENGHT * System.Storage_unit))  then
        Log ("Message size is: " & Natural'Image(Data'size/System.Storage_Unit), Always);
        Log("MAX_DATA_LENGHT is: " & Positive'Image(MAX_DATA_LENGHT), Always);
        raise Too_Big_Message;
      end if;

      Message.Message_Head.Identity := Identity;
      Message.Message_Body.Data (1..Integer (Data_Length)) := Data_To_Byte_Array(Data);
      Message.Message_Body.Data_Length := Data_Length;
      return Message;
    end Pack;
    ----------------------------------------------------
    function  Unpack (Message : Message_Type) return Data_Type is
      function Message_To_Data is new Unchecked_Conversion(Byte_Array_Message, Data_Type);
    begin
      return Message_To_Data (Message.Message_Body.Data (1..Data_Length));
    end Unpack;
   .....
  end Generic_io;


typical client usage is 

package tst is
  type bnl is record
    a : integer := 10;
    b : String(1..3) := "Hej";
    c : String(1..1017) := (others => '-');
  end record;
end tst;

--sending
  package bnl2_Package is new Generic_io
          (Identity        => 3000,
           Data_Type       => tst.bnl);

  rec : tst.bnl;
  m: message_type;

    rec.a := rec.a + 1;
  -- send to 'p2'
    bnl2_Package.send(("p2             ",(others => ' ')),rec);


--
--receiving
    receive(m);
    rec := bnl2_Package.unpack(m);
    text_io.put_line ("a'Img b c'Img' " & rec.a'Img & rec.b & rec.c'Img);-- & rec.d);


-----
This does not compile, I took the interesting bits only.

--
Björn






  parent reply	other threads:[~2012-09-11 16:28 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-11 15:52 'Size of an object Adam Beneschan
2012-09-11 16:20 ` Micronian Coder
2012-09-11 16:21 ` Vasiliy Molostov
2012-09-11 16:27 ` Dmitry A. Kazakov
2012-09-11 16:28 ` björn lundin [this message]
2012-09-11 17:07 ` Shark8
2012-09-11 18:11 ` AdaMagica
2012-09-11 22:21   ` Adam Beneschan
replies disabled

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