| 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 <memory_resource> |
| 18 |
#include <cassert> |
| 19 |
|
| 20 |
#include "test_macros.h" |
| 21 |
#include "count_new.h" |
| 22 |
|
| 23 |
int main(int, char**) { |
| 24 |
globalMemCounter.reset(); |
| 25 |
auto mono1 = std::pmr::monotonic_buffer_resource(1024, std::pmr::new_delete_resource()); |
| 26 |
std::pmr::memory_resource& r1 = mono1; |
| 27 |
|
| 28 |
constexpr std::size_t big_alignment = 8 * alignof(std::max_align_t); |
| 29 |
static_assert(big_alignment > 4); |
| 30 |
|
| 31 |
void* ret = r1.allocate(2048, big_alignment); |
| 32 |
assert(ret != nullptr); |
| 33 |
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledEq(1)); |
| 34 |
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkAlignedNewCalledEq(1)); |
| 35 |
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkLastNewSizeGe(2048)); |
| 36 |
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkLastNewAlignEq(big_alignment)); |
| 37 |
|
| 38 |
// Check that a single highly aligned allocation request doesn't |
| 39 |
// permanently "poison" the resource to allocate only super-aligned |
| 40 |
// blocks of memory. |
| 41 |
ret = r1.allocate(globalMemCounter.last_new_size, 4); |
| 42 |
assert(ret != nullptr); |
| 43 |
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledEq(2)); |
| 44 |
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkAlignedNewCalledEq(1)); |
| 45 |
|
| 46 |
return 0; |
| 47 |
} |