From 263ed4f7e7735aa101b1d327d49a0c00c366321f Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 6 Nov 2021 12:37:16 -0500 Subject: [PATCH] [ox/std] Make Vector use std::allocator --- deps/ox/src/ox/std/vector.hpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index 5526f4c6..2e3a887b 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -12,6 +12,7 @@ #include "initializerlist.hpp" #include "iterator.hpp" #include "math.hpp" +#include "memory.hpp" #include "new.hpp" #include "types.hpp" #include "utility.hpp" @@ -23,6 +24,7 @@ namespace detail { template struct SmallVector { private: + std::allocator m_allocator; AllocAlias m_data[Size] = {}; public: @@ -34,7 +36,7 @@ struct SmallVector { if (cap <= Size) { *items = reinterpret_cast(m_data); } else { - *items = reinterpret_cast(new AllocAlias[cap]); + *items = m_allocator.allocate(cap); } } @@ -60,9 +62,9 @@ struct SmallVector { } } - constexpr void clearItems(AllocAlias *items) noexcept { - if (items != m_data) { - delete[] items; + constexpr void clearItems(T *items, std::size_t n) noexcept { + if (static_cast(items) != static_cast(m_data)) { + m_allocator.deallocate(items, n); } } @@ -70,13 +72,15 @@ struct SmallVector { template struct SmallVector { + private: + std::allocator m_allocator; public: SmallVector() noexcept = default; SmallVector(SmallVector&) noexcept = default; SmallVector(SmallVector&&) noexcept = default; protected: constexpr void initItems(T **items, std::size_t cap) noexcept { - *items = reinterpret_cast(new AllocAlias[cap]); + *items = m_allocator.allocate(cap); } [[maybe_unused]] @@ -87,8 +91,8 @@ struct SmallVector { constexpr void moveItemsFrom(T**, SmallVector&, const std::size_t, const std::size_t) noexcept { } - constexpr void clearItems(AllocAlias *items) noexcept { - delete[] items; + constexpr void clearItems(T *items, std::size_t n) noexcept { + m_allocator.deallocate(items, n); } }; @@ -382,7 +386,7 @@ Vector::Vector(Vector &&other) noexcept { template Vector::~Vector() { clear(); - this->clearItems(reinterpret_cast*>(m_items)); + this->clearItems(m_items, m_cap); m_items = nullptr; } @@ -403,7 +407,7 @@ template constexpr Vector &Vector::operator=(const Vector &other) { if (this != &other) { clear(); - this->clearItems(reinterpret_cast*>(m_items)); + this->clearItems(m_items, m_cap); m_items = nullptr; m_size = other.m_size; m_cap = other.m_cap; @@ -419,7 +423,7 @@ template constexpr Vector &Vector::operator=(Vector &&other) noexcept { if (this != &other) { clear(); - this->clearItems(reinterpret_cast*>(m_items)); + this->clearItems(m_items, m_cap); m_size = other.m_size; m_cap = other.m_cap; m_items = other.m_items; @@ -604,7 +608,7 @@ void Vector::expandCap(std::size_t cap) { for (std::size_t i = itRange; i < m_cap; i++) { new (&m_items[i]) T; } - this->clearItems(reinterpret_cast*>(oldItems)); + this->clearItems(m_items, m_cap); } }