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 "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>
35 class GCActivityCallback
;
36 class GlobalCodeBlock
;
37 class HeapRootVisitor
;
41 class LiveObjectIterator
;
43 class MarkedArgumentBuffer
;
46 class WeakGCHandlePool
;
47 typedef MarkStack SlotVisitor
;
49 typedef std::pair
<JSValue
, UString
> ValueStringPair
;
50 typedef HashCountedSet
<JSCell
*> ProtectCountSet
;
51 typedef HashCountedSet
<const char*> TypeCountSet
;
53 enum OperationInProgress
{ NoOperation
, Allocation
, Collection
};
56 WTF_MAKE_NONCOPYABLE(Heap
);
58 static Heap
* heap(JSValue
); // 0 for immediate values
59 static Heap
* heap(JSCell
*);
61 static bool isMarked(const JSCell
*);
62 static bool testAndSetMarked(const JSCell
*);
63 static void setMarked(JSCell
*);
65 static void writeBarrier(const JSCell
*, JSValue
);
66 static void writeBarrier(const JSCell
*, JSCell
*);
70 void destroy(); // JSGlobalData must call destroy() before ~Heap().
72 JSGlobalData
* globalData() const { return m_globalData
; }
73 MarkedSpace
& markedSpace() { return m_markedSpace
; }
74 MachineThreads
& machineThreads() { return m_machineThreads
; }
76 GCActivityCallback
* activityCallback();
77 void setActivityCallback(PassOwnPtr
<GCActivityCallback
>);
79 // true if an allocation or collection is in progress
82 void* allocate(size_t);
83 void collectAllGarbage();
85 void reportExtraMemoryCost(size_t cost
);
87 void protect(JSValue
);
88 bool unprotect(JSValue
); // True when the protect count drops to 0.
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();
101 void pushTempSortVector(Vector
<ValueStringPair
>*);
102 void popTempSortVector(Vector
<ValueStringPair
>*);
104 HashSet
<MarkedArgumentBuffer
*>& markListSet() { if (!m_markListSet
) m_markListSet
= new HashSet
<MarkedArgumentBuffer
*>; return *m_markListSet
; }
106 template <typename Functor
> void forEach(Functor
&);
108 HandleSlot
allocateGlobalHandle() { return m_handleHeap
.allocate(); }
109 HandleSlot
allocateLocalHandle() { return m_handleStack
.push(); }
111 HandleStack
* handleStack() { return &m_handleStack
; }
112 void getConservativeRegisterRoots(HashSet
<JSCell
*>& roots
);
115 friend class JSGlobalData
;
117 static const size_t minExtraCost
= 256;
118 static const size_t maxExtraCost
= 1024 * 1024;
120 void* allocateSlowCase(size_t);
121 void reportExtraMemoryCostSlowCase(size_t);
124 void markProtectedObjects(HeapRootVisitor
&);
125 void markTempSortVectors(HeapRootVisitor
&);
127 enum SweepToggle
{ DoNotSweep
, DoSweep
};
128 void reset(SweepToggle
);
130 RegisterFile
& registerFile();
132 OperationInProgress m_operationInProgress
;
133 MarkedSpace m_markedSpace
;
135 ProtectCountSet m_protectedValues
;
136 Vector
<Vector
<ValueStringPair
>* > m_tempSortingVectors
;
138 HashSet
<MarkedArgumentBuffer
*>* m_markListSet
;
140 OwnPtr
<GCActivityCallback
> m_activityCallback
;
142 JSGlobalData
* m_globalData
;
144 MachineThreads m_machineThreads
;
145 MarkStack m_markStack
;
146 HandleHeap m_handleHeap
;
147 HandleStack m_handleStack
;
154 return m_operationInProgress
!= NoOperation
;
157 inline bool Heap::isMarked(const JSCell
* cell
)
159 return MarkedSpace::isMarked(cell
);
162 inline bool Heap::testAndSetMarked(const JSCell
* cell
)
164 return MarkedSpace::testAndSetMarked(cell
);
167 inline void Heap::setMarked(JSCell
* cell
)
169 MarkedSpace::setMarked(cell
);
172 inline void Heap::writeBarrier(const JSCell
*, JSValue
)
176 inline void Heap::writeBarrier(const JSCell
*, JSCell
*)
180 inline bool Heap::contains(void* p
)
182 return m_markedSpace
.contains(p
);
185 inline void Heap::reportExtraMemoryCost(size_t cost
)
187 if (cost
> minExtraCost
)
188 reportExtraMemoryCostSlowCase(cost
);
191 template <typename Functor
> inline void Heap::forEach(Functor
& functor
)
193 m_markedSpace
.forEach(functor
);