1/*
2 * Copyright (C) 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. 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 "ConservativeRoots.h"
28
29#include "CodeBlock.h"
30#include "CodeBlockSet.h"
31#include "CopiedSpace.h"
32#include "CopiedSpaceInlines.h"
33#include "HeapInlines.h"
34#include "JSCell.h"
35#include "JSObject.h"
36#include "JSCInlines.h"
37#include "Structure.h"
38#include <wtf/OSAllocator.h>
39
40namespace JSC {
41
42ConservativeRoots::ConservativeRoots(MarkedBlockSet* blocks, CopiedSpace* copiedSpace)
43 : m_roots(m_inlineRoots)
44 , m_size(0)
45 , m_capacity(inlineCapacity)
46 , m_blocks(blocks)
47 , m_copiedSpace(copiedSpace)
48{
49}
50
51ConservativeRoots::~ConservativeRoots()
52{
53 if (m_roots != m_inlineRoots)
54 OSAllocator::decommitAndRelease(m_roots, m_capacity * sizeof(JSCell*));
55}
56
57void ConservativeRoots::grow()
58{
59 size_t newCapacity = m_capacity == inlineCapacity ? nonInlineCapacity : m_capacity * 2;
60 JSCell** newRoots = static_cast<JSCell**>(OSAllocator::reserveAndCommit(newCapacity * sizeof(JSCell*)));
61 memcpy(newRoots, m_roots, m_size * sizeof(JSCell*));
62 if (m_roots != m_inlineRoots)
63 OSAllocator::decommitAndRelease(m_roots, m_capacity * sizeof(JSCell*));
64 m_capacity = newCapacity;
65 m_roots = newRoots;
66}
67
68template<typename MarkHook>
69inline void ConservativeRoots::genericAddPointer(void* p, TinyBloomFilter filter, MarkHook& markHook)
70{
71 markHook.mark(p);
72
73 m_copiedSpace->pinIfNecessary(p);
74
75 if (!Heap::isPointerGCObject(filter, *m_blocks, p))
76 return;
77
78 if (m_size == m_capacity)
79 grow();
80
81 m_roots[m_size++] = static_cast<JSCell*>(p);
82}
83
84template<typename MarkHook>
85SUPPRESS_ASAN
86void ConservativeRoots::genericAddSpan(void* begin, void* end, MarkHook& markHook)
87{
88 if (begin > end) {
89 void* swapTemp = begin;
90 begin = end;
91 end = swapTemp;
92 }
93
94 RELEASE_ASSERT(isPointerAligned(begin));
95 RELEASE_ASSERT(isPointerAligned(end));
96
97 TinyBloomFilter filter = m_blocks->filter(); // Make a local copy of filter to show the compiler it won't alias, and can be register-allocated.
98 for (char** it = static_cast<char**>(begin); it != static_cast<char**>(end); ++it)
99 genericAddPointer(*it, filter, markHook);
100}
101
102class DummyMarkHook {
103public:
104 void mark(void*) { }
105};
106
107void ConservativeRoots::add(void* begin, void* end)
108{
109 DummyMarkHook dummy;
110 genericAddSpan(begin, end, dummy);
111}
112
113void ConservativeRoots::add(void* begin, void* end, JITStubRoutineSet& jitStubRoutines)
114{
115 genericAddSpan(begin, end, jitStubRoutines);
116}
117
118class CompositeMarkHook {
119public:
120 CompositeMarkHook(JITStubRoutineSet& stubRoutines, CodeBlockSet& codeBlocks, const LockHolder& locker)
121 : m_stubRoutines(stubRoutines)
122 , m_codeBlocks(codeBlocks)
123 , m_codeBlocksLocker(locker)
124 {
125 }
126
127 void mark(void* address)
128 {
129 m_stubRoutines.mark(address);
130 m_codeBlocks.mark(m_codeBlocksLocker, address);
131 }
132
133private:
134 JITStubRoutineSet& m_stubRoutines;
135 CodeBlockSet& m_codeBlocks;
136 const LockHolder& m_codeBlocksLocker;
137};
138
139void ConservativeRoots::add(
140 void* begin, void* end, JITStubRoutineSet& jitStubRoutines, CodeBlockSet& codeBlocks)
141{
142 LockHolder locker(codeBlocks.getLock());
143 CompositeMarkHook markHook(jitStubRoutines, codeBlocks, locker);
144 genericAddSpan(begin, end, markHook);
145}
146
147} // namespace JSC
148