digitalmars.D.learn - Soft/weak references?
- Robert Fraser (5/5) Jun 21 2007 Hi all,
- BCS (21/33) Jun 21 2007 fill in the dummy function and this might work
- Robert Fraser (2/3) Jun 21 2007 But this means there's still a reference to it.
- BCS (4/10) Jun 21 2007 It's a total hack. You get a reference to a chunk of memory that contain...
- Sean Kelly (9/22) Jun 21 2007 The missing piece is that this reference won't be invalidated if the
- Myron Alexander (19/44) Jun 21 2007 By pure coincidence, I have just discovered a need for weak references.
- Bill Baxter (7/59) Jun 22 2007 Look at the code in Phobos's std.signals for an example of how to use
- Myron Alexander (3/11) Jun 22 2007 Thanks.
- Myron Alexander (12/20) Jun 22 2007 I have attached my implementation of weak references. It has been a long...
- Robert Fraser (2/16) Jun 21 2007 All right, I'll give it a shot. Thanks!
- BCS (2/19) Jun 21 2007 BTW I haven't even tried to complile that code so...
- Myron Alexander (5/5) Jun 21 2007 Hello.
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
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, Fraserfill 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
BCS Wrote:T** ptr;But this means there's still a reference to it.
Jun 21 2007
Reply to Robert,BCS Wrote: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.T** ptr;But this means there's still a reference to it.
Jun 21 2007
BCS wrote:Reply to Robert,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. SeanBCS Wrote: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.T** ptr;But this means there's still a reference to it.
Jun 21 2007
Sean Kelly wrote:BCS wrote: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.Reply to Robert,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. SeanBCS Wrote: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.T** ptr;But this means there's still a reference to it.
Jun 21 2007
Myron Alexander wrote:Sean Kelly 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. --bbBCS wrote: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.Reply to Robert,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. SeanBCS Wrote: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.T** ptr;But this means there's still a reference to it.
Jun 22 2007
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. --bbThanks. Myron.
Jun 22 2007
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. --bbI 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
BCS Wrote:Reply to Robert,All right, I'll give it a shot. Thanks!BCS Wrote: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.T** ptr;But this means there's still a reference to it.
Jun 21 2007
Reply to Robert,BCS Wrote:BTW I haven't even tried to complile that code so...Reply to Robert,All right, I'll give it a shot. Thanks!BCS Wrote: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.T** ptr;But this means there's still a reference to it.
Jun 21 2007
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