comp.lang.ada
 help / color / mirror / Atom feed
From: Dmitry A. Kazakov <mailbox@dmitry-kazakov.de>
Subject: Re: Question about OO programming in Ada
Date: Wed, 03 Dec 2003 14:41:59 +0100
Date: 2003-12-03T14:41:59+01:00	[thread overview]
Message-ID: <b0prsv8mp1a2qtadss47j7fpgg6nqumj8u@4ax.com> (raw)
In-Reply-To: m3y8tut6b3.fsf@insalien.org

On 03 Dec 2003 13:49:20 +0100, Ludovic Brenta
<ludovic.brenta@insalien.org> wrote:

>Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> writes:
>
>> On 03 Dec 2003 10:29:17 +0100, Pascal Obry <p.obry@wanadoo.fr> wrote:
>> 
>> >
>> >Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> writes:
>> >
>> >> All controlled objects allocated by the allocator new, being not
>> >> explicity destroyed using Unchecked_Deallocation, will be destroyed
>> >> upon finalization of the access type.
>> >
>> >There is no magic done by Finalize here! Finalize gives a chance to the
>> >component implementer to do the memory deallocation for the end user. That's
>> >all.
>> 
>> Yes, you are right. Controlled objects will be finalized, but the
>> memory might be not reclaimed. At least, it seems that ARM does not
>> require that.
>
>I am confused.  I thought that the storage would be reclaimed when the
>access type went out of scope, but I tested it with GNAT 3.15p and it
>isn't.  Granted, the ARM doesn't seem to require it, but GNAT has the
>following in its reference manual:
>
>    *53*.  The manner of choosing a storage pool for an access type
>    when `Storage_Pool' is not specified for the type.  See 13.11(17).
>
>    There are 3 different standard pools used by the compiler when
>    `Storage_Pool' is not specified depending whether the type is
>    local to a subprogram or defined at the library level and whether
>    `Storage_Size'is specified or not.  See documentation in the
>    runtime library units `System.Pool_Global', `System.Pool_Size' and
>    `System.Pool_Local' in files `s-poosiz.ads', `s-pooglo.ads' and
>    `s-pooloc.ads' for full details on the default pools used.
>
>And in s-pooloc.ads it says:
>
>   ----------------------------
>   -- Unbounded_Reclaim_Pool --
>   ----------------------------
>
>   --  Allocation strategy:
>
>   --    Call to malloc/free for each Allocate/Deallocate
>   --    no user specifiable size
>   --    Space of allocated objects is reclaimed at pool finalization
>   --    Manages a list of allocated objects
>
>   --  Default pool in the compiler for access types locally declared
>
>   type Unbounded_Reclaim_Pool is new [...]
>
>I tried to run a test program in gnatgdb but couldn't check what the
>storage pool actually was.  Would anyone care to shed some light on
>this?  Now I'm really curious.

No storage pool can help, if deallocator is not called. If we continue
the example:

with Ada.Finalization;
with System;  use System;
with System.Storage_Elements;  use System.Storage_Elements;
with System.Storage_Pools;  use System.Storage_Pools;
package A is
   type Misty is
      new Ada.Finalization.Limited_Controlled with null record;
   procedure Finalize (X : in out Misty);
   type Pool is new Root_Storage_Pool with record
      Free   : Storage_Offset := 1;
      Memory : aliased Storage_Array (1..1024);
   end record;
   procedure Allocate
             (  Stack     : in out Pool;
                Place     : out Address;
                Size      : Storage_Count;
                Alignment : Storage_Count
             );
   procedure Deallocate
             (  Stack     : in out Pool;
                Place     : Address;
                Size      : Storage_Count;
                Alignment : Storage_Count
             );
   function Storage_Size (Stack : Pool) return Storage_Count; 
end A;

with Text_IO; use Text_IO;
package body A is
   procedure Finalize (X : in out Misty) is
   begin
      Put_Line ("finalized!");
   end Finalize;
   procedure Allocate
             (  Stack     : in out Pool;
                Place     : out Address;
                Size      : Storage_Count;
                Alignment : Storage_Count
             )  is
   begin -- Alignment is ignored for simplicity sake
      Put_Line ("Allocated");
      Place := Stack.Memory (Stack.Free)'Address;
      Stack.Free := Stack.Free + Size;
   end Allocate;
   procedure Deallocate
             (  Stack     : in out Pool;
                Place     : Address;
                Size      : Storage_Count;
                Alignment : Storage_Count
             )  is
   begin -- Does nothing
      Put_Line ("Deallocated");   
   end Deallocate;
   function Storage_Size (Stack : Pool) return Storage_Count is
   begin
      return 0;
   end Storage_Size; 
end A;

with A;  use A;
with Text_IO;  use Text_IO;
procedure Test is
   Storage : Pool;
begin
   Put_Line ("Begin of a scope");
   declare
      type Pointer is access Misty;
      for Pointer'Storage_Pool use Storage;
      X : Pointer;
   begin
      X := new Misty; -- This is not dangled!
   end;
   Put_Line ("End of the scope");
end Test;

With GNAT Test should print:

Begin of a scope
Allocated
finalized!
End of the scope

So even with a user-defined pool it does not work as "should be".

Of course one could call Unchecked_Deallocation from Finalize as
Pascal noted. But it would be awful for an uncounted number of
reasons. To start with, you never know, who calls Finalize, then you
have no pointer to the object, you can get it, but that will be of a
generic access type, so if you have several pools how do you know,
where the object was allocated? and so on and so far.

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



  reply	other threads:[~2003-12-03 13:41 UTC|newest]

Thread overview: 109+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-25 19:04 Question about OO programming in Ada Ekkehard Morgenstern
2003-11-25 20:17 ` Randy Brukardt
2003-11-26  0:34   ` Ekkehard Morgenstern
2003-11-26  6:17     ` Vinzent 'Gadget' Hoefler
2003-11-26  9:29     ` Dmitry A. Kazakov
2003-11-26 15:54     ` Stephen Leake
2003-11-26 20:07       ` Randy Brukardt
2003-11-26 21:36         ` Stephen Leake
2003-11-26  8:56   ` Peter Hermann
2003-11-25 20:55 ` Martin Krischik
2003-11-26  0:22   ` Ekkehard Morgenstern
2003-11-26  1:00     ` Jeffrey Carter
2003-11-26 16:36     ` Martin Krischik
2003-11-26 18:09       ` Robert I. Eachus
2003-11-27 13:45         ` Jean-Pierre Rosen
2003-11-25 21:48 ` Stephen Leake
2003-11-26  0:01   ` Ekkehard Morgenstern
2003-11-26  1:16     ` Jeffrey Carter
2003-11-26 15:10     ` Georg Bauhaus
2003-11-26 15:48     ` Stephen Leake
2003-11-26 16:24       ` Hyman Rosen
2003-11-26 17:58     ` Robert I. Eachus
2003-11-27  2:10       ` Ekkehard Morgenstern
2003-11-27 10:15         ` Ludovic Brenta
2003-11-27 18:35         ` Jeffrey Carter
2003-11-28  4:35           ` Hyman Rosen
2003-11-28  7:28             ` Vinzent 'Gadget' Hoefler
2003-11-28  8:46               ` Dale Stanbrough
2003-11-28 10:16                 ` Vinzent 'Gadget' Hoefler
2003-12-01 15:57             ` Martin Krischik
2003-12-01 16:47               ` Hyman Rosen
2003-12-03 18:35                 ` Martin Krischik
2003-12-01 21:13               ` Jeffrey Carter
2003-12-02  8:47               ` Dmitry A. Kazakov
2003-12-03  9:29                 ` Pascal Obry
2003-12-03 11:26                   ` Dmitry A. Kazakov
2003-12-03 12:49                     ` Ludovic Brenta
2003-12-03 13:41                       ` Dmitry A. Kazakov [this message]
2003-12-03 14:11                         ` Ludovic Brenta
2003-12-03 14:45                           ` Dmitry A. Kazakov
2003-12-03 15:44                         ` Hyman Rosen
2003-12-03 16:11                           ` Dmitry A. Kazakov
2003-12-03 18:20                           ` David C. Hoos
     [not found]                           ` <28eb01c3b9ca$25b18870$b101a8c0@sy.com>
2003-12-03 18:35                             ` Hyman Rosen
2003-12-03 20:05                           ` Randy Brukardt
2003-12-03 20:57                             ` Hyman Rosen
2003-12-03 21:16                               ` Hyman Rosen
2003-12-03 22:04                           ` Pascal Obry
2003-12-03 22:34                             ` Hyman Rosen
2003-12-04  1:23                               ` Robert I. Eachus
2003-12-04  7:15                                 ` Hyman Rosen
2003-12-04 17:43                                   ` Warren W. Gay VE3WWG
2003-12-04  8:55                                 ` Dmitry A. Kazakov
2003-12-04 19:13                                   ` Randy Brukardt
2003-12-04 19:29                                     ` Hyman Rosen
2003-12-04 21:32                                   ` Robert I. Eachus
2003-12-05  8:43                                     ` Dmitry A. Kazakov
2003-11-27 22:12         ` Robert I. Eachus
2003-11-28  6:37           ` Simon Wright
2003-11-30  2:51             ` Robert I. Eachus
2003-12-06  7:48 ` Chad Bremmon
2003-12-06 13:33   ` Jeff C,
2003-12-06 22:44   ` Hyman Rosen
2003-12-07  3:02     ` Chad Bremmon
2003-12-07  7:53       ` Hyman Rosen
2003-12-07 15:34         ` James Rogers
2003-12-07 18:30           ` Martin Krischik
2003-12-07 20:25             ` James Rogers
2003-12-08  3:36               ` Hyman Rosen
2003-12-08  4:42                 ` Chad Bremmon
2003-12-08  8:42                   ` Hyman Rosen
2003-12-08  9:34                     ` Dmitry A. Kazakov
2003-12-08 13:25                       ` Hyman Rosen
2003-12-08 15:05                         ` Dmitry A. Kazakov
2003-12-09  4:38                           ` Hyman Rosen
2003-12-09  8:19                             ` Dmitry A. Kazakov
2003-12-09 13:29                               ` Hyman Rosen
2003-12-09 14:36                                 ` Dmitry A. Kazakov
2003-12-09 15:05                                   ` Hyman Rosen
2003-12-09 15:59                                     ` Dmitry A. Kazakov
2003-12-09 16:41                                       ` Hyman Rosen
2003-12-10 11:32                                         ` Dmitry A. Kazakov
2003-12-10 15:27                                           ` Hyman Rosen
2003-12-10 17:15                                             ` Dmitry A. Kazakov
2003-12-08 17:55                       ` Chad Bremmon
2003-12-08 23:09                         ` Hyman Rosen
2003-12-09  8:26                         ` Dmitry A. Kazakov
2003-12-08 19:33                       ` Martin Krischik
2003-12-09  4:41                         ` Hyman Rosen
2003-12-08 17:27                     ` Chad Bremmon
2003-12-08 18:44                       ` Georg Bauhaus
2003-12-08 19:27                         ` Martin Krischik
2003-12-08 19:36                         ` Chad Bremmon
2003-12-09  4:43                           ` Hyman Rosen
2003-12-08 23:23                       ` Hyman Rosen
2003-12-08 19:25                     ` Martin Krischik
2003-12-07 21:29           ` Peter C. Chapin
2003-12-08  3:44             ` Hyman Rosen
2003-12-08  3:46           ` Hyman Rosen
2003-12-08  5:54             ` James Rogers
2003-12-08  8:45               ` Hyman Rosen
2003-12-07 17:39         ` Chad Bremmon
2003-12-08 23:39           ` Hyman Rosen
2003-12-09  2:36             ` Chad Bremmon
2003-12-09  4:52               ` Hyman Rosen
2003-12-09 11:24               ` Georg Bauhaus
2003-12-09 18:42                 ` Chad Bremmon
2003-12-09 20:11                   ` Hyman Rosen
2003-12-08 23:40           ` Hyman Rosen
replies disabled

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