reduce verbosity
[gnash.git] / libbase / GC.cpp
blob014dc41ab60fe6c5d29a3de5657ddfeec268f3c0
1 // GC.cpp: Garbage Collector, for Gnash
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // Foundation, Inc
5 //
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.
10 //
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.
15 //
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
20 #include "GC.h"
22 #include <cstdlib>
24 #include "utility.h" // for typeName()
25 #include "GnashAlgorithm.h"
27 #ifdef GNASH_GC_DEBUG
28 # include "log.h"
29 #endif
32 namespace gnash {
34 GC::GC(GcRoot& root)
36 // might raise the default ...
37 _maxNewCollectablesCount(64),
38 _resListSize(0),
39 _root(root),
40 _lastResCount(0)
41 #ifdef GNASH_GC_DEBUG
42 , _collectorRuns(0)
43 #endif
45 #ifdef GNASH_GC_DEBUG
46 log_debug(_("GC %p created"), (void*)this);
47 #endif
48 char* gcgap = std::getenv("GNASH_GC_TRIGGER_THRESHOLD");
49 if (gcgap) {
50 const size_t gap = std::strtoul(gcgap, NULL, 0);
51 _maxNewCollectablesCount = gap;
55 GC::~GC()
57 #ifdef GNASH_GC_DEBUG
58 log_debug(_("GC deleted, deleting all managed resources - collector run %d times"), _collectorRuns);
59 #endif
60 for (ResList::const_iterator i = _resList.begin(), e = _resList.end();
61 i != e; ++i) {
62 delete *i;
66 size_t
67 GC::cleanUnreachable()
70 #if (GNASH_GC_DEBUG > 1)
71 log_debug(_("GC: sweep scan started"));
72 #endif
74 size_t deleted = 0;
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));
82 #endif
83 ++deleted;
84 delete res;
85 i = _resList.erase(i); // _resListSize updated at end of loop
87 else {
88 res->clearReachable();
89 ++i;
93 _resListSize -= deleted;
95 #ifdef GNASH_GC_DEBUG
96 log_debug(_("GC: recycled %d unreachable resources - %d left"),
97 deleted, _resListSize);
98 #endif
100 return deleted;
103 void
104 GC::runCycle()
107 // Collection cycle
110 #ifdef GNASH_GC_DEBUG
111 ++_collectorRuns;
112 #endif
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
122 markReachable();
124 // clean unreachable resources, and mark the others as reachable again
125 cleanUnreachable();
127 _lastResCount = _resListSize;
131 void
132 GC::countCollectables(CollectablesCount& count) const
134 for (ResList::const_iterator i = _resList.begin(), e = _resList.end();
135 i!=e; ++i) {
136 ++count[typeName(**i)];
140 } // end of namespace gnash