From a2e3c88dee7aa26dc6601e49b3987dbad53ffcf0 Mon Sep 17 00:00:00 2001 From: Alexey Arno Date: Fri, 8 Jul 2016 19:08:32 +0300 Subject: [PATCH] dbms: Server: Fixes. Fix for PODArray::swap(). [#METR-18844] --- dbms/include/DB/Common/PODArray.h | 71 +++++++++++-------------------- 1 file changed, 25 insertions(+), 46 deletions(-) diff --git a/dbms/include/DB/Common/PODArray.h b/dbms/include/DB/Common/PODArray.h index 54b48cbe535..dadd26b3475 100644 --- a/dbms/include/DB/Common/PODArray.h +++ b/dbms/include/DB/Common/PODArray.h @@ -166,54 +166,12 @@ public: PODArray(PODArray && other) { - if (other.isInitialized() && other.isAllocatedFromStack()) - { - alloc(other.allocated_size()); - memcpy(c_start, other.c_start, byte_size(other.size())); - c_end = c_start + (other.c_end - other.c_start); - } - else - { - c_start = other.c_start; - c_end = other.c_end; - c_end_of_storage = other.c_end_of_storage; - } - - other.c_start = nullptr; - other.c_end = nullptr; - other.c_end_of_storage = nullptr; + this->swap(other); } PODArray & operator=(PODArray && other) { - if (other.isInitialized() && other.isAllocatedFromStack()) - { - dealloc(); - alloc(other.allocated_size()); - memcpy(c_start, other.c_start, byte_size(other.size())); - c_end = c_start + (other.c_end - other.c_start); - - other.c_start = nullptr; - other.c_end = nullptr; - other.c_end_of_storage = nullptr; - } - else if (isInitialized() && isAllocatedFromStack()) - { - c_start = other.c_start; - c_end = other.c_end; - c_end_of_storage = other.c_end_of_storage; - - other.c_start = nullptr; - other.c_end = nullptr; - other.c_end_of_storage = nullptr; - } - else - { - std::swap(c_start, other.c_start); - std::swap(c_end, other.c_end); - std::swap(c_end_of_storage, other.c_end_of_storage); - } - + this->swap(other); return *this; } @@ -380,16 +338,37 @@ public: arr2.c_end = arr2.c_start + byte_size(stack_size); }; + auto do_move = [](PODArray & src, PODArray & dest) + { + if (src.isAllocatedFromStack()) + { + dest.dealloc(); + dest.alloc(src.allocated_size()); + memcpy(dest.c_start, src.c_start, byte_size(src.size())); + dest.c_end = dest.c_start + (src.c_end - src.c_start); + + src.c_start = nullptr; + src.c_end = nullptr; + src.c_end_of_storage = nullptr; + } + else + { + std::swap(dest.c_start, src.c_start); + std::swap(dest.c_end, src.c_end); + std::swap(dest.c_end_of_storage, src.c_end_of_storage); + } + }; + if (!isInitialized() && !rhs.isInitialized()) return; else if (!isInitialized() && rhs.isInitialized()) { - *this = std::move(rhs); + do_move(rhs, *this); return; } else if (isInitialized() && !rhs.isInitialized()) { - rhs = std::move(*this); + do_move(*this, rhs); return; }