1 // GC.cpp: Garbage Collector, for Gnash
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "utility.h" // for typeName()
25 #include "GnashAlgorithm.h"
36 // might raise the default ...
37 _maxNewCollectablesCount(64),
46 log_debug(_("GC %p created"), (void*)this);
48 char* gcgap
= std::getenv("GNASH_GC_TRIGGER_THRESHOLD");
50 const size_t gap
= std::strtoul(gcgap
, NULL
, 0);
51 _maxNewCollectablesCount
= gap
;
58 log_debug(_("GC deleted, deleting all managed resources - collector run %d times"), _collectorRuns
);
60 for (ResList::const_iterator i
= _resList
.begin(), e
= _resList
.end();
67 GC::cleanUnreachable()
70 #if (GNASH_GC_DEBUG > 1)
71 log_debug(_("GC: sweep scan started"));
76 for (ResList::iterator i
= _resList
.begin(), e
= _resList
.end(); i
!= e
;) {
77 const GcResource
* res
= *i
;
78 if (!res
->isReachable()) {
80 #if GNASH_GC_DEBUG > 1
81 log_debug(_("GC: recycling object %p (%s)"), res
, typeName(*res
));
85 i
= _resList
.erase(i
); // _resListSize updated at end of loop
88 res
->clearReachable();
93 _resListSize
-= deleted
;
96 log_debug(_("GC: recycled %d unreachable resources - %d left"),
97 deleted
, _resListSize
);
110 #ifdef GNASH_GC_DEBUG
114 #ifdef GNASH_GC_DEBUG
115 log_debug(_("GC: collection cycle started - %d/%d new resources "
116 "allocated since last run (from %d to %d)"),
117 _resListSize
- _lastResCount
, _maxNewCollectablesCount
,
118 _lastResCount
, _resListSize
);
119 #endif // GNASH_GC_DEBUG
121 // Mark all resources as reachable
124 // clean unreachable resources, and mark the others as reachable again
127 _lastResCount
= _resListSize
;
132 GC::countCollectables(CollectablesCount
& count
) const
134 for (ResList::const_iterator i
= _resList
.begin(), e
= _resList
.end();
136 ++count
[typeName(**i
)];
140 } // end of namespace gnash