]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/fl/garbagec.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Garbage collection algorithm for optimizing refresh.
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleksandras Gluchovas
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "garbagec.h"
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
29 #include "wx/fl/garbagec.h"
31 /***** Implementation for class GarbageCollector *****/
33 inline static GCItem
& node_to_item( wxNode
* pNode
)
35 return *( (GCItem
*)(pNode
->GetData()) );
38 GarbageCollector::~GarbageCollector()
43 /*** GC alg. helpers ***/
45 void GarbageCollector::DestroyItemList( wxList
& lst
)
47 wxNode
* pNode
= lst
.GetFirst();
51 delete &node_to_item( pNode
);
53 pNode
= pNode
->GetNext();
59 wxNode
* GarbageCollector::FindItemNode( void* pForObj
)
61 wxNode
* pNode
= mAllNodes
.GetFirst();
65 if ( node_to_item( pNode
).mpObj
== pForObj
)
69 pNode
= pNode
->GetNext();
75 wxNode
* GarbageCollector::FindReferenceFreeItemNode()
77 wxNode
* pNode
= mAllNodes
.GetFirst();
81 if ( node_to_item( pNode
).mRefs
.GetCount() == 0 )
85 pNode
= pNode
->GetNext();
91 void GarbageCollector::RemoveReferencesToNode( wxNode
* pItemNode
)
93 wxNode
* pNode
= mAllNodes
.GetFirst();
97 wxList
& refLst
= node_to_item( pNode
).mRefs
;
98 wxNode
* pRefNode
= refLst
.GetFirst();
102 if ( pRefNode
->GetData() == (wxObject
*)pItemNode
)
104 wxNode
* pNext
= pRefNode
->GetNext();
106 refLst
.DeleteNode( pRefNode
);
112 else pRefNode
= pRefNode
->GetNext();
115 pNode
= pNode
->GetNext();
119 void GarbageCollector::ResolveReferences()
121 wxNode
* pNode
= mAllNodes
.GetFirst();
125 GCItem
& item
= node_to_item( pNode
);
127 wxNode
* pRefNode
= item
.mRefs
.GetFirst();
131 pRefNode
->SetData( (wxObject
*) FindItemNode( (void*)pRefNode
->GetData() ) );
133 pRefNode
= pRefNode
->GetNext();
136 pNode
= pNode
->GetNext();
140 void GarbageCollector::AddObject( void* pObj
, int WXUNUSED(refCnt
) )
142 // FOR NOW:: initial ref-count is not used
144 GCItem
* pItem
= new GCItem();
148 mAllNodes
.Append( (wxObject
*) pItem
);
151 void GarbageCollector::AddDependency( void* pObj
, void* pDependsOnObj
)
153 node_to_item( FindItemNode( pObj
) ).mRefs
.Append( (wxObject
*)pDependsOnObj
);
156 /*** GC alg. implementation ***/
158 void GarbageCollector::ArrangeCollection()
164 // find node, which does not depend on anything
166 wxNode
* pItemNode
= FindReferenceFreeItemNode();
170 // append it to the list, where items are contained
171 // in the increasing order of dependencies
173 mRegularLst
.Append( pItemNode
->GetData() );
175 mAllNodes
.DeleteNode( pItemNode
);
177 // remove references to this current "least-dependent" node
178 // from reference lists of all the other nodes
180 RemoveReferencesToNode( pItemNode
);
184 // otherwise, what is left - all nodes, which
185 // are involved into cycled chains (rings)
187 wxNode
* pNode
= mAllNodes
.GetFirst();
191 mCycledLst
.Append( pNode
->GetData() );
193 pNode
= pNode
->GetNext();
200 // continue search for "least-dependent" nodes
205 wxList
& GarbageCollector::GetRegularObjects()
210 wxList
& GarbageCollector::GetCycledObjects()
215 void GarbageCollector::Reset()
217 DestroyItemList( mAllNodes
);
218 DestroyItemList( mRegularLst
);
219 DestroyItemList( mCycledLst
);