draft proposal for ref counting in D

Walter Bright newshound2 at digitalmars.com
Wed Oct 9 19:23:02 PDT 2013


Michel Fortin wrote:
Le 29-juin-2013 à 6:08, Jacob Carlborg a écrit :

 > On 29 jun 2013, at 06:42, Walter Bright wrote:
 >
 >> I think the simplest thing is to not allow ref counted classes to implement 
interfaces other than ones derived from IUnknown.
 >
 > What about Objective-C interfaces?

Implementing ARC for Objective-C is going to require some more compiler support 
anyway (for autoreleased objects notably). Tweaking the compiler it so accepts 
Objective-C interfaces should just be a matter of tweaking the boolean 
expression that makes this check. "if (classdelc->objc && interfacedecl->objc) 
return true;" or something like that.

As for which function the compiler should call, it'll probably need to be 
special-cased for Objective-C in the compiler too. Here's the list of changes 
that'd be needed for Objective-C ARC:

== Retain ==
COM:  if (obj) obj->AddRef();
ObjC: obj = objc_retain(obj);
ObjC blocks: obj = objc_retainBlock(obj); // objc_retainBlock might do a copy

== Release ==
COM:  if (obj) obj->Release();
ObjC: objc_release(obj);

== Assignment ==
COM:  if (obj) obj->AddRef(); if (var) var->Release(); var = obj;
ObjC: objc_storeStrong(&var, obj);
ObjC blocks: obj = objc_retainBlock(obj); objc_release(var); var = obj;

As long as Walter implements D ARC in a way we can make the above substitutions 
it shouldn't be too hard.

Then, support for autorelease is a matter of calling objc_autorelease on 
returned objects from autoreleasing functions, followed by objc_retain after the 
function call in the caller. We'll also have to check what ObjC ARC does for 
pointer write-backs to autoreleased variables and mimick that.

There's an optimized path for autoreleased return values that we should use, but 
I'd defer that to later. It involves a special no-op instruction to insert at 
the right place as a flag. Also, autoreleased returns should be eliminated when 
inlining.



More information about the Digitalmars-d mailing list