From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,19140af19dfa6e01 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-09-14 06:19:28 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!newsread2.news.atl.earthlink.net.POSTED!not-for-mail Sender: mheaney@MHEANEYX200 Newsgroups: comp.lang.ada Subject: Re: Ada 0Y plans for garbage collection? References: From: Matthew Heaney Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Sun, 14 Sep 2003 13:19:28 GMT NNTP-Posting-Host: 65.110.133.134 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.news.atl.earthlink.net 1063545568 65.110.133.134 (Sun, 14 Sep 2003 09:19:28 EDT) NNTP-Posting-Date: Sun, 14 Sep 2003 09:19:28 EDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: archiver1.google.com comp.lang.ada:42478 Date: 2003-09-14T13:19:28+00:00 List-Id: Jano 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. 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.