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-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17
10// TODO: Change to XFAIL once https://github.com/llvm/llvm-project/issues/40340 is fixed
11// UNSUPPORTED: availability-pmr-missing
12
13// test_memory_resource requires RTTI for dynamic_cast
14// UNSUPPORTED: no-rtti
15
16// <memory_resource>
17
18// polymorphic_allocator::new_object()
19// polymorphic_allocator::delete_object()
20
21#include <algorithm>
22#include <cassert>
23#include <concepts>
24#include <memory_resource>
25
26#include "tracking_mem_res.h"
27
28template <class T>
29void test() {
30 std::size_t last_size = 0;
31 std::size_t last_alignment = 0;
32 TrackingMemRes resource(&last_size, &last_alignment);
33
34 std::pmr::polymorphic_allocator<T> allocator(&resource);
35
36 {
37 std::same_as<int*> decltype(auto) allocation = allocator.template new_object<int>();
38 std::fill(allocation, allocation + 1, 4);
39 assert(last_size == sizeof(int));
40 assert(last_alignment == alignof(int));
41 allocator.delete_object(allocation);
42 }
43 {
44 std::same_as<int*> decltype(auto) allocation = allocator.template new_object<int>(3);
45 assert(*allocation == 3);
46 std::fill(allocation, allocation + 1, 4);
47 assert(last_size == sizeof(int));
48 assert(last_alignment == alignof(int));
49 allocator.delete_object(allocation);
50 }
51 {
52 struct TrackConstruction {
53 bool* is_constructed_;
54 TrackConstruction(bool* is_constructed) : is_constructed_(is_constructed) { *is_constructed = true; }
55 ~TrackConstruction() { *is_constructed_ = false; }
56 };
57
58 bool is_constructed = false;
59
60 std::same_as<TrackConstruction*> decltype(auto) allocation =
61 allocator.template new_object<TrackConstruction>(&is_constructed);
62 assert(is_constructed);
63 assert(last_size == sizeof(TrackConstruction));
64 assert(last_alignment == alignof(TrackConstruction));
65 allocator.delete_object(allocation);
66 assert(!is_constructed);
67 }
68}
69
70struct S {};
71
72int main(int, char**) {
73 test<std::byte>();
74 test<S>();
75
76 return 0;
77}
78

source code of libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/new_delete_object.pass.cpp