Author: ericwf Date: Fri Dec 9 03:51:09 2016 New Revision: 289197 URL: http://llvm.org/viewvc/llvm-project?rev=289197&view=rev Log: Refactor uses_allocator test types for upcoming fixes
Added: libcxx/trunk/test/support/controlled_allocators.hpp - copied, changed from r289195, libcxx/trunk/test/support/test_memory_resource.hpp Modified: libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_const_lvalue_pair.pass.cpp libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_rvalue.pass.cpp libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_values.pass.cpp libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_piecewise_pair.pass.cpp libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_types.pass.cpp libcxx/trunk/test/support/test_memory_resource.hpp libcxx/trunk/test/support/type_id.h libcxx/trunk/test/support/uses_alloc_types.hpp Modified: libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_const_lvalue_pair.pass.cpp URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_const_lvalue_pair.pass.cpp?rev=289197&r1=289196&r2=289197&view=diff ============================================================================== --- libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_const_lvalue_pair.pass.cpp (original) +++ libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_const_lvalue_pair.pass.cpp Fri Dec 9 03:51:09 2016 @@ -23,7 +23,12 @@ #include <tuple> #include <cassert> #include <cstdlib> + +#include "test_macros.h" +#include "test_memory_resource.hpp" #include "uses_alloc_types.hpp" +#include "controlled_allocators.hpp" +#include "test_allocator.h" namespace ex = std::experimental::pmr; Modified: libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_rvalue.pass.cpp URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_rvalue.pass.cpp?rev=289197&r1=289196&r2=289197&view=diff ============================================================================== --- libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_rvalue.pass.cpp (original) +++ libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_rvalue.pass.cpp Fri Dec 9 03:51:09 2016 @@ -23,7 +23,12 @@ #include <tuple> #include <cassert> #include <cstdlib> + +#include "test_macros.h" +#include "test_memory_resource.hpp" #include "uses_alloc_types.hpp" +#include "controlled_allocators.hpp" +#include "test_allocator.h" namespace ex = std::experimental::pmr; Modified: libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_values.pass.cpp URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_values.pass.cpp?rev=289197&r1=289196&r2=289197&view=diff ============================================================================== --- libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_values.pass.cpp (original) +++ libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_values.pass.cpp Fri Dec 9 03:51:09 2016 @@ -23,7 +23,12 @@ #include <tuple> #include <cassert> #include <cstdlib> + +#include "test_macros.h" +#include "test_memory_resource.hpp" #include "uses_alloc_types.hpp" +#include "controlled_allocators.hpp" +#include "test_allocator.h" namespace ex = std::experimental::pmr; Modified: libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_piecewise_pair.pass.cpp URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_piecewise_pair.pass.cpp?rev=289197&r1=289196&r2=289197&view=diff ============================================================================== --- libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_piecewise_pair.pass.cpp (original) +++ libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_piecewise_pair.pass.cpp Fri Dec 9 03:51:09 2016 @@ -24,7 +24,11 @@ #include <tuple> #include <cassert> #include <cstdlib> + +#include "test_macros.h" +#include "test_memory_resource.hpp" #include "uses_alloc_types.hpp" +#include "controlled_allocators.hpp" #include "test_allocator.h" namespace ex = std::experimental::pmr; Modified: libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_types.pass.cpp URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_types.pass.cpp?rev=289197&r1=289196&r2=289197&view=diff ============================================================================== --- libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_types.pass.cpp (original) +++ libcxx/trunk/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_types.pass.cpp Fri Dec 9 03:51:09 2016 @@ -21,7 +21,11 @@ #include <type_traits> #include <cassert> #include <cstdlib> + +#include "test_macros.h" +#include "test_memory_resource.hpp" #include "uses_alloc_types.hpp" +#include "controlled_allocators.hpp" #include "test_allocator.h" namespace ex = std::experimental::pmr; Copied: libcxx/trunk/test/support/controlled_allocators.hpp (from r289195, libcxx/trunk/test/support/test_memory_resource.hpp) URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/controlled_allocators.hpp?p2=libcxx/trunk/test/support/controlled_allocators.hpp&p1=libcxx/trunk/test/support/test_memory_resource.hpp&r1=289195&r2=289197&rev=289197&view=diff ============================================================================== --- libcxx/trunk/test/support/test_memory_resource.hpp (original) +++ libcxx/trunk/test/support/controlled_allocators.hpp Fri Dec 9 03:51:09 2016 @@ -7,10 +7,9 @@ // //===----------------------------------------------------------------------===// -#ifndef SUPPORT_TEST_MEMORY_RESOURCE_HPP -#define SUPPORT_TEST_MEMORY_RESOURCE_HPP +#ifndef SUPPORT_CONTROLLED_ALLOCATORS_HPP +#define SUPPORT_CONTROLLED_ALLOCATORS_HPP -#include <experimental/memory_resource> #include <memory> #include <type_traits> #include <cstddef> @@ -19,12 +18,13 @@ #include <cstdint> #include <cassert> #include "test_macros.h" +#include "type_id.h" struct AllocController; // 'AllocController' is a concrete type that instruments and controls the // behavior of of test allocators. -template <class T> +template <class T, size_t ID = 0> class CountingAllocator; // 'CountingAllocator' is an basic implementation of the 'Allocator' // requirements that use the 'AllocController' interface. @@ -79,6 +79,17 @@ struct AllocController { bool throw_on_alloc = false; + int construct_called = 0; + void *last_construct_pointer = nullptr; + TypeID const* last_construct_alloc = nullptr; + TypeID const* last_construct_type = nullptr; + TypeID const* last_construct_args = nullptr; + + int destroy_called = 0; + void *last_destroy_pointer = nullptr; + TypeID const* last_destroy_alloc = nullptr; + TypeID const* last_destroy_type = nullptr; + AllocController() = default; void countAlloc(void* p, size_t s, size_t a) { @@ -101,8 +112,32 @@ struct AllocController { last_align = last_dealloc_align = a; } - void reset() { std::memset(this, 0, sizeof(*this)); } + template <class ...Args, class Alloc, class Tp> + void countConstruct(Alloc const& a, Tp *p) { + ++construct_called; + last_construct_pointer = p; + last_construct_alloc = &makeTypeID<Alloc>(); + last_construct_type = &makeTypeID<Tp>(); + last_construct_args = &makeArgumentID<Args...>(); + } + template <class Alloc, class Tp> + void countDestroy(Alloc const& a, Tp *p) { + ++destroy_called; + last_destroy_alloc = &makeTypeID<Alloc>(); + last_destroy_type = &makeTypeID<Tp>(); + last_destroy_pointer = p; + } + + void reset() { std::memset(this, 0, sizeof(*this)); } + void resetConstructDestroy() { + construct_called = 0; + last_construct_pointer = nullptr; + last_construct_alloc = last_construct_args = last_construct_type = nullptr; + destroy_called = 0; + last_destroy_alloc = nullptr; + last_destroy_pointer = nullptr; + } public: bool checkAlloc(void* p, size_t s, size_t a) const { return p == last_alloc_pointer && @@ -143,6 +178,29 @@ public: last_dealloc_align == last_alloc_align; } + template <class ...Args, class Alloc, class Tp> + bool checkConstruct(Alloc const&, Tp *p) const { + auto expectAlloc = &makeTypeID<Alloc>(); + auto expectTp = &makeTypeID<Tp>(); + auto expectArgs = &makeArgumentID<Args...>(); + return last_construct_pointer == p && + COMPARE_TYPEID(last_construct_alloc, expectAlloc) && + COMPARE_TYPEID(last_construct_type, expectTp) && + COMPARE_TYPEID(last_construct_args, expectArgs); + } + + template <class Alloc, class Tp> + bool checkDestroy(Alloc const&, Tp *p) const { + return last_destroy_pointer == p && + last_destroy_alloc == &makeTypeID<Alloc>() && + last_destroy_type == &makeTypeID<Tp>(); + } + + bool checkDestroyMatchesConstruct() const { + return last_destroy_pointer == last_construct_pointer && + last_destroy_type == last_construct_type; + } + void countIsEqual() { ++is_equal_count; } @@ -154,12 +212,16 @@ private: DISALLOW_COPY(AllocController); }; -template <class T> +template <class T, size_t ID> class CountingAllocator { public: typedef T value_type; typedef T* pointer; + + template <class U> + struct rebind { using other = CountingAllocator<U, ID>; }; + CountingAllocator() = delete; explicit CountingAllocator(AllocController& PP) : P(&PP) {} @@ -172,12 +234,12 @@ public: } template <class U> - CountingAllocator(CountingAllocator<U> const& other) TEST_NOEXCEPT : P(other.P) { + CountingAllocator(CountingAllocator<U, ID> const& other) TEST_NOEXCEPT : P(other.P) { P->copy_constructed += 1; } template <class U> - CountingAllocator(CountingAllocator<U>&& other) TEST_NOEXCEPT : P(other.P) { + CountingAllocator(CountingAllocator<U, ID>&& other) TEST_NOEXCEPT : P(other.P) { P->move_constructed += 1; } @@ -195,22 +257,77 @@ public: ::operator delete(vp); } + template <class U, class ...Args> + void construct(U *p, Args&&... args) { + auto *c = ::new ((void*)p) U(std::forward<Args>(args)...); + P->countConstruct<Args&&...>(*this, p); + } + + template <class U> + void destroy(U* p) { + p->~U(); + P->countDestroy(*this, p); + } + AllocController& getController() const { return *P; } private: - template <class Tp> friend class CountingAllocator; + template <class Tp, size_t XID> friend class CountingAllocator; AllocController *P; }; -template <class T, class U> -inline bool operator==(CountingAllocator<T> const& x, - CountingAllocator<U> const& y) { + +template <size_t ID> +class CountingAllocator<void, ID> +{ +public: + typedef void* pointer; + typedef const void* const_pointer; + typedef void value_type; + + template <class U> + struct rebind { using other = CountingAllocator<U, ID>; }; + + CountingAllocator() = delete; + explicit CountingAllocator(AllocController& PP) : P(&PP) {} + + CountingAllocator(CountingAllocator const& other) : P(other.P) { + P->copy_constructed += 1; + } + + CountingAllocator(CountingAllocator&& other) : P(other.P) { + P->move_constructed += 1; + } + + template <class U> + CountingAllocator(CountingAllocator<U, ID> const& other) TEST_NOEXCEPT : P(other.P) { + P->copy_constructed += 1; + } + + template <class U> + CountingAllocator(CountingAllocator<U, ID>&& other) TEST_NOEXCEPT : P(other.P) { + P->move_constructed += 1; + } + + void construct(...) = delete; + void destroy(void*) = delete; + + AllocController& getController() const { return *P; } + +private: + template <class Tp, size_t> friend class CountingAllocator; + AllocController *P; +}; + +template <class T, class U, size_t ID> +inline bool operator==(CountingAllocator<T, ID> const& x, + CountingAllocator<U, ID> const& y) { return &x.getController() == &y.getController(); } -template <class T, class U> -inline bool operator!=(CountingAllocator<T> const& x, - CountingAllocator<U> const& y) { +template <class T, class U, size_t ID> +inline bool operator!=(CountingAllocator<T, ID> const& x, + CountingAllocator<U, ID> const& y) { return !(x == y); } @@ -266,6 +383,18 @@ public: return ::operator delete(static_cast<void*>(aligned_ptr)); } + template <class U, class ...Args> + void construct(U *p, Args&&... args) { + auto *c = ::new ((void*)p) U(std::forward<Args>(args)...); + P->countConstruct<Args&&...>(*this, p); + } + + template <class U> + void destroy(U* p) { + p->~U(); + P->countDestroy(*this, p); + } + AllocController& getController() const { return *P; } private: @@ -366,142 +495,4 @@ inline bool operator!=(NullAllocator<T> } - -template <class ProviderT, int = 0> -class TestResourceImp : public std::experimental::pmr::memory_resource -{ -public: - static int resource_alive; - static int resource_constructed; - static int resource_destructed; - - static void resetStatics() { - assert(resource_alive == 0); - resource_alive = 0; - resource_constructed = 0; - resource_destructed = 0; - } - - using memory_resource = std::experimental::pmr::memory_resource; - using Provider = ProviderT; - - int value; - - explicit TestResourceImp(int val = 0) : value(val) { - ++resource_alive; - ++resource_constructed; - } - - ~TestResourceImp() noexcept { - --resource_alive; - ++resource_destructed; - } - - void reset() { C.reset(); P.reset(); } - AllocController& getController() { return C; } - - bool checkAlloc(void* p, std::size_t s, std::size_t a) const - { return C.checkAlloc(p, s, a); } - - bool checkDealloc(void* p, std::size_t s, std::size_t a) const - { return C.checkDealloc(p, s, a); } - - bool checkIsEqualCalledEq(int n) const { return C.checkIsEqualCalledEq(n); } - -protected: - virtual void * do_allocate(std::size_t s, std::size_t a) { - if (C.throw_on_alloc) { -#ifndef TEST_HAS_NO_EXCEPTIONS - throw TestException{}; -#else - assert(false); -#endif - } - void* ret = P.allocate(s, a); - C.countAlloc(ret, s, a); - return ret; - } - - virtual void do_deallocate(void * p, std::size_t s, std::size_t a) { - C.countDealloc(p, s, a); - P.deallocate(p, s, a); - } - - virtual bool do_is_equal(memory_resource const & other) const noexcept { - C.countIsEqual(); - TestResourceImp const * o = dynamic_cast<TestResourceImp const *>(&other); - return o && o->value == value; - } -private: - mutable AllocController C; - mutable Provider P; - DISALLOW_COPY(TestResourceImp); -}; - -template <class Provider, int N> -int TestResourceImp<Provider, N>::resource_alive = 0; - -template <class Provider, int N> -int TestResourceImp<Provider, N>::resource_constructed = 0; - -template <class Provider, int N> -int TestResourceImp<Provider, N>::resource_destructed = 0; - - -struct NullProvider { - NullProvider() {} - void* allocate(size_t, size_t) { return nullptr; } - void deallocate(void*, size_t, size_t) {} - void reset() {} -private: - DISALLOW_COPY(NullProvider); -}; - -struct NewDeleteProvider { - NewDeleteProvider() {} - void* allocate(size_t s, size_t) { return ::operator new(s); } - void deallocate(void* p, size_t, size_t) { ::operator delete(p); } - void reset() {} -private: - DISALLOW_COPY(NewDeleteProvider); -}; - -template <size_t Size = 4096 * 10> // 10 pages worth of memory. -struct BufferProvider { - char buffer[Size]; - void* next = &buffer; - size_t space = Size; - - BufferProvider() {} - - void* allocate(size_t s, size_t a) { - void* ret = std::align(s, a, next, space); - if (ret == nullptr) { -#ifndef TEST_HAS_NO_EXCEPTIONS - throw std::bad_alloc(); -#else - assert(false); -#endif - } - - return ret; - } - - void deallocate(void*, size_t, size_t) {} - - void reset() { - next = &buffer; - space = Size; - } -private: - DISALLOW_COPY(BufferProvider); -}; - -using NullResource = TestResourceImp<NullProvider, 0>; -using NewDeleteResource = TestResourceImp<NewDeleteProvider, 0>; -using TestResource = TestResourceImp<BufferProvider<>, 0>; -using TestResource1 = TestResourceImp<BufferProvider<>, 1>; -using TestResource2 = TestResourceImp<BufferProvider<>, 2>; - - -#endif /* SUPPORT_TEST_MEMORY_RESOURCE_HPP */ +#endif /* SUPPORT_CONTROLLED_ALLOCATORS_HPP */ Modified: libcxx/trunk/test/support/test_memory_resource.hpp URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/test_memory_resource.hpp?rev=289197&r1=289196&r2=289197&view=diff ============================================================================== --- libcxx/trunk/test/support/test_memory_resource.hpp (original) +++ libcxx/trunk/test/support/test_memory_resource.hpp Fri Dec 9 03:51:09 2016 @@ -11,6 +11,7 @@ #define SUPPORT_TEST_MEMORY_RESOURCE_HPP #include <experimental/memory_resource> +#include <experimental/utility> #include <memory> #include <type_traits> #include <cstddef> @@ -19,354 +20,17 @@ #include <cstdint> #include <cassert> #include "test_macros.h" +#include "controlled_allocators.hpp" +#include "uses_alloc_types.hpp" -struct AllocController; - // 'AllocController' is a concrete type that instruments and controls the - // behavior of of test allocators. - -template <class T> -class CountingAllocator; - // 'CountingAllocator' is an basic implementation of the 'Allocator' - // requirements that use the 'AllocController' interface. - -template <class T> -class MinAlignAllocator; - // 'MinAlignAllocator' is an instrumented test type which implements the - // 'Allocator' requirements. 'MinAlignAllocator' ensures that it *never* - // returns a pointer to over-aligned storage. For example - // 'MinAlignPointer<char>{}.allocate(...)' will never a 2-byte aligned - // pointer. - -template <class T> -class NullAllocator; - // 'NullAllocator' is an instrumented test type which implements the - // 'Allocator' requirements except that 'allocator' and 'deallocate' are - // nops. - - -#define DISALLOW_COPY(Type) \ - Type(Type const&) = delete; \ - Type& operator=(Type const&) = delete - -constexpr std::size_t MaxAlignV = alignof(std::max_align_t); - -struct TestException {}; - -struct AllocController { - int copy_constructed = 0; - int move_constructed = 0; - - int alive = 0; - int alloc_count = 0; - int dealloc_count = 0; - int is_equal_count = 0; - - std::size_t alive_size; - std::size_t allocated_size; - std::size_t deallocated_size; - - std::size_t last_size = 0; - std::size_t last_align = 0; - void * last_pointer = 0; - - std::size_t last_alloc_size = 0; - std::size_t last_alloc_align = 0; - void * last_alloc_pointer = nullptr; - - std::size_t last_dealloc_size = 0; - std::size_t last_dealloc_align = 0; - void * last_dealloc_pointer = nullptr; - - bool throw_on_alloc = false; - - AllocController() = default; - - void countAlloc(void* p, size_t s, size_t a) { - ++alive; - ++alloc_count; - alive_size += s; - allocated_size += s; - last_pointer = last_alloc_pointer = p; - last_size = last_alloc_size = s; - last_align = last_alloc_align = a; - } - - void countDealloc(void* p, size_t s, size_t a) { - --alive; - ++dealloc_count; - alive_size -= s; - deallocated_size += s; - last_pointer = last_dealloc_pointer = p; - last_size = last_dealloc_size = s; - last_align = last_dealloc_align = a; - } - - void reset() { std::memset(this, 0, sizeof(*this)); } - -public: - bool checkAlloc(void* p, size_t s, size_t a) const { - return p == last_alloc_pointer && - s == last_alloc_size && - a == last_alloc_align; - } - - bool checkAlloc(void* p, size_t s) const { - return p == last_alloc_pointer && - s == last_alloc_size; - } - - bool checkAllocAtLeast(void* p, size_t s, size_t a) const { - return p == last_alloc_pointer && - s <= last_alloc_size && - a <= last_alloc_align; - } - - bool checkAllocAtLeast(void* p, size_t s) const { - return p == last_alloc_pointer && - s <= last_alloc_size; - } - - bool checkDealloc(void* p, size_t s, size_t a) const { - return p == last_dealloc_pointer && - s == last_dealloc_size && - a == last_dealloc_align; - } - - bool checkDealloc(void* p, size_t s) const { - return p == last_dealloc_pointer && - s == last_dealloc_size; - } - - bool checkDeallocMatchesAlloc() const { - return last_dealloc_pointer == last_alloc_pointer && - last_dealloc_size == last_alloc_size && - last_dealloc_align == last_alloc_align; - } - - void countIsEqual() { - ++is_equal_count; - } - - bool checkIsEqualCalledEq(int n) const { - return is_equal_count == n; - } -private: - DISALLOW_COPY(AllocController); -}; - -template <class T> -class CountingAllocator -{ -public: - typedef T value_type; - typedef T* pointer; - CountingAllocator() = delete; - explicit CountingAllocator(AllocController& PP) : P(&PP) {} - - CountingAllocator(CountingAllocator const& other) : P(other.P) { - P->copy_constructed += 1; - } - - CountingAllocator(CountingAllocator&& other) : P(other.P) { - P->move_constructed += 1; - } - - template <class U> - CountingAllocator(CountingAllocator<U> const& other) TEST_NOEXCEPT : P(other.P) { - P->copy_constructed += 1; - } - - template <class U> - CountingAllocator(CountingAllocator<U>&& other) TEST_NOEXCEPT : P(other.P) { - P->move_constructed += 1; - } - - T* allocate(std::size_t n) - { - void* ret = ::operator new(n*sizeof(T)); - P->countAlloc(ret, n*sizeof(T), alignof(T)); - return static_cast<T*>(ret); - } - - void deallocate(T* p, std::size_t n) - { - void* vp = static_cast<void*>(p); - P->countDealloc(vp, n*sizeof(T), alignof(T)); - ::operator delete(vp); - } - - AllocController& getController() const { return *P; } - -private: - template <class Tp> friend class CountingAllocator; - AllocController *P; -}; - -template <class T, class U> -inline bool operator==(CountingAllocator<T> const& x, - CountingAllocator<U> const& y) { - return &x.getController() == &y.getController(); -} - -template <class T, class U> -inline bool operator!=(CountingAllocator<T> const& x, - CountingAllocator<U> const& y) { - return !(x == y); -} - -template <class T> -class MinAlignedAllocator -{ -public: - typedef T value_type; - typedef T* pointer; - - MinAlignedAllocator() = delete; - - explicit MinAlignedAllocator(AllocController& R) : P(&R) {} - - MinAlignedAllocator(MinAlignedAllocator const& other) : P(other.P) { - P->copy_constructed += 1; - } - - MinAlignedAllocator(MinAlignedAllocator&& other) : P(other.P) { - P->move_constructed += 1; - } - - template <class U> - MinAlignedAllocator(MinAlignedAllocator<U> const& other) TEST_NOEXCEPT : P(other.P) { - P->copy_constructed += 1; - } - - template <class U> - MinAlignedAllocator(MinAlignedAllocator<U>&& other) TEST_NOEXCEPT : P(other.P) { - P->move_constructed += 1; - } - - T* allocate(std::size_t n) { - char* aligned_ptr = (char*)::operator new(alloc_size(n*sizeof(T))); - assert(is_max_aligned(aligned_ptr)); - - char* unaligned_ptr = aligned_ptr + alignof(T); - assert(is_min_aligned(unaligned_ptr)); - - P->countAlloc(unaligned_ptr, n * sizeof(T), alignof(T)); - - return ((T*)unaligned_ptr); - } - - void deallocate(T* p, std::size_t n) { - assert(is_min_aligned(p)); - - char* aligned_ptr = ((char*)p) - alignof(T); - assert(is_max_aligned(aligned_ptr)); - - P->countDealloc(p, n*sizeof(T), alignof(T)); - - return ::operator delete(static_cast<void*>(aligned_ptr)); - } - - AllocController& getController() const { return *P; } - -private: - static const std::size_t BlockSize = alignof(std::max_align_t); - - static std::size_t alloc_size(std::size_t s) { - std::size_t bytes = (s + BlockSize - 1) & ~(BlockSize - 1); - bytes += BlockSize; - assert(bytes % BlockSize == 0); - return bytes; - } - - static bool is_max_aligned(void* p) { - return reinterpret_cast<std::uintptr_t>(p) % BlockSize == 0; - } - - static bool is_min_aligned(void* p) { - if (alignof(T) == BlockSize) { - return is_max_aligned(p); - } else { - return reinterpret_cast<std::uintptr_t>(p) % BlockSize == alignof(T); - } - } - - template <class Tp> friend class MinAlignedAllocator; - mutable AllocController *P; +// FIXME: This is a hack to allow uses_allocator_types.hpp to work with +// erased_type. However we can't define that behavior directly in the header +// because it con't include <experimental/memory_resource> +template <> +struct TransformErasedTypeAlloc<std::experimental::erased_type> { + using type = std::experimental::pmr::memory_resource*; }; - -template <class T, class U> -inline bool operator==(MinAlignedAllocator<T> const& x, - MinAlignedAllocator<U> const& y) { - return &x.getController() == &y.getController(); -} - -template <class T, class U> -inline bool operator!=(MinAlignedAllocator<T> const& x, - MinAlignedAllocator<U> const& y) { - return !(x == y); -} - -template <class T> -class NullAllocator -{ -public: - typedef T value_type; - typedef T* pointer; - NullAllocator() = delete; - explicit NullAllocator(AllocController& PP) : P(&PP) {} - - NullAllocator(NullAllocator const& other) : P(other.P) { - P->copy_constructed += 1; - } - - NullAllocator(NullAllocator&& other) : P(other.P) { - P->move_constructed += 1; - } - - template <class U> - NullAllocator(NullAllocator<U> const& other) TEST_NOEXCEPT : P(other.P) { - P->copy_constructed += 1; - } - - template <class U> - NullAllocator(NullAllocator<U>&& other) TEST_NOEXCEPT : P(other.P) { - P->move_constructed += 1; - } - - T* allocate(std::size_t n) - { - P->countAlloc(nullptr, n*sizeof(T), alignof(T)); - return nullptr; - } - - void deallocate(T* p, std::size_t n) - { - void* vp = static_cast<void*>(p); - P->countDealloc(vp, n*sizeof(T), alignof(T)); - } - - AllocController& getController() const { return *P; } - -private: - template <class Tp> friend class NullAllocator; - AllocController *P; -}; - -template <class T, class U> -inline bool operator==(NullAllocator<T> const& x, - NullAllocator<U> const& y) { - return &x.getController() == &y.getController(); -} - -template <class T, class U> -inline bool operator!=(NullAllocator<T> const& x, - NullAllocator<U> const& y) { - return !(x == y); -} - - - template <class ProviderT, int = 0> class TestResourceImp : public std::experimental::pmr::memory_resource { Modified: libcxx/trunk/test/support/type_id.h URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/type_id.h?rev=289197&r1=289196&r2=289197&view=diff ============================================================================== --- libcxx/trunk/test/support/type_id.h (original) +++ libcxx/trunk/test/support/type_id.h Fri Dec 9 03:51:09 2016 @@ -10,9 +10,13 @@ #define SUPPORT_TYPE_ID_H #include <functional> +#include <typeinfo> +#include <string> +#include <cstdio> #include <cassert> #include "test_macros.h" +#include "demangle.h" #if TEST_STD_VER < 11 #error This header requires C++11 or greater @@ -25,25 +29,41 @@ struct TypeID { {return LHS.m_id == RHS.m_id; } friend bool operator!=(TypeID const& LHS, TypeID const& RHS) {return LHS.m_id != RHS.m_id; } + + std::string name() const { + return demangle(m_id); + } + + void dump() const { + std::string s = name(); + std::printf("TypeID: %s\n", s.c_str()); + } + private: - explicit constexpr TypeID(const int* xid) : m_id(xid) {} + explicit constexpr TypeID(const char* xid) : m_id(xid) {} TypeID(const TypeID&) = delete; TypeID& operator=(TypeID const&) = delete; - const int* const m_id; - template <class T> friend TypeID const& makeTypeID(); - + const char* const m_id; + template <class T> friend TypeID const& makeTypeIDImp(); }; // makeTypeID - Return the TypeID for the specified type 'T'. template <class T> -inline TypeID const& makeTypeID() { - static int dummy; - static const TypeID id(&dummy); +inline TypeID const& makeTypeIDImp() { + static const TypeID id(typeid(T).name()); return id; } +template <class T> +struct TypeWrapper {}; + +template <class T> +inline TypeID const& makeTypeID() { + return makeTypeIDImp<TypeWrapper<T>>(); +} + template <class ...Args> struct ArgumentListID {}; @@ -51,7 +71,23 @@ struct ArgumentListID {}; // of arguments. template <class ...Args> inline TypeID const& makeArgumentID() { - return makeTypeID<ArgumentListID<Args...>>(); + return makeTypeIDImp<ArgumentListID<Args...>>(); +} + + +// COMPARE_TYPEID(...) is a utility macro for generating diagnostics when +// two typeid's are expected to be equal +#define COMPARE_TYPEID(LHS, RHS) CompareTypeIDVerbose(#LHS, LHS, #RHS, RHS) + +inline bool CompareTypeIDVerbose(const char* LHSString, TypeID const* LHS, + const char* RHSString, TypeID const* RHS) { + if (*LHS == *RHS) + return true; + std::printf("TypeID's not equal:\n"); + std::printf("%s: %s\n----------\n%s: %s\n", + LHSString, LHS->name().c_str(), + RHSString, RHS->name().c_str()); + return false; } #endif // SUPPORT_TYPE_ID_H Modified: libcxx/trunk/test/support/uses_alloc_types.hpp URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/uses_alloc_types.hpp?rev=289197&r1=289196&r2=289197&view=diff ============================================================================== --- libcxx/trunk/test/support/uses_alloc_types.hpp (original) +++ libcxx/trunk/test/support/uses_alloc_types.hpp Fri Dec 9 03:51:09 2016 @@ -10,12 +10,11 @@ #ifndef USES_ALLOC_TYPES_HPP #define USES_ALLOC_TYPES_HPP -# include <experimental/memory_resource> -# include <experimental/utility> # include <memory> # include <cassert> +#include <cstdlib> -#include "test_memory_resource.hpp" +#include "test_macros.h" #include "type_id.h" // There are two forms of uses-allocator construction: @@ -31,6 +30,30 @@ constexpr UsesAllocatorType UA_None = Us constexpr UsesAllocatorType UA_AllocArg = UsesAllocatorType::UA_AllocArg; constexpr UsesAllocatorType UA_AllocLast = UsesAllocatorType::UA_AllocLast; +inline const char* toString(UsesAllocatorType UA) { + switch (UA) { + case UA_None: + return "UA_None"; + case UA_AllocArg: + return "UA_AllocArg"; + case UA_AllocLast: + return "UA_AllocLast"; + default: + std::abort(); + } +} + +#define COMPARE_ALLOC_TYPE(LHS, RHS) CompareVerbose(#LHS, LHS, #RHS, RHS) + +inline bool CompareVerbose(const char* LHSString, UsesAllocatorType LHS, + const char* RHSString, UsesAllocatorType RHS) { + if (LHS == RHS) + return true; + std::printf("UsesAllocatorType's don't match:\n%s %s\n----------\n%s %s\n", + LHSString, toString(LHS), RHSString, toString(RHS)); + return false; +} + template <class Alloc, std::size_t N> class UsesAllocatorV1; // Implements form (1) of uses-allocator construction from the specified @@ -115,66 +138,107 @@ using EnableIfB = typename std::enable_i } // end namespace detail +// FIXME: UsesAllocatorTestBase needs some special logic to deal with +// polymorphic allocators. However we don't want to include +// <experimental/memory_resource> in this header. Therefore in order +// to inject this behavior later we use a trait. +// See test_memory_resource.hpp for more info. +template <class Alloc> +struct TransformErasedTypeAlloc { + using type = Alloc; +}; + using detail::EnableIfB; struct AllocLastTag {}; +template <class Alloc, bool = std::is_default_constructible<Alloc>::value> +struct UsesAllocatorTestBaseStorage { + Alloc allocator; + UsesAllocatorTestBaseStorage() = default; + UsesAllocatorTestBaseStorage(Alloc const& a) : allocator(a) {} + const Alloc* get_allocator() const { return &allocator; } +}; + +template <class Alloc> +struct UsesAllocatorTestBaseStorage<Alloc, false> { + union { + char dummy; + Alloc alloc; + }; + bool has_alloc = false; + + UsesAllocatorTestBaseStorage() : dummy(), has_alloc(false) {} + UsesAllocatorTestBaseStorage(Alloc const& a) : alloc(a), has_alloc(true) {} + ~UsesAllocatorTestBaseStorage() { + if (has_alloc) + alloc.~Alloc(); + } + + Alloc const* get_allocator() const { + if (!has_alloc) + return nullptr; + return &alloc; + } +}; + template <class Self, class Alloc> struct UsesAllocatorTestBase { public: - using CtorAlloc = typename std::conditional< - std::is_same<Alloc, std::experimental::erased_type>::value, - std::experimental::pmr::memory_resource*, - Alloc - >::type; + using CtorAlloc = typename TransformErasedTypeAlloc<Alloc>::type; template <class ...ArgTypes> bool checkConstruct(UsesAllocatorType expectType) const { - return expectType == constructor_called && - makeArgumentID<ArgTypes...>() == *args_id; + auto expectArgs = &makeArgumentID<ArgTypes...>(); + return COMPARE_ALLOC_TYPE(expectType, constructor_called) && + COMPARE_TYPEID(args_id, expectArgs); } template <class ...ArgTypes> bool checkConstruct(UsesAllocatorType expectType, CtorAlloc const& expectAlloc) const { - return expectType == constructor_called && - makeArgumentID<ArgTypes...>() == *args_id && - expectAlloc == allocator; + auto ExpectID = &makeArgumentID<ArgTypes...>() ; + return COMPARE_ALLOC_TYPE(expectType, constructor_called) && + COMPARE_TYPEID(args_id, ExpectID) && + has_alloc() && expectAlloc == *get_alloc(); + } bool checkConstructEquiv(UsesAllocatorTestBase& O) const { - return constructor_called == O.constructor_called - && *args_id == *O.args_id - && allocator == O.allocator; + if (has_alloc() != O.has_alloc()) + return false; + return COMPARE_ALLOC_TYPE(constructor_called, O.constructor_called) + && COMPARE_TYPEID(args_id, O.args_id) + && (!has_alloc() || *get_alloc() == *O.get_alloc()); } protected: explicit UsesAllocatorTestBase(const TypeID* aid) - : args_id(aid), constructor_called(UA_None), allocator() + : args_id(aid), constructor_called(UA_None), alloc_store() {} UsesAllocatorTestBase(UsesAllocatorTestBase const&) : args_id(&makeArgumentID<Self const&>()), constructor_called(UA_None), - allocator() + alloc_store() {} UsesAllocatorTestBase(UsesAllocatorTestBase&&) : args_id(&makeArgumentID<Self&&>()), constructor_called(UA_None), - allocator() + alloc_store() {} template <class ...Args> UsesAllocatorTestBase(std::allocator_arg_t, CtorAlloc const& a, Args&&...) : args_id(&makeArgumentID<Args&&...>()), constructor_called(UA_AllocArg), - allocator(a) + alloc_store(a) {} template <class ...Args, class ArgsIDL = detail::TakeNArgs<sizeof...(Args) - 1, Args&&...>> UsesAllocatorTestBase(AllocLastTag, Args&&... args) - : args_id(&makeTypeID<typename ArgsIDL::type>()), + : args_id(&makeTypeIDImp<typename ArgsIDL::type>()), constructor_called(UA_AllocLast), - allocator(getAllocatorFromPack( + alloc_store(UsesAllocatorTestBase::getAllocatorFromPack( typename ArgsIDL::type{}, std::forward<Args>(args)...)) { @@ -183,7 +247,7 @@ protected: private: template <class ...LArgs, class ...Args> static CtorAlloc getAllocatorFromPack(ArgumentListID<LArgs...>, Args&&... args) { - return getAllocatorFromPackImp<LArgs const&...>(args...); + return UsesAllocatorTestBase::getAllocatorFromPackImp<LArgs const&...>(args...); } template <class ...LArgs> @@ -191,10 +255,13 @@ private: typename detail::Identity<LArgs>::type..., CtorAlloc const& alloc) { return alloc; } + + bool has_alloc() const { return alloc_store.get_allocator() != nullptr; } + const CtorAlloc *get_alloc() const { return alloc_store.get_allocator(); } public: const TypeID* args_id; UsesAllocatorType constructor_called = UA_None; - CtorAlloc allocator; + UsesAllocatorTestBaseStorage<CtorAlloc> alloc_store; }; template <class Alloc, size_t Arity> _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits