Use only requested amount of aligned_storage

This commit is contained in:
Nik Bougalis
2016-05-18 17:26:23 -07:00
parent 84e3184106
commit acebbf58eb
2 changed files with 34 additions and 28 deletions

View File

@@ -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

View File

@@ -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<max_size>::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>(args)...);
if(sizeof(T) > max_size)
p_ = new T(std::forward<Args>(args)...);
else
p_ = new(&d_) T(
std::forward<Args>(args)...);
p_ = new(&d_) T(std::forward<Args>(args)...);
}
bool