comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <jeffrey.carter@boeing.com>
Subject: Re: Question about Finalization Control and reference counting
Date: Wed, 25 Apr 2001 22:22:15 GMT
Date: 2001-04-25T22:22:15+00:00	[thread overview]
Message-ID: <3AE74E17.FD004E37@boeing.com> (raw)
In-Reply-To: slrn9edt40.9fn.randhol+abuse@kiuk0156.chembio.ntnu.no

Preben Randhol wrote:
> 
> On Tue, 24 Apr 2001 16:11:33 GMT, Jeffrey Carter wrote:
> > Adjust only applies to a controlled type, so you need to put your access
> > value inside a controlled type:
> >
> > type Node;
> >
> > type Node_Ptr is access all Node;
> >
> > type Node is new [Limited_]Controlled with record
> >    Prev  : Node_Ptr;
> >    Data  : Whatever;
> >    Count : Natural := 0;
> >    Next  : Node_Ptr;
> > end record;
> >
> > procedure Adjust (Object : in out Node);
> >
> > procedure Finalize (Object : in out Node);
> 
> But the problem is that if you have :
> 
>    Node1 : Node_Ptr;
>    Node2 : Node_Ptr;
>    Node3 : Node;
> 
>    ...
> 
>    After making the Nodes with new you:
> 
>    Node1.Next := Node2;
> 
>    Then Adjust of Node2 won't be called it will only be called if you
>    do:
> 
>    Node3 := Node1.all;
> 
>    if I understand things correctly. I think I need to rethink my
>    problem in a more Ada approach :-)

Perhaps I'm trying to be too user friendly. Or maybe I'm failing. For a
real list component, Node would not be controlled, but the list type
would.

A list component will generally not make its access type visible to its
clients. All modification of a list will be through the operations
provided, so freeing space when a node is deleted can be handled
properly through those operations. You would rather want to define
Adjust and Finalize for an entire List, to ensure that memory is managed
properly.

If you do really want to control individual nodes, then you need to
replace the access type with a "smart pointer", an access value inside a
controlled type that points to an object with a reference count. Adjust
would increment the count, and Finalize would decrement it and
deallocate the object when it reaches zero. Then

Node1.Next := Node2;

will Finalize Node1 and Adjust its new value. However, to access the
node directly, you have to say

Node.Value.all.

--
Jeff Carter



  parent reply	other threads:[~2001-04-25 22:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-21 12:37 Question about Finalization Control and reference counting Preben Randhol
2001-04-21 12:44 ` Preben Randhol
2001-04-24 16:11   ` Jeffrey Carter
2001-04-25 15:58     ` Preben Randhol
2001-04-25 16:01       ` Preben Randhol
2001-04-25 22:22       ` Jeffrey Carter [this message]
2001-04-26  8:50         ` Preben Randhol
2001-04-25 16:57     ` Stanley R. Allen
2001-04-24 16:10 ` Alexander Boucke
2001-04-25  3:14 ` DuckE
  -- strict thread matches above, loose matches on Subject: below --
2001-04-25 11:08 Christoph Grein
replies disabled

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