| 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 |
| 10 | // TODO: Change to XFAIL once https://github.com/llvm/llvm-project/issues/40340 is fixed |
| 11 | // UNSUPPORTED: availability-pmr-missing |
| 12 | |
| 13 | // <memory_resource> |
| 14 | |
| 15 | // class monotonic_buffer_resource |
| 16 | |
| 17 | #include <cassert> |
| 18 | #include <cstddef> |
| 19 | #include <memory_resource> |
| 20 | |
| 21 | #include "test_macros.h" |
| 22 | #include "count_new.h" |
| 23 | |
| 24 | int main(int, char**) { |
| 25 | globalMemCounter.reset(); |
| 26 | auto mono1 = std::pmr::monotonic_buffer_resource(1024, std::pmr::new_delete_resource()); |
| 27 | std::pmr::memory_resource& r1 = mono1; |
| 28 | |
| 29 | constexpr std::size_t big_alignment = 8 * alignof(std::max_align_t); |
| 30 | static_assert(big_alignment > 4); |
| 31 | |
| 32 | void* ret = r1.allocate(bytes: 2048, alignment: big_alignment); |
| 33 | assert(ret != nullptr); |
| 34 | ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledEq(1)); |
| 35 | ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkAlignedNewCalledEq(1)); |
| 36 | ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkLastNewSizeGe(2048)); |
| 37 | ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkLastNewAlignEq(big_alignment)); |
| 38 | |
| 39 | // Check that a single highly aligned allocation request doesn't |
| 40 | // permanently "poison" the resource to allocate only super-aligned |
| 41 | // blocks of memory. |
| 42 | ret = r1.allocate(bytes: globalMemCounter.last_new_size, alignment: 4); |
| 43 | assert(ret != nullptr); |
| 44 | ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledEq(2)); |
| 45 | ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkAlignedNewCalledEq(1)); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |