| 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::allocate_object() |
| 19 | // polymorphic_allocator::deallocate_object() |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <cassert> |
| 23 | #include <concepts> |
| 24 | #include <memory_resource> |
| 25 | |
| 26 | #include "tracking_mem_res.h" |
| 27 | |
| 28 | template <class T> |
| 29 | void 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 allocate_object<int>(); |
| 38 | std::fill(allocation, allocation + 1, 3); |
| 39 | assert(last_size == sizeof(int)); |
| 40 | assert(last_alignment == alignof(int)); |
| 41 | allocator.deallocate_object(allocation); |
| 42 | assert(last_size == sizeof(int)); |
| 43 | assert(last_alignment == alignof(int)); |
| 44 | } |
| 45 | { |
| 46 | int* allocation = allocator.template allocate_object<int>(3); |
| 47 | std::fill(first: allocation, last: allocation + 3, value: 3); |
| 48 | assert(last_size == sizeof(int) * 3); |
| 49 | assert(last_alignment == alignof(int)); |
| 50 | allocator.deallocate_object(allocation, 3); |
| 51 | assert(last_size == sizeof(int) * 3); |
| 52 | assert(last_alignment == alignof(int)); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | struct S {}; |
| 57 | |
| 58 | int main(int, char**) { |
| 59 | test<std::byte>(); |
| 60 | test<S>(); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |