]> git.saurik.com Git - apple/javascriptcore.git/blob - heap/Heap.h
JavaScriptCore-1097.13.tar.gz
[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 "BlockAllocator.h"
26 #include "DFGCodeBlocks.h"
27 #include "HandleSet.h"
28 #include "HandleStack.h"
29 #include "MarkedAllocator.h"
30 #include "MarkedBlock.h"
31 #include "MarkedBlockSet.h"
32 #include "MarkedSpace.h"
33 #include "SlotVisitor.h"
34 #include "WeakHandleOwner.h"
35 #include "WeakSet.h"
36 #include "WriteBarrierSupport.h"
37 #include <wtf/HashCountedSet.h>
38 #include <wtf/HashSet.h>
39
40 #define COLLECT_ON_EVERY_ALLOCATION 0
41
42 namespace JSC {
43
44 class CopiedSpace;
45 class CodeBlock;
46 class FunctionExecutable;
47 class GCActivityCallback;
48 class GlobalCodeBlock;
49 class Heap;
50 class HeapRootVisitor;
51 class JSCell;
52 class JSGlobalData;
53 class JSValue;
54 class LiveObjectIterator;
55 class LLIntOffsetsExtractor;
56 class MarkedArgumentBuffer;
57 class RegisterFile;
58 class UString;
59 class WeakGCHandlePool;
60 class SlotVisitor;
61
62 typedef std::pair<JSValue, UString> ValueStringPair;
63 typedef HashCountedSet<JSCell*> ProtectCountSet;
64 typedef HashCountedSet<const char*> TypeCountSet;
65
66 enum OperationInProgress { NoOperation, Allocation, Collection };
67
68 // Heap size hint.
69 enum HeapSize { SmallHeap, LargeHeap };
70
71 class Heap {
72 WTF_MAKE_NONCOPYABLE(Heap);
73 public:
74 friend class JIT;
75 friend class MarkStackThreadSharedData;
76 static Heap* heap(const JSValue); // 0 for immediate values
77 static Heap* heap(const JSCell*);
78
79 // This constant determines how many blocks we iterate between checks of our
80 // deadline when calling Heap::isPagedOut. Decreasing it will cause us to detect
81 // overstepping our deadline more quickly, while increasing it will cause
82 // our scan to run faster.
83 static const unsigned s_timeCheckResolution = 16;
84
85 static bool isMarked(const void*);
86 static bool testAndSetMarked(const void*);
87 static void setMarked(const void*);
88
89 static void writeBarrier(const JSCell*, JSValue);
90 static void writeBarrier(const JSCell*, JSCell*);
91 static uint8_t* addressOfCardFor(JSCell*);
92
93 Heap(JSGlobalData*, HeapSize);
94 ~Heap();
95 JS_EXPORT_PRIVATE void lastChanceToFinalize();
96
97 JSGlobalData* globalData() const { return m_globalData; }
98 MarkedSpace& objectSpace() { return m_objectSpace; }
99 MachineThreads& machineThreads() { return m_machineThreads; }
100
101 JS_EXPORT_PRIVATE GCActivityCallback* activityCallback();
102 JS_EXPORT_PRIVATE void setActivityCallback(GCActivityCallback*);
103
104 // true if an allocation or collection is in progress
105 inline bool isBusy();
106
107 MarkedAllocator& firstAllocatorWithoutDestructors() { return m_objectSpace.firstAllocator(); }
108 MarkedAllocator& allocatorForObjectWithoutDestructor(size_t bytes) { return m_objectSpace.allocatorFor(bytes); }
109 MarkedAllocator& allocatorForObjectWithDestructor(size_t bytes) { return m_objectSpace.destructorAllocatorFor(bytes); }
110 CopiedAllocator& storageAllocator() { return m_storageSpace.allocator(); }
111 CheckedBoolean tryAllocateStorage(size_t, void**);
112 CheckedBoolean tryReallocateStorage(void**, size_t, size_t);
113
114 typedef void (*Finalizer)(JSCell*);
115 JS_EXPORT_PRIVATE void addFinalizer(JSCell*, Finalizer);
116 void addFunctionExecutable(FunctionExecutable*);
117 void removeFunctionExecutable(FunctionExecutable*);
118
119 void notifyIsSafeToCollect() { m_isSafeToCollect = true; }
120
121 JS_EXPORT_PRIVATE void collectAllGarbage();
122 enum SweepToggle { DoNotSweep, DoSweep };
123 bool shouldCollect();
124 void collect(SweepToggle);
125
126 void reportExtraMemoryCost(size_t cost);
127 JS_EXPORT_PRIVATE void reportAbandonedObjectGraph();
128
129 JS_EXPORT_PRIVATE void protect(JSValue);
130 JS_EXPORT_PRIVATE bool unprotect(JSValue); // True when the protect count drops to 0.
131
132 void jettisonDFGCodeBlock(PassOwnPtr<CodeBlock>);
133
134 JS_EXPORT_PRIVATE size_t size();
135 JS_EXPORT_PRIVATE size_t capacity();
136 JS_EXPORT_PRIVATE size_t objectCount();
137 JS_EXPORT_PRIVATE size_t globalObjectCount();
138 JS_EXPORT_PRIVATE size_t protectedObjectCount();
139 JS_EXPORT_PRIVATE size_t protectedGlobalObjectCount();
140 JS_EXPORT_PRIVATE PassOwnPtr<TypeCountSet> protectedObjectTypeCounts();
141 JS_EXPORT_PRIVATE PassOwnPtr<TypeCountSet> objectTypeCounts();
142
143 void pushTempSortVector(Vector<ValueStringPair>*);
144 void popTempSortVector(Vector<ValueStringPair>*);
145
146 HashSet<MarkedArgumentBuffer*>& markListSet() { if (!m_markListSet) m_markListSet = new HashSet<MarkedArgumentBuffer*>; return *m_markListSet; }
147
148 template<typename Functor> typename Functor::ReturnType forEachProtectedCell(Functor&);
149 template<typename Functor> typename Functor::ReturnType forEachProtectedCell();
150
151 WeakSet* weakSet() { return &m_weakSet; }
152 HandleSet* handleSet() { return &m_handleSet; }
153 HandleStack* handleStack() { return &m_handleStack; }
154
155 void getConservativeRegisterRoots(HashSet<JSCell*>& roots);
156
157 double lastGCLength() { return m_lastGCLength; }
158 void increaseLastGCLength(double amount) { m_lastGCLength += amount; }
159
160 JS_EXPORT_PRIVATE void discardAllCompiledCode();
161
162 void didAllocate(size_t);
163 void didAbandon(size_t);
164
165 bool isPagedOut(double deadline);
166
167 private:
168 friend class CodeBlock;
169 friend class LLIntOffsetsExtractor;
170 friend class MarkedSpace;
171 friend class MarkedAllocator;
172 friend class MarkedBlock;
173 friend class CopiedSpace;
174 friend class SlotVisitor;
175 template<typename T> friend void* allocateCell(Heap&);
176
177 void* allocateWithDestructor(size_t);
178 void* allocateWithoutDestructor(size_t);
179
180 static const size_t minExtraCost = 256;
181 static const size_t maxExtraCost = 1024 * 1024;
182
183 class FinalizerOwner : public WeakHandleOwner {
184 virtual void finalize(Handle<Unknown>, void* context);
185 };
186
187 JS_EXPORT_PRIVATE bool isValidAllocation(size_t);
188 JS_EXPORT_PRIVATE void reportExtraMemoryCostSlowCase(size_t);
189
190 // Call this function before any operation that needs to know which cells
191 // in the heap are live. (For example, call this function before
192 // conservative marking, eager sweeping, or iterating the cells in a MarkedBlock.)
193 void canonicalizeCellLivenessData();
194
195 void resetAllocators();
196
197 void clearMarks();
198 void markRoots(bool fullGC);
199 void markProtectedObjects(HeapRootVisitor&);
200 void markTempSortVectors(HeapRootVisitor&);
201 void harvestWeakReferences();
202 void finalizeUnconditionalFinalizers();
203
204 void sweep();
205
206 RegisterFile& registerFile();
207 BlockAllocator& blockAllocator();
208
209 const HeapSize m_heapSize;
210 const size_t m_minBytesPerCycle;
211 size_t m_sizeAfterLastCollect;
212
213 size_t m_bytesAllocatedLimit;
214 size_t m_bytesAllocated;
215 size_t m_bytesAbandoned;
216
217 OperationInProgress m_operationInProgress;
218 MarkedSpace m_objectSpace;
219 CopiedSpace m_storageSpace;
220
221 BlockAllocator m_blockAllocator;
222
223 #if ENABLE(SIMPLE_HEAP_PROFILING)
224 VTableSpectrum m_destroyedTypeCounts;
225 #endif
226
227 ProtectCountSet m_protectedValues;
228 Vector<Vector<ValueStringPair>* > m_tempSortingVectors;
229 HashSet<MarkedArgumentBuffer*>* m_markListSet;
230
231 MachineThreads m_machineThreads;
232
233 MarkStackThreadSharedData m_sharedData;
234 SlotVisitor m_slotVisitor;
235
236 WeakSet m_weakSet;
237 HandleSet m_handleSet;
238 HandleStack m_handleStack;
239 DFGCodeBlocks m_dfgCodeBlocks;
240 FinalizerOwner m_finalizerOwner;
241
242 bool m_isSafeToCollect;
243
244 JSGlobalData* m_globalData;
245 double m_lastGCLength;
246 double m_lastCodeDiscardTime;
247
248 DoublyLinkedList<FunctionExecutable> m_functions;
249
250 GCActivityCallback* m_activityCallback;
251 };
252
253 inline bool Heap::shouldCollect()
254 {
255 #if ENABLE(GGC)
256 return m_objectSpace.nurseryWaterMark() >= m_minBytesPerCycle && m_isSafeToCollect && m_operationInProgress == NoOperation;
257 #else
258 return m_bytesAllocated > m_bytesAllocatedLimit && m_isSafeToCollect && m_operationInProgress == NoOperation;
259 #endif
260 }
261
262 bool Heap::isBusy()
263 {
264 return m_operationInProgress != NoOperation;
265 }
266
267 inline Heap* Heap::heap(const JSCell* cell)
268 {
269 return MarkedBlock::blockFor(cell)->heap();
270 }
271
272 inline Heap* Heap::heap(const JSValue v)
273 {
274 if (!v.isCell())
275 return 0;
276 return heap(v.asCell());
277 }
278
279 inline bool Heap::isMarked(const void* cell)
280 {
281 return MarkedBlock::blockFor(cell)->isMarked(cell);
282 }
283
284 inline bool Heap::testAndSetMarked(const void* cell)
285 {
286 return MarkedBlock::blockFor(cell)->testAndSetMarked(cell);
287 }
288
289 inline void Heap::setMarked(const void* cell)
290 {
291 MarkedBlock::blockFor(cell)->setMarked(cell);
292 }
293
294 #if ENABLE(GGC)
295 inline uint8_t* Heap::addressOfCardFor(JSCell* cell)
296 {
297 return MarkedBlock::blockFor(cell)->addressOfCardFor(cell);
298 }
299
300 inline void Heap::writeBarrier(const JSCell* owner, JSCell*)
301 {
302 WriteBarrierCounters::countWriteBarrier();
303 MarkedBlock* block = MarkedBlock::blockFor(owner);
304 if (block->isMarked(owner))
305 block->setDirtyObject(owner);
306 }
307
308 inline void Heap::writeBarrier(const JSCell* owner, JSValue value)
309 {
310 if (!value)
311 return;
312 if (!value.isCell())
313 return;
314 writeBarrier(owner, value.asCell());
315 }
316 #else
317
318 inline void Heap::writeBarrier(const JSCell*, JSCell*)
319 {
320 WriteBarrierCounters::countWriteBarrier();
321 }
322
323 inline void Heap::writeBarrier(const JSCell*, JSValue)
324 {
325 WriteBarrierCounters::countWriteBarrier();
326 }
327 #endif
328
329 inline void Heap::reportExtraMemoryCost(size_t cost)
330 {
331 if (cost > minExtraCost)
332 reportExtraMemoryCostSlowCase(cost);
333 }
334
335 template<typename Functor> inline typename Functor::ReturnType Heap::forEachProtectedCell(Functor& functor)
336 {
337 ProtectCountSet::iterator end = m_protectedValues.end();
338 for (ProtectCountSet::iterator it = m_protectedValues.begin(); it != end; ++it)
339 functor(it->first);
340 m_handleSet.forEachStrongHandle(functor, m_protectedValues);
341
342 return functor.returnValue();
343 }
344
345 template<typename Functor> inline typename Functor::ReturnType Heap::forEachProtectedCell()
346 {
347 Functor functor;
348 return forEachProtectedCell(functor);
349 }
350
351 inline void* Heap::allocateWithDestructor(size_t bytes)
352 {
353 ASSERT(isValidAllocation(bytes));
354 return m_objectSpace.allocateWithDestructor(bytes);
355 }
356
357 inline void* Heap::allocateWithoutDestructor(size_t bytes)
358 {
359 ASSERT(isValidAllocation(bytes));
360 return m_objectSpace.allocateWithoutDestructor(bytes);
361 }
362
363 inline CheckedBoolean Heap::tryAllocateStorage(size_t bytes, void** outPtr)
364 {
365 return m_storageSpace.tryAllocate(bytes, outPtr);
366 }
367
368 inline CheckedBoolean Heap::tryReallocateStorage(void** ptr, size_t oldSize, size_t newSize)
369 {
370 return m_storageSpace.tryReallocate(ptr, oldSize, newSize);
371 }
372
373 inline BlockAllocator& Heap::blockAllocator()
374 {
375 return m_blockAllocator;
376 }
377
378 } // namespace JSC
379
380 #endif // Heap_h