From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: Ada 0Y plans for garbage collection?
Date: Sun, 14 Sep 2003 13:19:28 GMT
Date: 2003-09-14T13:19:28+00:00 [thread overview]
Message-ID: <usmmz8q1l.fsf@earthlink.net> (raw)
In-Reply-To: MPG.19c7f581f148da0798975a@News.CIS.DFN.DE
Jano <nono@celes.unizar.es> writes:
> I'm very happy using Ada but when one needs to use heap-allocated
> objects and unchecked deallocation it becomes tiresome rapidly, and that
> leaving aside memory leaks.
Why don't you simply encapsulate the allocation and deallocation behind
an abstraction?
For example, if I have a container like this:
procedure Op (C : in out CT; E : ET) is
begin
Insert (C, E);
...
Clear (C);
The memory necessary to store element E in container C is allocated
behind the scenes in the implementation of Insert. That same memory is
deallocated when the container is cleared.
The container itself is controlled, so that when the scope of the
container ends, all the memory it owns is deallocation automatically.
If you must manipulte pointers --say, you're doing class-wide
programming-- then you can still hide the allocation and deallocation
like this:
package P is
type T is abstract tagged null record;
type T_Class_Access is access all T'Class;
procedure Free (X : in out T_Class_Access);
...
end P;
package P.C is
type NT (<>) is new T with private;
function New_NT return T_Class_Access;
...
private
type NT is new NT with null record;
end;
By declaring the partial view of the type as indefinite, then you force
clients to use your contructor function:
declare
O : T_Class_Access := New_NT;
begin
...
Free (O);
end;
That hides explicit calls, and allows each type in the class to allocate
objects in whatever manner is most appropriate.
If you want to automate calls to Free, you can use something like the
auto_ptr in C++, which transfers ownership of an access object across
assignments. In the Charles library, there is similar abstraction,
allowing you to do this:
declare
O, O2 : Pointer_Type;
begin
Initialize (O, New_NT); --O owns ptr
Op (+O);
Assign (O2, O); --now O2 owns ptr
Op (+O2);
end; --no Free: O2 does it automatically
Here, when the Pointer_Type object is finalized, it calls the Free
operation automatically.
See Charles.Access_Control for the full implementation.
<http://home.earthlink.net/~matthewjheaney/charles/index.html>
If you need to have pointer sharing, then you can use reference
counting, something like:
package P is
type T is abstract tagged null record;
type T_Class_Access is access all T'Class;
type Handle_Type is private; --aka "smart pointer"
...
end P;
package P.C is
type NT is new T with private;
function New_NT return Handle_Type;
end;
During assignment, the reference count is bumped:
declare
O1 : Handle_Type := New_NT; --cnt is 1
begin
Op (+O1);
declare
O2 : Handle_Type := O1; --inc cnt
begin
Op (+O2);
end; --dec cnt
end; --dec cnt; cnt = 0; call Free
The point is that the language already provides many abstraction
mechanims to make manipulation of memory easy and safe, and demonstrated
above.
GNAT also provides a debug storage pool, that can help find errors in
heap memory use. They'll probably add a garbage-collected pool if
there's interest.
prev parent reply other threads:[~2003-09-14 13:19 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-09-09 13:35 Ada 0Y plans for garbage collection? Jano
2003-09-09 16:24 ` Martin Krischik
2003-09-09 18:30 ` Jeffrey Carter
2003-09-10 6:55 ` olehjalmar kristensen - Sun Microsystems - Trondheim Norway
2003-09-11 15:09 ` Martin Krischik
2003-09-11 21:21 ` Robert I. Eachus
2003-09-12 8:43 ` Dmitry A. Kazakov
2003-09-12 15:38 ` Martin Krischik
2003-09-15 13:33 ` Dmitry A. Kazakov
2003-09-16 18:04 ` Martin Krischik
2003-09-12 15:49 ` Martin Krischik
2003-09-14 19:26 ` Matthew Heaney
2003-09-14 21:46 ` Berend de Boer
2003-09-14 21:58 ` Ludovic Brenta
2003-09-15 1:43 ` Robert I. Eachus
2003-09-16 16:48 ` Jon S. Anthony
2003-09-16 21:45 ` Berend de Boer
2003-09-17 1:39 ` Jeffrey Creem
2003-09-17 3:38 ` Larry Kilgallen
2003-09-17 14:14 ` Jon S. Anthony
2003-09-17 17:23 ` Gautier Write-only
2003-09-18 0:39 ` Berend de Boer
2003-09-18 16:52 ` chris
2003-09-22 15:51 ` Robert I. Eachus
2003-09-22 16:29 ` Warren W. Gay VE3WWG
2003-09-22 16:30 ` chris
2003-09-23 9:26 ` Dmitry A. Kazakov
2003-09-24 1:46 ` Nick Roberts
2003-09-24 14:28 ` Maxim S. Shatskih
2003-09-24 16:01 ` Preben Randhol
2003-09-24 16:52 ` Stephane Richard
2003-09-24 17:19 ` chris
2003-09-25 17:49 ` OT " Pascal Obry
2003-09-25 19:30 ` Pascal Obry
2003-09-25 20:32 ` tmoran
2003-09-26 18:57 ` Pascal Obry
2003-09-27 0:40 ` Stephane Richard
2003-09-27 8:05 ` Preben Randhol
2003-09-27 10:01 ` chris
2003-09-24 16:33 ` Time Taken to Boot Nick Roberts
2003-09-24 20:11 ` Mark A. Biggar
2003-09-25 15:14 ` Maxim S. Shatskih
2003-09-25 17:29 ` Warren W. Gay VE3WWG
2003-09-25 15:00 ` Ada 0Y plans for garbage collection? Robert C. Leif
2003-09-12 17:20 ` Robert I. Eachus
2003-09-13 9:54 ` Martin Krischik
2003-09-13 20:37 ` Robert I. Eachus
2003-09-15 13:33 ` Dmitry A. Kazakov
2003-09-15 21:17 ` Alexander Kopilovitch
2003-09-14 19:10 ` Matthew Heaney
2003-09-15 6:48 ` Martin Krischik
2003-09-14 13:19 ` Matthew Heaney [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox