www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Trying to implement a reference counting mechanism

reply "Martin" <martinbbjerregaard gmail.com> writes:
http://pastebin.com/8M2EuyfT

I'm fairly new to D, but would this work? Obviously I would need 
to make my own kind of associative array that uses manual memory 
management to completely circumvent the GC, but besides that?
Mar 19 2013
next sibling parent reply "Brad Anderson" <eco gnuk.net> writes:
On Tuesday, 19 March 2013 at 22:06:31 UTC, Martin wrote:
 http://pastebin.com/8M2EuyfT

 I'm fairly new to D, but would this work? Obviously I would 
 need to make my own kind of associative array that uses manual 
 memory management to completely circumvent the GC, but besides 
 that?
Have you seen std.typecons.RefCounted[1]? If you are just looking to learn more so than get a RefCounted class you could take some idea's from its source code[2]. [2] https://github.com/D-Programming-Language/phobos/blob/master/std/typecons.d#L2644
Mar 19 2013
parent "Martin" <martinbbjerregaard gmail.com> writes:
On Tuesday, 19 March 2013 at 22:09:22 UTC, Brad Anderson wrote:
 On Tuesday, 19 March 2013 at 22:06:31 UTC, Martin wrote:
 http://pastebin.com/8M2EuyfT

 I'm fairly new to D, but would this work? Obviously I would 
 need to make my own kind of associative array that uses manual 
 memory management to completely circumvent the GC, but besides 
 that?
Have you seen std.typecons.RefCounted[1]? If you are just looking to learn more so than get a RefCounted class you could take some idea's from its source code[2]. [2] https://github.com/D-Programming-Language/phobos/blob/master/std/typecons.d#L2644
Yes, I have indeed studied the RefCounted struct in std.typecons. Unfortunately it only works with structs, not classes. I just wanted to try a different approach to see if it would work
Mar 19 2013
prev sibling next sibling parent "Martin" <martinbbjerregaard gmail.com> writes:
I was inspired by the "Unmanaged" framework, and the usage would 
be something like:

class MyClass
{
private:
     int _a;
     int _b;

public:
     this(int a, int b)
     {
         _a = a;
         _b = b;
     }
}

class OtherClass
{
private:
     RefCountedObj!MyClass _myClassInstance;

public:
     this()
     {
         _myClassInstance = RefCountedObj!MyClass.create(10, 20);
     }
}
Mar 19 2013
prev sibling parent reply Mike Wey <mike-wey example.com> writes:
On 03/19/2013 11:06 PM, Martin wrote:
 http://pastebin.com/8M2EuyfT

 I'm fairly new to D, but would this work? Obviously I would need to make
 my own kind of associative array that uses manual memory management to
 completely circumvent the GC, but besides that?
It looks like your refCount is off by one, i think you need to set it to 1 in the refCountedObj function, or check for refCount < 0 before freeing the value. -- Mike Wey
Mar 20 2013
parent "Martin" <martinbbjerregaard gmail.com> writes:
On Wednesday, 20 March 2013 at 19:19:09 UTC, Mike Wey wrote:
 On 03/19/2013 11:06 PM, Martin wrote:
 http://pastebin.com/8M2EuyfT

 I'm fairly new to D, but would this work? Obviously I would 
 need to make
 my own kind of associative array that uses manual memory 
 management to
 completely circumvent the GC, but besides that?
It looks like your refCount is off by one, i think you need to set it to 1 in the refCountedObj function, or check for refCount < 0 before freeing the value.
Oh, my bad, I actually had that in the code, but when I copy/pasted it on pastebin, I had to remove some comments n stuff and I accidentally removed it along with a "import std.conv".
Mar 20 2013