//===----------------------------------------------------------------------===// // // 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. // //===----------------------------------------------------------------------===// // // allocator: // pointer allocate(size_type n, allocator::const_pointer hint=0); #include #include #include "test_macros.h" #include "count_new.hpp" #ifdef TEST_HAS_NO_ALIGNED_ALLOCATION static const bool UsingAlignedNew = false; #else static const bool UsingAlignedNew = true; #endif #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ static const size_t MaxAligned = __STDCPP_DEFAULT_NEW_ALIGNMENT__; #else static const size_t MaxAligned = std::alignment_of::value; #endif static const size_t OverAligned = MaxAligned * 2; template struct TEST_ALIGNAS(Align) AlignedType { char data; static int constructed; AlignedType() { ++constructed; } AlignedType(AlignedType const&) { ++constructed; } ~AlignedType() { --constructed; } }; template int AlignedType::constructed = 0; template void test_aligned() { typedef AlignedType T; T::constructed = 0; globalMemCounter.reset(); std::allocator a; const bool IsOverAlignedType = Align > MaxAligned; const bool ExpectAligned = IsOverAlignedType && UsingAlignedNew; { assert(globalMemCounter.checkOutstandingNewEq(0)); assert(T::constructed == 0); globalMemCounter.last_new_size = 0; globalMemCounter.last_new_align = 0; T* volatile ap = a.allocate(3); assert(globalMemCounter.checkOutstandingNewEq(1)); assert(globalMemCounter.checkNewCalledEq(1)); assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned)); assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(T))); assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0)); assert(T::constructed == 0); globalMemCounter.last_delete_align = 0; a.deallocate(ap, 3); assert(globalMemCounter.checkOutstandingNewEq(0)); assert(globalMemCounter.checkDeleteCalledEq(1)); assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned)); assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0)); assert(T::constructed == 0); } globalMemCounter.reset(); { globalMemCounter.last_new_size = 0; globalMemCounter.last_new_align = 0; T* volatile ap2 = a.allocate(11, (const void*)5); assert(globalMemCounter.checkOutstandingNewEq(1)); assert(globalMemCounter.checkNewCalledEq(1)); assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned)); assert(globalMemCounter.checkLastNewSizeEq(11 * sizeof(T))); assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0)); assert(T::constructed == 0); globalMemCounter.last_delete_align = 0; a.deallocate(ap2, 11); assert(globalMemCounter.checkOutstandingNewEq(0)); assert(globalMemCounter.checkDeleteCalledEq(1)); assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned)); assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0)); assert(T::constructed == 0); } } int main() { test_aligned<1>(); test_aligned<2>(); test_aligned<4>(); test_aligned<8>(); test_aligned<16>(); test_aligned(); test_aligned(); test_aligned(); }