93 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// <memory>10 11// allocator:12// T* allocate(size_t n, const void* hint);13 14// Removed in C++20, deprecated in C++17.15 16// REQUIRES: c++03 || c++11 || c++14 || c++1717// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS18 19#include <memory>20#include <cassert>21#include <cstddef> // for std::max_align_t22 23#include "test_macros.h"24#include "count_new.h"25 26#ifdef TEST_HAS_NO_ALIGNED_ALLOCATION27static const bool UsingAlignedNew = false;28#else29static const bool UsingAlignedNew = true;30#endif31 32#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__33static const std::size_t MaxAligned = __STDCPP_DEFAULT_NEW_ALIGNMENT__;34#else35static const std::size_t MaxAligned = std::alignment_of<std::max_align_t>::value;36#endif37 38static const std::size_t OverAligned = MaxAligned * 2;39 40template <std::size_t Align>41struct TEST_ALIGNAS(Align) AlignedType {42 char data;43 static int constructed;44 AlignedType() { ++constructed; }45 AlignedType(AlignedType const&) { ++constructed; }46 ~AlignedType() { --constructed; }47};48template <std::size_t Align>49int AlignedType<Align>::constructed = 0;50 51template <std::size_t Align>52void test_aligned() {53 typedef AlignedType<Align> T;54 T::constructed = 0;55 globalMemCounter.reset();56 std::allocator<T> a;57 const bool IsOverAlignedType = Align > MaxAligned;58 const bool ExpectAligned = IsOverAlignedType && UsingAlignedNew;59 {60 globalMemCounter.last_new_size = 0;61 globalMemCounter.last_new_align = 0;62 T* ap2 = a.allocate(11, (const void*)5);63 DoNotOptimize(ap2);64 assert(globalMemCounter.checkOutstandingNewEq(1));65 assert(globalMemCounter.checkNewCalledEq(1));66 assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned));67 assert(globalMemCounter.checkLastNewSizeEq(11 * sizeof(T)));68 assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0));69 assert(T::constructed == 0);70 globalMemCounter.last_delete_align = 0;71 a.deallocate(ap2, 11);72 DoNotOptimize(ap2);73 assert(globalMemCounter.checkOutstandingNewEq(0));74 assert(globalMemCounter.checkDeleteCalledEq(1));75 assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned));76 assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0));77 assert(T::constructed == 0);78 }79}80 81int main(int, char**) {82 test_aligned<1>();83 test_aligned<2>();84 test_aligned<4>();85 test_aligned<8>();86 test_aligned<16>();87 test_aligned<MaxAligned>();88 test_aligned<OverAligned>();89 test_aligned<OverAligned * 2>();90 91 return 0;92}93