| 1 |
// Copyright (C) 2018-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++17 } } |
| 19 |
|
| 20 |
#include <memory_resource> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
#include <testsuite_allocator.h> |
| 23 |
|
| 24 |
bool eq(const std::pmr::pool_options& lhs, const std::pmr::pool_options& rhs) |
| 25 |
{ |
| 26 |
return lhs.max_blocks_per_chunk == rhs.max_blocks_per_chunk |
| 27 |
&& lhs.largest_required_pool_block == rhs.largest_required_pool_block; |
| 28 |
} |
| 29 |
|
| 30 |
void |
| 31 |
test01() |
| 32 |
{ |
| 33 |
std::pmr::unsynchronized_pool_resource r0; |
| 34 |
const std::pmr::pool_options opts = r0.options(); |
| 35 |
VERIFY( opts.max_blocks_per_chunk != 0 ); |
| 36 |
VERIFY( opts.largest_required_pool_block != 0 ); |
| 37 |
|
| 38 |
std::pmr::unsynchronized_pool_resource r1(opts); |
| 39 |
const auto opts1 = r1.options(); |
| 40 |
VERIFY( eq(opts, opts1) ); |
| 41 |
|
| 42 |
std::pmr::unsynchronized_pool_resource r2(std::pmr::pool_options{0, 0}); |
| 43 |
const auto opts2 = r2.options(); |
| 44 |
VERIFY( eq(opts, opts2) ); |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
test02() |
| 49 |
{ |
| 50 |
std::pmr::pool_options opts{0, 0}; |
| 51 |
std::size_t num_allocs = 0; |
| 52 |
|
| 53 |
__gnu_test::memory_resource test_mr; |
| 54 |
|
| 55 |
std::pmr::unsynchronized_pool_resource r1(opts, &test_mr); |
| 56 |
opts = r1.options(); |
| 57 |
// opts.largest_required_pool_block should be set to the block size of |
| 58 |
// the largest pool (this is a GNU extension). Confirm this by checking |
| 59 |
// that allocations larger than opts.largest_required_pool_block come |
| 60 |
// directly from the upstream allocator, test_mr, not from r1's pools. |
| 61 |
|
| 62 |
// The following should result in a "large" allocation direct from upstream: |
| 63 |
(void) r1.allocate(opts.largest_required_pool_block + 1); |
| 64 |
num_allocs = test_mr.number_of_active_allocations(); |
| 65 |
// This should result in another "large" allocation direct from upstream: |
| 66 |
(void) r1.allocate(opts.largest_required_pool_block + 1); |
| 67 |
// Which means the number of upstream allocations should have increased: |
| 68 |
VERIFY( test_mr.number_of_active_allocations() > num_allocs ); |
| 69 |
r1.release(); |
| 70 |
|
| 71 |
// Repeat with a user-specified block size: |
| 72 |
opts.largest_required_pool_block = 64; |
| 73 |
std::pmr::unsynchronized_pool_resource r2(opts, &test_mr); |
| 74 |
opts = r2.options(); |
| 75 |
(void) r2.allocate(opts.largest_required_pool_block + 1); |
| 76 |
num_allocs = test_mr.number_of_active_allocations(); |
| 77 |
(void) r2.allocate(opts.largest_required_pool_block + 1); |
| 78 |
VERIFY( test_mr.number_of_active_allocations() > num_allocs ); |
| 79 |
r2.release(); |
| 80 |
|
| 81 |
// Repeat with an odd user-specified block size: |
| 82 |
opts.largest_required_pool_block = 71; |
| 83 |
std::pmr::unsynchronized_pool_resource r3(opts, &test_mr); |
| 84 |
opts = r3.options(); |
| 85 |
(void) r3.allocate(opts.largest_required_pool_block + 1); |
| 86 |
num_allocs = test_mr.number_of_active_allocations(); |
| 87 |
(void) r3.allocate(opts.largest_required_pool_block + 1); |
| 88 |
VERIFY( test_mr.number_of_active_allocations() > num_allocs ); |
| 89 |
r3.release(); |
| 90 |
} |
| 91 |
|
| 92 |
int |
| 93 |
main() |
| 94 |
{ |
| 95 |
test01(); |
| 96 |
test02(); |
| 97 |
} |