2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WeakBlock.h"
30 #include "HeapRootVisitor.h"
32 #include "ScopeChain.h"
33 #include "Structure.h"
37 WeakBlock
* WeakBlock::create()
39 PageAllocation allocation
= PageAllocation::allocate(blockSize
, OSAllocator::JSGCHeapPages
);
40 if (!static_cast<bool>(allocation
))
42 return new (NotNull
, allocation
.base()) WeakBlock(allocation
);
45 void WeakBlock::destroy(WeakBlock
* block
)
47 block
->m_allocation
.deallocate();
50 WeakBlock::WeakBlock(PageAllocation
& allocation
)
51 : m_allocation(allocation
)
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
);
59 ASSERT(!m_sweepResult
.isNull() && m_sweepResult
.blockIsFree
);
62 void WeakBlock::finalizeAll()
64 for (size_t i
= 0; i
< weakImplCount(); ++i
) {
65 WeakImpl
* weakImpl
= &weakImpls()[i
];
66 if (weakImpl
->state() >= WeakImpl::Finalized
)
68 weakImpl
->setState(WeakImpl::Dead
);
73 void WeakBlock::sweep()
75 if (!m_sweepResult
.isNull())
78 SweepResult sweepResult
;
79 for (size_t i
= 0; i
< weakImplCount(); ++i
) {
80 WeakImpl
* weakImpl
= &weakImpls()[i
];
81 if (weakImpl
->state() == WeakImpl::Dead
)
83 if (weakImpl
->state() == WeakImpl::Deallocated
)
84 addToFreeList(&sweepResult
.freeList
, weakImpl
);
86 sweepResult
.blockIsFree
= false;
89 m_sweepResult
= sweepResult
;
90 ASSERT(!m_sweepResult
.isNull());
93 void WeakBlock::visitLiveWeakImpls(HeapRootVisitor
& heapRootVisitor
)
95 // If a block is completely empty, a visit won't have any effect.
96 if (!m_sweepResult
.isNull() && m_sweepResult
.blockIsFree
)
99 SlotVisitor
& visitor
= heapRootVisitor
.visitor();
101 for (size_t i
= 0; i
< weakImplCount(); ++i
) {
102 WeakImpl
* weakImpl
= &weakImpls()[i
];
103 if (weakImpl
->state() != WeakImpl::Live
)
106 const JSValue
& jsValue
= weakImpl
->jsValue();
107 if (Heap::isMarked(jsValue
.asCell()))
110 WeakHandleOwner
* weakHandleOwner
= weakImpl
->weakHandleOwner();
111 if (!weakHandleOwner
)
114 if (!weakHandleOwner
->isReachableFromOpaqueRoots(Handle
<Unknown
>::wrapSlot(&const_cast<JSValue
&>(jsValue
)), weakImpl
->context(), visitor
))
117 heapRootVisitor
.visit(&const_cast<JSValue
&>(jsValue
));
121 void WeakBlock::visitDeadWeakImpls(HeapRootVisitor
&)
123 // If a block is completely empty, a visit won't have any effect.
124 if (!m_sweepResult
.isNull() && m_sweepResult
.blockIsFree
)
127 for (size_t i
= 0; i
< weakImplCount(); ++i
) {
128 WeakImpl
* weakImpl
= &weakImpls()[i
];
129 if (weakImpl
->state() > WeakImpl::Dead
)
132 if (Heap::isMarked(weakImpl
->jsValue().asCell())) {
133 ASSERT(weakImpl
->state() == WeakImpl::Live
);
137 weakImpl
->setState(WeakImpl::Dead
);