OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
8 * | 8 * |
9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 void reallocateBuffer(size_t newCapacity) | 272 void reallocateBuffer(size_t newCapacity) |
273 { | 273 { |
274 ASSERT(shouldReallocateBuffer(newCapacity)); | 274 ASSERT(shouldReallocateBuffer(newCapacity)); |
275 // Using "unsigned" is not a limitation because Chromium's max mallo
c() is 2GB even on 64-bit. | 275 // Using "unsigned" is not a limitation because Chromium's max mallo
c() is 2GB even on 64-bit. |
276 RELEASE_ASSERT(newCapacity <= std::numeric_limits<unsigned>::max() /
sizeof(T)); | 276 RELEASE_ASSERT(newCapacity <= std::numeric_limits<unsigned>::max() /
sizeof(T)); |
277 size_t sizeToAllocate = fastMallocGoodSize(newCapacity * sizeof(T)); | 277 size_t sizeToAllocate = fastMallocGoodSize(newCapacity * sizeof(T)); |
278 m_capacity = sizeToAllocate / sizeof(T); | 278 m_capacity = sizeToAllocate / sizeof(T); |
279 m_buffer = static_cast<T*>(fastRealloc(m_buffer, sizeToAllocate)); | 279 m_buffer = static_cast<T*>(fastRealloc(m_buffer, sizeToAllocate)); |
280 } | 280 } |
281 | 281 |
282 void deallocateBuffer(T* bufferToDeallocate) | |
283 { | |
284 if (!bufferToDeallocate) | |
285 return; | |
286 | |
287 if (m_buffer == bufferToDeallocate) { | |
288 m_buffer = 0; | |
289 m_capacity = 0; | |
290 } | |
291 | |
292 fastFree(bufferToDeallocate); | |
293 } | |
294 | |
295 T* buffer() { return m_buffer; } | 282 T* buffer() { return m_buffer; } |
296 const T* buffer() const { return m_buffer; } | 283 const T* buffer() const { return m_buffer; } |
297 size_t capacity() const { return m_capacity; } | 284 size_t capacity() const { return m_capacity; } |
298 | 285 |
299 protected: | 286 protected: |
300 VectorBufferBase() | 287 VectorBufferBase() |
301 : m_buffer(0) | 288 : m_buffer(0) |
302 , m_capacity(0) | 289 , m_capacity(0) |
303 { | 290 { |
304 } | 291 } |
(...skipping 30 matching lines...) Expand all Loading... |
335 { | 322 { |
336 // Calling malloc(0) might take a lock and may actually do an | 323 // Calling malloc(0) might take a lock and may actually do an |
337 // allocation on some systems. | 324 // allocation on some systems. |
338 if (capacity) | 325 if (capacity) |
339 allocateBuffer(capacity); | 326 allocateBuffer(capacity); |
340 } | 327 } |
341 | 328 |
342 ~VectorBuffer() | 329 ~VectorBuffer() |
343 { | 330 { |
344 deallocateBuffer(buffer()); | 331 deallocateBuffer(buffer()); |
| 332 m_buffer = 0; |
| 333 } |
| 334 |
| 335 void deallocateBuffer(T* bufferToDeallocate) |
| 336 { |
| 337 if (LIKELY(!bufferToDeallocate)) |
| 338 return; |
| 339 |
| 340 fastFree(bufferToDeallocate); |
| 341 } |
| 342 |
| 343 void clearBufferPointer() |
| 344 { |
| 345 m_buffer = 0; |
| 346 m_capacity = 0; |
345 } | 347 } |
346 | 348 |
347 void swap(VectorBuffer<T, 0>& other) | 349 void swap(VectorBuffer<T, 0>& other) |
348 { | 350 { |
349 std::swap(m_buffer, other.m_buffer); | 351 std::swap(m_buffer, other.m_buffer); |
350 std::swap(m_capacity, other.m_capacity); | 352 std::swap(m_capacity, other.m_capacity); |
351 } | 353 } |
352 | 354 |
353 void restoreInlineBufferIfNeeded() { } | 355 void restoreInlineBufferIfNeeded() { } |
354 | 356 |
355 using Base::allocateBuffer; | 357 using Base::allocateBuffer; |
356 using Base::shouldReallocateBuffer; | 358 using Base::shouldReallocateBuffer; |
357 using Base::reallocateBuffer; | 359 using Base::reallocateBuffer; |
358 using Base::deallocateBuffer; | |
359 | 360 |
360 using Base::buffer; | 361 using Base::buffer; |
361 using Base::capacity; | 362 using Base::capacity; |
362 | 363 |
363 protected: | 364 protected: |
364 using Base::m_size; | 365 using Base::m_size; |
365 | 366 |
366 private: | 367 private: |
367 using Base::m_buffer; | 368 using Base::m_buffer; |
368 using Base::m_capacity; | 369 using Base::m_capacity; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 else { | 401 else { |
401 m_buffer = inlineBuffer(); | 402 m_buffer = inlineBuffer(); |
402 m_capacity = inlineCapacity; | 403 m_capacity = inlineCapacity; |
403 } | 404 } |
404 } | 405 } |
405 | 406 |
406 void deallocateBuffer(T* bufferToDeallocate) | 407 void deallocateBuffer(T* bufferToDeallocate) |
407 { | 408 { |
408 if (LIKELY(bufferToDeallocate == inlineBuffer())) | 409 if (LIKELY(bufferToDeallocate == inlineBuffer())) |
409 return; | 410 return; |
410 Base::deallocateBuffer(bufferToDeallocate); | 411 |
| 412 fastFree(bufferToDeallocate); |
| 413 } |
| 414 |
| 415 void clearBufferPointer() |
| 416 { |
| 417 m_buffer = 0; |
| 418 m_capacity = 0; |
411 } | 419 } |
412 | 420 |
413 bool shouldReallocateBuffer(size_t newCapacity) const | 421 bool shouldReallocateBuffer(size_t newCapacity) const |
414 { | 422 { |
415 // We cannot reallocate the inline buffer. | 423 // We cannot reallocate the inline buffer. |
416 return Base::shouldReallocateBuffer(newCapacity) && std::min(static_
cast<size_t>(m_capacity), newCapacity) > inlineCapacity; | 424 return Base::shouldReallocateBuffer(newCapacity) && std::min(static_
cast<size_t>(m_capacity), newCapacity) > inlineCapacity; |
417 } | 425 } |
418 | 426 |
419 void reallocateBuffer(size_t newCapacity) | 427 void reallocateBuffer(size_t newCapacity) |
420 { | 428 { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 | 498 |
491 explicit Vector(size_t size) | 499 explicit Vector(size_t size) |
492 : Base(size) | 500 : Base(size) |
493 { | 501 { |
494 m_size = size; | 502 m_size = size; |
495 TypeOperations::initialize(begin(), end()); | 503 TypeOperations::initialize(begin(), end()); |
496 } | 504 } |
497 | 505 |
498 ~Vector() | 506 ~Vector() |
499 { | 507 { |
500 if (m_size) | 508 if (UNLIKELY(m_size)) |
501 shrink(0); | 509 shrink(0); |
502 } | 510 } |
503 | 511 |
504 Vector(const Vector&); | 512 Vector(const Vector&); |
505 template<size_t otherCapacity> | 513 template<size_t otherCapacity> |
506 Vector(const Vector<T, otherCapacity>&); | 514 Vector(const Vector<T, otherCapacity>&); |
507 | 515 |
508 Vector& operator=(const Vector&); | 516 Vector& operator=(const Vector&); |
509 template<size_t otherCapacity> | 517 template<size_t otherCapacity> |
510 Vector& operator=(const Vector<T, otherCapacity>&); | 518 Vector& operator=(const Vector<T, otherCapacity>&); |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
867 if (newCapacity > 0) { | 875 if (newCapacity > 0) { |
868 if (Base::shouldReallocateBuffer(newCapacity)) { | 876 if (Base::shouldReallocateBuffer(newCapacity)) { |
869 Base::reallocateBuffer(newCapacity); | 877 Base::reallocateBuffer(newCapacity); |
870 return; | 878 return; |
871 } | 879 } |
872 | 880 |
873 T* oldEnd = end(); | 881 T* oldEnd = end(); |
874 Base::allocateBuffer(newCapacity); | 882 Base::allocateBuffer(newCapacity); |
875 if (begin() != oldBuffer) | 883 if (begin() != oldBuffer) |
876 TypeOperations::move(oldBuffer, oldEnd, begin()); | 884 TypeOperations::move(oldBuffer, oldEnd, begin()); |
| 885 } else { |
| 886 Base::clearBufferPointer(); |
877 } | 887 } |
878 | 888 |
879 Base::deallocateBuffer(oldBuffer); | 889 Base::deallocateBuffer(oldBuffer); |
880 Base::restoreInlineBufferIfNeeded(); | 890 Base::restoreInlineBufferIfNeeded(); |
881 } | 891 } |
882 | 892 |
883 // Templatizing these is better than just letting the conversion happen impl
icitly, | 893 // Templatizing these is better than just letting the conversion happen impl
icitly, |
884 // because for instance it allows a PassRefPtr to be appended to a RefPtr ve
ctor | 894 // because for instance it allows a PassRefPtr to be appended to a RefPtr ve
ctor |
885 // without refcount thrash. | 895 // without refcount thrash. |
886 | 896 |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1064 inline bool operator!=(const Vector<T, inlineCapacity>& a, const Vector<T, i
nlineCapacity>& b) | 1074 inline bool operator!=(const Vector<T, inlineCapacity>& a, const Vector<T, i
nlineCapacity>& b) |
1065 { | 1075 { |
1066 return !(a == b); | 1076 return !(a == b); |
1067 } | 1077 } |
1068 | 1078 |
1069 } // namespace WTF | 1079 } // namespace WTF |
1070 | 1080 |
1071 using WTF::Vector; | 1081 using WTF::Vector; |
1072 | 1082 |
1073 #endif // WTF_Vector_h | 1083 #endif // WTF_Vector_h |
OLD | NEW |