Refactor uses_allocator test types for upcoming fixes
authorEric Fiselier <eric@efcs.ca>
Fri, 9 Dec 2016 09:51:09 +0000 (09:51 +0000)
committerEric Fiselier <eric@efcs.ca>
Fri, 9 Dec 2016 09:51:09 +0000 (09:51 +0000)
llvm-svn: 289197

libcxx/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_const_lvalue_pair.pass.cpp
libcxx/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_rvalue.pass.cpp
libcxx/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_pair_values.pass.cpp
libcxx/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_piecewise_pair.pass.cpp
libcxx/test/std/experimental/memory/memory.polymorphic.allocator.class/memory.polymorphic.allocator.mem/construct_types.pass.cpp
libcxx/test/support/controlled_allocators.hpp [new file with mode: 0644]
libcxx/test/support/test_memory_resource.hpp
libcxx/test/support/type_id.h
libcxx/test/support/uses_alloc_types.hpp

diff --git a/libcxx/test/support/controlled_allocators.hpp b/libcxx/test/support/controlled_allocators.hpp
new file mode 100644 (file)
index 0000000..4d5e74a
--- /dev/null
@@ -0,0 +1,498 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SUPPORT_CONTROLLED_ALLOCATORS_HPP
+#define SUPPORT_CONTROLLED_ALLOCATORS_HPP
+
+#include <memory>
+#include <type_traits>
+#include <cstddef>
+#include <cstdlib>
+#include <cstring>
+#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, size_t ID = 0>
+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;
+
+    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) {
+        ++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;
+    }
+
+    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 &&
+               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;
+    }
+
+    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;
+    }
+
+    bool checkIsEqualCalledEq(int n) const {
+        return is_equal_count == n;
+    }
+private:
+  DISALLOW_COPY(AllocController);
+};
+
+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) {}
+
+    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;
+    }
+
+    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);
+    }
+
+    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, size_t XID> friend class CountingAllocator;
+    AllocController *P;
+};
+
+
+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, size_t ID>
+inline bool operator!=(CountingAllocator<T, ID> const& x,
+                       CountingAllocator<U, ID> 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));
+    }
+
+    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:
+    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;
+};
+
+
+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);
+}
+
+
+#endif /* SUPPORT_CONTROLLED_ALLOCATORS_HPP */
index e7b067c..49dc173 100644 (file)
@@ -11,6 +11,7 @@
 #define SUPPORT_TEST_MEMORY_RESOURCE_HPP
 
 #include <experimental/memory_resource>
+#include <experimental/utility>
 #include <memory>
 #include <type_traits>
 #include <cstddef>
 #include <cstdint>
 #include <cassert>
 #include "test_macros.h"
-
-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);
+#include "controlled_allocators.hpp"
+#include "uses_alloc_types.hpp"
+
+// 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 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;
-};
-
-
-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
 {
index 309f088..0463365 100644 (file)
 #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
index 5435c69..6338ca4 100644 (file)
 #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 = UsesAllocatorType::UA_None;
 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_if<Value, bool>::type;
 
 } // 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>