| 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++14 } } |
| 19 | // { dg-require-cstdint "" } |
| 20 | |
| 21 | #include <experimental/memory_resource> |
| 22 | #include <cstdlib> |
| 23 | #include <cstdint> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | #if (defined __sun__ || defined __VXWORKS__) && defined __i386__ |
| 27 | // See PR libstdc++/77691 |
| 28 | # define BAD_MAX_ALIGN_T 1 |
| 29 | #endif |
| 30 | #if defined __hpux__ && defined __hppa__ && defined __LP64__ |
| 31 | // Ignore inconsistent long double and malloc alignment (libstdc++/77691) |
| 32 | # define BAD_MAX_ALIGN_T 1 |
| 33 | #endif |
| 34 | |
| 35 | bool new_called = false; |
| 36 | bool delete_called = false; |
| 37 | std::size_t bytes_allocated = 0; |
| 38 | |
| 39 | void* operator new(std::size_t n) |
| 40 | { |
| 41 | new_called = true; |
| 42 | if (void* p = malloc(n)) |
| 43 | { |
| 44 | bytes_allocated += n; |
| 45 | return p; |
| 46 | } |
| 47 | throw std::bad_alloc(); |
| 48 | } |
| 49 | |
| 50 | void operator delete(void* p) noexcept |
| 51 | { |
| 52 | delete_called = true; |
| 53 | std::free(p); |
| 54 | bytes_allocated = 0; // assume everything getting deleted |
| 55 | } |
| 56 | |
| 57 | void operator delete(void* p, std::size_t n) noexcept |
| 58 | { |
| 59 | delete_called = true; |
| 60 | std::free(p); |
| 61 | bytes_allocated -= n; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | template<std::size_t A> |
| 66 | bool aligned(void* p) |
| 67 | { return (reinterpret_cast<std::uintptr_t>(p) % A) == 0; } |
| 68 | |
| 69 | template<typename T> |
| 70 | bool aligned(void* p) |
| 71 | { return aligned<alignof(T)>(p); } |
| 72 | |
| 73 | // Extended alignments: |
| 74 | constexpr std::size_t al6 = (1ul << 6), al12 = (1ul << 12), al18 = (1ul << 18); |
| 75 | |
| 76 | using std::experimental::pmr::memory_resource; |
| 77 | using std::experimental::pmr::new_delete_resource; |
| 78 | |
| 79 | memory_resource* global = nullptr; |
| 80 | |
| 81 | void |
| 82 | test01() |
| 83 | { |
| 84 | memory_resource* r1 = new_delete_resource(); |
| 85 | VERIFY( *r1 == *r1 ); |
| 86 | memory_resource* r2 = new_delete_resource(); |
| 87 | VERIFY( r1 == r2 ); |
| 88 | VERIFY( *r1 == *r2 ); |
| 89 | global = r1; |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | test02() |
| 94 | { |
| 95 | memory_resource* r1 = new_delete_resource(); |
| 96 | VERIFY( r1 == global ); |
| 97 | VERIFY( *r1 == *global ); |
| 98 | |
| 99 | new_called = false; |
| 100 | delete_called = false; |
| 101 | void* p = r1->allocate(1); |
| 102 | VERIFY( new_called ); |
| 103 | VERIFY( ! delete_called ); |
| 104 | |
| 105 | new_called = false; |
| 106 | r1->deallocate(p, 1); |
| 107 | VERIFY( ! new_called ); |
| 108 | VERIFY( delete_called ); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | test03() |
| 113 | { |
| 114 | using std::max_align_t; |
| 115 | using std::size_t; |
| 116 | void* p = nullptr; |
| 117 | |
| 118 | auto round = [](size_t n, size_t a) { return n % a ? n + a - (n % a) : n; }; |
| 119 | |
| 120 | bytes_allocated = 0; |
| 121 | |
| 122 | memory_resource* r1 = new_delete_resource(); |
| 123 | p = r1->allocate(1); // uses alignment = alignof(max_align_t) |
| 124 | #ifdef BAD_MAX_ALIGN_T |
| 125 | VERIFY( bytes_allocated != 0 ); |
| 126 | #else |
| 127 | VERIFY( bytes_allocated == alignof(max_align_t) ); |
| 128 | #endif |
| 129 | VERIFY( aligned<max_align_t>(p) ); |
| 130 | r1->deallocate(p, 1); |
| 131 | VERIFY( bytes_allocated == 0 ); |
| 132 | |
| 133 | p = r1->allocate(2, alignof(char)); |
| 134 | VERIFY( bytes_allocated == 2 ); |
| 135 | r1->deallocate(p, 2, alignof(char)); |
| 136 | VERIFY( bytes_allocated == 0 ); |
| 137 | |
| 138 | p = r1->allocate(3, alignof(short)); |
| 139 | VERIFY( bytes_allocated == round(3, alignof(short)) ); |
| 140 | VERIFY( aligned<short>(p) ); |
| 141 | r1->deallocate(p, 3, alignof(short)); |
| 142 | VERIFY( bytes_allocated == 0 ); |
| 143 | |
| 144 | p = r1->allocate(4, alignof(long)); |
| 145 | VERIFY( bytes_allocated == round(4, alignof(long)) ); |
| 146 | VERIFY( aligned<long>(p) ); |
| 147 | r1->deallocate(p, 4, alignof(long)); |
| 148 | VERIFY( bytes_allocated == 0 ); |
| 149 | |
| 150 | // Test extended aligments: |
| 151 | p = r1->allocate(777, al6); |
| 152 | VERIFY( bytes_allocated >= 777 ); |
| 153 | VERIFY( bytes_allocated < (777 + al6 + 8) ); // reasonable upper bound |
| 154 | VERIFY( aligned<al6>(p) ); |
| 155 | r1->deallocate(p, 777, al6); |
| 156 | VERIFY( bytes_allocated == 0 ); |
| 157 | |
| 158 | p = r1->allocate(888, al12); |
| 159 | VERIFY( bytes_allocated >= 888 ); |
| 160 | VERIFY( bytes_allocated < (888 + al12 + 8) ); // reasonable upper bound |
| 161 | VERIFY( aligned<al12>(p) ); |
| 162 | r1->deallocate(p, 888, al12); |
| 163 | VERIFY( bytes_allocated == 0 ); |
| 164 | |
| 165 | p = r1->allocate(999, al18); |
| 166 | VERIFY( bytes_allocated >= 999 ); |
| 167 | VERIFY( bytes_allocated < (999 + al18 + 8) ); // reasonable upper bound |
| 168 | VERIFY( aligned<al18>(p) ); |
| 169 | r1->deallocate(p, 999, al18); |
| 170 | VERIFY( bytes_allocated == 0 ); |
| 171 | } |
| 172 | |
| 173 | int main() |
| 174 | { |
| 175 | test01(); |
| 176 | test02(); |
| 177 | test03(); |
| 178 | } |