diff --git a/radiant/detail/VectorOperations.h b/radiant/detail/VectorOperations.h index d5285f3..f86f83a 100644 --- a/radiant/detail/VectorOperations.h +++ b/radiant/detail/VectorOperations.h @@ -32,6 +32,8 @@ namespace detail template struct VectorAlloc { +public: + ~VectorAlloc() { if (buffer) @@ -42,10 +44,10 @@ struct VectorAlloc } explicit VectorAlloc(TAllocator& alloc) noexcept - : allocator(alloc), - buffer(nullptr), + : buffer(nullptr), size(0), - capacity(0) + capacity(0), + allocator(alloc) { } @@ -53,6 +55,8 @@ struct VectorAlloc bool Alloc(uint32_t count) { + RAD_ASSERT(buffer == nullptr); + buffer = allocator.Alloc(count); if (!buffer) { @@ -73,10 +77,13 @@ struct VectorAlloc return tmp; } - TAllocator& allocator; T* buffer; uint32_t size; uint32_t capacity; + +private: + + TAllocator& allocator; }; template @@ -401,8 +408,7 @@ struct VectorStorage } VectorAlloc vec(alloc); - vec.buffer = vec.allocator.Alloc(m_size); - if (!vec.buffer) + if (!vec.Alloc(m_size)) { return Error::NoMemory; } @@ -523,8 +529,7 @@ struct VectorStorage else { VectorAlloc vec(alloc); - vec.buffer = vec.allocator.Alloc(m_size); - if (!vec.buffer) + if (!vec.Alloc(m_size)) { return Error::NoMemory; } @@ -866,6 +871,8 @@ struct VectorOperations : public VectorStorage } else { + Free(alloc); + m_data = vec.Release(); m_capacity = count; } @@ -895,8 +902,7 @@ struct VectorOperations : public VectorStorage else { VectorAlloc vec(alloc); - vec.buffer = vec.allocator.Alloc(count); - if (!vec.buffer) + if (!vec.Alloc(count)) { return Error::NoMemory; } @@ -904,6 +910,8 @@ struct VectorOperations : public VectorStorage ManipType().CopyCtor(vec.buffer, count, value); ManipType().DtorRange(Data(), Data() + m_size); + Free(alloc); + m_data = vec.Release(); m_capacity = count; } @@ -942,6 +950,8 @@ struct VectorOperations : public VectorStorage } else { + Free(alloc); + m_data = vec.Release(); m_capacity = span.Size(); }