|
| 1 | +// -*- C++ -*- |
| 2 | +//===----------------------------------------------------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +// UNSUPPORTED: c++03, c++11, c++14 |
| 11 | +// UNSUPPORTED: availability-pmr-missing |
| 12 | + |
| 13 | +// <memory_resource> |
| 14 | + |
| 15 | +// class monotonic_buffer_resource |
| 16 | + |
| 17 | +#include <memory_resource> |
| 18 | +#include <cassert> |
| 19 | + |
| 20 | +#include "count_new.h" |
| 21 | +#include "test_macros.h" |
| 22 | + |
| 23 | +int main(int, char**) { |
| 24 | + { |
| 25 | + // https://cplusplus.github.io/LWG/issue3120 |
| 26 | + { |
| 27 | + // when init given a next buffer size, after release(), reset/not change next buffer size from initial state |
| 28 | + constexpr auto expect_next_buffer_size{512ULL}; |
| 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 | + std::ignore = mr.allocate(1); |
| 33 | + mr.release(); |
| 34 | + ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkLastNewSizeGe(expect_next_buffer_size)); |
| 35 | + } |
| 36 | + } |
| 37 | + { |
| 38 | + // Given |
| 39 | + // when init given a buffer, after release(), initial ptr will reset to it's initial state |
| 40 | + constexpr auto buffer_size{512ULL}; |
| 41 | + char buffer[buffer_size]; |
| 42 | + std::pmr::monotonic_buffer_resource mr{buffer, buffer_size, std::pmr::null_memory_resource()}; |
| 43 | + |
| 44 | + auto expect_mem_start = mr.allocate(1); |
| 45 | + for (int i = 0; i < 100; ++i) { |
| 46 | + mr.release(); |
| 47 | + auto ths_mem_start = mr.allocate(1); |
| 48 | + assert(expect_mem_start == ths_mem_start); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments