From 873dd34ebfa32e2b1c0719141be4b320fcf29bc0 Mon Sep 17 00:00:00 2001 From: Michael Schellenberger Costa Date: Thu, 26 Aug 2021 14:41:46 +0200 Subject: [PATCH 1/8] Expand the allocator aware tests to also run at compile time and fix vector again --- stl/inc/vector | 2 +- .../test.cpp | 79 ++++++++++--------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/stl/inc/vector b/stl/inc/vector index 503a506f83f..e2991641c43 100644 --- a/stl/inc/vector +++ b/stl/inc/vector @@ -1697,7 +1697,7 @@ private: _Clear_and_reserve_geometric(_Newsize); } - _Mylast = _Refancy(_Copy_memmove(_Unfancy(_First), _Unfancy(_Last), _Unfancy(_Myfirst))); + _Mylast = _Uninitialized_move(_First, _Last, _Myfirst, _Al); } else { auto _Oldsize = static_cast(_Mylast - _Myfirst); diff --git a/tests/std/tests/VSO_0000000_allocator_propagation/test.cpp b/tests/std/tests/VSO_0000000_allocator_propagation/test.cpp index f4cbcb8ee84..505024f2a79 100644 --- a/tests/std/tests/VSO_0000000_allocator_propagation/test.cpp +++ b/tests/std/tests/VSO_0000000_allocator_propagation/test.cpp @@ -24,72 +24,78 @@ using namespace std; template -void assert_equal(const C& c, initializer_list il) { +_CONSTEXPR20 void assert_equal(const C& c, initializer_list il) { assert(equal(c.begin(), c.end(), il.begin(), il.end())); } template -void assert_is_permutation(const C& c, initializer_list il) { +_CONSTEXPR20 void assert_is_permutation(const C& c, initializer_list il) { assert(is_permutation(c.begin(), c.end(), il.begin(), il.end())); } template class MyAlloc { private: - size_t m_offset; + size_t _offset; - size_t allocation_offset() const noexcept { - return is_always_equal::value ? 10 : m_offset; + [[nodiscard]] _CONSTEXPR20 size_t allocation_offset() const noexcept { + return is_always_equal::value ? 10 : _offset; } public: - size_t offset() const noexcept { - return m_offset; + [[nodiscard]] _CONSTEXPR20 size_t offset() const noexcept { + return _offset; } - typedef T value_type; + using value_type = T; - typedef POCCA propagate_on_container_copy_assignment; - typedef POCMA propagate_on_container_move_assignment; - typedef POCS propagate_on_container_swap; - typedef EQUAL is_always_equal; + using propagate_on_container_copy_assignment = POCCA; + using propagate_on_container_move_assignment = POCMA; + using propagate_on_container_swap = POCS; + using is_always_equal = EQUAL; - explicit MyAlloc(const size_t off) : m_offset(off) {} + _CONSTEXPR20 explicit MyAlloc(const size_t off) : _offset(off) {} template - MyAlloc(const MyAlloc& other) noexcept : m_offset(other.offset()) {} + _CONSTEXPR20 MyAlloc(const MyAlloc& other) noexcept : _offset(other.offset()) {} template - bool operator==(const MyAlloc& other) const noexcept { + [[nodiscard]] _CONSTEXPR20 bool operator==(const MyAlloc& other) const noexcept { return allocation_offset() == other.allocation_offset(); } template - bool operator!=(const MyAlloc& other) const noexcept { + [[nodiscard]] _CONSTEXPR20 bool operator!=(const MyAlloc& other) const noexcept { return allocation_offset() != other.allocation_offset(); } - T* allocate(const size_t n) { + _CONSTEXPR20 T* allocate(const size_t n) { if (n == 0) { return nullptr; } const auto allocationOffset = allocation_offset(); + const auto _allocated = n + allocationOffset; // Production code should check for integer overflow. - void* const pv = malloc((n + allocationOffset) * sizeof(T)); + auto pv = allocator{}.allocate(_allocated); if (!pv) { throw bad_alloc(); } - memset(pv, 0xAB, (n + allocationOffset) * sizeof(T)); +#if _HAS_CXX20 + if (!is_constant_evaluated()) +#endif // _HAS_CXX20 + { + memset(pv, 0xAB, (_allocated) * sizeof(T)); + } - return static_cast(pv) + allocationOffset; + return pv + allocationOffset; } - void deallocate(T* const p, size_t) noexcept { + _CONSTEXPR20 void deallocate(T* const p, const size_t size) noexcept { if (p) { - free(p - allocation_offset()); + allocator{}.deallocate(p - allocation_offset(), size + allocation_offset()); } } }; @@ -111,8 +117,7 @@ using SwapEqualAlloc = MyAlloc; template