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
;
112 auto max
= [](int n
, int a
) { return n
> a
? n
: a
; };
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 );