2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "MarkStack.h"
26 #include "MarkedSpace.h"
27 #include <wtf/Forward.h>
28 #include <wtf/HashSet.h>
30 #define ASSERT_CLASS_FITS_IN_CELL(class) COMPILE_ASSERT(sizeof(class) <= CELL_SIZE, class_fits_in_cell)
34 class GCActivityCallback;
39 class LiveObjectIterator;
41 class MarkedArgumentBuffer;
44 class WeakGCHandlePool;
46 typedef std::pair<JSValue, UString> ValueStringPair;
47 typedef HashCountedSet<JSCell*> ProtectCountSet;
49 enum OperationInProgress { NoOperation, Allocation, Collection };
52 WTF_MAKE_NONCOPYABLE(Heap);
54 static Heap* heap(JSValue); // 0 for immediate values
55 static Heap* heap(JSCell*);
57 static bool isCellMarked(const JSCell*);
58 static bool checkMarkCell(const JSCell*);
59 static void markCell(JSCell*);
63 void destroy(); // JSGlobalData must call destroy() before ~Heap().
65 JSGlobalData* globalData() const { return m_globalData; }
66 MarkedSpace& markedSpace() { return m_markedSpace; }
67 MachineStackMarker& machineStackMarker() { return m_machineStackMarker; }
69 GCActivityCallback* activityCallback();
70 void setActivityCallback(PassOwnPtr<GCActivityCallback>);
72 bool isBusy(); // true if an allocation or collection is in progress
73 void* allocate(size_t);
74 void collectAllGarbage();
76 void reportExtraMemoryCost(size_t cost);
78 void protect(JSValue);
79 bool unprotect(JSValue); // True when the protect count drops to 0.
84 size_t capacity() const;
85 size_t objectCount() const;
86 size_t globalObjectCount();
87 size_t protectedObjectCount();
88 size_t protectedGlobalObjectCount();
89 HashCountedSet<const char*>* protectedObjectTypeCounts();
90 HashCountedSet<const char*>* objectTypeCounts();
92 WeakGCHandle* addWeakGCHandle(JSCell*);
94 void pushTempSortVector(WTF::Vector<ValueStringPair>*);
95 void popTempSortVector(WTF::Vector<ValueStringPair>*);
97 HashSet<MarkedArgumentBuffer*>& markListSet() { if (!m_markListSet) m_markListSet = new HashSet<MarkedArgumentBuffer*>; return *m_markListSet; }
99 LiveObjectIterator primaryHeapBegin();
100 LiveObjectIterator primaryHeapEnd();
103 friend class JSGlobalData;
105 static const size_t minExtraCost = 256;
106 static const size_t maxExtraCost = 1024 * 1024;
108 void reportExtraMemoryCostSlowCase(size_t);
111 void markProtectedObjects(MarkStack&);
112 void markTempSortVectors(MarkStack&);
114 void updateWeakGCHandles();
115 WeakGCHandlePool* weakGCHandlePool(size_t index);
117 RegisterFile& registerFile();
119 MarkedSpace m_markedSpace;
120 OperationInProgress m_operationInProgress;
122 ProtectCountSet m_protectedValues;
123 WTF::Vector<PageAllocationAligned> m_weakGCHandlePools;
124 WTF::Vector<WTF::Vector<ValueStringPair>* > m_tempSortingVectors;
126 HashSet<MarkedArgumentBuffer*>* m_markListSet;
128 OwnPtr<GCActivityCallback> m_activityCallback;
130 JSGlobalData* m_globalData;
132 MachineStackMarker m_machineStackMarker;
133 MarkStack m_markStack;
138 inline bool Heap::isCellMarked(const JSCell* cell)
140 return MarkedSpace::isCellMarked(cell);
143 inline bool Heap::checkMarkCell(const JSCell* cell)
145 return MarkedSpace::checkMarkCell(cell);
148 inline void Heap::markCell(JSCell* cell)
150 MarkedSpace::markCell(cell);
153 inline bool Heap::contains(void* p)
155 return m_markedSpace.contains(p);
158 inline void Heap::reportExtraMemoryCost(size_t cost)
160 if (cost > minExtraCost)
161 reportExtraMemoryCostSlowCase(cost);
164 inline WeakGCHandlePool* Heap::weakGCHandlePool(size_t index)
166 return static_cast<WeakGCHandlePool*>(m_weakGCHandlePools[index].base());