diff --git a/deps/ox/src/ox/std/types.hpp b/deps/ox/src/ox/std/types.hpp index 2af748349..abaea5fe3 100644 --- a/deps/ox/src/ox/std/types.hpp +++ b/deps/ox/src/ox/std/types.hpp @@ -57,6 +57,16 @@ typedef uint32_t uintptr_t; namespace ox { +/** + * Aliases type T in size and alignment to allow allocating space for a T + * without running the constructor. + */ +template +struct alignas(alignof(T)) AllocAlias { + char buff[sizeof(T)]; +}; + + template struct SignedType { }; diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index bb93b5749..126a8e7aa 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -85,7 +85,7 @@ template Vector::Vector(std::size_t size) noexcept { m_size = size; m_cap = m_size; - m_items = reinterpret_cast(new char[m_cap * sizeof(T)]); + m_items = reinterpret_cast(new AllocAlias[m_cap]); for (std::size_t i = 0; i < size; i++) { m_items[i] = {}; } @@ -95,7 +95,7 @@ template Vector::Vector(Vector &other) noexcept { m_size = other.m_size; m_cap = other.m_cap; - m_items = reinterpret_cast(new char[m_cap * sizeof(T)]); + m_items = reinterpret_cast(new AllocAlias[m_cap]); for (std::size_t i = 0; i < m_size; i++) { m_items[i] = ox::move(other.m_items[i]); } @@ -118,7 +118,7 @@ Vector::~Vector() noexcept { m_items[i].~T(); } } - delete[] reinterpret_cast(m_items); + delete[] reinterpret_cast*>(m_items); m_items = nullptr; } @@ -127,7 +127,7 @@ Vector &Vector::operator=(Vector &other) noexcept { this->~Vector(); m_size = other.m_size; m_cap = other.m_cap; - m_items = reinterpret_cast(new char[m_cap * sizeof(T)]); + m_items = reinterpret_cast(new AllocAlias[m_cap]); for (std::size_t i = 0; i < m_size; i++) { m_items[i] = other.m_items[i]; } @@ -249,7 +249,7 @@ template void Vector::expandCap(std::size_t cap) noexcept { auto oldItems = m_items; m_cap = cap; - m_items = reinterpret_cast(new char[m_cap * sizeof(T)]); + m_items = reinterpret_cast(new AllocAlias[m_cap]); if (oldItems) { // move over old items const auto itRange = cap > m_size ? m_size : cap; for (std::size_t i = 0; i < itRange; i++) { @@ -258,7 +258,7 @@ void Vector::expandCap(std::size_t cap) noexcept { for (std::size_t i = itRange; i < m_cap; i++) { new (&m_items[i]) T; } - delete[] reinterpret_cast(m_items); + delete[] reinterpret_cast*>(m_items); } }