]> git.saurik.com Git - apple/javascriptcore.git/blob - heap/BlockAllocator.cpp
JavaScriptCore-1218.35.tar.gz
[apple/javascriptcore.git] / heap / BlockAllocator.cpp
1 /*
2 * Copyright (C) 2012 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "BlockAllocator.h"
28
29 #include "CopiedBlock.h"
30 #include "CopyWorkList.h"
31 #include "MarkedBlock.h"
32 #include "WeakBlock.h"
33 #include <wtf/CurrentTime.h>
34
35 namespace JSC {
36
37 BlockAllocator::BlockAllocator()
38 : m_superRegion()
39 , m_copiedRegionSet(CopiedBlock::blockSize)
40 , m_markedRegionSet(MarkedBlock::blockSize)
41 , m_fourKBBlockRegionSet(WeakBlock::blockSize)
42 , m_workListRegionSet(CopyWorkListSegment::blockSize)
43 , m_numberOfEmptyRegions(0)
44 , m_isCurrentlyAllocating(false)
45 , m_blockFreeingThreadShouldQuit(false)
46 #if PLATFORM(IOS)
47 , m_blockFreeingThread(GCActivityCallback::s_shouldCreateGCTimer ?
48 createThread(blockFreeingThreadStartFunc, this, "JavaScriptCore::BlockFree") : 0)
49 #else
50 , m_blockFreeingThread(createThread(blockFreeingThreadStartFunc, this, "JavaScriptCore::BlockFree"))
51 #endif
52 {
53 #if PLATFORM(IOS)
54 RELEASE_ASSERT(m_blockFreeingThread || !GCActivityCallback::s_shouldCreateGCTimer);
55 #else
56 RELEASE_ASSERT(m_blockFreeingThread);
57 #endif
58 m_regionLock.Init();
59 }
60
61 BlockAllocator::~BlockAllocator()
62 {
63 releaseFreeRegions();
64 {
65 MutexLocker locker(m_emptyRegionConditionLock);
66 m_blockFreeingThreadShouldQuit = true;
67 m_emptyRegionCondition.broadcast();
68 }
69 #if PLATFORM(IOS)
70 if (GCActivityCallback::s_shouldCreateGCTimer)
71 waitForThreadCompletion(m_blockFreeingThread);
72 #else
73 waitForThreadCompletion(m_blockFreeingThread);
74 #endif
75 ASSERT(allRegionSetsAreEmpty());
76 ASSERT(m_emptyRegions.isEmpty());
77 }
78
79 bool BlockAllocator::allRegionSetsAreEmpty() const
80 {
81 return m_copiedRegionSet.isEmpty()
82 && m_markedRegionSet.isEmpty()
83 && m_fourKBBlockRegionSet.isEmpty()
84 && m_workListRegionSet.isEmpty();
85 }
86
87 void BlockAllocator::releaseFreeRegions()
88 {
89 while (true) {
90 Region* region;
91 {
92 SpinLockHolder locker(&m_regionLock);
93 if (!m_numberOfEmptyRegions)
94 region = 0;
95 else {
96 region = m_emptyRegions.removeHead();
97 RELEASE_ASSERT(region);
98 m_numberOfEmptyRegions--;
99 }
100 }
101
102 if (!region)
103 break;
104
105 region->destroy();
106 }
107 }
108
109 void BlockAllocator::waitForRelativeTimeWhileHoldingLock(double relative)
110 {
111 if (m_blockFreeingThreadShouldQuit)
112 return;
113
114 m_emptyRegionCondition.timedWait(m_emptyRegionConditionLock, currentTime() + relative);
115 }
116
117 void BlockAllocator::waitForRelativeTime(double relative)
118 {
119 // If this returns early, that's fine, so long as it doesn't do it too
120 // frequently. It would only be a bug if this function failed to return
121 // when it was asked to do so.
122
123 MutexLocker locker(m_emptyRegionConditionLock);
124 waitForRelativeTimeWhileHoldingLock(relative);
125 }
126
127 void BlockAllocator::blockFreeingThreadStartFunc(void* blockAllocator)
128 {
129 static_cast<BlockAllocator*>(blockAllocator)->blockFreeingThreadMain();
130 }
131
132 void BlockAllocator::blockFreeingThreadMain()
133 {
134 size_t currentNumberOfEmptyRegions;
135 while (!m_blockFreeingThreadShouldQuit) {
136 // Generally wait for one second before scavenging free blocks. This
137 // may return early, particularly when we're being asked to quit.
138 waitForRelativeTime(1.0);
139 if (m_blockFreeingThreadShouldQuit)
140 break;
141
142 if (m_isCurrentlyAllocating) {
143 m_isCurrentlyAllocating = false;
144 continue;
145 }
146
147 // Sleep until there is actually work to do rather than waking up every second to check.
148 {
149 MutexLocker locker(m_emptyRegionConditionLock);
150 SpinLockHolder regionLocker(&m_regionLock);
151 while (!m_numberOfEmptyRegions && !m_blockFreeingThreadShouldQuit) {
152 m_regionLock.Unlock();
153 m_emptyRegionCondition.wait(m_emptyRegionConditionLock);
154 m_regionLock.Lock();
155 }
156 currentNumberOfEmptyRegions = m_numberOfEmptyRegions;
157 }
158
159 size_t desiredNumberOfEmptyRegions = currentNumberOfEmptyRegions / 2;
160
161 while (!m_blockFreeingThreadShouldQuit) {
162 Region* region;
163 {
164 SpinLockHolder locker(&m_regionLock);
165 if (m_numberOfEmptyRegions <= desiredNumberOfEmptyRegions)
166 region = 0;
167 else {
168 region = m_emptyRegions.removeHead();
169 RELEASE_ASSERT(region);
170 m_numberOfEmptyRegions--;
171 }
172 }
173
174 if (!region)
175 break;
176
177 region->destroy();
178 }
179 }
180 }
181
182 } // namespace JSC