www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Soft/weak references?

reply Robert Fraser <fraserofthenight gmail.com> writes:
Hi all,

In Java, there's the concept of a "soft" reference to garbage-collectable data.
Basically, a soft reference is a refrence to data that can be collected once
there are no more strong references to it. It's useful for implementing, say,
caches bounded by actual usage and memory availability, rather than some
arbitrary limit.

Out of curiosity, is there any way to emulate this with the D GC?

Thanks,
Fraser
Jun 21 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Robert,

 Hi all,
 
 In Java, there's the concept of a "soft" reference to
 garbage-collectable data. Basically, a soft reference is a refrence to
 data that can be collected once there are no more strong references to
 it. It's useful for implementing, say, caches bounded by actual usage
 and memory availability, rather than some arbitrary limit.
 
 Out of curiosity, is there any way to emulate this with the D GC?
 
 Thanks,
 Fraser
fill in the dummy function and this might work struct soft(T) { uint hashOfValue; // used to test if memeory was GCed and realloced T** ptr; set(T* t) { ptr = gcAllocaNonPtrMemory(void*.sizeof); *ptr = t; hash = t.hashOf; } get() { if(wontAV(ptr) && **ptr.hashOf == has) return *ptr; else retrun null; } }
Jun 21 2007
next sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
BCS Wrote:

   T** ptr;
But this means there's still a reference to it.
Jun 21 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Robert,

 BCS Wrote:
 
 T** ptr;
 
But this means there's still a reference to it.
It's a total hack. You get a reference to a chunk of memory that contains a reference to the data, but you lie to the GC and tell it that the chunk of memory doesn't contain any references so it won't notice the refernce.
Jun 21 2007
next sibling parent reply Sean Kelly <sean f4.ca> writes:
BCS wrote:
 Reply to Robert,
 
 BCS Wrote:

 T** ptr;
But this means there's still a reference to it.
It's a total hack. You get a reference to a chunk of memory that contains a reference to the data, but you lie to the GC and tell it that the chunk of memory doesn't contain any references so it won't notice the refernce.
The missing piece is that this reference won't be invalidated if the object is collected. Phobos has Object.notifyRegister() for this purpose, but it currently does not exist in Tango because such a callback could easily deadlock the app if it enters a "synchronized" block as a part of its processing. In short, I don't know of any entirely safe means of implementing a weak pointer in D. If someone can suggest one, I'd be glad to hear it. Sean
Jun 21 2007
parent reply Myron Alexander <someone somewhere.com> writes:
Sean Kelly wrote:
 BCS wrote:
 Reply to Robert,

 BCS Wrote:

 T** ptr;
But this means there's still a reference to it.
It's a total hack. You get a reference to a chunk of memory that contains a reference to the data, but you lie to the GC and tell it that the chunk of memory doesn't contain any references so it won't notice the refernce.
The missing piece is that this reference won't be invalidated if the object is collected. Phobos has Object.notifyRegister() for this purpose, but it currently does not exist in Tango because such a callback could easily deadlock the app if it enters a "synchronized" block as a part of its processing. In short, I don't know of any entirely safe means of implementing a weak pointer in D. If someone can suggest one, I'd be glad to hear it. Sean
By pure coincidence, I have just discovered a need for weak references. I am implementing chained exceptions that use property bags for storing additional information about the exception. Each exception class in the chain has it's own property bag. When I get a property, I want to search all the bags starting at the first exception in the chain, iterating through the chain until the first property with the given name is found. At the moment, the chain is a single linked list without predecessor reference so a get on middle node reference will not be able to search the preceding node bags. To fix this, I want to create a double linked list but for the sake of the GC, I want strong references in the forward direction, and weak references in the reverse direction. I am using Phobos and have no intention of moving the code to Tango (nothing against Tango, my project is still in the baby steps phase). If anyone could point me to a functional weak reference implementation for Phobos, I would be most thankful. Thanks ahead and best regards, Myron.
Jun 21 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Myron Alexander wrote:
 Sean Kelly wrote:
 BCS wrote:
 Reply to Robert,

 BCS Wrote:

 T** ptr;
But this means there's still a reference to it.
It's a total hack. You get a reference to a chunk of memory that contains a reference to the data, but you lie to the GC and tell it that the chunk of memory doesn't contain any references so it won't notice the refernce.
The missing piece is that this reference won't be invalidated if the object is collected. Phobos has Object.notifyRegister() for this purpose, but it currently does not exist in Tango because such a callback could easily deadlock the app if it enters a "synchronized" block as a part of its processing. In short, I don't know of any entirely safe means of implementing a weak pointer in D. If someone can suggest one, I'd be glad to hear it. Sean
By pure coincidence, I have just discovered a need for weak references. I am implementing chained exceptions that use property bags for storing additional information about the exception. Each exception class in the chain has it's own property bag. When I get a property, I want to search all the bags starting at the first exception in the chain, iterating through the chain until the first property with the given name is found. At the moment, the chain is a single linked list without predecessor reference so a get on middle node reference will not be able to search the preceding node bags. To fix this, I want to create a double linked list but for the sake of the GC, I want strong references in the forward direction, and weak references in the reverse direction. I am using Phobos and have no intention of moving the code to Tango (nothing against Tango, my project is still in the baby steps phase). If anyone could point me to a functional weak reference implementation for Phobos, I would be most thankful. Thanks ahead and best regards, Myron.
Look at the code in Phobos's std.signals for an example of how to use the notifyRegister thing to implement weak references. Note that this does not give you general purpose weak references. It will only work for class instances. It will not give you weak references to structs or arrays. --bb
Jun 22 2007
next sibling parent Myron Alexander <someone somewhere.com> writes:
Bill Baxter wrote:
 Look at the code in Phobos's std.signals for an example of how to use 
 the notifyRegister thing to implement weak references.
 
 Note that this does not give you general purpose weak references.  It 
 will only work for class instances.  It will not give you weak 
 references to structs or arrays.
 
 --bb
Thanks. Myron.
Jun 22 2007
prev sibling parent Myron Alexander <someone somewhere.com> writes:
Bill Baxter wrote:
 Look at the code in Phobos's std.signals for an example of how to use 
 the notifyRegister thing to implement weak references.
 
 Note that this does not give you general purpose weak references.  It 
 will only work for class instances.  It will not give you weak 
 references to structs or arrays.
 
 --bb
I have attached my implementation of weak references. It has been a long time since I used pointers and I'm not quite sure of my understanding of D's handling of pointers so if you see something wrong, or if you know of a better way to do this, please let me know. Best regards, Myron. dprogramming...myron...alexander...com replace the first ... with , remove the second, and replace the third with ".". P.S. I canceled the original message as a comment in the source was inaccurate.
Jun 22 2007
prev sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
BCS Wrote:

 Reply to Robert,
 
 BCS Wrote:
 
 T** ptr;
 
But this means there's still a reference to it.
It's a total hack. You get a reference to a chunk of memory that contains a reference to the data, but you lie to the GC and tell it that the chunk of memory doesn't contain any references so it won't notice the refernce.
All right, I'll give it a shot. Thanks!
Jun 21 2007
parent BCS <ao pathlink.com> writes:
Reply to Robert,

 BCS Wrote:
 
 Reply to Robert,
 
 BCS Wrote:
 
 T** ptr;
 
But this means there's still a reference to it.
It's a total hack. You get a reference to a chunk of memory that contains a reference to the data, but you lie to the GC and tell it that the chunk of memory doesn't contain any references so it won't notice the refernce.
All right, I'll give it a shot. Thanks!
BTW I haven't even tried to complile that code so...
Jun 21 2007
prev sibling parent Myron Alexander <someone somewhere.com> writes:
Hello.

Has anyone implemented soft/weak/phantom references for D?

Is it possible to implement these without changing the current Phobos GC?

Regards,

Myron.
Jun 21 2007