1 // Copyright (C) 2018 Free Software Foundation, Inc.
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)
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>
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
)
33 if (void* p
= malloc(n
))
38 throw std::bad_alloc();
41 void operator delete(void* p
)
45 bytes_allocated
= 0; // assume everything getting deleted
48 void operator delete(void* p
, std::size_t n
)
56 template<std::size_t A
>
59 return (reinterpret_cast<std::uintptr_t>(p
) % A
) == 0;
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;
77 memory_resource
* r1
= new_delete_resource();
79 memory_resource
* r2
= new_delete_resource();
88 memory_resource
* r1
= new_delete_resource();
89 VERIFY( r1
== global
);
90 VERIFY( *r1
== *global
);
93 delete_called
= false;
94 void* p
= r1
->allocate(1);
96 VERIFY( ! delete_called
);
100 VERIFY( ! new_called
);
101 VERIFY( delete_called
);
108 using std::max_align_t
;
114 memory_resource
* r1
= new_delete_resource();
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 );