]> git.saurik.com Git - apple/javascriptcore.git/blame - heap/MarkStack.cpp
JavaScriptCore-1218.34.tar.gz
[apple/javascriptcore.git] / heap / MarkStack.cpp
CommitLineData
14957cd0
A
1/*
2 * Copyright (C) 2009, 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "MarkStack.h"
93a37866 28#include "MarkStackInlines.h"
14957cd0
A
29
30#include "ConservativeRoots.h"
93a37866
A
31#include "CopiedSpace.h"
32#include "CopiedSpaceInlines.h"
14957cd0
A
33#include "Heap.h"
34#include "JSArray.h"
35#include "JSCell.h"
36#include "JSObject.h"
93a37866
A
37
38#include "SlotVisitorInlines.h"
14957cd0 39#include "Structure.h"
6fe7ccc8 40#include "WriteBarrier.h"
93a37866
A
41#include <wtf/Atomics.h>
42#include <wtf/DataLog.h>
6fe7ccc8 43#include <wtf/MainThread.h>
14957cd0
A
44
45namespace JSC {
46
93a37866 47COMPILE_ASSERT(MarkStackSegment::blockSize == WeakBlock::blockSize, blockSizeMatch);
6fe7ccc8 48
93a37866
A
49MarkStackArray::MarkStackArray(BlockAllocator& blockAllocator)
50 : m_blockAllocator(blockAllocator)
6fe7ccc8 51 , m_top(0)
93a37866 52 , m_numberOfSegments(0)
6fe7ccc8 53{
93a37866
A
54 m_segments.push(MarkStackSegment::create(m_blockAllocator.allocate<MarkStackSegment>()));
55 m_numberOfSegments++;
6fe7ccc8
A
56}
57
58MarkStackArray::~MarkStackArray()
59{
93a37866
A
60 ASSERT(m_numberOfSegments == 1 && m_segments.size() == 1);
61 m_blockAllocator.deallocate(MarkStackSegment::destroy(m_segments.removeHead()));
6fe7ccc8
A
62}
63
64void MarkStackArray::expand()
65{
93a37866 66 ASSERT(m_segments.head()->m_top == s_segmentCapacity);
6fe7ccc8 67
93a37866
A
68 MarkStackSegment* nextSegment = MarkStackSegment::create(m_blockAllocator.allocate<MarkStackSegment>());
69 m_numberOfSegments++;
6fe7ccc8 70
6fe7ccc8
A
71#if !ASSERT_DISABLED
72 nextSegment->m_top = 0;
73#endif
93a37866
A
74
75 m_segments.push(nextSegment);
6fe7ccc8
A
76 setTopForEmptySegment();
77 validatePrevious();
78}
79
80bool MarkStackArray::refill()
81{
82 validatePrevious();
83 if (top())
84 return true;
93a37866
A
85 m_blockAllocator.deallocate(MarkStackSegment::destroy(m_segments.removeHead()));
86 ASSERT(m_numberOfSegments > 1);
87 m_numberOfSegments--;
6fe7ccc8
A
88 setTopForFullSegment();
89 validatePrevious();
90 return true;
91}
92
93a37866 93void MarkStackArray::donateSomeCellsTo(MarkStackArray& other)
6fe7ccc8 94{
93a37866
A
95 // Try to donate about 1 / 2 of our cells. To reduce copying costs,
96 // we prefer donating whole segments over donating individual cells,
97 // even if this skews away from our 1 / 2 target.
6fe7ccc8 98
93a37866 99 size_t segmentsToDonate = m_numberOfSegments / 2; // If we only have one segment (our head) we don't donate any segments.
6fe7ccc8 100
93a37866
A
101 if (!segmentsToDonate) {
102 size_t cellsToDonate = m_top / 2; // Round down to donate 0 / 1 cells.
103 while (cellsToDonate--) {
104 ASSERT(m_top);
105 other.append(removeLast());
106 }
6fe7ccc8
A
107 return;
108 }
14957cd0 109
93a37866
A
110 validatePrevious();
111 other.validatePrevious();
6fe7ccc8 112
93a37866
A
113 // Remove our head and the head of the other list before we start moving segments around.
114 // We'll add them back on once we're done donating.
115 MarkStackSegment* myHead = m_segments.removeHead();
116 MarkStackSegment* otherHead = other.m_segments.removeHead();
14957cd0 117
93a37866
A
118 while (segmentsToDonate--) {
119 MarkStackSegment* current = m_segments.removeHead();
120 ASSERT(current);
121 ASSERT(m_numberOfSegments > 1);
122 other.m_segments.push(current);
123 m_numberOfSegments--;
124 other.m_numberOfSegments++;
14957cd0 125 }
6fe7ccc8 126
93a37866
A
127 // Put the original heads back in their places.
128 m_segments.push(myHead);
129 other.m_segments.push(otherHead);
6fe7ccc8 130
93a37866
A
131 validatePrevious();
132 other.validatePrevious();
14957cd0
A
133}
134
93a37866 135void MarkStackArray::stealSomeCellsFrom(MarkStackArray& other, size_t idleThreadCount)
14957cd0 136{
93a37866
A
137 // Try to steal 1 / Nth of the shared array, where N is the number of idle threads.
138 // To reduce copying costs, we prefer stealing a whole segment over stealing
139 // individual cells, even if this skews away from our 1 / N target.
6fe7ccc8 140
93a37866
A
141 validatePrevious();
142 other.validatePrevious();
6fe7ccc8 143
93a37866
A
144 // If other has an entire segment, steal it and return.
145 if (other.m_numberOfSegments > 1) {
146 // Move the heads of the lists aside. We'll push them back on after.
147 MarkStackSegment* otherHead = other.m_segments.removeHead();
148 MarkStackSegment* myHead = m_segments.removeHead();
14957cd0 149
93a37866 150 ASSERT(other.m_segments.head()->m_top == s_segmentCapacity);
14957cd0 151
93a37866 152 m_segments.push(other.m_segments.removeHead());
14957cd0 153
93a37866
A
154 m_numberOfSegments++;
155 other.m_numberOfSegments--;
6fe7ccc8 156
93a37866
A
157 m_segments.push(myHead);
158 other.m_segments.push(otherHead);
6fe7ccc8 159
93a37866
A
160 validatePrevious();
161 other.validatePrevious();
14957cd0 162 return;
93a37866 163 }
6fe7ccc8 164
93a37866
A
165 size_t numberOfCellsToSteal = (other.size() + idleThreadCount - 1) / idleThreadCount; // Round up to steal 1 / 1.
166 while (numberOfCellsToSteal-- > 0 && other.canRemoveLast())
167 append(other.removeLast());
6fe7ccc8 168}
14957cd0
A
169
170} // namespace JSC