]>
git.saurik.com Git - wxWidgets.git/blob - utils/framelayout/src/garbagec.cpp
afb41e7ae8adf282dc01d210a82765727d2f448a
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleksandras Gluchovas
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "garbagec.cpp"
15 #pragma interface "garbagec.cpp"
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
33 /***** Implementation for class GarbageCollector *****/
35 inline static GCItem
& node_to_item( wxNode
* pNode
)
37 return *( (GCItem
*)(pNode
->Data()) );
40 GarbageCollector::~GarbageCollector()
45 /*** GC alg. helpers ***/
47 void GarbageCollector::DestroyItemList( wxList
& lst
)
49 wxNode
* pNode
= lst
.First();
53 delete &node_to_item( pNode
);
55 pNode
= pNode
->Next();
61 wxNode
* GarbageCollector::FindItemNode( void* pForObj
)
63 wxNode
* pNode
= mAllNodes
.First();
67 if ( node_to_item( pNode
).mpObj
== pForObj
)
71 pNode
= pNode
->Next();
74 wxASSERT(0); // DBG:: item should be presnet
79 wxNode
* GarbageCollector::FindRefernceFreeItemNode()
81 wxNode
* pNode
= mAllNodes
.First();
85 if ( node_to_item( pNode
).mRefs
.Number() == 0 )
89 pNode
= pNode
->Next();
95 void GarbageCollector::RemoveReferencesToNode( wxNode
* pItemNode
)
97 wxNode
* pNode
= mAllNodes
.First();
101 wxList
& refLst
= node_to_item( pNode
).mRefs
;
102 wxNode
* pRefNode
= refLst
.First();
106 if ( pRefNode
->Data() == (wxObject
*)pItemNode
)
108 wxNode
* pNext
= pRefNode
->Next();
110 refLst
.DeleteNode( pRefNode
);
116 else pRefNode
= pRefNode
->Next();
119 pNode
= pNode
->Next();
123 void GarbageCollector::ResolveReferences()
125 wxNode
* pNode
= mAllNodes
.First();
129 GCItem
& item
= node_to_item( pNode
);
131 wxNode
* pRefNode
= item
.mRefs
.First();
135 pRefNode
->SetData( (wxObject
*) FindItemNode( (void*)pRefNode
->Data() ) );
137 pRefNode
= pRefNode
->Next();
140 pNode
= pNode
->Next();
144 void GarbageCollector::AddObject( void* pObj
, int refCnt
)
146 // FOR NOW:: inital ref-count is not used
148 GCItem
* pItem
= new GCItem();
152 mAllNodes
.Append( (wxObject
*) pItem
);
155 void GarbageCollector::AddDependency( void* pObj
, void* pDepnedsOnObj
)
157 node_to_item( FindItemNode( pObj
) ).mRefs
.Append( (wxObject
*)pDepnedsOnObj
);
160 /*** GC alg. implementation ***/
162 void GarbageCollector::ArrangeCollection()
168 // find node, which does not depend on anything
170 wxNode
* pItemNode
= FindRefernceFreeItemNode();
174 // append it to the list, where items are contained
175 // in the increasing order of dependencies
177 mRegularLst
.Append( pItemNode
->Data() );
179 mAllNodes
.DeleteNode( pItemNode
);
181 // remove references to this current "least-dependent" node
182 // from reference lists of all the other nodes
184 RemoveReferencesToNode( pItemNode
);
188 // otherwise, what is left - all nodes, which
189 // are involved into cycled chains (rings)
191 wxNode
* pNode
= mAllNodes
.First();
195 mCycledLst
.Append( pNode
->Data() );
197 pNode
= pNode
->Next();
203 // continue search for "least-dependent" nodes
208 wxList
& GarbageCollector::GetRegularObjects()
213 wxList
& GarbageCollector::GetCycledObjects()
218 void GarbageCollector::Reset()
220 DestroyItemList( mAllNodes
);