author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 14 May 2010 16:40:13 +0300 | |
changeset 22 | 79de32ba3296 |
parent 0 | 1918ee327afb |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 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 Collector_h |
|
23 |
#define Collector_h |
|
24 |
||
25 |
#include <stddef.h> |
|
26 |
#include <string.h> |
|
27 |
#include <wtf/HashCountedSet.h> |
|
28 |
#include <wtf/HashSet.h> |
|
29 |
#include <wtf/Noncopyable.h> |
|
30 |
#include <wtf/OwnPtr.h> |
|
31 |
#include <wtf/Threading.h> |
|
32 |
||
33 |
// This is supremely lame that we require pthreads to build on windows. |
|
34 |
#if ENABLE(JSC_MULTIPLE_THREADS) |
|
35 |
#include <pthread.h> |
|
36 |
#endif |
|
37 |
||
22
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
38 |
#if PLATFORM(SYMBIAN) |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
39 |
#include <wtf/symbian/BlockAllocatorSymbian.h> |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
40 |
#endif |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
41 |
|
0 | 42 |
#define ASSERT_CLASS_FITS_IN_CELL(class) COMPILE_ASSERT(sizeof(class) <= CELL_SIZE, class_fits_in_cell) |
43 |
||
44 |
namespace JSC { |
|
45 |
||
46 |
class CollectorBlock; |
|
47 |
class JSCell; |
|
48 |
class JSGlobalData; |
|
49 |
class JSValue; |
|
50 |
class MarkedArgumentBuffer; |
|
51 |
class MarkStack; |
|
52 |
||
53 |
enum OperationInProgress { NoOperation, Allocation, Collection }; |
|
54 |
enum HeapType { PrimaryHeap, NumberHeap }; |
|
55 |
||
56 |
template <HeapType> class CollectorHeapIterator; |
|
57 |
||
58 |
struct CollectorHeap { |
|
59 |
CollectorBlock** blocks; |
|
60 |
size_t numBlocks; |
|
61 |
size_t usedBlocks; |
|
62 |
size_t firstBlockWithPossibleSpace; |
|
63 |
||
64 |
size_t numLiveObjects; |
|
65 |
size_t numLiveObjectsAtLastCollect; |
|
66 |
size_t extraCost; |
|
67 |
||
68 |
OperationInProgress operationInProgress; |
|
69 |
}; |
|
70 |
||
71 |
class Heap : public Noncopyable { |
|
72 |
public: |
|
73 |
class Thread; |
|
74 |
typedef CollectorHeapIterator<PrimaryHeap> iterator; |
|
75 |
||
76 |
void destroy(); |
|
77 |
||
78 |
void* allocateNumber(size_t); |
|
79 |
void* allocate(size_t); |
|
80 |
||
81 |
bool collect(); |
|
82 |
bool isBusy(); // true if an allocation or collection is in progress |
|
83 |
||
84 |
static const size_t minExtraCostSize = 256; |
|
85 |
||
86 |
void reportExtraMemoryCost(size_t cost); |
|
87 |
||
88 |
size_t objectCount(); |
|
89 |
struct Statistics { |
|
90 |
size_t size; |
|
91 |
size_t free; |
|
92 |
}; |
|
93 |
Statistics statistics() const; |
|
94 |
||
95 |
void protect(JSValue); |
|
96 |
void unprotect(JSValue); |
|
97 |
||
98 |
static Heap* heap(JSValue); // 0 for immediate values |
|
99 |
static Heap* heap(JSCell*); |
|
100 |
||
101 |
size_t globalObjectCount(); |
|
102 |
size_t protectedObjectCount(); |
|
103 |
size_t protectedGlobalObjectCount(); |
|
104 |
HashCountedSet<const char*>* protectedObjectTypeCounts(); |
|
105 |
||
106 |
void registerThread(); // Only needs to be called by clients that can use the same heap from multiple threads. |
|
107 |
||
108 |
static bool isCellMarked(const JSCell*); |
|
109 |
static void markCell(JSCell*); |
|
110 |
||
111 |
void markConservatively(MarkStack&, void* start, void* end); |
|
112 |
||
113 |
HashSet<MarkedArgumentBuffer*>& markListSet() { if (!m_markListSet) m_markListSet = new HashSet<MarkedArgumentBuffer*>; return *m_markListSet; } |
|
114 |
||
115 |
JSGlobalData* globalData() const { return m_globalData; } |
|
116 |
static bool isNumber(JSCell*); |
|
117 |
||
118 |
// Iterators for the object heap. |
|
119 |
iterator primaryHeapBegin(); |
|
120 |
iterator primaryHeapEnd(); |
|
121 |
||
122 |
private: |
|
123 |
template <HeapType heapType> void* heapAllocate(size_t); |
|
124 |
template <HeapType heapType> size_t sweep(); |
|
125 |
static CollectorBlock* cellBlock(const JSCell*); |
|
126 |
static size_t cellOffset(const JSCell*); |
|
127 |
||
128 |
friend class JSGlobalData; |
|
129 |
Heap(JSGlobalData*); |
|
130 |
~Heap(); |
|
131 |
||
132 |
template <HeapType heapType> NEVER_INLINE CollectorBlock* allocateBlock(); |
|
133 |
template <HeapType heapType> NEVER_INLINE void freeBlock(size_t); |
|
134 |
NEVER_INLINE void freeBlock(CollectorBlock*); |
|
135 |
void freeBlocks(CollectorHeap*); |
|
136 |
||
137 |
void recordExtraCost(size_t); |
|
138 |
void markProtectedObjects(MarkStack&); |
|
139 |
void markCurrentThreadConservatively(MarkStack&); |
|
140 |
void markCurrentThreadConservativelyInternal(MarkStack&); |
|
141 |
void markOtherThreadConservatively(MarkStack&, Thread*); |
|
142 |
void markStackObjectsConservatively(MarkStack&); |
|
143 |
||
144 |
typedef HashCountedSet<JSCell*> ProtectCountSet; |
|
145 |
||
146 |
CollectorHeap primaryHeap; |
|
147 |
CollectorHeap numberHeap; |
|
148 |
||
149 |
ProtectCountSet m_protectedValues; |
|
150 |
||
151 |
HashSet<MarkedArgumentBuffer*>* m_markListSet; |
|
152 |
||
153 |
#if ENABLE(JSC_MULTIPLE_THREADS) |
|
154 |
void makeUsableFromMultipleThreads(); |
|
155 |
||
156 |
static void unregisterThread(void*); |
|
157 |
void unregisterThread(); |
|
158 |
||
159 |
Mutex m_registeredThreadsMutex; |
|
160 |
Thread* m_registeredThreads; |
|
161 |
pthread_key_t m_currentThreadRegistrar; |
|
162 |
#endif |
|
163 |
||
22
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
#if PLATFORM(SYMBIAN) |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
// Allocates collector blocks with correct alignment |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
WTF::AlignedBlockAllocator m_blockallocator; |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
#endif |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
|
0 | 169 |
JSGlobalData* m_globalData; |
170 |
}; |
|
171 |
||
172 |
// tunable parameters |
|
173 |
template<size_t bytesPerWord> struct CellSize; |
|
174 |
||
175 |
// cell size needs to be a power of two for certain optimizations in collector.cpp |
|
176 |
#if USE(JSVALUE32) |
|
177 |
template<> struct CellSize<sizeof(uint32_t)> { static const size_t m_value = 32; }; |
|
178 |
#else |
|
179 |
template<> struct CellSize<sizeof(uint32_t)> { static const size_t m_value = 64; }; |
|
180 |
#endif |
|
181 |
template<> struct CellSize<sizeof(uint64_t)> { static const size_t m_value = 64; }; |
|
182 |
||
183 |
#if PLATFORM(WINCE) || PLATFORM(SYMBIAN) |
|
184 |
const size_t BLOCK_SIZE = 64 * 1024; // 64k |
|
185 |
#else |
|
186 |
const size_t BLOCK_SIZE = 64 * 4096; // 256k |
|
187 |
#endif |
|
188 |
||
189 |
// derived constants |
|
190 |
const size_t BLOCK_OFFSET_MASK = BLOCK_SIZE - 1; |
|
191 |
const size_t BLOCK_MASK = ~BLOCK_OFFSET_MASK; |
|
192 |
const size_t MINIMUM_CELL_SIZE = CellSize<sizeof(void*)>::m_value; |
|
193 |
const size_t CELL_ARRAY_LENGTH = (MINIMUM_CELL_SIZE / sizeof(double)) + (MINIMUM_CELL_SIZE % sizeof(double) != 0 ? sizeof(double) : 0); |
|
194 |
const size_t CELL_SIZE = CELL_ARRAY_LENGTH * sizeof(double); |
|
195 |
const size_t SMALL_CELL_SIZE = CELL_SIZE / 2; |
|
196 |
const size_t CELL_MASK = CELL_SIZE - 1; |
|
197 |
const size_t CELL_ALIGN_MASK = ~CELL_MASK; |
|
198 |
const size_t CELLS_PER_BLOCK = (BLOCK_SIZE * 8 - sizeof(uint32_t) * 8 - sizeof(void *) * 8 - 2 * (7 + 3 * 8)) / (CELL_SIZE * 8 + 2); |
|
199 |
const size_t SMALL_CELLS_PER_BLOCK = 2 * CELLS_PER_BLOCK; |
|
200 |
const size_t BITMAP_SIZE = (CELLS_PER_BLOCK + 7) / 8; |
|
201 |
const size_t BITMAP_WORDS = (BITMAP_SIZE + 3) / sizeof(uint32_t); |
|
202 |
||
203 |
struct CollectorBitmap { |
|
204 |
uint32_t bits[BITMAP_WORDS]; |
|
205 |
bool get(size_t n) const { return !!(bits[n >> 5] & (1 << (n & 0x1F))); } |
|
206 |
void set(size_t n) { bits[n >> 5] |= (1 << (n & 0x1F)); } |
|
207 |
void clear(size_t n) { bits[n >> 5] &= ~(1 << (n & 0x1F)); } |
|
208 |
void clearAll() { memset(bits, 0, sizeof(bits)); } |
|
209 |
}; |
|
210 |
||
211 |
struct CollectorCell { |
|
212 |
union { |
|
213 |
double memory[CELL_ARRAY_LENGTH]; |
|
214 |
struct { |
|
215 |
void* zeroIfFree; |
|
216 |
ptrdiff_t next; |
|
217 |
} freeCell; |
|
218 |
} u; |
|
219 |
}; |
|
220 |
||
221 |
struct SmallCollectorCell { |
|
222 |
union { |
|
223 |
double memory[CELL_ARRAY_LENGTH / 2]; |
|
224 |
struct { |
|
225 |
void* zeroIfFree; |
|
226 |
ptrdiff_t next; |
|
227 |
} freeCell; |
|
228 |
} u; |
|
229 |
}; |
|
230 |
||
231 |
class CollectorBlock { |
|
232 |
public: |
|
233 |
CollectorCell cells[CELLS_PER_BLOCK]; |
|
234 |
uint32_t usedCells; |
|
235 |
CollectorCell* freeList; |
|
236 |
CollectorBitmap marked; |
|
237 |
Heap* heap; |
|
238 |
HeapType type; |
|
239 |
}; |
|
240 |
||
241 |
class SmallCellCollectorBlock { |
|
242 |
public: |
|
243 |
SmallCollectorCell cells[SMALL_CELLS_PER_BLOCK]; |
|
244 |
uint32_t usedCells; |
|
245 |
SmallCollectorCell* freeList; |
|
246 |
CollectorBitmap marked; |
|
247 |
Heap* heap; |
|
248 |
HeapType type; |
|
249 |
}; |
|
250 |
||
251 |
template <HeapType heapType> struct HeapConstants; |
|
252 |
||
253 |
template <> struct HeapConstants<PrimaryHeap> { |
|
254 |
static const size_t cellSize = CELL_SIZE; |
|
255 |
static const size_t cellsPerBlock = CELLS_PER_BLOCK; |
|
256 |
static const size_t bitmapShift = 0; |
|
257 |
typedef CollectorCell Cell; |
|
258 |
typedef CollectorBlock Block; |
|
259 |
}; |
|
260 |
||
261 |
template <> struct HeapConstants<NumberHeap> { |
|
262 |
static const size_t cellSize = SMALL_CELL_SIZE; |
|
263 |
static const size_t cellsPerBlock = SMALL_CELLS_PER_BLOCK; |
|
264 |
static const size_t bitmapShift = 1; |
|
265 |
typedef SmallCollectorCell Cell; |
|
266 |
typedef SmallCellCollectorBlock Block; |
|
267 |
}; |
|
268 |
||
269 |
inline CollectorBlock* Heap::cellBlock(const JSCell* cell) |
|
270 |
{ |
|
271 |
return reinterpret_cast<CollectorBlock*>(reinterpret_cast<uintptr_t>(cell) & BLOCK_MASK); |
|
272 |
} |
|
273 |
||
274 |
inline bool Heap::isNumber(JSCell* cell) |
|
275 |
{ |
|
276 |
return Heap::cellBlock(cell)->type == NumberHeap; |
|
277 |
} |
|
278 |
||
279 |
inline size_t Heap::cellOffset(const JSCell* cell) |
|
280 |
{ |
|
281 |
return (reinterpret_cast<uintptr_t>(cell) & BLOCK_OFFSET_MASK) / CELL_SIZE; |
|
282 |
} |
|
283 |
||
284 |
inline bool Heap::isCellMarked(const JSCell* cell) |
|
285 |
{ |
|
286 |
return cellBlock(cell)->marked.get(cellOffset(cell)); |
|
287 |
} |
|
288 |
||
289 |
inline void Heap::markCell(JSCell* cell) |
|
290 |
{ |
|
291 |
cellBlock(cell)->marked.set(cellOffset(cell)); |
|
292 |
} |
|
293 |
||
294 |
inline void Heap::reportExtraMemoryCost(size_t cost) |
|
295 |
{ |
|
296 |
if (cost > minExtraCostSize) |
|
297 |
recordExtraCost(cost / (CELL_SIZE * 2)); |
|
298 |
} |
|
299 |
||
300 |
} // namespace JSC |
|
301 |
||
302 |
#endif /* Collector_h */ |