]>
git.saurik.com Git - apple/javascriptcore.git/blob - heap/WeakSet.cpp
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
36 for (WeakBlock
* block
= m_blocks
.head(); block
; block
= next
) {
38 WeakBlock::destroy(block
);
43 void WeakSet::finalizeAll()
45 for (WeakBlock
* block
= m_blocks
.head(); block
; block
= block
->next())
49 void WeakSet::visitLiveWeakImpls(HeapRootVisitor
& visitor
)
51 for (WeakBlock
* block
= m_blocks
.head(); block
; block
= block
->next())
52 block
->visitLiveWeakImpls(visitor
);
55 void WeakSet::visitDeadWeakImpls(HeapRootVisitor
& visitor
)
57 for (WeakBlock
* block
= m_blocks
.head(); block
; block
= block
->next())
58 block
->visitDeadWeakImpls(visitor
);
64 for (WeakBlock
* block
= m_blocks
.head(); block
; block
= next
) {
67 // If a block is completely empty, a new sweep won't have any effect.
68 if (!block
->sweepResult().isNull() && block
->sweepResult().blockIsFree
)
71 block
->takeSweepResult(); // Force a new sweep by discarding the last sweep.
76 void WeakSet::shrink()
79 for (WeakBlock
* block
= m_blocks
.head(); block
; block
= next
) {
82 if (!block
->sweepResult().isNull() && block
->sweepResult().blockIsFree
)
83 removeAllocator(block
);
87 void WeakSet::resetAllocator()
90 m_nextAllocator
= m_blocks
.head();
93 WeakBlock::FreeCell
* WeakSet::findAllocator()
95 if (WeakBlock::FreeCell
* allocator
= tryFindAllocator())
98 return addAllocator();
101 WeakBlock::FreeCell
* WeakSet::tryFindAllocator()
103 while (m_nextAllocator
) {
104 WeakBlock
* block
= m_nextAllocator
;
105 m_nextAllocator
= m_nextAllocator
->next();
108 WeakBlock::SweepResult sweepResult
= block
->takeSweepResult();
109 if (sweepResult
.freeList
)
110 return sweepResult
.freeList
;
116 WeakBlock::FreeCell
* WeakSet::addAllocator()
118 WeakBlock
* block
= WeakBlock::create();
119 m_heap
->didAllocate(WeakBlock::blockSize
);
120 m_blocks
.append(block
);
121 WeakBlock::SweepResult sweepResult
= block
->takeSweepResult();
122 ASSERT(!sweepResult
.isNull() && sweepResult
.freeList
);
123 return sweepResult
.freeList
;
126 void WeakSet::removeAllocator(WeakBlock
* block
)
128 m_blocks
.remove(block
);
129 WeakBlock::destroy(block
);