comp.lang.ada
 help / color / mirror / Atom feed
* Allocating Memory with "new"
@ 2003-04-24 19:30 Peter Richtmyer
  2003-04-25  6:05 ` Vincent Smeets
  2003-04-25 15:09 ` Stephen Leake
  0 siblings, 2 replies; 6+ messages in thread
From: Peter Richtmyer @ 2003-04-24 19:30 UTC (permalink / raw)


I am stumped ...

My memory allocation scheme worked OK on Gnat / Win2K,
but on Sun Blade with Rational Ada it does not give me
what I expected. See after program for output.

I built a test program to illustrate my problem:
(forgive me, it is quick and ugly)

thanks
Peter
--------------------------------------------------------------
with system;
with system.address_to_access_conversions;
with Text_Io;
use  text_io;
with unchecked_conversion;

procedure Hi is

    first_time : boolean := true;
    
    procedure alloc (nbytes : integer) is
    
        type Mem_Block_T is array (1 .. Nbytes) of Character;
        package Mem_Block_Ptr_Pkg is new 
             system.address_to_access_conversions (Mem_Block_T);
	Mem_Block_Ptr :  Mem_Block_Ptr_Pkg.Object_Pointer := null;
	
	addr : system.address;
	
	In_Work_Area : String (1 .. 12) := "            ";
	
        function Addr_to_int is new Unchecked_Conversion
         			   (system.address, integer);
        
        package data_io is new integer_io(integer);    
         			   
     begin
        if first_time then
           first_time := false;
           put_line ("Mem_Block_Ptr_Pkg.Object_Pointer'size = " & 
                Integer'image(Mem_Block_Ptr_Pkg.Object_Pointer'size));
        end if;
                    
	Mem_Block_Ptr := new Mem_Block_T;
	Addr := Mem_Block_Ptr_Pkg.to_address (Mem_Block_Ptr);

	Data_Io.Put (In_Work_Area, addr_to_int (addr), 16);
	put_line ("allocated " & In_Work_Area);
	
     end alloc;


begin 

    put_line ("system.address'size = " & Integer'image(system.address'size));
    put_line ("integer'size = " & Integer'image(integer'size));

    for i in 1 .. 10 loop
      Put ("len 1: ");
      alloc (1);
    end loop; 
    
    for i in 1 .. 10 loop
      Put ("len 8: ");
      alloc (8);
    end loop; 
    
     for i in 1 .. 50 loop
      Put ("len" & integer'image(i * 8) & " : ");
      alloc (i * 8);
    end loop; 

end Hi;

-- Rational Apex Ada on SUN Blade:

-- system.address'size =  32
-- integer'size =  32
-- len 1: Mem_Block_Ptr_Pkg.Object_Pointer'size =  32
-- allocated    16#F8A88#
-- len 1: allocated    16#F8A88#
-- len 1: allocated    16#F8A88#
-- len 1: allocated    16#F8A88#
-- len 1: allocated    16#F8A88#
-- len 1: allocated    16#F8A88#
-- len 1: allocated    16#F8A88#
-- len 1: allocated    16#F8A88#
-- len 1: allocated    16#F8A88#
-- len 1: allocated    16#F8A88#
-- len 8: allocated    16#F8A88#
-- len 8: allocated    16#F8A88#
-- len 8: allocated    16#F8A88#
-- len 8: allocated    16#F8A88#
-- len 8: allocated    16#F8A88#
-- len 8: allocated    16#F8A88#
-- len 8: allocated    16#F8A88#
-- len 8: allocated    16#F8A88#
-- len 8: allocated    16#F8A88#
-- len 8: allocated    16#F8A88#
-- len 8 : allocated    16#F8A88#
-- len 16 : allocated    16#F8A80#
-- len 24 : allocated    16#F8A78#
-- len 32 : allocated    16#F8A70#
-- len 40 : allocated    16#F8A68#
-- len 48 : allocated    16#F8A60#
-- len 56 : allocated    16#F8A58#
-- len 64 : allocated    16#F8A50#
-- len 72 : allocated    16#F8A48#
-- len 80 : allocated    16#F8A40#
-- len 88 : allocated    16#F8A38#
-- len 96 : allocated    16#F8A30#

-- continues decreasing by 8

-------------------------------------------------------
-- Gnat 3.15P on Win2000       (OK)

--system.address'size =  32
--integer'size =  32
--len 1: Mem_Block_Ptr_Pkg.Object_Pointer'size =  32
--allocated  16#2602450#
--len 1: allocated  16#2602520#
--len 1: allocated  16#2602538#
--len 1: allocated  16#2602550#
--len 1: allocated  16#2602568#
--len 1: allocated  16#2602580#
--len 1: allocated  16#2602598#
--len 1: allocated  16#26025B0#
--len 1: allocated  16#26025C8#
--len 1: allocated  16#26025E0#
--len 8: allocated  16#26025F8#
--len 8: allocated  16#2602610#
--len 8: allocated  16#2602628#
--len 8: allocated  16#2602640#
--len 8: allocated  16#2602658#
--len 8: allocated  16#2602670#
--len 8: allocated  16#2602688#
--len 8: allocated  16#26026A0#
--len 8: allocated  16#26026B8#
--len 8: allocated  16#26026D0#
--len 8 : allocated  16#26026E8#
--len 16 : allocated  16#2602700#
--len 24 : allocated  16#2602718#
--len 32 : allocated  16#2602740#
--len 40 : allocated  16#2602768#
--len 48 : allocated  16#26027A0#
--len 56 : allocated  16#26027D8#
--len 64 : allocated  16#2602820#
--len 72 : allocated  16#26039C8#
--len 80 : allocated  16#2603A20#
--len 88 : allocated  16#2603A78#
--len 96 : allocated  16#2603AE0#
--len 104 : allocated  16#2603B48#
--len 112 : allocated  16#2603BC0#
--len 120 : allocated  16#2603C38#
--len 128 : allocated  16#2603CC0#
--len 136 : allocated  16#2603D48#



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

* Re: Allocating Memory with "new"
  2003-04-24 19:30 Peter Richtmyer
@ 2003-04-25  6:05 ` Vincent Smeets
  2003-04-25 10:33   ` Peter Richtmyer
  2003-04-25 15:09 ` Stephen Leake
  1 sibling, 1 reply; 6+ messages in thread
From: Vincent Smeets @ 2003-04-25  6:05 UTC (permalink / raw)


Hello,

I would say that the Sun version is correct! You defined the package
Mem_Block_Ptr_Pkg inside the procedure alloc. Every time the procedure is
left, the package becomes out of scope and so does the access type defined
in that package. The next time you call the procedure, a new package will be
instantiated and with that a new access type will be defined.

Every time an access type comes out of scope, the memory allocated by that
access type can be freed (garbage collection).

Conclusion:
Your program is erroneous.
Move the package Mem_Block_Ptr_Pkg to an outer scoop.



Vincent Smeets

"Peter Richtmyer" <prichtmyer@yahoo.com> wrote in message
news:1b585154.0304241130.35fa0a97@posting.google.com...
> I am stumped ...
>
> My memory allocation scheme worked OK on Gnat / Win2K,
> but on Sun Blade with Rational Ada it does not give me
> what I expected. See after program for output.
>
> I built a test program to illustrate my problem:
> (forgive me, it is quick and ugly)
>
> thanks
> Peter
> --------------------------------------------------------------
> with system;
> with system.address_to_access_conversions;
> with Text_Io;
> use  text_io;
> with unchecked_conversion;
>
> procedure Hi is
>
>     first_time : boolean := true;
>
>     procedure alloc (nbytes : integer) is
>
>         type Mem_Block_T is array (1 .. Nbytes) of Character;
>         package Mem_Block_Ptr_Pkg is new
>              system.address_to_access_conversions (Mem_Block_T);
> Mem_Block_Ptr :  Mem_Block_Ptr_Pkg.Object_Pointer := null;
>
> addr : system.address;
>
> In_Work_Area : String (1 .. 12) := "            ";
>
>         function Addr_to_int is new Unchecked_Conversion
>             (system.address, integer);
>
>         package data_io is new integer_io(integer);
>
>      begin
>         if first_time then
>            first_time := false;
>            put_line ("Mem_Block_Ptr_Pkg.Object_Pointer'size = " &
>                 Integer'image(Mem_Block_Ptr_Pkg.Object_Pointer'size));
>         end if;
>
> Mem_Block_Ptr := new Mem_Block_T;
> Addr := Mem_Block_Ptr_Pkg.to_address (Mem_Block_Ptr);
>
> Data_Io.Put (In_Work_Area, addr_to_int (addr), 16);
> put_line ("allocated " & In_Work_Area);
>
>      end alloc;
>
>
> begin
>
>     put_line ("system.address'size = " &
Integer'image(system.address'size));
>     put_line ("integer'size = " & Integer'image(integer'size));
>
>     for i in 1 .. 10 loop
>       Put ("len 1: ");
>       alloc (1);
>     end loop;
>
>     for i in 1 .. 10 loop
>       Put ("len 8: ");
>       alloc (8);
>     end loop;
>
>      for i in 1 .. 50 loop
>       Put ("len" & integer'image(i * 8) & " : ");
>       alloc (i * 8);
>     end loop;
>
> end Hi;
>
> -- Rational Apex Ada on SUN Blade:
>
> -- system.address'size =  32
> -- integer'size =  32
> -- len 1: Mem_Block_Ptr_Pkg.Object_Pointer'size =  32
> -- allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8 : allocated    16#F8A88#
> -- len 16 : allocated    16#F8A80#
> -- len 24 : allocated    16#F8A78#
> -- len 32 : allocated    16#F8A70#
> -- len 40 : allocated    16#F8A68#
> -- len 48 : allocated    16#F8A60#
> -- len 56 : allocated    16#F8A58#
> -- len 64 : allocated    16#F8A50#
> -- len 72 : allocated    16#F8A48#
> -- len 80 : allocated    16#F8A40#
> -- len 88 : allocated    16#F8A38#
> -- len 96 : allocated    16#F8A30#
>
> -- continues decreasing by 8
>
> -------------------------------------------------------
> -- Gnat 3.15P on Win2000       (OK)
>
> --system.address'size =  32
> --integer'size =  32
> --len 1: Mem_Block_Ptr_Pkg.Object_Pointer'size =  32
> --allocated  16#2602450#
> --len 1: allocated  16#2602520#
> --len 1: allocated  16#2602538#
> --len 1: allocated  16#2602550#
> --len 1: allocated  16#2602568#
> --len 1: allocated  16#2602580#
> --len 1: allocated  16#2602598#
> --len 1: allocated  16#26025B0#
> --len 1: allocated  16#26025C8#
> --len 1: allocated  16#26025E0#
> --len 8: allocated  16#26025F8#
> --len 8: allocated  16#2602610#
> --len 8: allocated  16#2602628#
> --len 8: allocated  16#2602640#
> --len 8: allocated  16#2602658#
> --len 8: allocated  16#2602670#
> --len 8: allocated  16#2602688#
> --len 8: allocated  16#26026A0#
> --len 8: allocated  16#26026B8#
> --len 8: allocated  16#26026D0#
> --len 8 : allocated  16#26026E8#
> --len 16 : allocated  16#2602700#
> --len 24 : allocated  16#2602718#
> --len 32 : allocated  16#2602740#
> --len 40 : allocated  16#2602768#
> --len 48 : allocated  16#26027A0#
> --len 56 : allocated  16#26027D8#
> --len 64 : allocated  16#2602820#
> --len 72 : allocated  16#26039C8#
> --len 80 : allocated  16#2603A20#
> --len 88 : allocated  16#2603A78#
> --len 96 : allocated  16#2603AE0#
> --len 104 : allocated  16#2603B48#
> --len 112 : allocated  16#2603BC0#
> --len 120 : allocated  16#2603C38#
> --len 128 : allocated  16#2603CC0#
> --len 136 : allocated  16#2603D48#




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

* Re: Allocating Memory with "new"
  2003-04-25  6:05 ` Vincent Smeets
@ 2003-04-25 10:33   ` Peter Richtmyer
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Richtmyer @ 2003-04-25 10:33 UTC (permalink / raw)


"Vincent Smeets" <No.Spam@localhost> wrote in message news:<3ea8d041$0$4083$4d4ebb8e@read.news.de.uu.net>...

> Every time an access type comes out of scope, the memory allocated by that
> access type can be freed (garbage collection).

Thanks - that certainly explains it. I thought the programmer had
complete control of the memory allocated and that it would not be
freed until an Unchecked Deallocate (or the program ended).

Peter



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

* Re: Allocating Memory with "new"
@ 2003-04-25 12:46 David C. Hoos
  2003-04-25 22:19 ` Peter Richtmyer
  0 siblings, 1 reply; 6+ messages in thread
From: David C. Hoos @ 2003-04-25 12:46 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway


----- Original Message -----
From: "Peter Richtmyer" <prichtmyer@yahoo.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Friday, April 25, 2003 5:33 AM
Subject: Re: Allocating Memory with "new"


> "Vincent Smeets" <No.Spam@localhost> wrote in message
news:<3ea8d041$0$4083$4d4ebb8e@read.news.de.uu.net>...
>
> > Every time an access type comes out of scope, the memory allocated by
that
> > access type can be freed (garbage collection).
>
> Thanks - that certainly explains it. I thought the programmer had
> complete control of the memory allocated and that it would not be
> freed until an Unchecked Deallocate (or the program ended).
>
>From the addresses allocated, it looks like the allocation was done on the
stack, instead of the heap.  The compiler is free to do this, given that
Mem_Block_Ptr is declared on the stack.

Thus, this is not "garbage collection," but simply the fact that the
allocated memory goes out of scope when the procedure returns.

In this sense, the Rational compiler is "smarter" than GNAT, since
allocation on the heap is more expensive than allocation on the stack.

Incidentally, setting Mem_Block_Ptr to null is unnecessary, since
the rules of Ada require access objects to be initialized to null.

David




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

* Re: Allocating Memory with "new"
  2003-04-24 19:30 Peter Richtmyer
  2003-04-25  6:05 ` Vincent Smeets
@ 2003-04-25 15:09 ` Stephen Leake
  1 sibling, 0 replies; 6+ messages in thread
From: Stephen Leake @ 2003-04-25 15:09 UTC (permalink / raw)


prichtmyer@yahoo.com (Peter Richtmyer) writes:

> I am stumped ...

Others have pointed out your main problem. I'd like to point out a
couple minor things, that you may not be aware of.

>         function Addr_to_int is new Unchecked_Conversion
>          			   (system.address, integer);

You should use System.Storage_Elements.Integer_Address, To_Address,
To_Integer, instead of this unchecked_conversion. Guarranteed correct
and portable.

>         package data_io is new integer_io(integer);

Ada.Integer_Text_IO has done this for you.

-- 
-- Stephe



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

* Re: Allocating Memory with "new"
  2003-04-25 12:46 Allocating Memory with "new" David C. Hoos
@ 2003-04-25 22:19 ` Peter Richtmyer
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Richtmyer @ 2003-04-25 22:19 UTC (permalink / raw)


Thanks, all the responses have been helpful, and I have tried to
incorporate
the suggestions.

So my new (still ugly) solution, for 1 to 256 bytes (thats enough for
me):

with system;
with System.Storage_Elements;
with system.address_to_access_conversions;
with Text_Io;
use  text_io;
with unchecked_conversion;

procedure Hi is
    
    subtype mem_bytes is natural range 1 .. 256; 
    type nat_8 is new natural range 0 .. 255;
    for nat_8'size use 8;    

    type array_t is array (nat_8 range <>) of character;
     
    type Mem_Block_T ( n_minus_1 : nat_8) is 
       record
         t : array_t (1 .. n_minus_1);
       end record;   
    for Mem_Block_T use 
       record
         n_minus_1 at 0 range 0 .. 7;
       end record;
    pragma pack (Mem_Block_T);    
     
    package Mem_Block_Ptr_Pkg is new
system.address_to_access_conversions (Mem_Block_T);

    Mem_Block_Ptr :  Mem_Block_Ptr_Pkg.Object_Pointer := null;
    --------------------------------------------------------------------
    procedure alloc (nbytes : mem_bytes) is
    
      n_minus_1 : nat_8 := nat_8 (nbytes - 1);
	
	addr : system.address;
	
	In_Work_Area : String (1 .. 12) := "            ";
	        
        package data_io is new integer_io(integer);    
         			   
	    
     begin     
                    
	Mem_Block_Ptr := new Mem_Block_T (n_minus_1);
	Addr := Mem_Block_Ptr_Pkg.to_address (Mem_Block_Ptr);
      put ("Bytes allocated: " & Integer'image(Mem_Block_Ptr.all'size
/ 8));

	Data_Io.Put (In_Work_Area, Integer(System.Storage_Elements.to_integer
(addr)), 16);
	put_line (" at " & In_Work_Area);
	
     end alloc;
     --------------------------------------------------------------------
begin 
    
    for i in 1 .. 256 loop
      Put ("len" & integer'image(i) & " : ");
      alloc (i);
    end loop; 

end Hi;

============================
some output:

len 1 : Bytes allocated:  1 at  16#2602450#
len 2 : Bytes allocated:  2 at  16#2602520#
len 3 : Bytes allocated:  3 at  16#2602538#
len 4 : Bytes allocated:  4 at  16#2602550#
len 5 : Bytes allocated:  5 at  16#2602568#
           .              .
           :              :
len 252 : Bytes allocated:  252 at  16#260C140#
len 253 : Bytes allocated:  253 at  16#260C248#
len 254 : Bytes allocated:  254 at  16#260C350#
len 255 : Bytes allocated:  255 at  16#260C458#
len 256 : Bytes allocated:  256 at  16#260C560#



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

end of thread, other threads:[~2003-04-25 22:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-25 12:46 Allocating Memory with "new" David C. Hoos
2003-04-25 22:19 ` Peter Richtmyer
  -- strict thread matches above, loose matches on Subject: below --
2003-04-24 19:30 Peter Richtmyer
2003-04-25  6:05 ` Vincent Smeets
2003-04-25 10:33   ` Peter Richtmyer
2003-04-25 15:09 ` Stephen Leake

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