]> git.saurik.com Git - apple/javascriptcore.git/blame - heap/WeakBlock.cpp
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / heap / WeakBlock.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 "WeakBlock.h"
28
29#include "Heap.h"
30#include "HeapRootVisitor.h"
31#include "JSObject.h"
32#include "ScopeChain.h"
33#include "Structure.h"
34
35namespace JSC {
36
37WeakBlock* WeakBlock::create()
38{
39 PageAllocation allocation = PageAllocation::allocate(blockSize, OSAllocator::JSGCHeapPages);
40 if (!static_cast<bool>(allocation))
41 CRASH();
42 return new (NotNull, allocation.base()) WeakBlock(allocation);
43}
44
45void WeakBlock::destroy(WeakBlock* block)
46{
47 block->m_allocation.deallocate();
48}
49
50WeakBlock::WeakBlock(PageAllocation& allocation)
51 : m_allocation(allocation)
52{
53 for (size_t i = 0; i < weakImplCount(); ++i) {
54 WeakImpl* weakImpl = &weakImpls()[i];
55 new (NotNull, weakImpl) WeakImpl;
56 addToFreeList(&m_sweepResult.freeList, weakImpl);
57 }
58
59 ASSERT(!m_sweepResult.isNull() && m_sweepResult.blockIsFree);
60}
61
62void WeakBlock::finalizeAll()
63{
64 for (size_t i = 0; i < weakImplCount(); ++i) {
65 WeakImpl* weakImpl = &weakImpls()[i];
66 if (weakImpl->state() >= WeakImpl::Finalized)
67 continue;
68 weakImpl->setState(WeakImpl::Dead);
69 finalize(weakImpl);
70 }
71}
72
73void WeakBlock::sweep()
74{
75 if (!m_sweepResult.isNull())
76 return;
77
78 SweepResult sweepResult;
79 for (size_t i = 0; i < weakImplCount(); ++i) {
80 WeakImpl* weakImpl = &weakImpls()[i];
81 if (weakImpl->state() == WeakImpl::Dead)
82 finalize(weakImpl);
83 if (weakImpl->state() == WeakImpl::Deallocated)
84 addToFreeList(&sweepResult.freeList, weakImpl);
85 else
86 sweepResult.blockIsFree = false;
87 }
88
89 m_sweepResult = sweepResult;
90 ASSERT(!m_sweepResult.isNull());
91}
92
93void WeakBlock::visitLiveWeakImpls(HeapRootVisitor& heapRootVisitor)
94{
95 // If a block is completely empty, a visit won't have any effect.
96 if (!m_sweepResult.isNull() && m_sweepResult.blockIsFree)
97 return;
98
99 SlotVisitor& visitor = heapRootVisitor.visitor();
100
101 for (size_t i = 0; i < weakImplCount(); ++i) {
102 WeakImpl* weakImpl = &weakImpls()[i];
103 if (weakImpl->state() != WeakImpl::Live)
104 continue;
105
106 const JSValue& jsValue = weakImpl->jsValue();
107 if (Heap::isMarked(jsValue.asCell()))
108 continue;
109
110 WeakHandleOwner* weakHandleOwner = weakImpl->weakHandleOwner();
111 if (!weakHandleOwner)
112 continue;
113
114 if (!weakHandleOwner->isReachableFromOpaqueRoots(Handle<Unknown>::wrapSlot(&const_cast<JSValue&>(jsValue)), weakImpl->context(), visitor))
115 continue;
116
117 heapRootVisitor.visit(&const_cast<JSValue&>(jsValue));
118 }
119}
120
121void WeakBlock::visitDeadWeakImpls(HeapRootVisitor&)
122{
123 // If a block is completely empty, a visit won't have any effect.
124 if (!m_sweepResult.isNull() && m_sweepResult.blockIsFree)
125 return;
126
127 for (size_t i = 0; i < weakImplCount(); ++i) {
128 WeakImpl* weakImpl = &weakImpls()[i];
129 if (weakImpl->state() > WeakImpl::Dead)
130 continue;
131
132 if (Heap::isMarked(weakImpl->jsValue().asCell())) {
133 ASSERT(weakImpl->state() == WeakImpl::Live);
134 continue;
135 }
136
137 weakImpl->setState(WeakImpl::Dead);
138 }
139}
140
141} // namespace JSC