1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
/*
* Copyright (C) 2009, 2011 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef MarkStack_h
#define MarkStack_h
#include "HandleTypes.h"
#include "Options.h"
#include "JSValue.h"
#include "Register.h"
#include "UnconditionalFinalizer.h"
#include "VTableSpectrum.h"
#include "WeakReferenceHarvester.h"
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/Vector.h>
#include <wtf/Noncopyable.h>
#include <wtf/OSAllocator.h>
#include <wtf/PageBlock.h>
namespace JSC {
class ConservativeRoots;
class JSGlobalData;
class MarkStack;
class ParallelModeEnabler;
class Register;
class SlotVisitor;
template<typename T> class WriteBarrierBase;
template<typename T> class JITWriteBarrier;
struct MarkStackSegment {
MarkStackSegment* m_previous;
#if !ASSERT_DISABLED
size_t m_top;
#endif
const JSCell** data()
{
return bitwise_cast<const JSCell**>(this + 1);
}
static size_t capacityFromSize(size_t size)
{
return (size - sizeof(MarkStackSegment)) / sizeof(const JSCell*);
}
static size_t sizeFromCapacity(size_t capacity)
{
return sizeof(MarkStackSegment) + capacity * sizeof(const JSCell*);
}
};
class MarkStackSegmentAllocator {
public:
MarkStackSegmentAllocator();
~MarkStackSegmentAllocator();
MarkStackSegment* allocate();
void release(MarkStackSegment*);
void shrinkReserve();
private:
Mutex m_lock;
MarkStackSegment* m_nextFreeSegment;
};
class MarkStackArray {
public:
MarkStackArray(MarkStackSegmentAllocator&);
~MarkStackArray();
void append(const JSCell*);
bool canRemoveLast();
const JSCell* removeLast();
bool refill();
bool isEmpty();
bool canDonateSomeCells(); // Returns false if you should definitely not call doanteSomeCellsTo().
bool donateSomeCellsTo(MarkStackArray& other); // Returns true if some cells were donated.
void stealSomeCellsFrom(MarkStackArray& other);
size_t size();
private:
MarkStackSegment* m_topSegment;
void expand();
MarkStackSegmentAllocator& m_allocator;
size_t m_segmentCapacity;
size_t m_top;
size_t m_numberOfPreviousSegments;
size_t postIncTop()
{
size_t result = m_top++;
ASSERT(result == m_topSegment->m_top++);
return result;
}
size_t preDecTop()
{
size_t result = --m_top;
ASSERT(result == --m_topSegment->m_top);
return result;
}
void setTopForFullSegment()
{
ASSERT(m_topSegment->m_top == m_segmentCapacity);
m_top = m_segmentCapacity;
}
void setTopForEmptySegment()
{
ASSERT(!m_topSegment->m_top);
m_top = 0;
}
size_t top()
{
ASSERT(m_top == m_topSegment->m_top);
return m_top;
}
#if ASSERT_DISABLED
void validatePrevious() { }
#else
void validatePrevious()
{
unsigned count = 0;
for (MarkStackSegment* current = m_topSegment->m_previous; current; current = current->m_previous)
count++;
ASSERT(count == m_numberOfPreviousSegments);
}
#endif
};
class MarkStackThreadSharedData {
public:
MarkStackThreadSharedData(JSGlobalData*);
~MarkStackThreadSharedData();
void reset();
private:
friend class MarkStack;
friend class SlotVisitor;
#if ENABLE(PARALLEL_GC)
void markingThreadMain();
static void* markingThreadStartFunc(void* heap);
#endif
JSGlobalData* m_globalData;
MarkStackSegmentAllocator m_segmentAllocator;
Vector<ThreadIdentifier> m_markingThreads;
Mutex m_markingLock;
ThreadCondition m_markingCondition;
MarkStackArray m_sharedMarkStack;
unsigned m_numberOfActiveParallelMarkers;
bool m_parallelMarkersShouldExit;
Mutex m_opaqueRootsLock;
HashSet<void*> m_opaqueRoots;
ListableHandler<WeakReferenceHarvester>::List m_weakReferenceHarvesters;
ListableHandler<UnconditionalFinalizer>::List m_unconditionalFinalizers;
};
class MarkStack {
WTF_MAKE_NONCOPYABLE(MarkStack);
friend class HeapRootVisitor; // Allowed to mark a JSValue* or JSCell** directly.
public:
MarkStack(MarkStackThreadSharedData&);
~MarkStack();
void append(ConservativeRoots&);
template<typename T> void append(JITWriteBarrier<T>*);
template<typename T> void append(WriteBarrierBase<T>*);
void appendValues(WriteBarrierBase<Unknown>*, size_t count);
template<typename T>
void appendUnbarrieredPointer(T**);
void addOpaqueRoot(void*);
bool containsOpaqueRoot(void*);
int opaqueRootCount();
bool isEmpty() { return m_stack.isEmpty(); }
void reset();
size_t visitCount() const { return m_visitCount; }
#if ENABLE(SIMPLE_HEAP_PROFILING)
VTableSpectrum m_visitedTypeCounts;
#endif
void addWeakReferenceHarvester(WeakReferenceHarvester* weakReferenceHarvester)
{
m_shared.m_weakReferenceHarvesters.addThreadSafe(weakReferenceHarvester);
}
void addUnconditionalFinalizer(UnconditionalFinalizer* unconditionalFinalizer)
{
m_shared.m_unconditionalFinalizers.addThreadSafe(unconditionalFinalizer);
}
protected:
static void validate(JSCell*);
void append(JSValue*);
void append(JSValue*, size_t count);
void append(JSCell**);
void internalAppend(JSCell*);
void internalAppend(JSValue);
void mergeOpaqueRoots();
void mergeOpaqueRootsIfNecessary()
{
if (m_opaqueRoots.isEmpty())
return;
mergeOpaqueRoots();
}
void mergeOpaqueRootsIfProfitable()
{
if (static_cast<unsigned>(m_opaqueRoots.size()) < Options::opaqueRootMergeThreshold)
return;
mergeOpaqueRoots();
}
MarkStackArray m_stack;
HashSet<void*> m_opaqueRoots; // Handle-owning data structures not visible to the garbage collector.
#if !ASSERT_DISABLED
public:
bool m_isCheckingForDefaultMarkViolation;
bool m_isDraining;
#endif
protected:
friend class ParallelModeEnabler;
size_t m_visitCount;
bool m_isInParallelMode;
MarkStackThreadSharedData& m_shared;
};
inline MarkStack::MarkStack(MarkStackThreadSharedData& shared)
: m_stack(shared.m_segmentAllocator)
#if !ASSERT_DISABLED
, m_isCheckingForDefaultMarkViolation(false)
, m_isDraining(false)
#endif
, m_visitCount(0)
, m_isInParallelMode(false)
, m_shared(shared)
{
}
inline MarkStack::~MarkStack()
{
ASSERT(m_stack.isEmpty());
}
inline void MarkStack::addOpaqueRoot(void* root)
{
#if ENABLE(PARALLEL_GC)
if (Options::numberOfGCMarkers == 1) {
// Put directly into the shared HashSet.
m_shared.m_opaqueRoots.add(root);
return;
}
// Put into the local set, but merge with the shared one every once in
// a while to make sure that the local sets don't grow too large.
mergeOpaqueRootsIfProfitable();
m_opaqueRoots.add(root);
#else
m_opaqueRoots.add(root);
#endif
}
inline bool MarkStack::containsOpaqueRoot(void* root)
{
ASSERT(!m_isInParallelMode);
#if ENABLE(PARALLEL_GC)
ASSERT(m_opaqueRoots.isEmpty());
return m_shared.m_opaqueRoots.contains(root);
#else
return m_opaqueRoots.contains(root);
#endif
}
inline int MarkStack::opaqueRootCount()
{
ASSERT(!m_isInParallelMode);
#if ENABLE(PARALLEL_GC)
ASSERT(m_opaqueRoots.isEmpty());
return m_shared.m_opaqueRoots.size();
#else
return m_opaqueRoots.size();
#endif
}
inline void MarkStackArray::append(const JSCell* cell)
{
if (m_top == m_segmentCapacity)
expand();
m_topSegment->data()[postIncTop()] = cell;
}
inline bool MarkStackArray::canRemoveLast()
{
return !!m_top;
}
inline const JSCell* MarkStackArray::removeLast()
{
return m_topSegment->data()[preDecTop()];
}
inline bool MarkStackArray::isEmpty()
{
if (m_top)
return false;
if (m_topSegment->m_previous) {
ASSERT(m_topSegment->m_previous->m_top == m_segmentCapacity);
return false;
}
return true;
}
inline bool MarkStackArray::canDonateSomeCells()
{
size_t numberOfCellsToKeep = Options::minimumNumberOfCellsToKeep;
// Another check: see if we have enough cells to warrant donation.
if (m_top <= numberOfCellsToKeep) {
// This indicates that we might not want to donate anything; check if we have
// another full segment. If not, then don't donate.
if (!m_topSegment->m_previous)
return false;
ASSERT(m_topSegment->m_previous->m_top == m_segmentCapacity);
}
return true;
}
inline size_t MarkStackArray::size()
{
return m_top + m_segmentCapacity * m_numberOfPreviousSegments;
}
ALWAYS_INLINE void MarkStack::append(JSValue* slot, size_t count)
{
for (size_t i = 0; i < count; ++i) {
JSValue& value = slot[i];
if (!value)
continue;
internalAppend(value);
}
}
template<typename T>
inline void MarkStack::appendUnbarrieredPointer(T** slot)
{
ASSERT(slot);
JSCell* cell = *slot;
if (cell)
internalAppend(cell);
}
ALWAYS_INLINE void MarkStack::append(JSValue* slot)
{
ASSERT(slot);
internalAppend(*slot);
}
ALWAYS_INLINE void MarkStack::append(JSCell** slot)
{
ASSERT(slot);
internalAppend(*slot);
}
ALWAYS_INLINE void MarkStack::internalAppend(JSValue value)
{
ASSERT(value);
if (!value.isCell())
return;
internalAppend(value.asCell());
}
class ParallelModeEnabler {
public:
ParallelModeEnabler(MarkStack& stack)
: m_stack(stack)
{
ASSERT(!m_stack.m_isInParallelMode);
m_stack.m_isInParallelMode = true;
}
~ParallelModeEnabler()
{
ASSERT(m_stack.m_isInParallelMode);
m_stack.m_isInParallelMode = false;
}
private:
MarkStack& m_stack;
};
} // namespace JSC
#endif
|