1/*
2 * Copyright (C) 2012-2019 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 "WeakBlock.h"
30#include <wtf/SentinelLinkedList.h>
31
32namespace JSC {
33
34class Heap;
35class WeakImpl;
36
37class WeakSet : public BasicRawSentinelNode<WeakSet> {
38 friend class LLIntOffsetsExtractor;
39
40public:
41 static WeakImpl* allocate(JSValue, WeakHandleOwner* = 0, void* context = 0);
42 static void deallocate(WeakImpl*);
43
44 WeakSet(VM&);
45 ~WeakSet();
46 void lastChanceToFinalize();
47
48 Heap* heap() const;
49 VM& vm() const;
50
51 bool isEmpty() const;
52 bool isTriviallyDestructible() const;
53
54 void visit(SlotVisitor&);
55
56 void reap();
57 void sweep();
58 void shrink();
59 void resetAllocator();
60
61private:
62 JS_EXPORT_PRIVATE WeakBlock::FreeCell* findAllocator(CellContainer);
63 WeakBlock::FreeCell* tryFindAllocator();
64 WeakBlock::FreeCell* addAllocator(CellContainer);
65 void removeAllocator(WeakBlock*);
66
67 WeakBlock::FreeCell* m_allocator { nullptr };
68 WeakBlock* m_nextAllocator { nullptr };
69 DoublyLinkedList<WeakBlock> m_blocks;
70 // m_vm must be a pointer (instead of a reference) because the JSCLLIntOffsetsExtractor
71 // cannot handle it being a reference.
72 VM* m_vm;
73};
74
75inline WeakSet::WeakSet(VM& vm)
76 : m_vm(&vm)
77{
78}
79
80inline VM& WeakSet::vm() const
81{
82 return *m_vm;
83}
84
85inline bool WeakSet::isEmpty() const
86{
87 for (WeakBlock* block = m_blocks.head(); block; block = block->next()) {
88 if (!block->isEmpty())
89 return false;
90 }
91
92 return true;
93}
94
95inline bool WeakSet::isTriviallyDestructible() const
96{
97 if (!m_blocks.isEmpty())
98 return false;
99 if (isOnList())
100 return false;
101 return true;
102}
103
104inline void WeakSet::deallocate(WeakImpl* weakImpl)
105{
106 weakImpl->setState(WeakImpl::Deallocated);
107}
108
109inline void WeakSet::lastChanceToFinalize()
110{
111 for (WeakBlock* block = m_blocks.head(); block; block = block->next())
112 block->lastChanceToFinalize();
113}
114
115inline void WeakSet::visit(SlotVisitor& visitor)
116{
117 for (WeakBlock* block = m_blocks.head(); block; block = block->next())
118 block->visit(visitor);
119}
120
121inline void WeakSet::reap()
122{
123 for (WeakBlock* block = m_blocks.head(); block; block = block->next())
124 block->reap();
125}
126
127inline void WeakSet::resetAllocator()
128{
129 m_allocator = nullptr;
130 m_nextAllocator = m_blocks.head();
131}
132
133} // namespace JSC
134