fix embedded vector copy constructor and assignment.

Thanks to Tobias Kaes, an issue with default copy constructor and assignment operator is found and fixed.

BUG=http://code.google.com/p/v8/issues/detail?id=358

Review URL: http://codereview.chromium.org/113992


git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2077 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent b4a21977
......@@ -379,6 +379,9 @@ class Vector {
// Factory method for creating empty vectors.
static Vector<T> empty() { return Vector<T>(NULL, 0); }
protected:
void set_start(T* start) { start_ = start; }
private:
T* start_;
int length_;
......@@ -406,6 +409,22 @@ template <typename T, int kSize>
class EmbeddedVector : public Vector<T> {
public:
EmbeddedVector() : Vector<T>(buffer_, kSize) { }
// When copying, make underlying Vector to reference our buffer.
EmbeddedVector(const EmbeddedVector& rhs)
: Vector<T>(rhs) {
memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
set_start(buffer_);
}
EmbeddedVector& operator=(const EmbeddedVector& rhs) {
if (this == &rhs) return *this;
Vector<T>::operator=(rhs);
memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
set_start(buffer_);
return *this;
}
private:
T buffer_[kSize];
};
......
......@@ -177,3 +177,32 @@ TEST(SNPrintF) {
buffer.Dispose();
}
}
// Issue 358: When copying EmbeddedVector, Vector::start_ must point
// to the buffer in the copy, not in the source.
TEST(EmbeddedVectorCopy) {
EmbeddedVector<int, 1> src;
src[0] = 100;
EmbeddedVector<int, 1> dst = src;
CHECK_NE(src.start(), dst.start());
CHECK_EQ(src[0], dst[0]);
src[0] = 200;
CHECK_NE(src[0], dst[0]);
}
// Also Issue 358, assignment case.
TEST(EmbeddedVectorAssign) {
EmbeddedVector<int, 1> src;
src[0] = 100;
EmbeddedVector<int, 1> dst;
dst[0] = 200;
CHECK_NE(src.start(), dst.start());
CHECK_NE(src[0], dst[0]);
dst = src;
CHECK_NE(src.start(), dst.start());
CHECK_EQ(src[0], dst[0]);
src[0] = 200;
CHECK_NE(src[0], dst[0]);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment