diff --git a/src/util/buffer.h b/src/util/buffer.h index e1721f94ea3..718befbd120 100644 --- a/src/util/buffer.h +++ b/src/util/buffer.h @@ -27,11 +27,11 @@ Revision History: template class buffer { protected: - T * m_buffer; - unsigned m_pos; - unsigned m_capacity; - char m_initial_buffer[INITIAL_SIZE * sizeof(T)]; - + T * m_buffer = reinterpret_cast(m_initial_buffer); + unsigned m_pos = 0; + unsigned m_capacity = INITIAL_SIZE; + typename std::aligned_storage::type m_initial_buffer[INITIAL_SIZE]; + void free_memory() { if (m_buffer != reinterpret_cast(m_initial_buffer)) { dealloc_svect(m_buffer); @@ -39,7 +39,7 @@ class buffer { } void expand() { - static_assert(std::is_nothrow_move_constructible::value, ""); + static_assert(std::is_nothrow_move_constructible::value); unsigned new_capacity = m_capacity << 1; T * new_buffer = reinterpret_cast(memory::allocate(sizeof(T) * new_capacity)); for (unsigned i = 0; i < m_pos; ++i) { @@ -73,26 +73,29 @@ class buffer { typedef T * iterator; typedef const T * const_iterator; - buffer(): - m_buffer(reinterpret_cast(m_initial_buffer)), - m_pos(0), - m_capacity(INITIAL_SIZE) { - } + buffer() = default; - buffer(const buffer & source): - m_buffer(reinterpret_cast(m_initial_buffer)), - m_pos(0), - m_capacity(INITIAL_SIZE) { - unsigned sz = source.size(); - for(unsigned i = 0; i < sz; i++) { + buffer(const buffer & source) { + for (unsigned i = 0, sz = source.size(); i < sz; ++i) { push_back(source.m_buffer[i]); } } - - buffer(unsigned sz, const T & elem): - m_buffer(reinterpret_cast(m_initial_buffer)), - m_pos(0), - m_capacity(INITIAL_SIZE) { + + buffer(buffer && source) { + if (source.m_buffer == reinterpret_cast(source.m_initial_buffer)) { + for (unsigned i = 0, sz = source.size(); i < sz; ++i) { + push_back(std::move(source.m_buffer[i])); + } + } else { + m_pos = source.m_pos; + m_capacity = source.m_capacity; + m_buffer = source.m_buffer; + source.m_buffer = reinterpret_cast(source.m_initial_buffer); + source.m_pos = 0; + } + } + + buffer(unsigned sz, const T & elem) { for (unsigned i = 0; i < sz; i++) { push_back(elem); }