www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Automatic reference counting

reply Robert Clipsham <robert octarineparrot.com> writes:
Hey all,

For those of you that doesn't know, Objective-C primarily uses reference 
counting to manage memory. With the latest releases of clang, it also 
supports automatic reference counting, whereby the compiler 
automatically inserts retain/release messages ("calls" if you don't know 
Obj-C) into the code.

There's a complete spec for it at:
http://clang.llvm.org/docs/AutomaticReferenceCounting.html

My main reason for posting is curiosity - how feasible would it be to 
have something like this in D?

There are obviously a few issues that would need looking at, a way of 
marking weak references for example.

-- 
Robert
http://octarineparrot.com/
Aug 29 2011
parent Michel Fortin <michel.fortin michelf.com> writes:
On 2011-08-29 11:44:46 +0000, Robert Clipsham <robert octarineparrot.com> said:

 Hey all,
 
 For those of you that doesn't know, Objective-C primarily uses 
 reference counting to manage memory. With the latest releases of clang, 
 it also supports automatic reference counting, whereby the compiler 
 automatically inserts retain/release messages ("calls" if you don't 
 know Obj-C) into the code.
 
 There's a complete spec for it at:
 http://clang.llvm.org/docs/AutomaticReferenceCounting.html
 
 My main reason for posting is curiosity - how feasible would it be to 
 have something like this in D?

It all depends on what you want. Note that you can already create a struct that wraps any type while doing automatic reference counting. RefCounted in std.typecons is meant to work with non-class types (or so I think), but a similar version could be made to work with classes. Or you might want to create a class hierarchy which is automatically reference counted by the compiler. Any class in that hierarchy would see the compiler create 'retain' and 'release' calls as necessary to manage the reference counter, the rest of D would still use the GC. Or you might want to entirely replace the GC with a reference-counted scheme. Everything on the heap would be reference counted instead of garbage collected, from arrays to classes. Each of these have their advantages, but the further down you go the more complicated it becomes to implement.
 There are obviously a few issues that would need looking at, a way of 
 marking weak references for example.

Weak references are already a weak spot in D… even with a GC you sometime want weak references. Although the need for them is weaker with a GC than with reference counting. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Aug 29 2011