提交 12e411f8 编写于 作者: M Maksim Kita

PODArray swap fix

上级 289712f1
...@@ -568,7 +568,7 @@ public: ...@@ -568,7 +568,7 @@ public:
/// arr1 takes ownership of the heap memory of arr2. /// arr1 takes ownership of the heap memory of arr2.
arr1.c_start = arr2.c_start; arr1.c_start = arr2.c_start;
arr1.c_end_of_storage = arr1.c_start + heap_allocated - arr1.pad_right; arr1.c_end_of_storage = arr1.c_start + heap_allocated - arr2.pad_right - arr2.pad_left;
arr1.c_end = arr1.c_start + this->byte_size(heap_size); arr1.c_end = arr1.c_start + this->byte_size(heap_size);
/// Allocate stack space for arr2. /// Allocate stack space for arr2.
...@@ -585,7 +585,7 @@ public: ...@@ -585,7 +585,7 @@ public:
dest.dealloc(); dest.dealloc();
dest.alloc(src.allocated_bytes(), std::forward<TAllocatorParams>(allocator_params)...); dest.alloc(src.allocated_bytes(), std::forward<TAllocatorParams>(allocator_params)...);
memcpy(dest.c_start, src.c_start, this->byte_size(src.size())); memcpy(dest.c_start, src.c_start, this->byte_size(src.size()));
dest.c_end = dest.c_start + (src.c_end - src.c_start); dest.c_end = dest.c_start + this->byte_size(src.size());
src.c_start = Base::null; src.c_start = Base::null;
src.c_end = Base::null; src.c_end = Base::null;
...@@ -597,6 +597,11 @@ public: ...@@ -597,6 +597,11 @@ public:
std::swap(dest.c_end, src.c_end); std::swap(dest.c_end, src.c_end);
std::swap(dest.c_end_of_storage, src.c_end_of_storage); std::swap(dest.c_end_of_storage, src.c_end_of_storage);
} }
#ifndef NDEBUG
this->protect();
rhs.protect();
#endif
}; };
if (!this->isInitialized() && !rhs.isInitialized()) if (!this->isInitialized() && !rhs.isInitialized())
...@@ -639,8 +644,8 @@ public: ...@@ -639,8 +644,8 @@ public:
size_t rhs_size = rhs.size(); size_t rhs_size = rhs.size();
size_t rhs_allocated = rhs.allocated_bytes(); size_t rhs_allocated = rhs.allocated_bytes();
this->c_end_of_storage = this->c_start + rhs_allocated - Base::pad_right; this->c_end_of_storage = this->c_start + rhs_allocated - Base::pad_right - Base::pad_left;
rhs.c_end_of_storage = rhs.c_start + lhs_allocated - Base::pad_right; rhs.c_end_of_storage = rhs.c_start + lhs_allocated - Base::pad_right - Base::pad_left;
this->c_end = this->c_start + this->byte_size(rhs_size); this->c_end = this->c_start + this->byte_size(rhs_size);
rhs.c_end = rhs.c_start + this->byte_size(lhs_size); rhs.c_end = rhs.c_start + this->byte_size(lhs_size);
...@@ -659,6 +664,11 @@ public: ...@@ -659,6 +664,11 @@ public:
std::swap(this->c_end, rhs.c_end); std::swap(this->c_end, rhs.c_end);
std::swap(this->c_end_of_storage, rhs.c_end_of_storage); std::swap(this->c_end_of_storage, rhs.c_end_of_storage);
} }
#ifndef NDEBUG
this->protect();
rhs.protect();
#endif
} }
template <typename... TAllocatorParams> template <typename... TAllocatorParams>
...@@ -693,34 +703,34 @@ public: ...@@ -693,34 +703,34 @@ public:
} }
bool operator== (const PODArray & other) const bool operator== (const PODArray & rhs) const
{ {
if (this->size() != other.size()) if (this->size() != rhs.size())
return false; return false;
const_iterator this_it = begin(); const_iterator lhs_it = begin();
const_iterator that_it = other.begin(); const_iterator rhs_it = rhs.begin();
while (this_it != end()) while (lhs_it != end())
{ {
if (*this_it != *that_it) if (*lhs_it != *rhs_it)
return false; return false;
++this_it; ++lhs_it;
++that_it; ++rhs_it;
} }
return true; return true;
} }
bool operator!= (const PODArray & other) const bool operator!= (const PODArray & rhs) const
{ {
return !operator==(other); return !operator==(rhs);
} }
}; };
template <typename T, size_t initial_bytes, typename TAllocator, size_t pad_right_> template <typename T, size_t initial_bytes, typename TAllocator, size_t pad_right_, size_t pad_left_>
void swap(PODArray<T, initial_bytes, TAllocator, pad_right_> & lhs, PODArray<T, initial_bytes, TAllocator, pad_right_> & rhs) void swap(PODArray<T, initial_bytes, TAllocator, pad_right_, pad_left_> & lhs, PODArray<T, initial_bytes, TAllocator, pad_right_, pad_left_> & rhs)
{ {
lhs.swap(rhs); lhs.swap(rhs);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册