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