blob: ec07db2e0eb832360b8c819d647b4c588917b594 [file] [log] [blame]
fpizlo@apple.com195d7b82013-09-03 16:39:29 +00001/*
fpizlo@apple.comea379af2016-10-21 02:17:35 +00002 * Copyright (C) 2013-2016 Apple Inc. All rights reserved.
fpizlo@apple.com195d7b82013-09-03 16:39:29 +00003 *
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 "CodeBlockSet.h"
28
29#include "CodeBlock.h"
fpizlo@apple.comfb7eff22014-02-11 01:45:50 +000030#include "JSCInlines.h"
fpizlo@apple.com229fa342014-05-08 02:35:22 +000031#include <wtf/CommaPrinter.h>
fpizlo@apple.com195d7b82013-09-03 16:39:29 +000032
33namespace JSC {
34
35static const bool verbose = false;
36
ggaren@apple.comccb53a72015-01-29 19:20:06 +000037CodeBlockSet::CodeBlockSet()
mhahnenberg@apple.com87a6cde2014-02-05 17:31:47 +000038{
39}
fpizlo@apple.com195d7b82013-09-03 16:39:29 +000040
41CodeBlockSet::~CodeBlockSet()
42{
fpizlo@apple.com195d7b82013-09-03 16:39:29 +000043}
44
ggaren@apple.com81def5f2015-10-09 23:10:16 +000045void CodeBlockSet::add(CodeBlock* codeBlock)
fpizlo@apple.com195d7b82013-09-03 16:39:29 +000046{
sbarati@apple.coma4ce86b2016-01-11 06:49:49 +000047 LockHolder locker(&m_lock);
ggaren@apple.com81def5f2015-10-09 23:10:16 +000048 bool isNewEntry = m_newCodeBlocks.add(codeBlock).isNewEntry;
fpizlo@apple.com195d7b82013-09-03 16:39:29 +000049 ASSERT_UNUSED(isNewEntry, isNewEntry);
50}
51
sbarati@apple.coma4ce86b2016-01-11 06:49:49 +000052void CodeBlockSet::promoteYoungCodeBlocks(const LockHolder&)
fpizlo@apple.com195d7b82013-09-03 16:39:29 +000053{
sbarati@apple.coma4ce86b2016-01-11 06:49:49 +000054 ASSERT(m_lock.isLocked());
mhahnenberg@apple.com7d223bb2014-04-02 23:50:25 +000055 m_oldCodeBlocks.add(m_newCodeBlocks.begin(), m_newCodeBlocks.end());
56 m_newCodeBlocks.clear();
57}
58
59void CodeBlockSet::clearMarksForFullCollection()
60{
sbarati@apple.coma4ce86b2016-01-11 06:49:49 +000061 LockHolder locker(&m_lock);
ggaren@apple.com0eb16442015-09-02 19:52:04 +000062 for (CodeBlock* codeBlock : m_oldCodeBlocks)
ggaren@apple.com81def5f2015-10-09 23:10:16 +000063 codeBlock->clearVisitWeaklyHasBeenCalled();
mhahnenberg@apple.com7d223bb2014-04-02 23:50:25 +000064
65 // We promote after we clear marks on the old generation CodeBlocks because
66 // none of the young generations CodeBlocks need to be cleared.
sbarati@apple.coma4ce86b2016-01-11 06:49:49 +000067 promoteYoungCodeBlocks(locker);
fpizlo@apple.com195d7b82013-09-03 16:39:29 +000068}
69
ggaren@apple.com81def5f2015-10-09 23:10:16 +000070void CodeBlockSet::lastChanceToFinalize()
commit-queue@webkit.org87dc3e72015-10-04 01:45:21 +000071{
sbarati@apple.coma4ce86b2016-01-11 06:49:49 +000072 LockHolder locker(&m_lock);
ggaren@apple.com81def5f2015-10-09 23:10:16 +000073 for (CodeBlock* codeBlock : m_newCodeBlocks)
74 codeBlock->classInfo()->methodTable.destroy(codeBlock);
75
76 for (CodeBlock* codeBlock : m_oldCodeBlocks)
77 codeBlock->classInfo()->methodTable.destroy(codeBlock);
commit-queue@webkit.org87dc3e72015-10-04 01:45:21 +000078}
79
fpizlo@apple.comea379af2016-10-21 02:17:35 +000080void CodeBlockSet::deleteUnmarkedAndUnreferenced(CollectionScope scope)
mhahnenberg@apple.com7d223bb2014-04-02 23:50:25 +000081{
sbarati@apple.coma4ce86b2016-01-11 06:49:49 +000082 LockHolder locker(&m_lock);
fpizlo@apple.comea379af2016-10-21 02:17:35 +000083 HashSet<CodeBlock*>& set = scope == CollectionScope::Eden ? m_newCodeBlocks : m_oldCodeBlocks;
ggaren@apple.com81def5f2015-10-09 23:10:16 +000084 Vector<CodeBlock*> unmarked;
85 for (CodeBlock* codeBlock : set) {
86 if (Heap::isMarked(codeBlock))
87 continue;
88 unmarked.append(codeBlock);
89 }
mhahnenberg@apple.com7d223bb2014-04-02 23:50:25 +000090
ggaren@apple.com81def5f2015-10-09 23:10:16 +000091 for (CodeBlock* codeBlock : unmarked) {
92 codeBlock->classInfo()->methodTable.destroy(codeBlock);
93 set.remove(codeBlock);
fpizlo@apple.com195d7b82013-09-03 16:39:29 +000094 }
mhahnenberg@apple.com7d223bb2014-04-02 23:50:25 +000095
96 // Any remaining young CodeBlocks are live and need to be promoted to the set of old CodeBlocks.
fpizlo@apple.comea379af2016-10-21 02:17:35 +000097 if (scope == CollectionScope::Eden)
sbarati@apple.coma4ce86b2016-01-11 06:49:49 +000098 promoteYoungCodeBlocks(locker);
fpizlo@apple.com195d7b82013-09-03 16:39:29 +000099}
100
sbarati@apple.coma4ce86b2016-01-11 06:49:49 +0000101bool CodeBlockSet::contains(const LockHolder&, void* candidateCodeBlock)
mark.lam@apple.comaebf6852014-03-03 21:39:21 +0000102{
sbarati@apple.coma4ce86b2016-01-11 06:49:49 +0000103 RELEASE_ASSERT(m_lock.isLocked());
104 CodeBlock* codeBlock = static_cast<CodeBlock*>(candidateCodeBlock);
105 if (!HashSet<CodeBlock*>::isValidValue(codeBlock))
106 return false;
107 return m_oldCodeBlocks.contains(codeBlock) || m_newCodeBlocks.contains(codeBlock) || m_currentlyExecuting.contains(codeBlock);
mark.lam@apple.comaebf6852014-03-03 21:39:21 +0000108}
109
fpizlo@apple.com7b231642016-10-11 23:52:02 +0000110void CodeBlockSet::writeBarrierCurrentlyExecuting(Heap* heap)
mhahnenberg@apple.com3ddd7ac2014-01-10 02:28:27 +0000111{
sbarati@apple.coma4ce86b2016-01-11 06:49:49 +0000112 LockHolder locker(&m_lock);
fpizlo@apple.com04a048c2014-04-28 19:01:07 +0000113 if (verbose)
114 dataLog("Remembering ", m_currentlyExecuting.size(), " code blocks.\n");
ggaren@apple.com81def5f2015-10-09 23:10:16 +0000115 for (CodeBlock* codeBlock : m_currentlyExecuting)
116 heap->writeBarrier(codeBlock);
fpizlo@apple.com7b231642016-10-11 23:52:02 +0000117}
commit-queue@webkit.org87dc3e72015-10-04 01:45:21 +0000118
fpizlo@apple.com7b231642016-10-11 23:52:02 +0000119void CodeBlockSet::clearCurrentlyExecuting()
120{
mhahnenberg@apple.com3ddd7ac2014-01-10 02:28:27 +0000121 m_currentlyExecuting.clear();
mhahnenberg@apple.com3ddd7ac2014-01-10 02:28:27 +0000122}
123
fpizlo@apple.com229fa342014-05-08 02:35:22 +0000124void CodeBlockSet::dump(PrintStream& out) const
125{
126 CommaPrinter comma;
127 out.print("{old = [");
128 for (CodeBlock* codeBlock : m_oldCodeBlocks)
129 out.print(comma, pointerDump(codeBlock));
130 out.print("], new = [");
131 comma = CommaPrinter();
132 for (CodeBlock* codeBlock : m_newCodeBlocks)
133 out.print(comma, pointerDump(codeBlock));
134 out.print("], currentlyExecuting = [");
135 comma = CommaPrinter();
ggaren@apple.com81def5f2015-10-09 23:10:16 +0000136 for (CodeBlock* codeBlock : m_currentlyExecuting)
137 out.print(comma, pointerDump(codeBlock));
fpizlo@apple.com229fa342014-05-08 02:35:22 +0000138 out.print("]}");
139}
140
fpizlo@apple.com195d7b82013-09-03 16:39:29 +0000141} // namespace JSC
142