| 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 | // REQUIRES: std-at-least-c++17 |
| 10 | // UNSUPPORTED: availability-pmr-missing |
| 11 | |
| 12 | // <memory_resource> |
| 13 | |
| 14 | // class monotonic_buffer_resource |
| 15 | |
| 16 | // This test checks the behavior required by LWG3120. |
| 17 | |
| 18 | #include <memory_resource> |
| 19 | #include <cassert> |
| 20 | |
| 21 | #include "count_new.h" |
| 22 | #include "test_macros.h" |
| 23 | |
| 24 | int main(int, char**) { |
| 25 | { |
| 26 | { |
| 27 | // When ctor with a next buffer size. After release(), check whether the next buffer size has been reset after release() |
| 28 | constexpr size_t expect_next_buffer_size = 512; |
| 29 | std::pmr::monotonic_buffer_resource mr{nullptr, expect_next_buffer_size, std::pmr::new_delete_resource()}; |
| 30 | |
| 31 | for (int i = 0; i < 100; ++i) { |
| 32 | (void)mr.allocate(bytes: 1); |
| 33 | mr.release(); |
| 34 | ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkLastNewSizeGe(expect_next_buffer_size)); |
| 35 | } |
| 36 | } |
| 37 | { |
| 38 | // Check whether the offset of the initial buffer has been reset after release() |
| 39 | constexpr size_t buffer_size = 100; |
| 40 | char buffer[buffer_size]; |
| 41 | std::pmr::monotonic_buffer_resource mr{buffer, buffer_size, std::pmr::null_memory_resource()}; |
| 42 | |
| 43 | mr.release(); |
| 44 | auto expect_mem_start = mr.allocate(bytes: 60); |
| 45 | mr.release(); |
| 46 | auto ths_mem_start = mr.allocate(bytes: 60); |
| 47 | assert(expect_mem_start == ths_mem_start); |
| 48 | } |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |