]> git.saurik.com Git - apple/javascriptcore.git/blob - heap/MarkStack.cpp
66201f4f107449165ee9f0e6e540e64df58af034
[apple/javascriptcore.git] / heap / MarkStack.cpp
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"
28
29 #include "JSCInlines.h"
30
31 namespace JSC {
32
33 MarkStackArray::MarkStackArray(BlockAllocator& blockAllocator)
34 : GCSegmentedArray<const JSCell*>(blockAllocator)
35 {
36 }
37
38 void MarkStackArray::donateSomeCellsTo(MarkStackArray& other)
39 {
40 // Try to donate about 1 / 2 of our cells. To reduce copying costs,
41 // we prefer donating whole segments over donating individual cells,
42 // even if this skews away from our 1 / 2 target.
43
44 size_t segmentsToDonate = m_numberOfSegments / 2; // If we only have one segment (our head) we don't donate any segments.
45
46 if (!segmentsToDonate) {
47 size_t cellsToDonate = m_top / 2; // Round down to donate 0 / 1 cells.
48 while (cellsToDonate--) {
49 ASSERT(m_top);
50 other.append(removeLast());
51 }
52 return;
53 }
54
55 validatePrevious();
56 other.validatePrevious();
57
58 // Remove our head and the head of the other list before we start moving segments around.
59 // We'll add them back on once we're done donating.
60 GCArraySegment<const JSCell*>* myHead = m_segments.removeHead();
61 GCArraySegment<const JSCell*>* otherHead = other.m_segments.removeHead();
62
63 while (segmentsToDonate--) {
64 GCArraySegment<const JSCell*>* current = m_segments.removeHead();
65 ASSERT(current);
66 ASSERT(m_numberOfSegments > 1);
67 other.m_segments.push(current);
68 m_numberOfSegments--;
69 other.m_numberOfSegments++;
70 }
71
72 // Put the original heads back in their places.
73 m_segments.push(myHead);
74 other.m_segments.push(otherHead);
75
76 validatePrevious();
77 other.validatePrevious();
78 }
79
80 void MarkStackArray::stealSomeCellsFrom(MarkStackArray& other, size_t idleThreadCount)
81 {
82 // Try to steal 1 / Nth of the shared array, where N is the number of idle threads.
83 // To reduce copying costs, we prefer stealing a whole segment over stealing
84 // individual cells, even if this skews away from our 1 / N target.
85
86 validatePrevious();
87 other.validatePrevious();
88
89 // If other has an entire segment, steal it and return.
90 if (other.m_numberOfSegments > 1) {
91 // Move the heads of the lists aside. We'll push them back on after.
92 GCArraySegment<const JSCell*>* otherHead = other.m_segments.removeHead();
93 GCArraySegment<const JSCell*>* myHead = m_segments.removeHead();
94
95 ASSERT(other.m_segments.head()->m_top == s_segmentCapacity);
96
97 m_segments.push(other.m_segments.removeHead());
98
99 m_numberOfSegments++;
100 other.m_numberOfSegments--;
101
102 m_segments.push(myHead);
103 other.m_segments.push(otherHead);
104
105 validatePrevious();
106 other.validatePrevious();
107 return;
108 }
109
110 size_t numberOfCellsToSteal = (other.size() + idleThreadCount - 1) / idleThreadCount; // Round up to steal 1 / 1.
111 while (numberOfCellsToSteal-- > 0 && other.canRemoveLast())
112 append(other.removeLast());
113 }
114
115 } // namespace JSC