]> git.saurik.com Git - apple/javascriptcore.git/blame - heap/CopyWorkList.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / heap / CopyWorkList.h
CommitLineData
93a37866 1/*
81345200 2 * Copyright (C) 2012, 2013 Apple Inc. All rights reserved.
93a37866
A
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
81345200 29#include "CopyToken.h"
ed1e77d3 30#include <wtf/DoublyLinkedList.h>
93a37866
A
31#include <wtf/Vector.h>
32
33namespace JSC {
34
35class JSCell;
36
81345200
A
37class CopyWorklistItem {
38public:
39 CopyWorklistItem()
40 : m_value(0)
41 {
42 }
43
44 CopyWorklistItem(JSCell* cell, CopyToken token)
45 : m_value(bitwise_cast<uintptr_t>(cell) | static_cast<uintptr_t>(token))
46 {
47 ASSERT(!(bitwise_cast<uintptr_t>(cell) & static_cast<uintptr_t>(mask)));
48 ASSERT(static_cast<uintptr_t>(token) <= mask);
49 }
50
51 JSCell* cell() const { return bitwise_cast<JSCell*>(m_value & ~static_cast<uintptr_t>(mask)); }
52 CopyToken token() const { return static_cast<CopyToken>(m_value & mask); }
53
54private:
55 static const unsigned requiredAlignment = 8;
56 static const unsigned mask = requiredAlignment - 1;
57
58 uintptr_t m_value;
59};
60
ed1e77d3
A
61class CopyWorkListSegment : public DoublyLinkedListNode<CopyWorkListSegment> {
62 friend class WTF::DoublyLinkedListNode<CopyWorkListSegment>;
93a37866 63public:
ed1e77d3 64 static CopyWorkListSegment* create()
93a37866 65 {
ed1e77d3
A
66 return new (NotNull, fastMalloc(blockSize)) CopyWorkListSegment();
67 }
68
69 static void destroy(CopyWorkListSegment* segment)
70 {
71 segment->~CopyWorkListSegment();
72 fastFree(segment);
93a37866
A
73 }
74
75 size_t size() { return m_size; }
76 bool isFull() { return reinterpret_cast<char*>(&data()[size()]) >= endOfBlock(); }
81345200 77 CopyWorklistItem get(size_t index) { return data()[index]; }
93a37866 78
81345200 79 void append(CopyWorklistItem item)
93a37866
A
80 {
81 ASSERT(!isFull());
81345200 82 data()[m_size] = item;
93a37866
A
83 m_size += 1;
84 }
85
86 static const size_t blockSize = 512;
87
88private:
ed1e77d3
A
89 CopyWorkListSegment()
90 : DoublyLinkedListNode<CopyWorkListSegment>()
93a37866
A
91 , m_size(0)
92 {
93 }
94
81345200 95 CopyWorklistItem* data() { return reinterpret_cast<CopyWorklistItem*>(this + 1); }
93a37866
A
96 char* endOfBlock() { return reinterpret_cast<char*>(this) + blockSize; }
97
ed1e77d3
A
98 CopyWorkListSegment* m_prev;
99 CopyWorkListSegment* m_next;
93a37866
A
100 size_t m_size;
101};
102
103class CopyWorkListIterator {
104 friend class CopyWorkList;
105public:
81345200
A
106 CopyWorklistItem get() { return m_currentSegment->get(m_currentIndex); }
107 CopyWorklistItem operator*() { return get(); }
108 CopyWorklistItem operator->() { return get(); }
93a37866
A
109
110 CopyWorkListIterator& operator++()
111 {
112 m_currentIndex++;
113
114 if (m_currentIndex >= m_currentSegment->size()) {
115 m_currentIndex = 0;
116 m_currentSegment = m_currentSegment->next();
117
118 ASSERT(!m_currentSegment || m_currentSegment->size());
119 }
120
121 return *this;
122 }
123
124 bool operator==(const CopyWorkListIterator& other) const
125 {
126 return m_currentSegment == other.m_currentSegment && m_currentIndex == other.m_currentIndex;
127 }
128
129 bool operator!=(const CopyWorkListIterator& other) const
130 {
131 return !(*this == other);
132 }
133
134 CopyWorkListIterator()
135 : m_currentSegment(0)
136 , m_currentIndex(0)
137 {
138 }
139
140private:
141 CopyWorkListIterator(CopyWorkListSegment* startSegment, size_t startIndex)
142 : m_currentSegment(startSegment)
143 , m_currentIndex(startIndex)
144 {
145 }
146
147 CopyWorkListSegment* m_currentSegment;
148 size_t m_currentIndex;
149};
150
151class CopyWorkList {
81345200 152 WTF_MAKE_FAST_ALLOCATED;
93a37866
A
153public:
154 typedef CopyWorkListIterator iterator;
155
ed1e77d3 156 CopyWorkList();
93a37866
A
157 ~CopyWorkList();
158
81345200 159 void append(CopyWorklistItem);
93a37866
A
160 iterator begin();
161 iterator end();
162
163private:
164 DoublyLinkedList<CopyWorkListSegment> m_segments;
93a37866
A
165};
166
ed1e77d3 167inline CopyWorkList::CopyWorkList()
93a37866
A
168{
169}
170
171inline CopyWorkList::~CopyWorkList()
172{
173 while (!m_segments.isEmpty())
ed1e77d3 174 CopyWorkListSegment::destroy(m_segments.removeHead());
93a37866
A
175}
176
81345200 177inline void CopyWorkList::append(CopyWorklistItem item)
93a37866
A
178{
179 if (m_segments.isEmpty() || m_segments.tail()->isFull())
ed1e77d3 180 m_segments.append(CopyWorkListSegment::create());
93a37866
A
181
182 ASSERT(!m_segments.tail()->isFull());
183
81345200 184 m_segments.tail()->append(item);
93a37866
A
185}
186
187inline CopyWorkList::iterator CopyWorkList::begin()
188{
189 return CopyWorkListIterator(m_segments.head(), 0);
190}
191
192inline CopyWorkList::iterator CopyWorkList::end()
193{
194 return CopyWorkListIterator();
195}
196
197} // namespace JSC
198
199#endif // CopyWorkList_h