From 8b1e82086963d3ed634fecba594d9e8c910d26cc Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 21 Feb 2026 04:00:04 +0200 Subject: [PATCH] BAN: Add reallocator support to Vector --- BAN/include/BAN/Vector.h | 45 ++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index 7e90c0f5..18f0b777 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -381,19 +381,46 @@ namespace BAN template ErrorOr Vector::ensure_capacity(size_type size) { + static_assert(alignof(T) <= alignof(max_align_t), "over aligned types not supported"); + if (m_capacity >= size) return {}; - size_type new_cap = BAN::Math::max(size, m_capacity * 2); - T* new_data = (T*)BAN::allocator(new_cap * sizeof(T)); - if (new_data == nullptr) - return Error::from_errno(ENOMEM); - for (size_type i = 0; i < m_size; i++) + + const size_type new_cap = BAN::Math::max(size, m_capacity * 2); + + if constexpr (BAN::is_trivially_copyable_v) { - new (new_data + i) T(move(m_data[i])); - m_data[i].~T(); + if constexpr (BAN::reallocator) + { + auto* new_data = static_cast(BAN::reallocator(m_data, new_cap * sizeof(T))); + if (new_data == nullptr) + return Error::from_errno(ENOMEM); + m_data = new_data; + } + else + { + auto* new_data = static_cast(BAN::allocator(new_cap * sizeof(T))); + if (new_data == nullptr) + return Error::from_errno(ENOMEM); + memcpy(new_data, m_data, m_size * sizeof(T)); + BAN::deallocator(m_data); + m_data = new_data; + } } - BAN::deallocator(m_data); - m_data = new_data; + else + { + auto* new_data = static_cast(BAN::allocator(new_cap * sizeof(T))); + if (new_data == nullptr) + return Error::from_errno(ENOMEM); + for (size_type i = 0; i < m_size; i++) + { + new (new_data + i) T(move(m_data[i])); + m_data[i].~T(); + } + BAN::deallocator(m_data); + m_data = new_data; + } + m_capacity = new_cap; return {}; }