[24/46] Make stmt_info_for_cost use a stmt_vec_info
[official-gcc.git] / libstdc++-v3 / testsuite / experimental / memory_resource / new_delete_resource.cc
blob11667b1d138f0450e05c127966f8a0f12fcb0b66
1 // Copyright (C) 2018 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.
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.
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/>.
18 // { dg-do run { target c++14 } }
19 // { dg-require-cstdint "" }
20 // { dg-xfail-run-if "PR libstdc++/77691" { { i?86-*-solaris2.* x86_64-*-solaris2.* } && ilp32 } }
22 #include <experimental/memory_resource>
23 #include <cstdlib>
24 #include <testsuite_hooks.h>
26 bool new_called = false;
27 bool delete_called = false;
28 std::size_t bytes_allocated = 0;
30 void* operator new(std::size_t n)
32 new_called = true;
33 if (void* p = malloc(n))
35 bytes_allocated += n;
36 return p;
38 throw std::bad_alloc();
41 void operator delete(void* p)
43 delete_called = true;
44 std::free(p);
45 bytes_allocated = 0; // assume everything getting deleted
48 void operator delete(void* p, std::size_t n)
50 delete_called = true;
51 std::free(p);
52 bytes_allocated -= n;
56 template<std::size_t A>
57 bool aligned(void* p)
59 return (reinterpret_cast<std::uintptr_t>(p) % A) == 0;
62 template<typename T>
63 bool aligned(void* p)
64 { return aligned<alignof(T)>(p); }
66 // Extended alignments:
67 constexpr std::size_t al6 = (1ul << 6), al12 = (1ul << 12), al18 = (1ul << 18);
69 using std::experimental::pmr::memory_resource;
70 using std::experimental::pmr::new_delete_resource;
72 memory_resource* global = nullptr;
74 void
75 test01()
77 memory_resource* r1 = new_delete_resource();
78 VERIFY( *r1 == *r1 );
79 memory_resource* r2 = new_delete_resource();
80 VERIFY( r1 == r2 );
81 VERIFY( *r1 == *r2 );
82 global = r1;
85 void
86 test02()
88 memory_resource* r1 = new_delete_resource();
89 VERIFY( r1 == global );
90 VERIFY( *r1 == *global );
92 new_called = false;
93 delete_called = false;
94 void* p = r1->allocate(1);
95 VERIFY( new_called );
96 VERIFY( ! delete_called );
98 new_called = false;
99 r1->deallocate(p, 1);
100 VERIFY( ! new_called );
101 VERIFY( delete_called );
104 void
105 test03()
108 using std::max_align_t;
109 using std::size_t;
110 void* p = nullptr;
112 bytes_allocated = 0;
114 memory_resource* r1 = new_delete_resource();
115 p = r1->allocate(1);
116 VERIFY( bytes_allocated == 1 );
117 VERIFY( aligned<max_align_t>(p) );
118 r1->deallocate(p, 1);
119 VERIFY( bytes_allocated == 0 );
121 p = r1->allocate(2, alignof(char));
122 VERIFY( bytes_allocated == 2 );
123 VERIFY( aligned<max_align_t>(p) );
124 r1->deallocate(p, 2);
125 VERIFY( bytes_allocated == 0 );
127 p = r1->allocate(3, alignof(short));
128 VERIFY( bytes_allocated == 3 );
129 VERIFY( aligned<short>(p) );
130 r1->deallocate(p, 3, alignof(short));
131 VERIFY( bytes_allocated == 0 );
133 p = r1->allocate(4, alignof(long));
134 VERIFY( bytes_allocated == 4 );
135 VERIFY( aligned<long>(p) );
136 r1->deallocate(p, 4, alignof(long));
137 VERIFY( bytes_allocated == 0 );
139 // Test extended aligments:
140 p = r1->allocate(777, al6);
141 VERIFY( bytes_allocated >= 777 );
142 VERIFY( bytes_allocated < (777 + al6 + 8) ); // reasonable upper bound
143 VERIFY( aligned<al6>(p) );
144 r1->deallocate(p, 777, al6);
145 VERIFY( bytes_allocated == 0 );
147 p = r1->allocate(888, al12);
148 VERIFY( bytes_allocated >= 888 );
149 VERIFY( bytes_allocated < (888 + al12 + 8) ); // reasonable upper bound
150 VERIFY( aligned<al12>(p) );
151 r1->deallocate(p, 888, al12);
152 VERIFY( bytes_allocated == 0 );
154 p = r1->allocate(999, al18);
155 VERIFY( bytes_allocated >= 999 );
156 VERIFY( bytes_allocated < (999 + al18 + 8) ); // reasonable upper bound
157 VERIFY( aligned<al18>(p) );
158 r1->deallocate(p, 999, al18);
159 VERIFY( bytes_allocated == 0 );
162 int main()
164 test01();
165 test02();
166 test03();