1/*
2 * Copyright (C) 2012, 2016 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#include "config.h"
27#include "WeakSet.h"
28
29#include "Heap.h"
30#include "JSCInlines.h"
31#include "VM.h"
32
33namespace JSC {
34
35WeakSet::~WeakSet()
36{
37 if (isOnList())
38 remove();
39
40 Heap& heap = *this->heap();
41 WeakBlock* next = nullptr;
42 for (WeakBlock* block = m_blocks.head(); block; block = next) {
43 next = block->next();
44 WeakBlock::destroy(heap, block);
45 }
46 m_blocks.clear();
47}
48
49void WeakSet::sweep()
50{
51 for (WeakBlock* block = m_blocks.head(); block;) {
52 heap()->sweepNextLogicallyEmptyWeakBlock();
53
54 WeakBlock* nextBlock = block->next();
55 block->sweep();
56 if (block->isLogicallyEmptyButNotFree()) {
57 // If this WeakBlock is logically empty, but still has Weaks pointing into it,
58 // we can't destroy it just yet. Detach it from the WeakSet and hand ownership
59 // to the Heap so we don't pin down the entire MarkedBlock or PreciseAllocation.
60 m_blocks.remove(block);
61 heap()->addLogicallyEmptyWeakBlock(block);
62 block->disconnectContainer();
63 }
64 block = nextBlock;
65 }
66
67 resetAllocator();
68}
69
70void WeakSet::shrink()
71{
72 WeakBlock* next;
73 for (WeakBlock* block = m_blocks.head(); block; block = next) {
74 next = block->next();
75
76 if (block->isEmpty())
77 removeAllocator(block);
78 }
79
80 resetAllocator();
81
82 if (m_blocks.isEmpty() && isOnList())
83 remove();
84}
85
86WeakBlock::FreeCell* WeakSet::findAllocator(CellContainer container)
87{
88 if (WeakBlock::FreeCell* allocator = tryFindAllocator())
89 return allocator;
90
91 return addAllocator(container);
92}
93
94WeakBlock::FreeCell* WeakSet::tryFindAllocator()
95{
96 while (m_nextAllocator) {
97 WeakBlock* block = m_nextAllocator;
98 m_nextAllocator = m_nextAllocator->next();
99
100 WeakBlock::SweepResult sweepResult = block->takeSweepResult();
101 if (sweepResult.freeList)
102 return sweepResult.freeList;
103 }
104
105 return 0;
106}
107
108WeakBlock::FreeCell* WeakSet::addAllocator(CellContainer container)
109{
110 if (!isOnList())
111 heap()->objectSpace().addActiveWeakSet(this);
112
113 WeakBlock* block = WeakBlock::create(*heap(), container);
114 heap()->didAllocate(WeakBlock::blockSize);
115 m_blocks.append(block);
116 WeakBlock::SweepResult sweepResult = block->takeSweepResult();
117 ASSERT(!sweepResult.isNull() && sweepResult.freeList);
118 return sweepResult.freeList;
119}
120
121void WeakSet::removeAllocator(WeakBlock* block)
122{
123 m_blocks.remove(block);
124 WeakBlock::destroy(*heap(), block);
125}
126
127} // namespace JSC
128