95 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// template <class... Args> void construct(pointer p, Args&&... args);13 14// In C++20, parts of std::allocator<T> have been removed.15// In C++17, they were deprecated.16// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS17// REQUIRES: c++03 || c++11 || c++14 || c++1718 19#include <memory>20#include <cassert>21 22#include "test_macros.h"23#include "count_new.h"24 25int A_constructed = 0;26 27struct A {28 int data;29 A() { ++A_constructed; }30 31 A(const A&) { ++A_constructed; }32 33 explicit A(int) { ++A_constructed; }34 A(int, int*) { ++A_constructed; }35 36 ~A() { --A_constructed; }37};38 39int move_only_constructed = 0;40 41int main(int, char**) {42 globalMemCounter.reset();43 {44 std::allocator<A> a;45 assert(globalMemCounter.checkOutstandingNewEq(0));46 assert(A_constructed == 0);47 48 globalMemCounter.last_new_size = 0;49 A* ap = a.allocate(3);50 DoNotOptimize(ap);51 assert(globalMemCounter.checkOutstandingNewEq(1));52 assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(int)));53 assert(A_constructed == 0);54 55 a.construct(ap);56 assert(globalMemCounter.checkOutstandingNewEq(1));57 assert(A_constructed == 1);58 59 a.destroy(ap);60 assert(globalMemCounter.checkOutstandingNewEq(1));61 assert(A_constructed == 0);62 63 a.construct(ap, A());64 assert(globalMemCounter.checkOutstandingNewEq(1));65 assert(A_constructed == 1);66 67 a.destroy(ap);68 assert(globalMemCounter.checkOutstandingNewEq(1));69 assert(A_constructed == 0);70 71 a.construct(ap, 5);72 assert(globalMemCounter.checkOutstandingNewEq(1));73 assert(A_constructed == 1);74 75 a.destroy(ap);76 assert(globalMemCounter.checkOutstandingNewEq(1));77 assert(A_constructed == 0);78 79 a.construct(ap, 5, (int*)0);80 assert(globalMemCounter.checkOutstandingNewEq(1));81 assert(A_constructed == 1);82 83 a.destroy(ap);84 assert(globalMemCounter.checkOutstandingNewEq(1));85 assert(A_constructed == 0);86 87 a.deallocate(ap, 3);88 DoNotOptimize(ap);89 assert(globalMemCounter.checkOutstandingNewEq(0));90 assert(A_constructed == 0);91 }92 93 return 0;94}95