]> git.saurik.com Git - apple/javascriptcore.git/blob - heap/CopyWorkList.h
JavaScriptCore-1218.34.tar.gz
[apple/javascriptcore.git] / heap / CopyWorkList.h
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 #ifndef CopyWorkList_h
27 #define CopyWorkList_h
28
29 #include <wtf/Vector.h>
30
31 namespace JSC {
32
33 class JSCell;
34
35 class CopyWorkListSegment : public HeapBlock<CopyWorkListSegment> {
36 public:
37 static CopyWorkListSegment* create(DeadBlock* block)
38 {
39 return new (NotNull, block) CopyWorkListSegment(block->region());
40 }
41
42 size_t size() { return m_size; }
43 bool isFull() { return reinterpret_cast<char*>(&data()[size()]) >= endOfBlock(); }
44 JSCell* get(size_t index) { return data()[index]; }
45
46 void append(JSCell* cell)
47 {
48 ASSERT(!isFull());
49 data()[m_size] = cell;
50 m_size += 1;
51 }
52
53 static const size_t blockSize = 512;
54
55 private:
56 CopyWorkListSegment(Region* region)
57 : HeapBlock<CopyWorkListSegment>(region)
58 , m_size(0)
59 {
60 }
61
62 JSCell** data() { return reinterpret_cast<JSCell**>(this + 1); }
63 char* endOfBlock() { return reinterpret_cast<char*>(this) + blockSize; }
64
65 size_t m_size;
66 };
67
68 class CopyWorkListIterator {
69 friend class CopyWorkList;
70 public:
71 JSCell* get() { return m_currentSegment->get(m_currentIndex); }
72 JSCell* operator*() { return get(); }
73 JSCell* operator->() { return get(); }
74
75 CopyWorkListIterator& operator++()
76 {
77 m_currentIndex++;
78
79 if (m_currentIndex >= m_currentSegment->size()) {
80 m_currentIndex = 0;
81 m_currentSegment = m_currentSegment->next();
82
83 ASSERT(!m_currentSegment || m_currentSegment->size());
84 }
85
86 return *this;
87 }
88
89 bool operator==(const CopyWorkListIterator& other) const
90 {
91 return m_currentSegment == other.m_currentSegment && m_currentIndex == other.m_currentIndex;
92 }
93
94 bool operator!=(const CopyWorkListIterator& other) const
95 {
96 return !(*this == other);
97 }
98
99 CopyWorkListIterator()
100 : m_currentSegment(0)
101 , m_currentIndex(0)
102 {
103 }
104
105 private:
106 CopyWorkListIterator(CopyWorkListSegment* startSegment, size_t startIndex)
107 : m_currentSegment(startSegment)
108 , m_currentIndex(startIndex)
109 {
110 }
111
112 CopyWorkListSegment* m_currentSegment;
113 size_t m_currentIndex;
114 };
115
116 class CopyWorkList {
117 public:
118 typedef CopyWorkListIterator iterator;
119
120 CopyWorkList(BlockAllocator&);
121 ~CopyWorkList();
122
123 void append(JSCell*);
124 iterator begin();
125 iterator end();
126
127 private:
128 DoublyLinkedList<CopyWorkListSegment> m_segments;
129 BlockAllocator& m_blockAllocator;
130 };
131
132 inline CopyWorkList::CopyWorkList(BlockAllocator& blockAllocator)
133 : m_blockAllocator(blockAllocator)
134 {
135 }
136
137 inline CopyWorkList::~CopyWorkList()
138 {
139 while (!m_segments.isEmpty())
140 m_blockAllocator.deallocate(CopyWorkListSegment::destroy(m_segments.removeHead()));
141 }
142
143 inline void CopyWorkList::append(JSCell* cell)
144 {
145 if (m_segments.isEmpty() || m_segments.tail()->isFull())
146 m_segments.append(CopyWorkListSegment::create(m_blockAllocator.allocate<CopyWorkListSegment>()));
147
148 ASSERT(!m_segments.tail()->isFull());
149
150 m_segments.tail()->append(cell);
151 }
152
153 inline CopyWorkList::iterator CopyWorkList::begin()
154 {
155 return CopyWorkListIterator(m_segments.head(), 0);
156 }
157
158 inline CopyWorkList::iterator CopyWorkList::end()
159 {
160 return CopyWorkListIterator();
161 }
162
163 } // namespace JSC
164
165 #endif // CopyWorkList_h