Jump to page: 1 2
Thread overview
Soft/weak references?
Jun 21, 2007
Robert Fraser
Jun 21, 2007
BCS
Jun 21, 2007
Robert Fraser
Jun 21, 2007
BCS
Jun 21, 2007
Sean Kelly
Jun 22, 2007
Myron Alexander
Jun 22, 2007
Bill Baxter
Jun 22, 2007
Myron Alexander
My implementation - was - Re: Soft/weak references?
Jun 22, 2007
Myron Alexander
Jun 22, 2007
Robert Fraser
Jun 22, 2007
BCS
Jun 21, 2007
Myron Alexander
June 21, 2007
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
June 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,
> 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;
 }
}


June 21, 2007
BCS Wrote:

>   T** ptr;

But this means there's still a reference to it.
June 21, 2007
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.


June 21, 2007
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
June 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.
June 22, 2007
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.
June 22, 2007
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!

June 22, 2007
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... 


June 22, 2007
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
« First   ‹ Prev
1 2