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,3498dd887729ed19 X-Google-Attributes: gid103376,public From: James Rogers Subject: Re: Garbage Collection in Ada Date: 1996/10/30 Message-ID: <3277815F.41C6@velveeta.apdev.cs.mci.com> X-Deja-AN: 193274582 references: <199610181934142408603@dialup101-3-15.swipnet.se> content-type: text/plain; charset=us-ascii organization: MCI Telecommunications Colorado Springs, CO mime-version: 1.0 newsgroups: comp.lang.ada x-mailer: Mozilla 2.0 (X11; I; AIX 2) Date: 1996-10-30T00:00:00+00:00 List-Id: Jonas Nygren wrote: > > To me GC solves the problem of deallocation when there are two or more > references > to the same object. Without GC this is a difficult problem to solve, > specially so > in very large programs, where you have the problem of possible dangling > pointers. > > I have tried Java a bit and recommend it to NO-GC advocates, it is > rather > refreshing not to have to think about deallocation. You should try it. > > /jonas The solution in non-garbage collection implementations is to use "smart pointers" or reference counting. An excellent example of how this is done in Ada comes from John English's new textbook "Ada 95: The Craft of Object-Oriented Programming" His example is: ---------------------------------------------------------------------------- -- -- File: je.ads -- Purpose: Ultimate parent of all example packages (specification) -- Author: John English (je@brighton.ac.uk) -- -- This code is from "Ada 95: The Craft of Object-Oriented Programming" -- by John English (Prentice Hall 1997). Copyright (c) John English. -- Permission is granted to copy and distribute this file for -- non-commercial use only. -- ---------------------------------------------------------------------------- package JE is -- an empty package! end JE; ---------------------------------------------------------------------------- -- -- File: je-pointers.ads -- Purpose: Generic "smart pointers" package (specification) -- Author: John English (je@brighton.ac.uk) -- -- This code is from "Ada 95: The Craft of Object-Oriented Programming" -- by John English (Prentice Hall 1997). Copyright (c) John English. -- Permission is granted to copy and distribute this file for -- non-commercial use only. -- ---------------------------------------------------------------------------- with Ada.Finalization; generic type Item_Type(<>) is limited private; type Access_Type is access Item_Type; package JE.Pointers is type Pointer_Type is private; function Pointer (Value : Access_Type) return Pointer_Type; function Value (Pointer : Pointer_Type) return Access_Type; private type Reference_Counted_Object is record Value : Access_Type; Count : Natural; end record; type Reference_Counted_Pointer is access Reference_Counted_Object; type Pointer_Type is new Ada.Finalization.Controlled with record Pointer : Reference_Counted_Pointer; end record; procedure Finalize (Object : in out Pointer_Type); procedure Adjust (Object : in out Pointer_Type); end JE.Pointers; ---------------------------------------------------------------------------- -- -- File: je-pointers.adb -- Purpose: Smart pointers package (body) -- Author: John English (je@brighton.ac.uk) -- -- This code is from "Ada 95: The Craft of Object-Oriented Programming" -- by John English (Prentice Hall 1997). Copyright (c) John English. -- Permission is granted to copy and distribute this file for -- non-commercial use only. -- ---------------------------------------------------------------------------- with Ada.Unchecked_Deallocation, Ada.Text_IO; package body JE.Pointers is procedure Delete_Item is new Ada.Unchecked_Deallocation (Item_Type, Access_Type); procedure Delete_Pointer is new Ada.Unchecked_Deallocation (Reference_Counted_Object, Reference_Counted_Pointer); function Pointer (Value : Access_Type) return Pointer_Type is Object : Pointer_Type; begin if Object.Pointer /= null then Delete_Item (Object.Pointer.Value); else Object.Pointer := new Reference_Counted_Object; end if; Object.Pointer.all := (Value => Value, Count => 1); return Object; end Pointer; function Value (Pointer : Pointer_Type) return Access_Type is begin if Pointer.Pointer = null then return null; else return Pointer.Pointer.Value; end if; end Value; procedure Finalize (Object : in out Pointer_Type) is begin Ada.Text_IO.Put ("Finalising pointer..."); if Object.Pointer /= null then Object.Pointer.Count := Object.Pointer.Count - 1; if Object.Pointer.Count = 0 then Ada.Text_IO.Put (" reference count = 0, deleting"); Delete_Item (Object.Pointer.Value); Delete_Pointer (Object.Pointer); else Ada.Text_IO.Put (" reference count > 0"); end if; else Ada.Text_IO.Put (" null, ignored"); end if; Ada.Text_IO.New_Line; end Finalize; procedure Adjust (Object : in out Pointer_Type) is begin Ada.Text_IO.Put ("Copying pointer..."); if Object.Pointer /= null then Object.Pointer.Count := Object.Pointer.Count + 1; else Ada.Text_IO.Put (" null, ignored"); end if; Ada.Text_IO.New_Line; end Adjust; end JE.Pointers; -- Jim Rogers ************************************************************* Team Ada