tizen beta release
[profile/ivi/webkit-efl.git] / Source / JavaScriptCore / 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 "AllocationSpace.h"
26 #include "DFGCodeBlocks.h"
27 #include "HandleHeap.h"
28 #include "HandleStack.h"
29 #include "MarkedBlock.h"
30 #include "MarkedBlockSet.h"
31 #include "MarkedSpace.h"
32 #include "SlotVisitor.h"
33 #include "WriteBarrierSupport.h"
34 #include <wtf/Forward.h>
35 #include <wtf/HashCountedSet.h>
36 #include <wtf/HashSet.h>
37
38 namespace JSC {
39
40     class CodeBlock;
41     class GCActivityCallback;
42     class GlobalCodeBlock;
43     class Heap;
44     class HeapRootVisitor;
45     class JSCell;
46     class JSGlobalData;
47     class JSValue;
48     class LiveObjectIterator;
49     class MarkedArgumentBuffer;
50     class RegisterFile;
51     class UString;
52     class WeakGCHandlePool;
53     class SlotVisitor;
54
55     typedef std::pair<JSValue, UString> ValueStringPair;
56     typedef HashCountedSet<JSCell*> ProtectCountSet;
57     typedef HashCountedSet<const char*> TypeCountSet;
58
59     enum OperationInProgress { NoOperation, Allocation, Collection };
60     
61     // Heap size hint.
62     enum HeapSize { SmallHeap, LargeHeap };
63
64     class Heap {
65         WTF_MAKE_NONCOPYABLE(Heap);
66     public:
67         friend class JIT;
68         static Heap* heap(JSValue); // 0 for immediate values
69         static Heap* heap(JSCell*);
70
71         static bool isMarked(const void*);
72         static bool testAndSetMarked(const void*);
73         static void setMarked(const void*);
74
75         static void writeBarrier(const JSCell*, JSValue);
76         static void writeBarrier(const JSCell*, JSCell*);
77         static uint8_t* addressOfCardFor(JSCell*);
78
79         Heap(JSGlobalData*, HeapSize);
80         ~Heap();
81         void destroy(); // JSGlobalData must call destroy() before ~Heap().
82
83         JSGlobalData* globalData() const { return m_globalData; }
84         AllocationSpace& objectSpace() { return m_objectSpace; }
85         MachineThreads& machineThreads() { return m_machineThreads; }
86
87         GCActivityCallback* activityCallback();
88         void setActivityCallback(PassOwnPtr<GCActivityCallback>);
89
90         // true if an allocation or collection is in progress
91         inline bool isBusy();
92         
93         MarkedSpace::SizeClass& sizeClassForObject(size_t bytes) { return m_objectSpace.sizeClassFor(bytes); }
94         void* allocate(size_t);
95
96         typedef void (*Finalizer)(JSCell*);
97         void addFinalizer(JSCell*, Finalizer);
98
99         void notifyIsSafeToCollect() { m_isSafeToCollect = true; }
100         void collectAllGarbage();
101
102         void reportExtraMemoryCost(size_t cost);
103
104         void protect(JSValue);
105         bool unprotect(JSValue); // True when the protect count drops to 0.
106         
107         void jettisonDFGCodeBlock(PassOwnPtr<CodeBlock>);
108
109         size_t size();
110         size_t capacity();
111         size_t objectCount();
112         size_t globalObjectCount();
113         size_t protectedObjectCount();
114         size_t protectedGlobalObjectCount();
115         PassOwnPtr<TypeCountSet> protectedObjectTypeCounts();
116         PassOwnPtr<TypeCountSet> objectTypeCounts();
117
118         void pushTempSortVector(Vector<ValueStringPair>*);
119         void popTempSortVector(Vector<ValueStringPair>*);
120     
121         HashSet<MarkedArgumentBuffer*>& markListSet() { if (!m_markListSet) m_markListSet = new HashSet<MarkedArgumentBuffer*>; return *m_markListSet; }
122         
123         template<typename Functor> typename Functor::ReturnType forEachProtectedCell(Functor&);
124         template<typename Functor> typename Functor::ReturnType forEachProtectedCell();
125
126         HandleHeap* handleHeap() { return &m_handleHeap; }
127         HandleStack* handleStack() { return &m_handleStack; }
128
129         void getConservativeRegisterRoots(HashSet<JSCell*>& roots);
130
131     private:
132         friend class MarkedBlock;
133         friend class AllocationSpace;
134         friend class SlotVisitor;
135         friend class CodeBlock;
136
137         static const size_t minExtraCost = 256;
138         static const size_t maxExtraCost = 1024 * 1024;
139         
140         class FinalizerOwner : public WeakHandleOwner {
141             virtual void finalize(Handle<Unknown>, void* context);
142         };
143
144         bool isValidAllocation(size_t);
145         void reportExtraMemoryCostSlowCase(size_t);
146
147         // Call this function before any operation that needs to know which cells
148         // in the heap are live. (For example, call this function before
149         // conservative marking, eager sweeping, or iterating the cells in a MarkedBlock.)
150         void canonicalizeCellLivenessData();
151
152         void resetAllocator();
153         void freeBlocks(MarkedBlock*);
154
155         void clearMarks();
156         void markRoots(bool fullGC);
157         void markProtectedObjects(HeapRootVisitor&);
158         void markTempSortVectors(HeapRootVisitor&);
159         void harvestWeakReferences();
160         void finalizeUnconditionalFinalizers();
161         
162         enum SweepToggle { DoNotSweep, DoSweep };
163         void collect(SweepToggle);
164         void shrink();
165         void releaseFreeBlocks();
166         void sweep();
167
168         RegisterFile& registerFile();
169
170         void waitForRelativeTimeWhileHoldingLock(double relative);
171         void waitForRelativeTime(double relative);
172         void blockFreeingThreadMain();
173         static void* blockFreeingThreadStartFunc(void* heap);
174         
175         const HeapSize m_heapSize;
176         const size_t m_minBytesPerCycle;
177         size_t m_lastFullGCSize;
178         
179         OperationInProgress m_operationInProgress;
180         AllocationSpace m_objectSpace;
181
182         DoublyLinkedList<MarkedBlock> m_freeBlocks;
183         size_t m_numberOfFreeBlocks;
184         
185         ThreadIdentifier m_blockFreeingThread;
186         Mutex m_freeBlockLock;
187         ThreadCondition m_freeBlockCondition;
188         bool m_blockFreeingThreadShouldQuit;
189
190 #if ENABLE(SIMPLE_HEAP_PROFILING)
191         VTableSpectrum m_destroyedTypeCounts;
192 #endif
193
194         size_t m_extraCost;
195
196         ProtectCountSet m_protectedValues;
197         Vector<Vector<ValueStringPair>* > m_tempSortingVectors;
198         HashSet<MarkedArgumentBuffer*>* m_markListSet;
199
200         OwnPtr<GCActivityCallback> m_activityCallback;
201         
202         MachineThreads m_machineThreads;
203         
204         MarkStackThreadSharedData m_sharedData;
205         SlotVisitor m_slotVisitor;
206
207         HandleHeap m_handleHeap;
208         HandleStack m_handleStack;
209         DFGCodeBlocks m_dfgCodeBlocks;
210         FinalizerOwner m_finalizerOwner;
211         
212         bool m_isSafeToCollect;
213
214         JSGlobalData* m_globalData;
215     };
216
217     bool Heap::isBusy()
218     {
219         return m_operationInProgress != NoOperation;
220     }
221
222     inline Heap* Heap::heap(JSCell* cell)
223     {
224         return MarkedBlock::blockFor(cell)->heap();
225     }
226
227     inline Heap* Heap::heap(JSValue v)
228     {
229         if (!v.isCell())
230             return 0;
231         return heap(v.asCell());
232     }
233
234     inline bool Heap::isMarked(const void* cell)
235     {
236         return MarkedBlock::blockFor(cell)->isMarked(cell);
237     }
238
239     inline bool Heap::testAndSetMarked(const void* cell)
240     {
241         return MarkedBlock::blockFor(cell)->testAndSetMarked(cell);
242     }
243
244     inline void Heap::setMarked(const void* cell)
245     {
246         MarkedBlock::blockFor(cell)->setMarked(cell);
247     }
248
249 #if ENABLE(GGC)
250     inline uint8_t* Heap::addressOfCardFor(JSCell* cell)
251     {
252         return MarkedBlock::blockFor(cell)->addressOfCardFor(cell);
253     }
254
255     inline void Heap::writeBarrier(const JSCell* owner, JSCell*)
256     {
257         WriteBarrierCounters::countWriteBarrier();
258         MarkedBlock* block = MarkedBlock::blockFor(owner);
259         if (block->isMarked(owner))
260             block->setDirtyObject(owner);
261     }
262
263     inline void Heap::writeBarrier(const JSCell* owner, JSValue value)
264     {
265         if (!value)
266             return;
267         if (!value.isCell())
268             return;
269         writeBarrier(owner, value.asCell());
270     }
271 #else
272
273     inline void Heap::writeBarrier(const JSCell*, JSCell*)
274     {
275         WriteBarrierCounters::countWriteBarrier();
276     }
277
278     inline void Heap::writeBarrier(const JSCell*, JSValue)
279     {
280         WriteBarrierCounters::countWriteBarrier();
281     }
282 #endif
283
284     inline void Heap::reportExtraMemoryCost(size_t cost)
285     {
286         if (cost > minExtraCost) 
287             reportExtraMemoryCostSlowCase(cost);
288     }
289
290     template<typename Functor> inline typename Functor::ReturnType Heap::forEachProtectedCell(Functor& functor)
291     {
292         ProtectCountSet::iterator end = m_protectedValues.end();
293         for (ProtectCountSet::iterator it = m_protectedValues.begin(); it != end; ++it)
294             functor(it->first);
295         m_handleHeap.forEachStrongHandle(functor, m_protectedValues);
296
297         return functor.returnValue();
298     }
299
300     template<typename Functor> inline typename Functor::ReturnType Heap::forEachProtectedCell()
301     {
302         Functor functor;
303         return forEachProtectedCell(functor);
304     }
305
306     inline void* Heap::allocate(size_t bytes)
307     {
308         ASSERT(isValidAllocation(bytes));
309         return m_objectSpace.allocate(bytes);
310     }
311
312 } // namespace JSC
313
314 #endif // Heap_h