draft proposal for ref counting in D

Walter Bright newshound2 at digitalmars.com
Wed Oct 9 18:46:34 PDT 2013


Michel Fortin wrote:

Le 2013-06-26 à 17:42, Rainer Schuetze <r.sagitario at gmx.de> a écrit :

 > On 26.06.2013 13:12, Michel Fortin wrote:
 >> Le 26-juin-2013 à 5:38, Walter Bright  a
 >> écrit :
 >>
 >>> On 6/26/2013 12:19 AM, Rainer Schuetze wrote:
 >>>>
 >>>> As Michel also said, the reference count does not have to be in
 >>>> inside the object itself, so we might want to allow reference
 >>>> counting on other types aswell.
 >>>
 >>> That opens the question of what is the point of other RC types? For
 >>> example, C++ can throw any type - but it turns out that throwing
 >>> anything but class types is largely pointless.
 >>
 >> RC is just another garbage collection scheme. You might favor it for
 >> its performance characteristics, its determinism, or the lower memory
 >> footprint.
 >>
 >> Or you might need it to interact with foreign code that relies on it
 >> (COM, Objective-C, etc.), in which case it needs to be customizable
 >> (use the foreign implementation) or be manually managed.
 >>
 >> That's two different use cases. And in the later case you can't use
 >> the GC to release cycles because foreign code is using memory
 >> invisible to the GC. It is important to note that when foreign code
 >> calls AddRef you don't want the GC to collect that object, at least
 >> not until Release is called.
 >
 > That means you have to maintain two reference counts if you have both ARC and 
COM used in the same class. ARC can only release the object if both counters are 
0, while the COM implementation has to add/remove roots to the GC when counting 
from/to 0 to avoid that the object is collected while external references exist.

Yes. The "external" reference count prevents the gc from releasing memory and 
the "internal" reference count keeps track of the number of references from 
gc-scanned memory. When both fall to zero, the memory is released immediately; 
when the external count falls to zero, the gc can collect memory if it isn't 
connected to any of its roots.

Note that you could implement the external count as a separate hash table 
that'll include a gc-scanned pointer to the object. That pointer would keep the 
internal count above zero as long as it is present in the table. The external 
count doesn't need to be formally part of the gc data structure.



More information about the Digitalmars-d mailing list