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

Unified Diff: Source/wtf/Vector.h

Issue 23531010: Ensure that Vector destruction always clears the buffer pointer. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/wtf/Deque.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/wtf/Vector.h
diff --git a/Source/wtf/Vector.h b/Source/wtf/Vector.h
index efaa47ce883978c2ccf5df4cefa240823a923223..6043295f62d04b4a6e4ea756f910397fec5084b1 100644
--- a/Source/wtf/Vector.h
+++ b/Source/wtf/Vector.h
@@ -276,31 +276,10 @@ static const size_t kInitialVectorSize = 16;
m_buffer = static_cast<T*>(fastRealloc(m_buffer, sizeToAllocate));
}
- void deallocateBuffer(T* bufferToDeallocate)
- {
- if (!bufferToDeallocate)
- return;
-
- if (m_buffer == bufferToDeallocate) {
- m_buffer = 0;
- m_capacity = 0;
- }
-
- fastFree(bufferToDeallocate);
- }
-
T* buffer() { return m_buffer; }
const T* buffer() const { return m_buffer; }
size_t capacity() const { return m_capacity; }
- T* releaseBuffer()
- {
- T* buffer = m_buffer;
- m_buffer = 0;
- m_capacity = 0;
- return buffer;
- }
-
protected:
VectorBufferBase()
: m_buffer(0)
@@ -316,7 +295,8 @@ static const size_t kInitialVectorSize = 16;
~VectorBufferBase()
{
- // FIXME: It would be nice to find a way to ASSERT that m_buffer hasn't leaked here.
+ m_buffer = 0;
+ m_size = 0;
}
T* m_buffer;
@@ -344,6 +324,17 @@ static const size_t kInitialVectorSize = 16;
allocateBuffer(capacity);
}
+ void deallocateBuffer(T* bufferToDeallocate)
+ {
+ fastFree(bufferToDeallocate);
+ }
+
+ void clearBufferPointer()
+ {
+ m_buffer = 0;
+ m_capacity = 0;
+ }
+
~VectorBuffer()
{
deallocateBuffer(buffer());
@@ -360,13 +351,10 @@ static const size_t kInitialVectorSize = 16;
using Base::allocateBuffer;
using Base::shouldReallocateBuffer;
using Base::reallocateBuffer;
- using Base::deallocateBuffer;
using Base::buffer;
using Base::capacity;
- using Base::releaseBuffer;
-
protected:
using Base::m_size;
@@ -393,6 +381,18 @@ static const size_t kInitialVectorSize = 16;
Base::allocateBuffer(capacity);
}
+ void deallocateBuffer(T* bufferToDeallocate)
+ {
+ if (UNLIKELY(bufferToDeallocate != inlineBuffer()))
+ fastFree(bufferToDeallocate);
+ }
+
+ void clearBufferPointer()
+ {
+ m_buffer = 0;
+ m_capacity = 0;
+ }
+
~VectorBuffer()
{
deallocateBuffer(buffer());
@@ -409,13 +409,6 @@ static const size_t kInitialVectorSize = 16;
}
}
- void deallocateBuffer(T* bufferToDeallocate)
- {
- if (bufferToDeallocate == inlineBuffer())
- return;
- Base::deallocateBuffer(bufferToDeallocate);
- }
-
bool shouldReallocateBuffer(size_t newCapacity) const
{
// We cannot reallocate the inline buffer.
@@ -460,13 +453,6 @@ static const size_t kInitialVectorSize = 16;
using Base::buffer;
using Base::capacity;
- T* releaseBuffer()
- {
- if (buffer() == inlineBuffer())
- return 0;
- return Base::releaseBuffer();
- }
-
protected:
using Base::m_size;
@@ -505,14 +491,12 @@ static const size_t kInitialVectorSize = 16;
: Base(size)
{
m_size = size;
- if (begin())
- TypeOperations::initialize(begin(), end());
+ TypeOperations::initialize(begin(), end());
}
~Vector()
{
- if (m_size)
- shrink(0);
+ shrink(0);
}
Vector(const Vector&);
@@ -605,8 +589,7 @@ static const size_t kInitialVectorSize = 16;
: Base(size)
{
m_size = size;
- if (begin())
- TypeOperations::uninitializedFill(begin(), end(), val);
+ TypeOperations::uninitializedFill(begin(), end(), val);
}
void fill(const T&, size_t);
@@ -614,8 +597,6 @@ static const size_t kInitialVectorSize = 16;
template<typename Iterator> void appendRange(Iterator start, Iterator end);
- T* releaseBuffer();
-
void swap(Vector<T, inlineCapacity>& other)
{
std::swap(m_size, other.m_size);
@@ -635,11 +616,9 @@ static const size_t kInitialVectorSize = 16;
using Base::capacity;
using Base::swap;
using Base::allocateBuffer;
- using Base::deallocateBuffer;
using Base::shouldReallocateBuffer;
using Base::reallocateBuffer;
using Base::restoreInlineBufferIfNeeded;
- using Base::releaseBuffer;
};
template<typename T, size_t inlineCapacity>
@@ -647,8 +626,7 @@ static const size_t kInitialVectorSize = 16;
: Base(other.capacity())
{
m_size = other.size();
- if (begin())
- TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
+ TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
}
template<typename T, size_t inlineCapacity>
@@ -657,14 +635,13 @@ static const size_t kInitialVectorSize = 16;
: Base(other.capacity())
{
m_size = other.size();
- if (begin())
- TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
+ TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
}
template<typename T, size_t inlineCapacity>
Vector<T, inlineCapacity>& Vector<T, inlineCapacity>::operator=(const Vector<T, inlineCapacity>& other)
{
- if (&other == this)
+ if (UNLIKELY(&other == this))
return *this;
if (size() > other.size())
@@ -829,8 +806,7 @@ static const size_t kInitialVectorSize = 16;
else {
if (size > capacity())
expandCapacity(size);
- if (begin())
- TypeOperations::initialize(end(), begin() + size);
+ TypeOperations::initialize(end(), begin() + size);
}
m_size = size;
@@ -850,21 +826,19 @@ static const size_t kInitialVectorSize = 16;
ASSERT(size >= m_size);
if (size > capacity())
expandCapacity(size);
- if (begin())
- TypeOperations::initialize(end(), begin() + size);
+ TypeOperations::initialize(end(), begin() + size);
m_size = size;
}
template<typename T, size_t inlineCapacity>
void Vector<T, inlineCapacity>::reserveCapacity(size_t newCapacity)
{
- if (newCapacity <= capacity())
+ if (UNLIKELY(newCapacity <= capacity()))
return;
T* oldBuffer = begin();
T* oldEnd = end();
Base::allocateBuffer(newCapacity);
- if (begin())
- TypeOperations::move(oldBuffer, oldEnd, begin());
+ TypeOperations::move(oldBuffer, oldEnd, begin());
Base::deallocateBuffer(oldBuffer);
}
@@ -897,6 +871,8 @@ static const size_t kInitialVectorSize = 16;
Base::allocateBuffer(newCapacity);
if (begin() != oldBuffer)
TypeOperations::move(oldBuffer, oldEnd, begin());
+ } else {
+ Base::clearBufferPointer();
}
Base::deallocateBuffer(oldBuffer);
@@ -1060,22 +1036,6 @@ static const size_t kInitialVectorSize = 16;
}
template<typename T, size_t inlineCapacity>
- inline T* Vector<T, inlineCapacity>::releaseBuffer()
- {
- T* buffer = Base::releaseBuffer();
- if (inlineCapacity && !buffer && m_size) {
- // If the vector had some data, but no buffer to release,
- // that means it was using the inline buffer. In that case,
- // we create a brand new buffer so the caller always gets one.
- size_t bytes = m_size * sizeof(T);
- buffer = static_cast<T*>(fastMalloc(bytes));
- memcpy(buffer, data(), bytes);
- }
- m_size = 0;
- return buffer;
- }
-
- template<typename T, size_t inlineCapacity>
void deleteAllValues(const Vector<T, inlineCapacity>& collection)
{
typedef typename Vector<T, inlineCapacity>::const_iterator iterator;
« no previous file with comments | « Source/wtf/Deque.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698