[Dlang-study] [lifetime] Initial thoughts on lifetime management
Andrei Alexandrescu
andrei at erdani.com
Wed Oct 28 11:27:56 PDT 2015
On 10/28/2015 07:24 AM, Michel Fortin wrote:
> True. Note that "each reference" must also counts temporaries. So if you have this:
>
> foo(getObject());
>
> what you really have is this:
>
> foo(getObject(){-1});
>
> where {-1} is a notation I made up to denote that the counter of the object returned by getObject() is decremented at the end of the statement. (The returned value is already {+1} by the caller.)
Nice notation. There is an amendment I'd need to add (I think I
mentioned this already): at least in v1.0 we're considering having the
callER do the increment and the callEE do the decrement. This is how D
currently handles copy construction and destruction, and it turns out it
has a number of advantages over C++ (where the caller does all the work).
So, the protocol in foo(getObject()) is, foo's caller gets an rvalue
with the already-incremented refcount and bitblits it into the call to
foo, no additional operation. The code generated by foo includes the
refcount decrement.
This will make for a simple implementation for v1.0 (uses the same
backend as the ctors/dtors), but we need to figure whether that is the
optimal approach for manipulating refcounts.
> And if you have this:
>
> c = getObject();
>
> it becomes this:
>
> c{-1}{+1} = getObject(){-1};
Applying the already-discussed manipulation, the temporary returned by
getObject() comes with the already-bumped refcount so all we need to do
is decref on c prior to the assignment. Then bitblt.
Andrei
More information about the Dlang-study
mailing list