From acebbf58eb9d09e8763b485623607bed62a733d2 Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Wed, 18 May 2016 17:26:23 -0700 Subject: [PATCH] Use only requested amount of aligned_storage --- src/ripple/protocol/impl/STVar.cpp | 45 +++++++++++++++++------------- src/ripple/protocol/impl/STVar.h | 17 +++++------ 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/ripple/protocol/impl/STVar.cpp b/src/ripple/protocol/impl/STVar.cpp index 7609a76fb..dbf27130d 100644 --- a/src/ripple/protocol/impl/STVar.cpp +++ b/src/ripple/protocol/impl/STVar.cpp @@ -45,11 +45,9 @@ STVar::~STVar() } STVar::STVar (STVar const& other) - : p_(nullptr) { if (other.p_ != nullptr) - p_ = other.p_->copy( - sizeof(d_), &d_); + p_ = other.p_->copy(max_size, &d_); } STVar::STVar (STVar&& other) @@ -61,37 +59,42 @@ STVar::STVar (STVar&& other) } else { - p_ = other.p_->move( - sizeof(d_), &d_); + p_ = other.p_->move(max_size, &d_); } } STVar& STVar::operator= (STVar const& rhs) { - destroy(); - p_ = nullptr; - if (rhs.p_) - p_ = rhs.p_->copy( - sizeof(d_), &d_); + if (&rhs != this) + { + destroy(); + if (rhs.p_) + p_ = rhs.p_->copy(max_size, &d_); + else + p_ = nullptr; + } + return *this; } STVar& STVar::operator= (STVar&& rhs) { - destroy(); - if (rhs.on_heap()) + if (&rhs != this) { - p_ = rhs.p_; - rhs.p_ = nullptr; - } - else - { - p_ = nullptr; - p_ = rhs.p_->move( - sizeof(d_), &d_); + destroy(); + if (rhs.on_heap()) + { + p_ = rhs.p_; + rhs.p_ = nullptr; + } + else + { + p_ = rhs.p_->move(max_size, &d_); + } } + return *this; } @@ -161,6 +164,8 @@ STVar::destroy() delete p_; else p_->~STBase(); + + p_ = nullptr; } } // detail diff --git a/src/ripple/protocol/impl/STVar.h b/src/ripple/protocol/impl/STVar.h index 8722f24d7..d8e221bee 100644 --- a/src/ripple/protocol/impl/STVar.h +++ b/src/ripple/protocol/impl/STVar.h @@ -42,7 +42,10 @@ extern nonPresentObject_t nonPresentObject; class STVar { private: - std::aligned_storage<72>::type d_; + // The largest "small object" we can accomodate + static std::size_t constexpr max_size = 72; + + std::aligned_storage::type d_; STBase* p_ = nullptr; public: @@ -54,12 +57,12 @@ public: STVar (STBase&& t) { - p_ = t.move(sizeof(d_), &d_); + p_ = t.move(max_size, &d_); } STVar (STBase const& t) { - p_ = t.copy(sizeof(d_), &d_); + p_ = t.copy(max_size, &d_); } STVar (defaultObject_t, SField const& name); @@ -89,12 +92,10 @@ private: void construct(Args&&... args) { - if(sizeof(T) > sizeof(d_)) - p_ = new T( - std::forward(args)...); + if(sizeof(T) > max_size) + p_ = new T(std::forward(args)...); else - p_ = new(&d_) T( - std::forward(args)...); + p_ = new(&d_) T(std::forward(args)...); } bool