Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: Source/wtf/Deque.h

Issue 228403002: Deque: Add HeapDeque and prevent buggy use of swap and operator= (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fix whitespace Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved. 3 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 20 matching lines...) Expand all
31 #define WTF_Deque_h 31 #define WTF_Deque_h
32 32
33 // FIXME: Could move what Vector and Deque share into a separate file. 33 // FIXME: Could move what Vector and Deque share into a separate file.
34 // Deque doesn't actually use Vector. 34 // Deque doesn't actually use Vector.
35 35
36 #include "wtf/PassTraits.h" 36 #include "wtf/PassTraits.h"
37 #include "wtf/Vector.h" 37 #include "wtf/Vector.h"
38 #include <iterator> 38 #include <iterator>
39 39
40 namespace WTF { 40 namespace WTF {
41 template<typename T, size_t inlineCapacity, typename Allocator> class DequeI teratorBase;
42 template<typename T, size_t inlineCapacity, typename Allocator> class DequeI terator;
43 template<typename T, size_t inlineCapacity, typename Allocator> class DequeC onstIterator;
41 44
42 template<typename T, size_t inlineCapacity> class DequeIteratorBase; 45 template<typename T, size_t inlineCapacity = 0, typename Allocator = Default Allocator>
43 template<typename T, size_t inlineCapacity> class DequeIterator; 46 class Deque : public VectorDestructorBase<Deque<T, inlineCapacity, Allocator >, T, (inlineCapacity > 0), Allocator::isGarbageCollected> {
44 template<typename T, size_t inlineCapacity> class DequeConstIterator;
45
46 template<typename T, size_t inlineCapacity = 0>
47 class Deque {
48 WTF_MAKE_FAST_ALLOCATED;
49 public: 47 public:
50 typedef DequeIterator<T, inlineCapacity> iterator; 48 typedef DequeIterator<T, inlineCapacity, Allocator> iterator;
51 typedef DequeConstIterator<T, inlineCapacity> const_iterator; 49 typedef DequeConstIterator<T, inlineCapacity, Allocator> const_iterator;
52 typedef std::reverse_iterator<iterator> reverse_iterator; 50 typedef std::reverse_iterator<iterator> reverse_iterator;
53 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 51 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
54 typedef PassTraits<T> Pass; 52 typedef PassTraits<T> Pass;
55 typedef typename PassTraits<T>::PassType PassType; 53 typedef typename PassTraits<T>::PassType PassType;
56 54
55 void* operator new(size_t size)
56 {
Mikhail 2014/04/08 12:11:30 Off topic: the same allocation operators are copy-
57 return Allocator::template malloc<void*, Deque>(size);
58 }
59 void operator delete(void* p) { Allocator::free(p); }
60 void* operator new[](size_t size) { return Allocator::template newArray< Vector>(size); }
61 void operator delete[](void* p) { Allocator::deleteArray(p); }
62 void* operator new(size_t, NotNullTag, void* location)
63 {
Mikhail 2014/04/08 12:11:30 is this compatible with GC? shouldn't COMPILE_ASSE
Erik Corry 2014/04/08 12:14:36 We do allow heap collections to be off heap, but t
64 ASSERT(location);
65 return location;
66 }
67
57 Deque(); 68 Deque();
58 Deque(const Deque<T, inlineCapacity>&); 69 Deque(const Deque<T, inlineCapacity, Allocator>&);
59 Deque& operator=(const Deque<T, inlineCapacity>&); 70 // FIXME: Doesn't work if there is an inline buffer, due to crbug.com/36 0572
60 ~Deque(); 71 Deque<T, 0, Allocator>& operator=(const Deque&);
61 72
62 void swap(Deque<T, inlineCapacity>&); 73 void finalize();
74 void finalizeGarbageCollectedObject() { finalize(); }
75
76 // We hard wire the inlineCapacity to zero here, due to crbug.com/360572
77 void swap(Deque<T, 0, Allocator>&);
63 78
64 size_t size() const { return m_start <= m_end ? m_end - m_start : m_end + m_buffer.capacity() - m_start; } 79 size_t size() const { return m_start <= m_end ? m_end - m_start : m_end + m_buffer.capacity() - m_start; }
65 bool isEmpty() const { return m_start == m_end; } 80 bool isEmpty() const { return m_start == m_end; }
66 81
67 iterator begin() { return iterator(this, m_start); } 82 iterator begin() { return iterator(this, m_start); }
68 iterator end() { return iterator(this, m_end); } 83 iterator end() { return iterator(this, m_end); }
69 const_iterator begin() const { return const_iterator(this, m_start); } 84 const_iterator begin() const { return const_iterator(this, m_start); }
70 const_iterator end() const { return const_iterator(this, m_end); } 85 const_iterator end() const { return const_iterator(this, m_end); }
71 reverse_iterator rbegin() { return reverse_iterator(end()); } 86 reverse_iterator rbegin() { return reverse_iterator(end()); }
72 reverse_iterator rend() { return reverse_iterator(begin()); } 87 reverse_iterator rend() { return reverse_iterator(begin()); }
(...skipping 13 matching lines...) Expand all
86 void removeFirst(); 101 void removeFirst();
87 void removeLast(); 102 void removeLast();
88 void remove(iterator&); 103 void remove(iterator&);
89 void remove(const_iterator&); 104 void remove(const_iterator&);
90 105
91 void clear(); 106 void clear();
92 107
93 template<typename Predicate> 108 template<typename Predicate>
94 iterator findIf(Predicate&); 109 iterator findIf(Predicate&);
95 110
111 void trace(typename Allocator::Visitor*);
112
96 private: 113 private:
97 friend class DequeIteratorBase<T, inlineCapacity>; 114 friend class DequeIteratorBase<T, inlineCapacity, Allocator>;
98 115
99 typedef VectorBuffer<T, inlineCapacity> Buffer; 116 typedef VectorBuffer<T, inlineCapacity, Allocator> Buffer;
100 typedef VectorTypeOperations<T> TypeOperations; 117 typedef VectorTypeOperations<T> TypeOperations;
101 typedef DequeIteratorBase<T, inlineCapacity> IteratorBase; 118 typedef DequeIteratorBase<T, inlineCapacity, Allocator> IteratorBase;
102 119
103 void remove(size_t position); 120 void remove(size_t position);
104 void destroyAll(); 121 void destroyAll();
105 void expandCapacityIfNeeded(); 122 void expandCapacityIfNeeded();
106 void expandCapacity(); 123 void expandCapacity();
107 124
108 Buffer m_buffer; 125 Buffer m_buffer;
109 unsigned m_start; 126 unsigned m_start;
110 unsigned m_end; 127 unsigned m_end;
111 }; 128 };
112 129
113 template<typename T, size_t inlineCapacity = 0> 130 template<typename T, size_t inlineCapacity, typename Allocator>
114 class DequeIteratorBase { 131 class DequeIteratorBase {
115 protected: 132 protected:
116 DequeIteratorBase(); 133 DequeIteratorBase();
117 DequeIteratorBase(const Deque<T, inlineCapacity>*, size_t); 134 DequeIteratorBase(const Deque<T, inlineCapacity, Allocator>*, size_t);
118 DequeIteratorBase(const DequeIteratorBase&); 135 DequeIteratorBase(const DequeIteratorBase&);
119 DequeIteratorBase& operator=(const DequeIteratorBase&); 136 DequeIteratorBase<T, 0, Allocator>& operator=(const DequeIteratorBase<T, 0, Allocator>&);
120 ~DequeIteratorBase(); 137 ~DequeIteratorBase();
121 138
122 void assign(const DequeIteratorBase& other) { *this = other; } 139 void assign(const DequeIteratorBase& other) { *this = other; }
123 140
124 void increment(); 141 void increment();
125 void decrement(); 142 void decrement();
126 143
127 T* before() const; 144 T* before() const;
128 T* after() const; 145 T* after() const;
129 146
130 bool isEqual(const DequeIteratorBase&) const; 147 bool isEqual(const DequeIteratorBase&) const;
131 148
132 private: 149 private:
133 Deque<T, inlineCapacity>* m_deque; 150 Deque<T, inlineCapacity, Allocator>* m_deque;
134 unsigned m_index; 151 unsigned m_index;
135 152
136 friend class Deque<T, inlineCapacity>; 153 friend class Deque<T, inlineCapacity, Allocator>;
137 }; 154 };
138 155
139 template<typename T, size_t inlineCapacity = 0> 156 template<typename T, size_t inlineCapacity = 0, typename Allocator = Default Allocator>
140 class DequeIterator : public DequeIteratorBase<T, inlineCapacity> { 157 class DequeIterator : public DequeIteratorBase<T, inlineCapacity, Allocator> {
141 private: 158 private:
142 typedef DequeIteratorBase<T, inlineCapacity> Base; 159 typedef DequeIteratorBase<T, inlineCapacity, Allocator> Base;
143 typedef DequeIterator<T, inlineCapacity> Iterator; 160 typedef DequeIterator<T, inlineCapacity, Allocator> Iterator;
144 161
145 public: 162 public:
146 typedef ptrdiff_t difference_type; 163 typedef ptrdiff_t difference_type;
147 typedef T value_type; 164 typedef T value_type;
148 typedef T* pointer; 165 typedef T* pointer;
149 typedef T& reference; 166 typedef T& reference;
150 typedef std::bidirectional_iterator_tag iterator_category; 167 typedef std::bidirectional_iterator_tag iterator_category;
151 168
152 DequeIterator(Deque<T, inlineCapacity>* deque, size_t index) : Base(dequ e, index) { } 169 DequeIterator(Deque<T, inlineCapacity, Allocator>* deque, size_t index) : Base(deque, index) { }
153 170
154 DequeIterator(const Iterator& other) : Base(other) { } 171 DequeIterator(const Iterator& other) : Base(other) { }
155 DequeIterator& operator=(const Iterator& other) { Base::assign(other); r eturn *this; } 172 DequeIterator& operator=(const Iterator& other) { Base::assign(other); r eturn *this; }
156 173
157 T& operator*() const { return *Base::after(); } 174 T& operator*() const { return *Base::after(); }
158 T* operator->() const { return Base::after(); } 175 T* operator->() const { return Base::after(); }
159 176
160 bool operator==(const Iterator& other) const { return Base::isEqual(othe r); } 177 bool operator==(const Iterator& other) const { return Base::isEqual(othe r); }
161 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth er); } 178 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth er); }
162 179
163 Iterator& operator++() { Base::increment(); return *this; } 180 Iterator& operator++() { Base::increment(); return *this; }
164 // postfix ++ intentionally omitted 181 // postfix ++ intentionally omitted
165 Iterator& operator--() { Base::decrement(); return *this; } 182 Iterator& operator--() { Base::decrement(); return *this; }
166 // postfix -- intentionally omitted 183 // postfix -- intentionally omitted
167 }; 184 };
168 185
169 template<typename T, size_t inlineCapacity = 0> 186 template<typename T, size_t inlineCapacity = 0, typename Allocator = Default Allocator>
170 class DequeConstIterator : public DequeIteratorBase<T, inlineCapacity> { 187 class DequeConstIterator : public DequeIteratorBase<T, inlineCapacity, Alloc ator> {
171 private: 188 private:
172 typedef DequeIteratorBase<T, inlineCapacity> Base; 189 typedef DequeIteratorBase<T, inlineCapacity, Allocator> Base;
173 typedef DequeConstIterator<T, inlineCapacity> Iterator; 190 typedef DequeConstIterator<T, inlineCapacity, Allocator> Iterator;
174 typedef DequeIterator<T, inlineCapacity> NonConstIterator; 191 typedef DequeIterator<T, inlineCapacity, Allocator> NonConstIterator;
175 192
176 public: 193 public:
177 typedef ptrdiff_t difference_type; 194 typedef ptrdiff_t difference_type;
178 typedef T value_type; 195 typedef T value_type;
179 typedef const T* pointer; 196 typedef const T* pointer;
180 typedef const T& reference; 197 typedef const T& reference;
181 typedef std::bidirectional_iterator_tag iterator_category; 198 typedef std::bidirectional_iterator_tag iterator_category;
182 199
183 DequeConstIterator(const Deque<T, inlineCapacity>* deque, size_t index) : Base(deque, index) { } 200 DequeConstIterator(const Deque<T, inlineCapacity, Allocator>* deque, siz e_t index) : Base(deque, index) { }
184 201
185 DequeConstIterator(const Iterator& other) : Base(other) { } 202 DequeConstIterator(const Iterator& other) : Base(other) { }
186 DequeConstIterator(const NonConstIterator& other) : Base(other) { } 203 DequeConstIterator(const NonConstIterator& other) : Base(other) { }
187 DequeConstIterator& operator=(const Iterator& other) { Base::assign(othe r); return *this; } 204 DequeConstIterator& operator=(const Iterator& other) { Base::assign(othe r); return *this; }
188 DequeConstIterator& operator=(const NonConstIterator& other) { Base::ass ign(other); return *this; } 205 DequeConstIterator& operator=(const NonConstIterator& other) { Base::ass ign(other); return *this; }
189 206
190 const T& operator*() const { return *Base::after(); } 207 const T& operator*() const { return *Base::after(); }
191 const T* operator->() const { return Base::after(); } 208 const T* operator->() const { return Base::after(); }
192 209
193 bool operator==(const Iterator& other) const { return Base::isEqual(othe r); } 210 bool operator==(const Iterator& other) const { return Base::isEqual(othe r); }
194 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth er); } 211 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth er); }
195 212
196 Iterator& operator++() { Base::increment(); return *this; } 213 Iterator& operator++() { Base::increment(); return *this; }
197 // postfix ++ intentionally omitted 214 // postfix ++ intentionally omitted
198 Iterator& operator--() { Base::decrement(); return *this; } 215 Iterator& operator--() { Base::decrement(); return *this; }
199 // postfix -- intentionally omitted 216 // postfix -- intentionally omitted
200 }; 217 };
201 218
202 template<typename T, size_t inlineCapacity> 219 template<typename T, size_t inlineCapacity, typename Allocator>
203 inline Deque<T, inlineCapacity>::Deque() 220 inline Deque<T, inlineCapacity, Allocator>::Deque()
204 : m_start(0) 221 : m_start(0)
205 , m_end(0) 222 , m_end(0)
206 { 223 {
207 } 224 }
208 225
209 template<typename T, size_t inlineCapacity> 226 template<typename T, size_t inlineCapacity, typename Allocator>
210 inline Deque<T, inlineCapacity>::Deque(const Deque<T, inlineCapacity>& other ) 227 inline Deque<T, inlineCapacity, Allocator>::Deque(const Deque<T, inlineCapac ity, Allocator>& other)
211 : m_buffer(other.m_buffer.capacity()) 228 : m_buffer(other.m_buffer.capacity())
212 , m_start(other.m_start) 229 , m_start(other.m_start)
213 , m_end(other.m_end) 230 , m_end(other.m_end)
214 { 231 {
215 const T* otherBuffer = other.m_buffer.buffer(); 232 const T* otherBuffer = other.m_buffer.buffer();
216 if (m_start <= m_end) 233 if (m_start <= m_end)
217 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_end, m_buffer.buffer() + m_start); 234 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_end, m_buffer.buffer() + m_start);
218 else { 235 else {
219 TypeOperations::uninitializedCopy(otherBuffer, otherBuffer + m_end, m_buffer.buffer()); 236 TypeOperations::uninitializedCopy(otherBuffer, otherBuffer + m_end, m_buffer.buffer());
220 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_buffer.capacity(), m_buffer.buffer() + m_start); 237 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_buffer.capacity(), m_buffer.buffer() + m_start);
221 } 238 }
222 } 239 }
223 240
224 template<typename T, size_t inlineCapacity> 241 template<typename T, size_t inlineCapacity, typename Allocator>
225 void deleteAllValues(const Deque<T, inlineCapacity>& collection) 242 void deleteAllValues(const Deque<T, inlineCapacity, Allocator>& collection)
226 { 243 {
227 typedef typename Deque<T, inlineCapacity>::const_iterator iterator; 244 typedef typename Deque<T, inlineCapacity, Allocator>::const_iterator ite rator;
228 iterator end = collection.end(); 245 iterator end = collection.end();
229 for (iterator it = collection.begin(); it != end; ++it) 246 for (iterator it = collection.begin(); it != end; ++it)
230 delete *it; 247 delete *it;
231 } 248 }
232 249
233 template<typename T, size_t inlineCapacity> 250 template<typename T, size_t inlineCapacity, typename Allocator>
234 inline Deque<T, inlineCapacity>& Deque<T, inlineCapacity>::operator=(const D eque<T, inlineCapacity>& other) 251 inline Deque<T, 0, Allocator>& Deque<T, inlineCapacity, Allocator>::operator =(const Deque& other)
235 { 252 {
236 // FIXME: This is inefficient if we're using an inline buffer and T is
237 // expensive to copy since it will copy the buffer twice instead of once .
238 Deque<T> copy(other); 253 Deque<T> copy(other);
239 swap(copy); 254 swap(copy);
240 return *this; 255 return *this;
241 } 256 }
242 257
243 template<typename T, size_t inlineCapacity> 258 template<typename T, size_t inlineCapacity, typename Allocator>
244 inline void Deque<T, inlineCapacity>::destroyAll() 259 inline void Deque<T, inlineCapacity, Allocator>::destroyAll()
245 { 260 {
246 if (m_start <= m_end) 261 if (m_start <= m_end)
247 TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffe r() + m_end); 262 TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffe r() + m_end);
248 else { 263 else {
249 TypeOperations::destruct(m_buffer.buffer(), m_buffer.buffer() + m_en d); 264 TypeOperations::destruct(m_buffer.buffer(), m_buffer.buffer() + m_en d);
250 TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffe r() + m_buffer.capacity()); 265 TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffe r() + m_buffer.capacity());
251 } 266 }
252 } 267 }
253 268
254 template<typename T, size_t inlineCapacity> 269 // Off-GC-heap deques: Destructor should be called.
255 inline Deque<T, inlineCapacity>::~Deque() 270 // On-GC-heap deques: Destructor should be called for inline buffers
271 // (if any) but destructor shouldn't be called for vector backing since
272 // it is managed by the traced GC heap.
273 template<typename T, size_t inlineCapacity, typename Allocator>
274 inline void Deque<T, inlineCapacity, Allocator>::finalize()
256 { 275 {
257 destroyAll(); 276 if (!inlineCapacity && !m_buffer.buffer())
277 return;
278 if (!isEmpty() && !(Allocator::isGarbageCollected && m_buffer.hasOutOfLi neBuffer()))
279 destroyAll();
280
258 m_buffer.destruct(); 281 m_buffer.destruct();
259 } 282 }
260 283
261 template<typename T, size_t inlineCapacity> 284 // FIXME: Doesn't work if there is an inline buffer, so for now we only
262 inline void Deque<T, inlineCapacity>::swap(Deque<T, inlineCapacity>& other) 285 // define the version without.
286 template<typename T, size_t inlineCapacity, typename Allocator>
287 inline void Deque<T, inlineCapacity, Allocator>::swap(Deque<T, 0, Allocator> & other)
263 { 288 {
264 std::swap(m_start, other.m_start); 289 std::swap(m_start, other.m_start);
265 std::swap(m_end, other.m_end); 290 std::swap(m_end, other.m_end);
266 m_buffer.swapVectorBuffer(other.m_buffer); 291 m_buffer.swapVectorBuffer(other.m_buffer);
267 } 292 }
268 293
269 template<typename T, size_t inlineCapacity> 294 template<typename T, size_t inlineCapacity, typename Allocator>
270 inline void Deque<T, inlineCapacity>::clear() 295 inline void Deque<T, inlineCapacity, Allocator>::clear()
271 { 296 {
272 destroyAll(); 297 destroyAll();
273 m_start = 0; 298 m_start = 0;
274 m_end = 0; 299 m_end = 0;
275 m_buffer.deallocateBuffer(m_buffer.buffer()); 300 m_buffer.deallocateBuffer(m_buffer.buffer());
276 m_buffer.resetBufferPointer(); 301 m_buffer.resetBufferPointer();
277 } 302 }
278 303
279 template<typename T, size_t inlineCapacity> 304 template<typename T, size_t inlineCapacity, typename Allocator>
280 template<typename Predicate> 305 template<typename Predicate>
281 inline DequeIterator<T, inlineCapacity> Deque<T, inlineCapacity>::findIf(Pre dicate& predicate) 306 inline DequeIterator<T, inlineCapacity, Allocator> Deque<T, inlineCapacity, Allocator>::findIf(Predicate& predicate)
282 { 307 {
283 iterator end_iterator = end(); 308 iterator end_iterator = end();
284 for (iterator it = begin(); it != end_iterator; ++it) { 309 for (iterator it = begin(); it != end_iterator; ++it) {
285 if (predicate(*it)) 310 if (predicate(*it))
286 return it; 311 return it;
287 } 312 }
288 return end_iterator; 313 return end_iterator;
289 } 314 }
290 315
291 template<typename T, size_t inlineCapacity> 316 template<typename T, size_t inlineCapacity, typename Allocator>
292 inline void Deque<T, inlineCapacity>::expandCapacityIfNeeded() 317 inline void Deque<T, inlineCapacity, Allocator>::expandCapacityIfNeeded()
293 { 318 {
294 if (m_start) { 319 if (m_start) {
295 if (m_end + 1 != m_start) 320 if (m_end + 1 != m_start)
296 return; 321 return;
297 } else if (m_end) { 322 } else if (m_end) {
298 if (m_end != m_buffer.capacity() - 1) 323 if (m_end != m_buffer.capacity() - 1)
299 return; 324 return;
300 } else if (m_buffer.capacity()) 325 } else if (m_buffer.capacity())
301 return; 326 return;
302 327
303 expandCapacity(); 328 expandCapacity();
304 } 329 }
305 330
306 template<typename T, size_t inlineCapacity> 331 template<typename T, size_t inlineCapacity, typename Allocator>
307 void Deque<T, inlineCapacity>::expandCapacity() 332 void Deque<T, inlineCapacity, Allocator>::expandCapacity()
308 { 333 {
309 size_t oldCapacity = m_buffer.capacity(); 334 size_t oldCapacity = m_buffer.capacity();
310 T* oldBuffer = m_buffer.buffer(); 335 T* oldBuffer = m_buffer.buffer();
311 m_buffer.allocateBuffer(std::max(static_cast<size_t>(16), oldCapacity + oldCapacity / 4 + 1)); 336 m_buffer.allocateBuffer(std::max(static_cast<size_t>(16), oldCapacity + oldCapacity / 4 + 1));
312 if (m_start <= m_end) 337 if (m_start <= m_end)
313 TypeOperations::move(oldBuffer + m_start, oldBuffer + m_end, m_buffe r.buffer() + m_start); 338 TypeOperations::move(oldBuffer + m_start, oldBuffer + m_end, m_buffe r.buffer() + m_start);
314 else { 339 else {
315 TypeOperations::move(oldBuffer, oldBuffer + m_end, m_buffer.buffer() ); 340 TypeOperations::move(oldBuffer, oldBuffer + m_end, m_buffer.buffer() );
316 size_t newStart = m_buffer.capacity() - (oldCapacity - m_start); 341 size_t newStart = m_buffer.capacity() - (oldCapacity - m_start);
317 TypeOperations::move(oldBuffer + m_start, oldBuffer + oldCapacity, m _buffer.buffer() + newStart); 342 TypeOperations::move(oldBuffer + m_start, oldBuffer + oldCapacity, m _buffer.buffer() + newStart);
318 m_start = newStart; 343 m_start = newStart;
319 } 344 }
320 m_buffer.deallocateBuffer(oldBuffer); 345 m_buffer.deallocateBuffer(oldBuffer);
321 } 346 }
322 347
323 template<typename T, size_t inlineCapacity> 348 template<typename T, size_t inlineCapacity, typename Allocator>
324 inline typename Deque<T, inlineCapacity>::PassType Deque<T, inlineCapacity>: :takeFirst() 349 inline typename Deque<T, inlineCapacity, Allocator>::PassType Deque<T, inlin eCapacity, Allocator>::takeFirst()
325 { 350 {
326 T oldFirst = Pass::transfer(first()); 351 T oldFirst = Pass::transfer(first());
327 removeFirst(); 352 removeFirst();
328 return Pass::transfer(oldFirst); 353 return Pass::transfer(oldFirst);
329 } 354 }
330 355
331 template<typename T, size_t inlineCapacity> 356 template<typename T, size_t inlineCapacity, typename Allocator>
332 inline typename Deque<T, inlineCapacity>::PassType Deque<T, inlineCapacity>: :takeLast() 357 inline typename Deque<T, inlineCapacity, Allocator>::PassType Deque<T, inlin eCapacity, Allocator>::takeLast()
333 { 358 {
334 T oldLast = Pass::transfer(last()); 359 T oldLast = Pass::transfer(last());
335 removeLast(); 360 removeLast();
336 return Pass::transfer(oldLast); 361 return Pass::transfer(oldLast);
337 } 362 }
338 363
339 template<typename T, size_t inlineCapacity> template<typename U> 364 template<typename T, size_t inlineCapacity, typename Allocator> template<typ ename U>
340 inline void Deque<T, inlineCapacity>::append(const U& value) 365 inline void Deque<T, inlineCapacity, Allocator>::append(const U& value)
341 { 366 {
342 expandCapacityIfNeeded(); 367 expandCapacityIfNeeded();
343 new (NotNull, &m_buffer.buffer()[m_end]) T(value); 368 new (NotNull, &m_buffer.buffer()[m_end]) T(value);
344 if (m_end == m_buffer.capacity() - 1) 369 if (m_end == m_buffer.capacity() - 1)
345 m_end = 0; 370 m_end = 0;
346 else 371 else
347 ++m_end; 372 ++m_end;
348 } 373 }
349 374
350 template<typename T, size_t inlineCapacity> template<typename U> 375 template<typename T, size_t inlineCapacity, typename Allocator> template<typ ename U>
351 inline void Deque<T, inlineCapacity>::prepend(const U& value) 376 inline void Deque<T, inlineCapacity, Allocator>::prepend(const U& value)
352 { 377 {
353 expandCapacityIfNeeded(); 378 expandCapacityIfNeeded();
354 if (!m_start) 379 if (!m_start)
355 m_start = m_buffer.capacity() - 1; 380 m_start = m_buffer.capacity() - 1;
356 else 381 else
357 --m_start; 382 --m_start;
358 new (NotNull, &m_buffer.buffer()[m_start]) T(value); 383 new (NotNull, &m_buffer.buffer()[m_start]) T(value);
359 } 384 }
360 385
361 template<typename T, size_t inlineCapacity> 386 template<typename T, size_t inlineCapacity, typename Allocator>
362 inline void Deque<T, inlineCapacity>::removeFirst() 387 inline void Deque<T, inlineCapacity, Allocator>::removeFirst()
363 { 388 {
364 ASSERT(!isEmpty()); 389 ASSERT(!isEmpty());
365 TypeOperations::destruct(&m_buffer.buffer()[m_start], &m_buffer.buffer() [m_start + 1]); 390 TypeOperations::destruct(&m_buffer.buffer()[m_start], &m_buffer.buffer() [m_start + 1]);
366 if (m_start == m_buffer.capacity() - 1) 391 if (m_start == m_buffer.capacity() - 1)
367 m_start = 0; 392 m_start = 0;
368 else 393 else
369 ++m_start; 394 ++m_start;
370 } 395 }
371 396
372 template<typename T, size_t inlineCapacity> 397 template<typename T, size_t inlineCapacity, typename Allocator>
373 inline void Deque<T, inlineCapacity>::removeLast() 398 inline void Deque<T, inlineCapacity, Allocator>::removeLast()
374 { 399 {
375 ASSERT(!isEmpty()); 400 ASSERT(!isEmpty());
376 if (!m_end) 401 if (!m_end)
377 m_end = m_buffer.capacity() - 1; 402 m_end = m_buffer.capacity() - 1;
378 else 403 else
379 --m_end; 404 --m_end;
380 TypeOperations::destruct(&m_buffer.buffer()[m_end], &m_buffer.buffer()[m _end + 1]); 405 TypeOperations::destruct(&m_buffer.buffer()[m_end], &m_buffer.buffer()[m _end + 1]);
381 } 406 }
382 407
383 template<typename T, size_t inlineCapacity> 408 template<typename T, size_t inlineCapacity, typename Allocator>
384 inline void Deque<T, inlineCapacity>::remove(iterator& it) 409 inline void Deque<T, inlineCapacity, Allocator>::remove(iterator& it)
385 { 410 {
386 remove(it.m_index); 411 remove(it.m_index);
387 } 412 }
388 413
389 template<typename T, size_t inlineCapacity> 414 template<typename T, size_t inlineCapacity, typename Allocator>
390 inline void Deque<T, inlineCapacity>::remove(const_iterator& it) 415 inline void Deque<T, inlineCapacity, Allocator>::remove(const_iterator& it)
391 { 416 {
392 remove(it.m_index); 417 remove(it.m_index);
393 } 418 }
394 419
395 template<typename T, size_t inlineCapacity> 420 template<typename T, size_t inlineCapacity, typename Allocator>
396 inline void Deque<T, inlineCapacity>::remove(size_t position) 421 inline void Deque<T, inlineCapacity, Allocator>::remove(size_t position)
397 { 422 {
398 if (position == m_end) 423 if (position == m_end)
399 return; 424 return;
400 425
401 T* buffer = m_buffer.buffer(); 426 T* buffer = m_buffer.buffer();
402 TypeOperations::destruct(&buffer[position], &buffer[position + 1]); 427 TypeOperations::destruct(&buffer[position], &buffer[position + 1]);
403 428
404 // Find which segment of the circular buffer contained the remove elemen t, and only move elements in that part. 429 // Find which segment of the circular buffer contained the remove elemen t, and only move elements in that part.
405 if (position >= m_start) { 430 if (position >= m_start) {
406 TypeOperations::moveOverlapping(buffer + m_start, buffer + position, buffer + m_start + 1); 431 TypeOperations::moveOverlapping(buffer + m_start, buffer + position, buffer + m_start + 1);
407 m_start = (m_start + 1) % m_buffer.capacity(); 432 m_start = (m_start + 1) % m_buffer.capacity();
408 } else { 433 } else {
409 TypeOperations::moveOverlapping(buffer + position + 1, buffer + m_en d, buffer + position); 434 TypeOperations::moveOverlapping(buffer + position + 1, buffer + m_en d, buffer + position);
410 m_end = (m_end - 1 + m_buffer.capacity()) % m_buffer.capacity(); 435 m_end = (m_end - 1 + m_buffer.capacity()) % m_buffer.capacity();
411 } 436 }
412 } 437 }
413 438
414 template<typename T, size_t inlineCapacity> 439 template<typename T, size_t inlineCapacity, typename Allocator>
415 inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase() 440 inline DequeIteratorBase<T, inlineCapacity, Allocator>::DequeIteratorBase()
416 : m_deque(0) 441 : m_deque(0)
417 { 442 {
418 } 443 }
419 444
420 template<typename T, size_t inlineCapacity> 445 template<typename T, size_t inlineCapacity, typename Allocator>
421 inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase(const Deque<T , inlineCapacity>* deque, size_t index) 446 inline DequeIteratorBase<T, inlineCapacity, Allocator>::DequeIteratorBase(co nst Deque<T, inlineCapacity, Allocator>* deque, size_t index)
422 : m_deque(const_cast<Deque<T, inlineCapacity>*>(deque)) 447 : m_deque(const_cast<Deque<T, inlineCapacity, Allocator>*>(deque))
423 , m_index(index) 448 , m_index(index)
424 { 449 {
425 } 450 }
426 451
427 template<typename T, size_t inlineCapacity> 452 template<typename T, size_t inlineCapacity, typename Allocator>
428 inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase(const DequeIt eratorBase& other) 453 inline DequeIteratorBase<T, inlineCapacity, Allocator>::DequeIteratorBase(co nst DequeIteratorBase& other)
429 : m_deque(other.m_deque) 454 : m_deque(other.m_deque)
430 , m_index(other.m_index) 455 , m_index(other.m_index)
431 { 456 {
432 } 457 }
433 458
434 template<typename T, size_t inlineCapacity> 459 template<typename T, size_t inlineCapacity, typename Allocator>
435 inline DequeIteratorBase<T, inlineCapacity>& DequeIteratorBase<T, inlineCapa city>::operator=(const DequeIteratorBase& other) 460 inline DequeIteratorBase<T, 0, Allocator>& DequeIteratorBase<T, inlineCapaci ty, Allocator>::operator=(const DequeIteratorBase<T, 0, Allocator>& other)
436 { 461 {
437 m_deque = other.m_deque; 462 m_deque = other.m_deque;
438 m_index = other.m_index; 463 m_index = other.m_index;
439 return *this; 464 return *this;
440 } 465 }
441 466
442 template<typename T, size_t inlineCapacity> 467 template<typename T, size_t inlineCapacity, typename Allocator>
443 inline DequeIteratorBase<T, inlineCapacity>::~DequeIteratorBase() 468 inline DequeIteratorBase<T, inlineCapacity, Allocator>::~DequeIteratorBase()
444 { 469 {
445 } 470 }
446 471
447 template<typename T, size_t inlineCapacity> 472 template<typename T, size_t inlineCapacity, typename Allocator>
448 inline bool DequeIteratorBase<T, inlineCapacity>::isEqual(const DequeIterato rBase& other) const 473 inline bool DequeIteratorBase<T, inlineCapacity, Allocator>::isEqual(const D equeIteratorBase& other) const
449 { 474 {
450 return m_index == other.m_index; 475 return m_index == other.m_index;
451 } 476 }
452 477
453 template<typename T, size_t inlineCapacity> 478 template<typename T, size_t inlineCapacity, typename Allocator>
454 inline void DequeIteratorBase<T, inlineCapacity>::increment() 479 inline void DequeIteratorBase<T, inlineCapacity, Allocator>::increment()
455 { 480 {
456 ASSERT(m_index != m_deque->m_end); 481 ASSERT(m_index != m_deque->m_end);
457 ASSERT(m_deque->m_buffer.capacity()); 482 ASSERT(m_deque->m_buffer.capacity());
458 if (m_index == m_deque->m_buffer.capacity() - 1) 483 if (m_index == m_deque->m_buffer.capacity() - 1)
459 m_index = 0; 484 m_index = 0;
460 else 485 else
461 ++m_index; 486 ++m_index;
462 } 487 }
463 488
464 template<typename T, size_t inlineCapacity> 489 template<typename T, size_t inlineCapacity, typename Allocator>
465 inline void DequeIteratorBase<T, inlineCapacity>::decrement() 490 inline void DequeIteratorBase<T, inlineCapacity, Allocator>::decrement()
466 { 491 {
467 ASSERT(m_index != m_deque->m_start); 492 ASSERT(m_index != m_deque->m_start);
468 ASSERT(m_deque->m_buffer.capacity()); 493 ASSERT(m_deque->m_buffer.capacity());
469 if (!m_index) 494 if (!m_index)
470 m_index = m_deque->m_buffer.capacity() - 1; 495 m_index = m_deque->m_buffer.capacity() - 1;
471 else 496 else
472 --m_index; 497 --m_index;
473 } 498 }
474 499
475 template<typename T, size_t inlineCapacity> 500 template<typename T, size_t inlineCapacity, typename Allocator>
476 inline T* DequeIteratorBase<T, inlineCapacity>::after() const 501 inline T* DequeIteratorBase<T, inlineCapacity, Allocator>::after() const
477 { 502 {
478 ASSERT(m_index != m_deque->m_end); 503 ASSERT(m_index != m_deque->m_end);
479 return &m_deque->m_buffer.buffer()[m_index]; 504 return &m_deque->m_buffer.buffer()[m_index];
480 } 505 }
481 506
482 template<typename T, size_t inlineCapacity> 507 template<typename T, size_t inlineCapacity, typename Allocator>
483 inline T* DequeIteratorBase<T, inlineCapacity>::before() const 508 inline T* DequeIteratorBase<T, inlineCapacity, Allocator>::before() const
484 { 509 {
485 ASSERT(m_index != m_deque->m_start); 510 ASSERT(m_index != m_deque->m_start);
486 if (!m_index) 511 if (!m_index)
487 return &m_deque->m_buffer.buffer()[m_deque->m_buffer.capacity() - 1] ; 512 return &m_deque->m_buffer.buffer()[m_deque->m_buffer.capacity() - 1] ;
488 return &m_deque->m_buffer.buffer()[m_index - 1]; 513 return &m_deque->m_buffer.buffer()[m_index - 1];
489 } 514 }
490 515
516 // This is only called if the allocator is a HeapAllocator. It is used when
517 // visiting during a tracing GC.
518 template<typename T, size_t inlineCapacity, typename Allocator>
519 void Deque<T, inlineCapacity, Allocator>::trace(typename Allocator::Visitor* visitor)
520 {
521 COMPILE_ASSERT(Allocator::isGarbageCollected, Garbage_collector_must_be_ enabled);
522 const T* bufferBegin = m_buffer.buffer();
523 const T* end = bufferBegin + m_end;
524 if (ShouldBeTraced<VectorTraits<T> >::value) {
525 if (m_start <= m_end) {
526 for (const T* bufferEntry = bufferBegin + m_start; bufferEntry ! = end; bufferEntry++)
527 Allocator::template trace<T, VectorTraits<T> >(visitor, *con st_cast<T*>(bufferEntry));
528 } else {
529 for (const T* bufferEntry = bufferBegin; bufferEntry != end; buf ferEntry++)
530 Allocator::template trace<T, VectorTraits<T> >(visitor, *con st_cast<T*>(bufferEntry));
531 const T* bufferEnd = m_buffer.buffer() + m_buffer.capacity();
532 for (const T* bufferEntry = bufferBegin + m_start; bufferEntry ! = bufferEnd; bufferEntry++)
533 Allocator::template trace<T, VectorTraits<T> >(visitor, *con st_cast<T*>(bufferEntry));
534 }
535 }
536 if (m_buffer.hasOutOfLineBuffer())
537 Allocator::markNoTracing(visitor, m_buffer.buffer());
538 }
539
491 } // namespace WTF 540 } // namespace WTF
492 541
493 using WTF::Deque; 542 using WTF::Deque;
494 543
495 #endif // WTF_Deque_h 544 #endif // WTF_Deque_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698