[Dlang-study] [lifetime] Few root decisions to take on RC classes

Michel Fortin michel.fortin at michelf.ca
Fri Oct 30 19:27:13 PDT 2015


Le 30 oct. 2015 à 17:31, Andrei Alexandrescu <andrei at erdani.com> a écrit :

> A few matters Walter and I just discussed and wanted to submit for scrutiny:
> 
> * @rc classes shall not be single-rooted. Rationale: there is no strong advantage in it and it would force all @rc classes to embed a vptr. This has been already discussed and largely agreed upon in this group.
> 
> * @rc classes will not use COM's AddRef and Release automatically. Rationale: (a) that's what C++'s shared_ptr does and not one soul ever complained; (b) the calls are virtual so less efficient and fusion is not possible; (c) it's a lot extra work for one platform.

Makes sense to me.


> * Undecided about integrating automatically with Objective-C's refcounts. I'm hoping more details from platform experts.

Three things to remember about Objective-C:

1. Parameters are passed non-consuming, except for methods of the "init" family that take `this` consumed.
2. Returned objects are autoreleased, but not for methods of the "init", "new", "copy" and "mutableCopy" families.
3. For function parameters that are pointers to NSError objects, the pointer points to an autoreleased NSError reference.

The compiler needs a way to know whether parameters are consumed, non-consummed, and autoreleased, and whether a function return an autoreleased object or not. Clang guesses almost always correctly by looking at the method name prefix, and when it does not there are some attributes you can use in the function signature to override the default.

The mechanics of Objective-C ARC are the same as anything else. The only difference is the addition of autorelease: you need to insert an extra opAutorelease call when returning an autoreleased object, and an extra opInc call when receiving one in the caller. For autoreleased object references passed to a function by a pointer or by 'ref', add an opAutorelease before the call, and an opInc after. That's it. Then let the optimizer do elision as usual: opAutorelease counts as an opDec with the only difference being that it cannot be elided.

-- 
Michel Fortin
michel.fortin at michelf.ca
https://michelf.ca




More information about the Dlang-study mailing list