]> git.saurik.com Git - apple/javascriptcore.git/blame - heap/BlockAllocator.cpp
JavaScriptCore-1218.34.tar.gz
[apple/javascriptcore.git] / heap / BlockAllocator.cpp
CommitLineData
6fe7ccc8
A
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
93a37866
A
29#include "CopiedBlock.h"
30#include "CopyWorkList.h"
6fe7ccc8 31#include "MarkedBlock.h"
93a37866 32#include "WeakBlock.h"
6fe7ccc8
A
33#include <wtf/CurrentTime.h>
34
35namespace JSC {
36
37BlockAllocator::BlockAllocator()
93a37866
A
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)
6fe7ccc8
A
44 , m_isCurrentlyAllocating(false)
45 , m_blockFreeingThreadShouldQuit(false)
93a37866 46#if PLATFORM(IOS)
6fe7ccc8
A
47 , m_blockFreeingThread(GCActivityCallback::s_shouldCreateGCTimer ?
48 createThread(blockFreeingThreadStartFunc, this, "JavaScriptCore::BlockFree") : 0)
93a37866
A
49#else
50 , m_blockFreeingThread(createThread(blockFreeingThreadStartFunc, this, "JavaScriptCore::BlockFree"))
51#endif
6fe7ccc8 52{
93a37866
A
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();
6fe7ccc8
A
59}
60
61BlockAllocator::~BlockAllocator()
62{
93a37866 63 releaseFreeRegions();
6fe7ccc8 64 {
93a37866 65 MutexLocker locker(m_emptyRegionConditionLock);
6fe7ccc8 66 m_blockFreeingThreadShouldQuit = true;
93a37866 67 m_emptyRegionCondition.broadcast();
6fe7ccc8 68 }
93a37866 69#if PLATFORM(IOS)
6fe7ccc8
A
70 if (GCActivityCallback::s_shouldCreateGCTimer)
71 waitForThreadCompletion(m_blockFreeingThread);
93a37866
A
72#else
73 waitForThreadCompletion(m_blockFreeingThread);
74#endif
75 ASSERT(allRegionSetsAreEmpty());
76 ASSERT(m_emptyRegions.isEmpty());
6fe7ccc8
A
77}
78
93a37866
A
79bool BlockAllocator::allRegionSetsAreEmpty() const
80{
81 return m_copiedRegionSet.isEmpty()
82 && m_markedRegionSet.isEmpty()
83 && m_fourKBBlockRegionSet.isEmpty()
84 && m_workListRegionSet.isEmpty();
85}
86
87void BlockAllocator::releaseFreeRegions()
6fe7ccc8
A
88{
89 while (true) {
93a37866 90 Region* region;
6fe7ccc8 91 {
93a37866
A
92 SpinLockHolder locker(&m_regionLock);
93 if (!m_numberOfEmptyRegions)
94 region = 0;
6fe7ccc8 95 else {
93a37866
A
96 region = m_emptyRegions.removeHead();
97 RELEASE_ASSERT(region);
98 m_numberOfEmptyRegions--;
6fe7ccc8
A
99 }
100 }
101
93a37866 102 if (!region)
6fe7ccc8 103 break;
93a37866
A
104
105 region->destroy();
6fe7ccc8
A
106 }
107}
108
109void BlockAllocator::waitForRelativeTimeWhileHoldingLock(double relative)
110{
111 if (m_blockFreeingThreadShouldQuit)
112 return;
93a37866
A
113
114 m_emptyRegionCondition.timedWait(m_emptyRegionConditionLock, currentTime() + relative);
6fe7ccc8
A
115}
116
117void 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
93a37866 123 MutexLocker locker(m_emptyRegionConditionLock);
6fe7ccc8
A
124 waitForRelativeTimeWhileHoldingLock(relative);
125}
126
127void BlockAllocator::blockFreeingThreadStartFunc(void* blockAllocator)
128{
129 static_cast<BlockAllocator*>(blockAllocator)->blockFreeingThreadMain();
130}
131
132void BlockAllocator::blockFreeingThreadMain()
133{
93a37866 134 size_t currentNumberOfEmptyRegions;
6fe7ccc8
A
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
93a37866
A
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 }
6fe7ccc8 158
93a37866 159 size_t desiredNumberOfEmptyRegions = currentNumberOfEmptyRegions / 2;
6fe7ccc8
A
160
161 while (!m_blockFreeingThreadShouldQuit) {
93a37866 162 Region* region;
6fe7ccc8 163 {
93a37866
A
164 SpinLockHolder locker(&m_regionLock);
165 if (m_numberOfEmptyRegions <= desiredNumberOfEmptyRegions)
166 region = 0;
6fe7ccc8 167 else {
93a37866
A
168 region = m_emptyRegions.removeHead();
169 RELEASE_ASSERT(region);
170 m_numberOfEmptyRegions--;
6fe7ccc8
A
171 }
172 }
173
93a37866 174 if (!region)
6fe7ccc8
A
175 break;
176
93a37866 177 region->destroy();
6fe7ccc8
A
178 }
179 }
180}
181
182} // namespace JSC