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 "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"
36 #include "WriteBarrierSupport.h"
37 #include <wtf/HashCountedSet.h>
38 #include <wtf/HashSet.h>
40 #define COLLECT_ON_EVERY_ALLOCATION 0
46 class FunctionExecutable
;
47 class GCActivityCallback
;
48 class GlobalCodeBlock
;
50 class HeapRootVisitor
;
54 class LiveObjectIterator
;
55 class LLIntOffsetsExtractor
;
56 class MarkedArgumentBuffer
;
59 class WeakGCHandlePool
;
62 typedef std::pair
<JSValue
, UString
> ValueStringPair
;
63 typedef HashCountedSet
<JSCell
*> ProtectCountSet
;
64 typedef HashCountedSet
<const char*> TypeCountSet
;
66 enum OperationInProgress
{ NoOperation
, Allocation
, Collection
};
69 enum HeapSize
{ SmallHeap
, LargeHeap
};
72 WTF_MAKE_NONCOPYABLE(Heap
);
75 friend class MarkStackThreadSharedData
;
76 static Heap
* heap(const JSValue
); // 0 for immediate values
77 static Heap
* heap(const JSCell
*);
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;
85 static bool isMarked(const void*);
86 static bool testAndSetMarked(const void*);
87 static void setMarked(const void*);
89 static void writeBarrier(const JSCell
*, JSValue
);
90 static void writeBarrier(const JSCell
*, JSCell
*);
91 static uint8_t* addressOfCardFor(JSCell
*);
93 Heap(JSGlobalData
*, HeapSize
);
95 JS_EXPORT_PRIVATE
void lastChanceToFinalize();
97 JSGlobalData
* globalData() const { return m_globalData
; }
98 MarkedSpace
& objectSpace() { return m_objectSpace
; }
99 MachineThreads
& machineThreads() { return m_machineThreads
; }
101 JS_EXPORT_PRIVATE GCActivityCallback
* activityCallback();
102 JS_EXPORT_PRIVATE
void setActivityCallback(GCActivityCallback
*);
104 // true if an allocation or collection is in progress
105 inline bool isBusy();
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);
114 typedef void (*Finalizer
)(JSCell
*);
115 JS_EXPORT_PRIVATE
void addFinalizer(JSCell
*, Finalizer
);
116 void addFunctionExecutable(FunctionExecutable
*);
117 void removeFunctionExecutable(FunctionExecutable
*);
119 void notifyIsSafeToCollect() { m_isSafeToCollect
= true; }
121 JS_EXPORT_PRIVATE
void collectAllGarbage();
122 enum SweepToggle
{ DoNotSweep
, DoSweep
};
123 bool shouldCollect();
124 void collect(SweepToggle
);
126 void reportExtraMemoryCost(size_t cost
);
127 JS_EXPORT_PRIVATE
void reportAbandonedObjectGraph();
129 JS_EXPORT_PRIVATE
void protect(JSValue
);
130 JS_EXPORT_PRIVATE
bool unprotect(JSValue
); // True when the protect count drops to 0.
132 void jettisonDFGCodeBlock(PassOwnPtr
<CodeBlock
>);
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();
143 void pushTempSortVector(Vector
<ValueStringPair
>*);
144 void popTempSortVector(Vector
<ValueStringPair
>*);
146 HashSet
<MarkedArgumentBuffer
*>& markListSet() { if (!m_markListSet
) m_markListSet
= new HashSet
<MarkedArgumentBuffer
*>; return *m_markListSet
; }
148 template<typename Functor
> typename
Functor::ReturnType
forEachProtectedCell(Functor
&);
149 template<typename Functor
> typename
Functor::ReturnType
forEachProtectedCell();
151 WeakSet
* weakSet() { return &m_weakSet
; }
152 HandleSet
* handleSet() { return &m_handleSet
; }
153 HandleStack
* handleStack() { return &m_handleStack
; }
155 void getConservativeRegisterRoots(HashSet
<JSCell
*>& roots
);
157 double lastGCLength() { return m_lastGCLength
; }
158 void increaseLastGCLength(double amount
) { m_lastGCLength
+= amount
; }
160 JS_EXPORT_PRIVATE
void discardAllCompiledCode();
162 void didAllocate(size_t);
163 void didAbandon(size_t);
165 bool isPagedOut(double deadline
);
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
&);
177 void* allocateWithDestructor(size_t);
178 void* allocateWithoutDestructor(size_t);
180 static const size_t minExtraCost
= 256;
181 static const size_t maxExtraCost
= 1024 * 1024;
183 class FinalizerOwner
: public WeakHandleOwner
{
184 virtual void finalize(Handle
<Unknown
>, void* context
);
187 JS_EXPORT_PRIVATE
bool isValidAllocation(size_t);
188 JS_EXPORT_PRIVATE
void reportExtraMemoryCostSlowCase(size_t);
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();
195 void resetAllocators();
198 void markRoots(bool fullGC
);
199 void markProtectedObjects(HeapRootVisitor
&);
200 void markTempSortVectors(HeapRootVisitor
&);
201 void harvestWeakReferences();
202 void finalizeUnconditionalFinalizers();
206 RegisterFile
& registerFile();
207 BlockAllocator
& blockAllocator();
209 const HeapSize m_heapSize
;
210 const size_t m_minBytesPerCycle
;
211 size_t m_sizeAfterLastCollect
;
213 size_t m_bytesAllocatedLimit
;
214 size_t m_bytesAllocated
;
215 size_t m_bytesAbandoned
;
217 OperationInProgress m_operationInProgress
;
218 MarkedSpace m_objectSpace
;
219 CopiedSpace m_storageSpace
;
221 BlockAllocator m_blockAllocator
;
223 #if ENABLE(SIMPLE_HEAP_PROFILING)
224 VTableSpectrum m_destroyedTypeCounts
;
227 ProtectCountSet m_protectedValues
;
228 Vector
<Vector
<ValueStringPair
>* > m_tempSortingVectors
;
229 HashSet
<MarkedArgumentBuffer
*>* m_markListSet
;
231 MachineThreads m_machineThreads
;
233 MarkStackThreadSharedData m_sharedData
;
234 SlotVisitor m_slotVisitor
;
237 HandleSet m_handleSet
;
238 HandleStack m_handleStack
;
239 DFGCodeBlocks m_dfgCodeBlocks
;
240 FinalizerOwner m_finalizerOwner
;
242 bool m_isSafeToCollect
;
244 JSGlobalData
* m_globalData
;
245 double m_lastGCLength
;
246 double m_lastCodeDiscardTime
;
248 DoublyLinkedList
<FunctionExecutable
> m_functions
;
250 GCActivityCallback
* m_activityCallback
;
253 inline bool Heap::shouldCollect()
256 return m_objectSpace
.nurseryWaterMark() >= m_minBytesPerCycle
&& m_isSafeToCollect
&& m_operationInProgress
== NoOperation
;
258 return m_bytesAllocated
> m_bytesAllocatedLimit
&& m_isSafeToCollect
&& m_operationInProgress
== NoOperation
;
264 return m_operationInProgress
!= NoOperation
;
267 inline Heap
* Heap::heap(const JSCell
* cell
)
269 return MarkedBlock::blockFor(cell
)->heap();
272 inline Heap
* Heap::heap(const JSValue v
)
276 return heap(v
.asCell());
279 inline bool Heap::isMarked(const void* cell
)
281 return MarkedBlock::blockFor(cell
)->isMarked(cell
);
284 inline bool Heap::testAndSetMarked(const void* cell
)
286 return MarkedBlock::blockFor(cell
)->testAndSetMarked(cell
);
289 inline void Heap::setMarked(const void* cell
)
291 MarkedBlock::blockFor(cell
)->setMarked(cell
);
295 inline uint8_t* Heap::addressOfCardFor(JSCell
* cell
)
297 return MarkedBlock::blockFor(cell
)->addressOfCardFor(cell
);
300 inline void Heap::writeBarrier(const JSCell
* owner
, JSCell
*)
302 WriteBarrierCounters::countWriteBarrier();
303 MarkedBlock
* block
= MarkedBlock::blockFor(owner
);
304 if (block
->isMarked(owner
))
305 block
->setDirtyObject(owner
);
308 inline void Heap::writeBarrier(const JSCell
* owner
, JSValue value
)
314 writeBarrier(owner
, value
.asCell());
318 inline void Heap::writeBarrier(const JSCell
*, JSCell
*)
320 WriteBarrierCounters::countWriteBarrier();
323 inline void Heap::writeBarrier(const JSCell
*, JSValue
)
325 WriteBarrierCounters::countWriteBarrier();
329 inline void Heap::reportExtraMemoryCost(size_t cost
)
331 if (cost
> minExtraCost
)
332 reportExtraMemoryCostSlowCase(cost
);
335 template<typename Functor
> inline typename
Functor::ReturnType
Heap::forEachProtectedCell(Functor
& functor
)
337 ProtectCountSet::iterator end
= m_protectedValues
.end();
338 for (ProtectCountSet::iterator it
= m_protectedValues
.begin(); it
!= end
; ++it
)
340 m_handleSet
.forEachStrongHandle(functor
, m_protectedValues
);
342 return functor
.returnValue();
345 template<typename Functor
> inline typename
Functor::ReturnType
Heap::forEachProtectedCell()
348 return forEachProtectedCell(functor
);
351 inline void* Heap::allocateWithDestructor(size_t bytes
)
353 ASSERT(isValidAllocation(bytes
));
354 return m_objectSpace
.allocateWithDestructor(bytes
);
357 inline void* Heap::allocateWithoutDestructor(size_t bytes
)
359 ASSERT(isValidAllocation(bytes
));
360 return m_objectSpace
.allocateWithoutDestructor(bytes
);
363 inline CheckedBoolean
Heap::tryAllocateStorage(size_t bytes
, void** outPtr
)
365 return m_storageSpace
.tryAllocate(bytes
, outPtr
);
368 inline CheckedBoolean
Heap::tryReallocateStorage(void** ptr
, size_t oldSize
, size_t newSize
)
370 return m_storageSpace
.tryReallocate(ptr
, oldSize
, newSize
);
373 inline BlockAllocator
& Heap::blockAllocator()
375 return m_blockAllocator
;