| 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 |
// { dg-additional-options "-pthread" { target pthread } } |
| 20 |
// { dg-require-gthreads "" } |
| 21 |
|
| 22 |
#include <memory_resource> |
| 23 |
#include <testsuite_allocator.h> |
| 24 |
#include <testsuite_hooks.h> |
| 25 |
|
| 26 |
void |
| 27 |
test01() |
| 28 |
{ |
| 29 |
__gnu_test::memory_resource test_mr; |
| 30 |
std::pmr::synchronized_pool_resource r(&test_mr); |
| 31 |
r.release(); |
| 32 |
VERIFY( test_mr.number_of_active_allocations() == 0 ); |
| 33 |
r.release(); |
| 34 |
VERIFY( test_mr.number_of_active_allocations() == 0 ); |
| 35 |
(void) r.allocate(1); |
| 36 |
VERIFY( test_mr.number_of_active_allocations() != 0 ); |
| 37 |
r.release(); |
| 38 |
VERIFY( test_mr.number_of_active_allocations() == 0 ); |
| 39 |
r.release(); |
| 40 |
VERIFY( test_mr.number_of_active_allocations() == 0 ); |
| 41 |
} |
| 42 |
|
| 43 |
void |
| 44 |
test02() |
| 45 |
{ |
| 46 |
struct nullable_memory_resource : public std::pmr::memory_resource |
| 47 |
{ |
| 48 |
void* |
| 49 |
do_allocate(std::size_t bytes, std::size_t alignment) override |
| 50 |
{ return upstream->allocate(bytes, alignment); } |
| 51 |
|
| 52 |
void |
| 53 |
do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override |
| 54 |
{ upstream->deallocate(p, bytes, alignment); } |
| 55 |
|
| 56 |
bool |
| 57 |
do_is_equal(const memory_resource& r) const noexcept override |
| 58 |
{ return &r == this; } |
| 59 |
|
| 60 |
std::pmr::memory_resource* upstream = std::pmr::get_default_resource(); |
| 61 |
}; |
| 62 |
|
| 63 |
nullable_memory_resource test_mr; |
| 64 |
std::pmr::synchronized_pool_resource r(&test_mr); |
| 65 |
r.release(); |
| 66 |
test_mr.upstream = nullptr; |
| 67 |
r.release(); // should not need to call anything through upstream pointer |
| 68 |
} |
| 69 |
|
| 70 |
void |
| 71 |
test03() |
| 72 |
{ |
| 73 |
__gnu_test::memory_resource test_mr; |
| 74 |
{ |
| 75 |
std::pmr::synchronized_pool_resource r(&test_mr); |
| 76 |
// Destructor calls release() |
| 77 |
} |
| 78 |
VERIFY( test_mr.number_of_active_allocations() == 0 ); |
| 79 |
{ |
| 80 |
std::pmr::synchronized_pool_resource r(&test_mr); |
| 81 |
(void) r.allocate(1); |
| 82 |
VERIFY( test_mr.number_of_active_allocations() != 0 ); |
| 83 |
// Destructor calls release() |
| 84 |
} |
| 85 |
VERIFY( test_mr.number_of_active_allocations() == 0 ); |
| 86 |
{ |
| 87 |
std::pmr::synchronized_pool_resource r({10, 16}, &test_mr); |
| 88 |
(void) r.allocate(2 * r.options().largest_required_pool_block); |
| 89 |
VERIFY( test_mr.number_of_active_allocations() != 0 ); |
| 90 |
// Destructor calls release() |
| 91 |
} |
| 92 |
VERIFY( test_mr.number_of_active_allocations() == 0 ); |
| 93 |
{ |
| 94 |
std::pmr::synchronized_pool_resource r({16, 16}, &test_mr); |
| 95 |
(void) r.allocate(2); |
| 96 |
(void) r.allocate(8); |
| 97 |
(void) r.allocate(16); |
| 98 |
(void) r.allocate(2); |
| 99 |
(void) r.allocate(8); |
| 100 |
(void) r.allocate(16); |
| 101 |
(void) r.allocate(2 * r.options().largest_required_pool_block); |
| 102 |
VERIFY( test_mr.number_of_active_allocations() != 0 ); |
| 103 |
// Destructor calls release() |
| 104 |
} |
| 105 |
VERIFY( test_mr.number_of_active_allocations() == 0 ); |
| 106 |
} |
| 107 |
|
| 108 |
int |
| 109 |
main() |
| 110 |
{ |
| 111 |
test01(); |
| 112 |
test02(); |
| 113 |
test03(); |
| 114 |
} |