comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: Freeing Pointers to classwide types
Date: 1998/10/09
Date: 1998-10-09T00:00:00+00:00	[thread overview]
Message-ID: <m3r9wjvt6c.fsf@mheaney.ni.net> (raw)
In-Reply-To: 1ftmFTC69GA.191@samson.airnet.net

"joecool" <nobody@nowhere.com> writes:

> I understand (I think) how to dynamically allocate different objects within
> a class and place them in a heterogenous list but I'm not sure if I
> understand how to properly free the memory.
> 
> How do you go about freeing such critters?

I like each specific type in the hierarchy to maintain its own free list
of already-allocated objects.  Provide a class-wide operation that
dynamically dispatches a private Do_Free op.  Something like:

package P is 
   
   type T is abstract null record;

   type T_Access is access all T'Class;
   for T_Access'Storage_Size use 0;
   --
   -- By making this have a storage size of zero, you force 
   -- clients to call a constructor (allocator) for the specific
   -- type.

   procedure Free (O : in out T_Access);
...
private

   procedure Do_Free (O : access T);

end;

package body P is

   procedure Free (O : in out T_Access) is
   begin
      if O /= null then
         Do_Free (O);
         O := null;
      end if;
   end Free;

   procedure Do_Free (O : access T) is
   begin
      null;
   end;

end P;


Now create a derived type:

package P.Q is

   type NT is new T with private;

   function New_NT (...) return T_Access;
...
private

   type NT is ...;

   procedure Do_Free (O : access NT);

end;

package body P.Q is

   Free_List : <list of pointers to NT>;

   function New_NT (...) return T_Access is
   begin
      if Free_List = null then
         allocate new NT object, and return that
      else
         pop item off free list, and return that
      end if;
   end;

   procedure Do_Free (O : access NT) is
   begin
      <put O on the free list>
   end;

end P.Q;


I worked up an example of this kind of thing for the Interpreter
pattern.  Email me if you like a copy of the code/text.

Matt








  parent reply	other threads:[~1998-10-09  0:00 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-09-24  0:00 Freeing Pointers to classwide types joecool
1998-09-25  0:00 ` Tom Moran
1998-09-25  0:00   ` Bob Fletcher
1998-09-25  0:00     ` dennison
1998-09-25  0:00     ` Samuel Tardieu
1998-10-09  0:00     ` Matthew Heaney
1998-10-12  0:00       ` Mats Weber
1998-10-12  0:00         ` Pat Rogers
1998-09-25  0:00   ` dewarr
1998-09-25  0:00     ` Tom Moran
1998-09-25  0:00       ` dewarr
1998-09-26  0:00     ` Tom Moran
1998-09-26  0:00       ` dewarr
1998-09-26  0:00         ` Tom Moran
1998-09-27  0:00           ` dewarr
1998-09-27  0:00             ` Tom Moran
1998-09-28  0:00               ` dewarr
1998-09-28  0:00                 ` Tom Moran
1998-09-28  0:00                   ` dewarr
1998-09-28  0:00                     ` Tom Moran
1998-09-28  0:00                       ` Pat Rogers
1998-09-28  0:00                         ` Tom Moran
1998-09-28  0:00                           ` Pat Rogers
1998-09-29  0:00                           ` dewarr
1998-09-29  0:00                             ` Tom Moran
1998-09-30  0:00                               ` Tom Moran
1998-10-01  0:00                                 ` dewar
1998-10-01  0:00                                   ` Tom Moran
1998-10-01  0:00                                     ` dewarr
1998-10-01  0:00                                       ` Tom Moran
1998-10-01  0:00                                     ` dewarr
1998-10-01  0:00                                     ` Samuel Tardieu
1998-10-01  0:00                                       ` Tom Moran
1998-10-01  0:00                                         ` dennison
1998-10-01  0:00                                         ` Tucker Taft
1998-10-01  0:00                                           ` Tom Moran
1998-10-02  0:00                                           ` dennison
1998-10-02  0:00                                             ` dewarr
1998-10-02  0:00                                           ` dewarr
1998-10-02  0:00                                             ` Larry Kilgallen
1998-10-02  0:00                                               ` dewarr
1998-10-02  0:00                                         ` dewarr
1998-10-09  0:00                                           ` Matthew Heaney
1998-10-09  0:00                                             ` dennison
1998-10-09  0:00                                               ` Matthew Heaney
1998-09-28  0:00                       ` Tom Moran
1998-09-28  0:00                         ` Brian Rogoff
1998-09-28  0:00                   ` dewarr
1998-09-28  0:00                     ` Richard D Riehle
1998-09-28  0:00                       ` Pat Rogers
1998-09-29  0:00                       ` dewarr
1998-10-09  0:00           ` Matthew Heaney
1998-10-09  0:00   ` Matthew Heaney
1998-09-25  0:00 ` alan walkington
1998-09-26  0:00 ` Simon Wright
1998-10-09  0:00 ` Matthew Heaney [this message]
1998-10-09  0:00   ` Niklas Holsti
1998-10-10  0:00     ` Matthew Heaney
1998-10-11  0:00       ` Niklas Holsti
1998-10-11  0:00         ` Matthew Heaney
  -- strict thread matches above, loose matches on Subject: below --
1998-09-25  0:00 bpr5549
replies disabled

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