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