]> git.saurik.com Git - apple/javascriptcore.git/blob - heap/WeakSet.cpp
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / heap / WeakSet.cpp
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 #include "config.h"
27 #include "WeakSet.h"
28
29 #include "Heap.h"
30
31 namespace JSC {
32
33 WeakSet::~WeakSet()
34 {
35 WeakBlock* next = 0;
36 for (WeakBlock* block = m_blocks.head(); block; block = next) {
37 next = block->next();
38 WeakBlock::destroy(block);
39 }
40 m_blocks.clear();
41 }
42
43 void WeakSet::finalizeAll()
44 {
45 for (WeakBlock* block = m_blocks.head(); block; block = block->next())
46 block->finalizeAll();
47 }
48
49 void WeakSet::visitLiveWeakImpls(HeapRootVisitor& visitor)
50 {
51 for (WeakBlock* block = m_blocks.head(); block; block = block->next())
52 block->visitLiveWeakImpls(visitor);
53 }
54
55 void WeakSet::visitDeadWeakImpls(HeapRootVisitor& visitor)
56 {
57 for (WeakBlock* block = m_blocks.head(); block; block = block->next())
58 block->visitDeadWeakImpls(visitor);
59 }
60
61 void WeakSet::sweep()
62 {
63 WeakBlock* next;
64 for (WeakBlock* block = m_blocks.head(); block; block = next) {
65 next = block->next();
66
67 // If a block is completely empty, a new sweep won't have any effect.
68 if (!block->sweepResult().isNull() && block->sweepResult().blockIsFree)
69 continue;
70
71 block->takeSweepResult(); // Force a new sweep by discarding the last sweep.
72 block->sweep();
73 }
74 }
75
76 void WeakSet::shrink()
77 {
78 WeakBlock* next;
79 for (WeakBlock* block = m_blocks.head(); block; block = next) {
80 next = block->next();
81
82 if (!block->sweepResult().isNull() && block->sweepResult().blockIsFree)
83 removeAllocator(block);
84 }
85 }
86
87 void WeakSet::resetAllocator()
88 {
89 m_allocator = 0;
90 m_nextAllocator = m_blocks.head();
91 }
92
93 WeakBlock::FreeCell* WeakSet::findAllocator()
94 {
95 if (WeakBlock::FreeCell* allocator = tryFindAllocator())
96 return allocator;
97
98 return addAllocator();
99 }
100
101 WeakBlock::FreeCell* WeakSet::tryFindAllocator()
102 {
103 while (m_nextAllocator) {
104 WeakBlock* block = m_nextAllocator;
105 m_nextAllocator = m_nextAllocator->next();
106
107 block->sweep();
108 WeakBlock::SweepResult sweepResult = block->takeSweepResult();
109 if (sweepResult.freeList)
110 return sweepResult.freeList;
111 }
112
113 return 0;
114 }
115
116 WeakBlock::FreeCell* WeakSet::addAllocator()
117 {
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;
124 }
125
126 void WeakSet::removeAllocator(WeakBlock* block)
127 {
128 m_blocks.remove(block);
129 WeakBlock::destroy(block);
130 }
131
132 } // namespace JSC