comp.lang.ada
 help / color / mirror / Atom feed
* 'Size of an object
@ 2012-09-11 15:52 Adam Beneschan
  2012-09-11 16:20 ` Micronian Coder
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Adam Beneschan @ 2012-09-11 15:52 UTC (permalink / raw)


I had a customer ask a simple question and found that I didn't really
know the answer, and I'm not even sure where there is a clear answer.

The question has to do with X'Size where X is an object.  RM 13.3(40)
says this "denotes the size in bits of the representation of the
object".  Maybe I'm being dense, but this doesn't seem to clarify
things.

Suppose you have an enumeration type with eight literals, and no other
representation or aspect clauses apply to the type:

   type Enum is (E0, E1, E2, E3, E4, E5, E6, E7);

I think Enum'Size should be 3 in most or all implementations.  Now suppose
you have a local variable of that type:

   procedure Proc is
      E : Enum;
   begin
      Put_Line (Integer'Image (E'Size));
   end Proc;

What would you expect E'Size to be, and why?  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?

                        -- thanks, Adam



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: 'Size of an object
  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
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Micronian Coder @ 2012-09-11 16:20 UTC (permalink / raw)


That's a good question. I would expect it to be the size of the object, which could be anything really (8, 16, 32) and not the size of Enum'Size (ie. 3). If I'm not mistaken, this confusion is why AdaCore implemented an Object_Size attribute which sadly didn't make it into any recent version of Ada. A quick search on www.ada-auth.org turns up http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ais/ai-00319.txt?rev=1.5 , which is voted as NO ACTION. I should read the AI discussion to see what the ARG was thinking.


On Tuesday, September 11, 2012 8:52:03 AM UTC-7, Adam Beneschan wrote:
> I had a customer ask a simple question and found that I didn't really
> 
> know the answer, and I'm not even sure where there is a clear answer.
> 
> 
> 
> The question has to do with X'Size where X is an object.  RM 13.3(40)
> 
> says this "denotes the size in bits of the representation of the
> 
> object".  Maybe I'm being dense, but this doesn't seem to clarify
> 
> things.
> 
> 
> 
> Suppose you have an enumeration type with eight literals, and no other
> 
> representation or aspect clauses apply to the type:
> 
> 
> 
>    type Enum is (E0, E1, E2, E3, E4, E5, E6, E7);
> 
> 
> 
> I think Enum'Size should be 3 in most or all implementations.  Now suppose
> 
> you have a local variable of that type:
> 
> 
> 
>    procedure Proc is
> 
>       E : Enum;
> 
>    begin
> 
>       Put_Line (Integer'Image (E'Size));
> 
>    end Proc;
> 
> 
> 
> What would you expect E'Size to be, and why?  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?
> 
> 
> 
>                         -- thanks, Adam




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: 'Size of an object
  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
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Vasiliy Molostov @ 2012-09-11 16:21 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> писал(а) в своём письме Tue, 11 Sep 2012  
19:52:03 +0400:

> I had a customer ask a simple question and found that I didn't really
> know the answer, and I'm not even sure where there is a clear answer.
>
> The question has to do with X'Size where X is an object.  RM 13.3(40)
> says this "denotes the size in bits of the representation of the
> object".  Maybe I'm being dense, but this doesn't seem to clarify
> things.

RM says truth. always. This means you can supply representation clause for  
X and ask lately in code about this representation. If you have not  
supplied representation, it will be almost equal to system unit size  
(8-bit byte or 16-bit word, or else).

> Suppose you have an enumeration type with eight literals, and no other
> representation or aspect clauses apply to the type:
>
>    type Enum is (E0, E1, E2, E3, E4, E5, E6, E7);
>
> I think Enum'Size should be 3 in most or all implementations.  Now  
> suppose
> you have a local variable of that type:

For intel x86 it would be a 8 or 16, or 32, depending on how memory  
alignment is constrained for this object. You can redefine size to 3 by  
adding representation clause.

>
>    procedure Proc is
>       E : Enum;
>    begin
>       Put_Line (Integer'Image (E'Size));
>    end Proc;
>
> What would you expect E'Size to be, and why?

In this case, it would be a default value of max(system unit size, default  
memory alignment).

> 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?

You can use it in bit string copy ops, in record representation clauses or  
any other, depending on purpose, a similar thing in C is a bit field  
definitions inside 'struct' notation.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: 'Size of an object
  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
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2012-09-11 16:27 UTC (permalink / raw)


On Tue, 11 Sep 2012 08:52:03 -0700 (PDT), Adam Beneschan wrote:

> What would you expect E'Size to be, and why?  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?

Does the question has an answer? What is the object size when stored in a
register? Do temp copies count, e.g. when passed by copy, loaded into a
cache? Does the memory allocated upon unpacking packed representations
count?

I think that at best, one could say that the object size is an interval
with the upper and lower bound reachable. This interval could used to
estimate container type sizes, though the obtained bounds might turn become
unreachable.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: 'Size of an object
  2012-09-11 15:52 'Size of an object Adam Beneschan
                   ` (2 preceding siblings ...)
  2012-09-11 16:27 ` Dmitry A. Kazakov
@ 2012-09-11 16:28 ` björn lundin
  2012-09-11 17:07 ` Shark8
  2012-09-11 18:11 ` AdaMagica
  5 siblings, 0 replies; 8+ messages in thread
From: björn lundin @ 2012-09-11 16:28 UTC (permalink / raw)


>                         -- 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






^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: 'Size of an object
  2012-09-11 15:52 'Size of an object Adam Beneschan
                   ` (3 preceding siblings ...)
  2012-09-11 16:28 ` björn lundin
@ 2012-09-11 17:07 ` Shark8
  2012-09-11 18:11 ` AdaMagica
  5 siblings, 0 replies; 8+ messages in thread
From: Shark8 @ 2012-09-11 17:07 UTC (permalink / raw)


On Tuesday, September 11, 2012 9:52:03 AM UTC-6, Adam Beneschan wrote:
> I had a customer ask a simple question and found that I didn't really
> know the answer, and I'm not even sure where there is a clear answer.
> 
> The question has to do with X'Size where X is an object.  RM 13.3(40)
> says this "denotes the size in bits of the representation of the
> object".  Maybe I'm being dense, but this doesn't seem to clarify
> things.
> 
> Suppose you have an enumeration type with eight literals, and no other
> representation or aspect clauses apply to the type:
>    type Enum is (E0, E1, E2, E3, E4, E5, E6, E7);
> I think Enum'Size should be 3 in most or all implementations.  Now suppose
> you have a local variable of that type:
> 
>    procedure Proc is
>       E : Enum;
>    begin
>       Put_Line (Integer'Image (E'Size));
>    end Proc;
> 
> What would you expect E'Size to be, and why?  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?

Well, 8, if it's on an x86 machine.
There's no Pragma Pack, or Optimize(Space) so that means that the machine's byte-size or word-size would make sensible choices for the compiler: the packing/unpacking could be too expensive WRT memory/processing-time if that were the default for everything [every access to every variable having to be packed/unpacked - calculated on - repacked and stored].



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: 'Size of an object
  2012-09-11 15:52 'Size of an object Adam Beneschan
                   ` (4 preceding siblings ...)
  2012-09-11 17:07 ` Shark8
@ 2012-09-11 18:11 ` AdaMagica
  2012-09-11 22:21   ` Adam Beneschan
  5 siblings, 1 reply; 8+ messages in thread
From: AdaMagica @ 2012-09-11 18:11 UTC (permalink / raw)


On Tuesday, September 11, 2012 5:52:03 PM UTC+2, Adam Beneschan wrote:
> The question has to do with X'Size where X is an object.  RM 13.3(40)
> says this "denotes the size in bits of the representation of the
> object".  Maybe I'm being dense, but this doesn't seem to clarify
> things.
> 
> Suppose you have an enumeration type with eight literals, and no other
> representation or aspect clauses apply to the type:
> 
>    type Enum is (E0, E1, E2, E3, E4, E5, E6, E7);
> 
> I think Enum'Size should be 3 in most or all implementations.
It must be 3.
Take Integer, for instance. For the subtype Natural'Size = Integer'Size -1,
because it doesn't need the sign bit. This was a much disputed decision during
the Ada9X process (Ada 83 compilers had different values for 'Size, and Ada9X
fixed this - it was on Tucker Taft's vote IIRM). GNAT implemented therefore the
'Object_Size attribute.

Now stand-alone objects normally have a size fitting in a standard word
depending on the OS. The RM says, T'Size is used for objects in packed records
and Unchecked_Conversion only.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: 'Size of an object
  2012-09-11 18:11 ` AdaMagica
@ 2012-09-11 22:21   ` Adam Beneschan
  0 siblings, 0 replies; 8+ messages in thread
From: Adam Beneschan @ 2012-09-11 22:21 UTC (permalink / raw)


On Tuesday, September 11, 2012 11:11:45 AM UTC-7, AdaMagica wrote:
> On Tuesday, September 11, 2012 5:52:03 PM UTC+2, Adam Beneschan wrote:

> 
> > I think Enum'Size should be 3 in most or all implementations.
> 
> It must be 3.
> Take Integer, for instance. For the subtype Natural'Size = Integer'Size -1,
> because it doesn't need the sign bit. This was a much disputed decision during
> the Ada9X process (Ada 83 compilers had different values for 'Size, and Ada9X
> fixed this - it was on Tucker Taft's vote IIRM).

Well, since the rule that makes this true is under "Implementation Advice", and in a passage starting with "The recommended level of support...", the word "must" may be too strong.  But other than that, you're right.

                       -- Adam



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-09-17  2:48 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2012-09-11 17:07 ` Shark8
2012-09-11 18:11 ` AdaMagica
2012-09-11 22:21   ` Adam Beneschan

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