]> git.saurik.com Git - apple/javascriptcore.git/blob - heap/Heap.h
feba1cfccf22f383dc2adc122e22860098fdeab2
[apple/javascriptcore.git] / heap / Heap.h
1 /*
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.
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22 #ifndef Heap_h
23 #define Heap_h
24
25 #include "HandleHeap.h"
26 #include "HandleStack.h"
27 #include "MarkStack.h"
28 #include "MarkedSpace.h"
29 #include <wtf/Forward.h>
30 #include <wtf/HashCountedSet.h>
31 #include <wtf/HashSet.h>
32
33 namespace JSC {
34
35 class GCActivityCallback;
36 class GlobalCodeBlock;
37 class HeapRootVisitor;
38 class JSCell;
39 class JSGlobalData;
40 class JSValue;
41 class LiveObjectIterator;
42 class MarkStack;
43 class MarkedArgumentBuffer;
44 class RegisterFile;
45 class UString;
46 class WeakGCHandlePool;
47 typedef MarkStack SlotVisitor;
48
49 typedef std::pair<JSValue, UString> ValueStringPair;
50 typedef HashCountedSet<JSCell*> ProtectCountSet;
51 typedef HashCountedSet<const char*> TypeCountSet;
52
53 enum OperationInProgress { NoOperation, Allocation, Collection };
54
55 class Heap {
56 WTF_MAKE_NONCOPYABLE(Heap);
57 public:
58 static Heap* heap(JSValue); // 0 for immediate values
59 static Heap* heap(JSCell*);
60
61 static bool isMarked(const JSCell*);
62 static bool testAndSetMarked(const JSCell*);
63 static void setMarked(JSCell*);
64
65 static void writeBarrier(const JSCell*, JSValue);
66 static void writeBarrier(const JSCell*, JSCell*);
67
68 Heap(JSGlobalData*);
69 ~Heap();
70 void destroy(); // JSGlobalData must call destroy() before ~Heap().
71
72 JSGlobalData* globalData() const { return m_globalData; }
73 MarkedSpace& markedSpace() { return m_markedSpace; }
74 MachineThreads& machineThreads() { return m_machineThreads; }
75
76 GCActivityCallback* activityCallback();
77 void setActivityCallback(PassOwnPtr<GCActivityCallback>);
78
79 // true if an allocation or collection is in progress
80 inline bool isBusy();
81
82 void* allocate(size_t);
83 void collectAllGarbage();
84
85 void reportExtraMemoryCost(size_t cost);
86
87 void protect(JSValue);
88 bool unprotect(JSValue); // True when the protect count drops to 0.
89
90 bool contains(void*);
91
92 size_t size() const;
93 size_t capacity() const;
94 size_t objectCount() const;
95 size_t globalObjectCount();
96 size_t protectedObjectCount();
97 size_t protectedGlobalObjectCount();
98 PassOwnPtr<TypeCountSet> protectedObjectTypeCounts();
99 PassOwnPtr<TypeCountSet> objectTypeCounts();
100
101 void pushTempSortVector(Vector<ValueStringPair>*);
102 void popTempSortVector(Vector<ValueStringPair>*);
103
104 HashSet<MarkedArgumentBuffer*>& markListSet() { if (!m_markListSet) m_markListSet = new HashSet<MarkedArgumentBuffer*>; return *m_markListSet; }
105
106 template <typename Functor> void forEach(Functor&);
107
108 HandleSlot allocateGlobalHandle() { return m_handleHeap.allocate(); }
109 HandleSlot allocateLocalHandle() { return m_handleStack.push(); }
110
111 HandleStack* handleStack() { return &m_handleStack; }
112 void getConservativeRegisterRoots(HashSet<JSCell*>& roots);
113
114 private:
115 friend class JSGlobalData;
116
117 static const size_t minExtraCost = 256;
118 static const size_t maxExtraCost = 1024 * 1024;
119
120 void* allocateSlowCase(size_t);
121 void reportExtraMemoryCostSlowCase(size_t);
122
123 void markRoots();
124 void markProtectedObjects(HeapRootVisitor&);
125 void markTempSortVectors(HeapRootVisitor&);
126
127 enum SweepToggle { DoNotSweep, DoSweep };
128 void reset(SweepToggle);
129
130 RegisterFile& registerFile();
131
132 OperationInProgress m_operationInProgress;
133 MarkedSpace m_markedSpace;
134
135 ProtectCountSet m_protectedValues;
136 Vector<Vector<ValueStringPair>* > m_tempSortingVectors;
137
138 HashSet<MarkedArgumentBuffer*>* m_markListSet;
139
140 OwnPtr<GCActivityCallback> m_activityCallback;
141
142 JSGlobalData* m_globalData;
143
144 MachineThreads m_machineThreads;
145 MarkStack m_markStack;
146 HandleHeap m_handleHeap;
147 HandleStack m_handleStack;
148
149 size_t m_extraCost;
150 };
151
152 bool Heap::isBusy()
153 {
154 return m_operationInProgress != NoOperation;
155 }
156
157 inline bool Heap::isMarked(const JSCell* cell)
158 {
159 return MarkedSpace::isMarked(cell);
160 }
161
162 inline bool Heap::testAndSetMarked(const JSCell* cell)
163 {
164 return MarkedSpace::testAndSetMarked(cell);
165 }
166
167 inline void Heap::setMarked(JSCell* cell)
168 {
169 MarkedSpace::setMarked(cell);
170 }
171
172 inline void Heap::writeBarrier(const JSCell*, JSValue)
173 {
174 }
175
176 inline void Heap::writeBarrier(const JSCell*, JSCell*)
177 {
178 }
179
180 inline bool Heap::contains(void* p)
181 {
182 return m_markedSpace.contains(p);
183 }
184
185 inline void Heap::reportExtraMemoryCost(size_t cost)
186 {
187 if (cost > minExtraCost)
188 reportExtraMemoryCostSlowCase(cost);
189 }
190
191 template <typename Functor> inline void Heap::forEach(Functor& functor)
192 {
193 m_markedSpace.forEach(functor);
194 }
195
196 } // namespace JSC
197
198 #endif // Heap_h