Soft/weak references?

BCS ao at pathlink.com
Thu Jun 21 11:07:49 PDT 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;
  }
}




More information about the Digitalmars-d-learn mailing list