1 | /* |
2 | * Copyright (C) 2012-2017 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 | #pragma once |
27 | |
28 | #include "CellContainer.h" |
29 | #include "WeakImpl.h" |
30 | #include <wtf/DoublyLinkedList.h> |
31 | #include <wtf/StdLibExtras.h> |
32 | |
33 | namespace JSC { |
34 | |
35 | class Heap; |
36 | class SlotVisitor; |
37 | |
38 | class WeakBlock : public DoublyLinkedListNode<WeakBlock> { |
39 | public: |
40 | friend class WTF::DoublyLinkedListNode<WeakBlock>; |
41 | static const size_t blockSize = 256; // 1/16 of MarkedBlock size |
42 | |
43 | struct FreeCell { |
44 | FreeCell* next; |
45 | }; |
46 | |
47 | struct SweepResult { |
48 | bool isNull() const; |
49 | |
50 | bool blockIsFree { true }; |
51 | bool blockIsLogicallyEmpty { true }; |
52 | FreeCell* freeList { nullptr }; |
53 | }; |
54 | |
55 | static WeakBlock* create(Heap&, CellContainer); |
56 | static void destroy(Heap&, WeakBlock*); |
57 | |
58 | static WeakImpl* asWeakImpl(FreeCell*); |
59 | |
60 | bool isEmpty(); |
61 | bool isLogicallyEmptyButNotFree() const; |
62 | |
63 | void sweep(); |
64 | SweepResult takeSweepResult(); |
65 | |
66 | void visit(SlotVisitor&); |
67 | |
68 | void reap(); |
69 | |
70 | void lastChanceToFinalize(); |
71 | void disconnectContainer() { m_container = CellContainer(); } |
72 | |
73 | private: |
74 | static FreeCell* asFreeCell(WeakImpl*); |
75 | |
76 | template<typename ContainerType> |
77 | void specializedVisit(ContainerType&, SlotVisitor&); |
78 | |
79 | explicit WeakBlock(CellContainer); |
80 | void finalize(WeakImpl*); |
81 | WeakImpl* weakImpls(); |
82 | size_t weakImplCount(); |
83 | void addToFreeList(FreeCell**, WeakImpl*); |
84 | |
85 | CellContainer m_container; |
86 | WeakBlock* m_prev; |
87 | WeakBlock* m_next; |
88 | SweepResult m_sweepResult; |
89 | }; |
90 | |
91 | inline bool WeakBlock::SweepResult::isNull() const |
92 | { |
93 | return blockIsFree && !freeList; // This state is impossible, so we can use it to mean null. |
94 | } |
95 | |
96 | inline WeakImpl* WeakBlock::asWeakImpl(FreeCell* freeCell) |
97 | { |
98 | return reinterpret_cast_ptr<WeakImpl*>(freeCell); |
99 | } |
100 | |
101 | inline WeakBlock::SweepResult WeakBlock::takeSweepResult() |
102 | { |
103 | SweepResult tmp; |
104 | std::swap(tmp, m_sweepResult); |
105 | ASSERT(m_sweepResult.isNull()); |
106 | return tmp; |
107 | } |
108 | |
109 | inline WeakBlock::FreeCell* WeakBlock::asFreeCell(WeakImpl* weakImpl) |
110 | { |
111 | return reinterpret_cast_ptr<FreeCell*>(weakImpl); |
112 | } |
113 | |
114 | inline WeakImpl* WeakBlock::weakImpls() |
115 | { |
116 | return reinterpret_cast_ptr<WeakImpl*>(this) + ((sizeof(WeakBlock) + sizeof(WeakImpl) - 1) / sizeof(WeakImpl)); |
117 | } |
118 | |
119 | inline size_t WeakBlock::weakImplCount() |
120 | { |
121 | return (blockSize / sizeof(WeakImpl)) - ((sizeof(WeakBlock) + sizeof(WeakImpl) - 1) / sizeof(WeakImpl)); |
122 | } |
123 | |
124 | inline void WeakBlock::addToFreeList(FreeCell** freeList, WeakImpl* weakImpl) |
125 | { |
126 | ASSERT(weakImpl->state() == WeakImpl::Deallocated); |
127 | FreeCell* freeCell = asFreeCell(weakImpl); |
128 | ASSERT(!*freeList || ((char*)*freeList > (char*)this && (char*)*freeList < (char*)this + blockSize)); |
129 | ASSERT((char*)freeCell > (char*)this && (char*)freeCell < (char*)this + blockSize); |
130 | freeCell->next = *freeList; |
131 | *freeList = freeCell; |
132 | } |
133 | |
134 | inline bool WeakBlock::isEmpty() |
135 | { |
136 | return !m_sweepResult.isNull() && m_sweepResult.blockIsFree; |
137 | } |
138 | |
139 | inline bool WeakBlock::isLogicallyEmptyButNotFree() const |
140 | { |
141 | return !m_sweepResult.isNull() && !m_sweepResult.blockIsFree && m_sweepResult.blockIsLogicallyEmpty; |
142 | } |
143 | |
144 | } // namespace JSC |
145 | |