Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Ada > Re: Prohibiting...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 18 of 20 Topic 5612 of 5828
Post > Topic >>

Re: Prohibiting dynamic allocation for the given type

by Eric Hughes <eric.eh9@[EMAIL PROTECTED] > Mar 20, 2008 at 02:35 PM

On Mar 19, 6:48 pm, Robert A Duff <bobd...@[EMAIL PROTECTED]
>
wrote:
> Maybe you could give an outline of the code.

I'll presume the as-yet-unpublished Strong_Access and Weak_Access
(that's what I'm working on now) (think: like Boost shared_ptr and
weak_ptr).  Here's a bare outline.

type X is null record ;  -- nonce type
package Smart_Access_to_X is new Smart_Access( X ) ;
use Smart_Access_to_X ;
-- type Object is contingent upon one of its components for some or
all of its functionality
type Object is record
   Lifetime : Weak_Access ;
   -- ...
end record ;
....
procedure Operation( Obj : in out Object )
is begin
   if ( Is_Null( Obj.Lifetime ) ) then return ; end if ;
   -- Assert Lifetime referent continues to exist
   -- ...
end ;
....
procedure Foo
is
   Bar : Object_Access := new Object'( Lifetime =>
Null_Weak_Access ) ;
   -- Bar does not work, can't make it do anything.
begin
   declare
      Sentry : Strong_Access := Construct_Strong_Access( Access_Value
=> new X ) ;
   begin
      Bar.all := Object'( Lifetime =>
Copy_As_Weak_Access( Strong_Accessor => Sentry ) ) ;
      -- Bar now works
   end
   -- Sentry is now out of scope; referent implicitly deallocated;
weak reference to it is null; Bar has stopped working
end

> In particular, how do you ensure that the "sentry" is not
heap-allocated?

As I said in my original post, if you want scope lifetime, you need to
instantiate a scope dependency.  This technique does not enforce any
particular lifetime policy.

> If you trust the client on that, then why not trust
> the client to avoid heap allocation for the "real" object in the
> first place?

The distinction here is that the original object still exists.  Access
values to it will remain valid.  Depending on how the object is used,
it might be necessary to have that.  Personally, I can't imagine how
this might be useful unless two or more tasks are at issue, say,
passing a 'not null access all' parameter to a subprogram that assumes
continued existence of the referent of its parameter.  Since the
dependency mechanism is generic, it would be just as possible to make
the functional lifetime of a variable have the scope of some task.

The original question was somewhat theoretical, as is this answer.

Eric
 




 20 Posts in Topic:
Prohibiting dynamic allocation for the given type
Maciej Sobczak <see.my  2008-03-18 14:30:37 
Re: Prohibiting dynamic allocation for the given type
gpriv@[EMAIL PROTECTED]   2008-03-18 20:06:45 
Re: Prohibiting dynamic allocation for the given type
Maciej Sobczak <see.my  2008-03-19 01:24:57 
Re: Prohibiting dynamic allocation for the given type
Georg Bauhaus <rm.tsoh  2008-03-19 12:31:23 
Re: Prohibiting dynamic allocation for the given type
Georg Bauhaus <rm.tsoh  2008-03-19 23:17:46 
Re: Prohibiting dynamic allocation for the given type
gpriv@[EMAIL PROTECTED]   2008-03-19 06:13:16 
Re: Prohibiting dynamic allocation for the given type
Maciej Sobczak <see.my  2008-03-19 06:54:45 
Re: Prohibiting dynamic allocation for the given type
Pascal Obry <pascal@[E  2008-03-19 15:12:05 
Re: Prohibiting dynamic allocation for the given type
"Randy Brukardt"  2008-03-19 18:08:34 
Re: Prohibiting dynamic allocation for the given type
Simon Wright <simon.j.  2008-03-20 20:26:19 
Re: Prohibiting dynamic allocation for the given type
Adam Beneschan <adam@[  2008-03-19 07:38:37 
Re: Prohibiting dynamic allocation for the given type
gpriv@[EMAIL PROTECTED]   2008-03-19 08:43:17 
Re: Prohibiting dynamic allocation for the given type
Eric Hughes <eric.eh9@  2008-03-19 09:37:46 
Re: Prohibiting dynamic allocation for the given type
Robert A Duff <bobduff  2008-03-19 20:48:18 
Re: Prohibiting dynamic allocation for the given type
gpriv@[EMAIL PROTECTED]   2008-03-19 16:40:46 
Re: Prohibiting dynamic allocation for the given type
Robert A Duff <bobduff  2008-03-19 20:43:56 
Re: Prohibiting dynamic allocation for the given type
Maciej Sobczak <see.my  2008-03-20 14:11:29 
Re: Prohibiting dynamic allocation for the given type
Eric Hughes <eric.eh9@  2008-03-20 14:35:42 
Re: Prohibiting dynamic allocation for the given type
Eric Hughes <eric.eh9@  2008-03-20 15:03:46 
Re: Prohibiting dynamic allocation for the given type
"Randy Brukardt"  2008-03-20 20:04:06 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 14:44:04 CDT 2008.