Idea #1 on integrating RC with GC

Ola Fosheim Grøstad" <ola.fosheim.grostad+dlang at gmail.com> Ola Fosheim Grøstad" <ola.fosheim.grostad+dlang at gmail.com>
Wed Feb 5 06:04:09 PST 2014


On Wednesday, 5 February 2014 at 12:40:13 UTC, Manu wrote:
> Counter question; why approach it this way?
> Is there a reason that it needs to be of one kind or the other?

Sure, you could make all allocations with RC enabled make space 
for a counter at a negative offset (ptr-offset), but that would 
not work with C structs or internal pointers and aligned data 
might hog a bit of extra space.

You also need to handle internal pointers to embedded objects and 
arrays of objects (not pointers to objects). How do you ref count 
those? I guess you could switch all pointers into 
(allocated_base,offset) tuples and special case (ptr,0).

You could do like C++, have the ref counter be a separate object. 
Then record the properties of the pointer (such as offset), then 
have special magic checks for deallocation: testing for internal 
reference then decrease the real ref counter of the parent rather 
than deallocate. This is quite compatible with having a GC, you 
could free the object only when proven safe and just ignore it 
and leave it for GC when you cannot prove it.


IMHO you proabably need to redesign the language in order to 
support transparent or efficient automatic memory handling. If 
you retain C-legacy, you also retain manual memory handling or a 
limited set of opportunites for automatic garbage collection.



More information about the Digitalmars-d mailing list