PR libstdc++/77691 increase allocation size to at least alignment
[official-gcc.git] / libstdc++-v3 / testsuite / experimental / memory_resource / new_delete_resource.cc
blob3af3861d1a0c1b406580f0a7ed57e64b2dfa4099
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 auto max = [](int n, int a) { return n > a ? n : a; };
114 bytes_allocated = 0;
116 memory_resource* r1 = new_delete_resource();
117 p = r1->allocate(1); // uses alignment = alignof(max_align_t)
118 VERIFY( bytes_allocated <= alignof(max_align_t) );
119 VERIFY( aligned<max_align_t>(p) );
120 r1->deallocate(p, 1);
121 VERIFY( bytes_allocated == 0 );
123 p = r1->allocate(2, alignof(char));
124 VERIFY( bytes_allocated == 2 );
125 VERIFY( aligned<max_align_t>(p) );
126 r1->deallocate(p, 2);
127 VERIFY( bytes_allocated == 0 );
129 p = r1->allocate(3, alignof(short));
130 VERIFY( bytes_allocated == max(3, alignof(short)) );
131 VERIFY( aligned<short>(p) );
132 r1->deallocate(p, 3, alignof(short));
133 VERIFY( bytes_allocated == 0 );
135 p = r1->allocate(4, alignof(long));
136 VERIFY( bytes_allocated == max(4, alignof(long)) );
137 VERIFY( aligned<long>(p) );
138 r1->deallocate(p, 4, alignof(long));
139 VERIFY( bytes_allocated == 0 );
141 // Test extended aligments:
142 p = r1->allocate(777, al6);
143 VERIFY( bytes_allocated >= 777 );
144 VERIFY( bytes_allocated < (777 + al6 + 8) ); // reasonable upper bound
145 VERIFY( aligned<al6>(p) );
146 r1->deallocate(p, 777, al6);
147 VERIFY( bytes_allocated == 0 );
149 p = r1->allocate(888, al12);
150 VERIFY( bytes_allocated >= 888 );
151 VERIFY( bytes_allocated < (888 + al12 + 8) ); // reasonable upper bound
152 VERIFY( aligned<al12>(p) );
153 r1->deallocate(p, 888, al12);
154 VERIFY( bytes_allocated == 0 );
156 p = r1->allocate(999, al18);
157 VERIFY( bytes_allocated >= 999 );
158 VERIFY( bytes_allocated < (999 + al18 + 8) ); // reasonable upper bound
159 VERIFY( aligned<al18>(p) );
160 r1->deallocate(p, 999, al18);
161 VERIFY( bytes_allocated == 0 );
164 int main()
166 test01();
167 test02();
168 test03();