pool: add clone and move assignment operators

This commit is contained in:
Danny Robson 2019-08-02 08:14:18 +10:00
parent 766e7d0370
commit 0bb092b92f

View File

@ -62,7 +62,35 @@ namespace cruft {
//---------------------------------------------------------------------
pool& operator= (pool&&);
pool& operator= (pool &&rhs)
{
std::swap (m_available, rhs.m_available);
std::swap (m_store, rhs.m_store);
std::swap (m_capacity, rhs.m_capacity);
return *this;
}
//---------------------------------------------------------------------
pool clone (void) const
{
pool res (m_capacity);
memcpy (res.m_store, m_store, sizeof (T) * m_capacity);
res.m_available = m_available.clone ();
// Rebase the pointers to available data so they point into the
// cloned object.
auto offset = reinterpret_cast<uintptr_t> (res.m_store) - reinterpret_cast<uintptr_t> (m_store);
auto data = res.m_available.store (
decltype(res.m_available)::contract::I_HAVE_LOCKED_THIS_STRUCTURE
);
for (auto &ptr: data)
ptr = reinterpret_cast<void*> (reinterpret_cast<uintptr_t> (ptr) + offset);
return res;
}
//---------------------------------------------------------------------