34 PSpaceRoot
WARNING: This could easily be confused with PSpaceRoots
A PSpace needs to make sure various pointers to IObjects are visited by the CSpace GC or else they will be garbage collected.
$adt+ implement PSpace isa IObject
{
void VisitObjects(IObjectVisitor& v) const
{
m_dq.VisitObjects(v);
m_roots.VisitObjects(v);
m_dos.VisitObjects(v);
}
};
Normally a GC root of a CSpace must be heap allocated, registered in the GC extents, and will be deleted
when the CSpace is destroyed.
A PSpace can't directly be a GC root of its associated CSpace because it will end up being
deleted by its own CSpace.
A PSpaceRoots is a small object that is heap allocated, registered in the GC extents and added as
a GC root for the CSpace associated with a PSpace.
$class PSpaceRoot isa IObject
{
cxNotCloneable(PSpaceRoot)
public:
PSpaceRoot(PSpace& pspace) : m_pspace(pspace) {}
void VisitObjects(IObjectVisitor& v) const
{
m_pspace.VisitObjects(v);
}
private:
PSpace& m_pspace;
};
The PSpace creates a PSpaceRoots object in its constructor, registers it in the GC extend and adds
it as a GC root so it can't be deleted until the CSpace is destroyed:
PSpace::PSpace(PersistStore& ps, ConstStringZ name, OID oid, bool creating, CSpace* cspace) ...
{
PSpaceRoot* r = new PSpaceRoot(*this);
RegisterGcObject(m_cspace,r);
AddGcRoot(m_cspace,r);
}
Proposal
PSpaceRoot is not needed, there is a way to register the PSpace as a GC root without it being deleted by the CSpace.
We can instead register the PSpace by calling RegisterNonGcObject(m_cspace,this), then AddGcRoot(m_cspace,this).
PSpace::PSpace(PersistStore& ps, ConstStringZ name, OID oid, bool creating, CSpace* cspace) ...
{
RegisterNonGcObject(m_cspace,this);
AddGcRoot(m_cspace,this);
}
void PSpace::Close()
{
RemoveGcRoot(m_cspace,this);
}
That ensures the CSpace never tries to delete the PSpace. We should be sure to call RemoveGcRoot(m_cspace,this) when the PSpace is closed, given that the application may have created the CSpace and provided it to the PSpace when the PSpace was opened. That can mean the CSpace needs to outlive the PSpace?