//===----------------------------------------------------------------------===// // // 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 #include #include #include #include #include #include #include "test_macros.h" #include "type_id.h" #if TEST_STD_VER < 11 #error This header requires C++11 or greater #endif struct AllocController; // 'AllocController' is a concrete type that instruments and controls the // behavior of test allocators. template class CountingAllocator; // 'CountingAllocator' is an basic implementation of the 'Allocator' // requirements that use the 'AllocController' interface. template 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{}.allocate(...)' will never a 2-byte aligned // pointer. template 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 void countConstruct(Alloc const&, Tp *p) { ++construct_called; last_construct_pointer = p; last_construct_alloc = &makeTypeID(); last_construct_type = &makeTypeID(); last_construct_args = &makeArgumentID(); } template void countDestroy(Alloc const&, Tp *p) { ++destroy_called; last_destroy_alloc = &makeTypeID(); last_destroy_type = &makeTypeID(); 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 bool checkConstruct(Alloc const&, Tp *p) const { auto expectAlloc = &makeTypeID(); auto expectTp = &makeTypeID(); auto expectArgs = &makeArgumentID(); return last_construct_pointer == p && COMPARE_TYPEID(last_construct_alloc, expectAlloc) && COMPARE_TYPEID(last_construct_type, expectTp) && COMPARE_TYPEID(last_construct_args, expectArgs); } template bool checkDestroy(Alloc const&, Tp *p) const { return last_destroy_pointer == p && last_destroy_alloc == &makeTypeID() && last_destroy_type == &makeTypeID(); } 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 CountingAllocator { public: typedef T value_type; typedef T* pointer; template struct rebind { using other = CountingAllocator; }; 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 CountingAllocator(CountingAllocator const& other) TEST_NOEXCEPT : P(other.P) { P->copy_constructed += 1; } template CountingAllocator(CountingAllocator&& 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(ret); } void deallocate(T* p, std::size_t n) { void* vp = static_cast(p); P->countDealloc(vp, n*sizeof(T), alignof(T)); ::operator delete(vp); } template void construct(U *p, Args&&... args) { ::new ((void*)p) U(std::forward(args)...); P->countConstruct(*this, p); } template void destroy(U* p) { p->~U(); P->countDestroy(*this, p); } AllocController& getController() const { return *P; } private: template friend class CountingAllocator; AllocController *P; }; template class CountingAllocator { public: typedef void* pointer; typedef const void* const_pointer; typedef void value_type; template struct rebind { using other = CountingAllocator; }; 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 CountingAllocator(CountingAllocator const& other) TEST_NOEXCEPT : P(other.P) { P->copy_constructed += 1; } template CountingAllocator(CountingAllocator&& other) TEST_NOEXCEPT : P(other.P) { P->move_constructed += 1; } void construct(...) = delete; void destroy(void*) = delete; AllocController& getController() const { return *P; } private: template friend class CountingAllocator; AllocController *P; }; template inline bool operator==(CountingAllocator const& x, CountingAllocator const& y) { return &x.getController() == &y.getController(); } template inline bool operator!=(CountingAllocator const& x, CountingAllocator const& y) { return !(x == y); } template 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 MinAlignedAllocator(MinAlignedAllocator const& other) TEST_NOEXCEPT : P(other.P) { P->copy_constructed += 1; } template MinAlignedAllocator(MinAlignedAllocator&& 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(aligned_ptr)); } template void construct(U *p, Args&&... args) { auto *c = ::new ((void*)p) U(std::forward(args)...); P->countConstruct(*this, p); } template 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(p) % BlockSize == 0; } static bool is_min_aligned(void* p) { if (alignof(T) == BlockSize) { return is_max_aligned(p); } else { return reinterpret_cast(p) % BlockSize == alignof(T); } } template friend class MinAlignedAllocator; mutable AllocController *P; }; template inline bool operator==(MinAlignedAllocator const& x, MinAlignedAllocator const& y) { return &x.getController() == &y.getController(); } template inline bool operator!=(MinAlignedAllocator const& x, MinAlignedAllocator const& y) { return !(x == y); } template 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 NullAllocator(NullAllocator const& other) TEST_NOEXCEPT : P(other.P) { P->copy_constructed += 1; } template NullAllocator(NullAllocator&& 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(p); P->countDealloc(vp, n*sizeof(T), alignof(T)); } AllocController& getController() const { return *P; } private: template friend class NullAllocator; AllocController *P; }; template inline bool operator==(NullAllocator const& x, NullAllocator const& y) { return &x.getController() == &y.getController(); } template inline bool operator!=(NullAllocator const& x, NullAllocator const& y) { return !(x == y); } #endif /* SUPPORT_CONTROLLED_ALLOCATORS_HPP */