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