]>
Commit | Line | Data |
---|---|---|
bfeffbd1 FY |
1 | // <experimental/memory_resource> -*- C++ -*- |
2 | ||
8d9254fc | 3 | // Copyright (C) 2015-2020 Free Software Foundation, Inc. |
bfeffbd1 FY |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 3, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // Under Section 7 of GPL version 3, you are granted additional | |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
24 | ||
25 | /** @file experimental/memory_resource | |
26 | * This is a TS C++ Library header. | |
1ababc8b | 27 | * @ingroup libfund-ts |
bfeffbd1 FY |
28 | */ |
29 | ||
30 | #ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE | |
31 | #define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1 | |
32 | ||
52ea1caf JW |
33 | #pragma GCC system_header |
34 | ||
35 | #if __cplusplus >= 201402L | |
36 | ||
7a4be380 JW |
37 | #include <memory> // align, uses_allocator, __uses_alloc |
38 | #include <experimental/utility> // pair, experimental::erased_type | |
39 | #include <atomic> // atomic | |
16d0ab7f JW |
40 | #include <new> // placement new |
41 | #include <cstddef> // max_align_t | |
7956c508 | 42 | #include <ext/new_allocator.h> |
7a4be380 | 43 | #include <debug/assertions.h> |
bfeffbd1 | 44 | |
1ababc8b | 45 | /// @cond |
2d6c8eea JW |
46 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
47 | { | |
48 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
49 | template<typename _Tp> class malloc_allocator; | |
50 | _GLIBCXX_END_NAMESPACE_VERSION | |
51 | } // namespace __gnu_cxx | |
1ababc8b | 52 | /// @endcond |
2d6c8eea | 53 | |
bfeffbd1 | 54 | namespace std { |
4a15d842 FD |
55 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
56 | ||
bfeffbd1 FY |
57 | namespace experimental { |
58 | inline namespace fundamentals_v2 { | |
59 | namespace pmr { | |
80144045 JW |
60 | #define __cpp_lib_experimental_memory_resources 201402L |
61 | ||
7a4be380 JW |
62 | // Standard memory resources |
63 | ||
64 | // 8.5 Class memory_resource | |
bfeffbd1 FY |
65 | class memory_resource; |
66 | ||
7a4be380 JW |
67 | // 8.6 Class template polymorphic_allocator |
68 | template<typename _Tp> | |
bfeffbd1 FY |
69 | class polymorphic_allocator; |
70 | ||
7a4be380 | 71 | template<typename _Alloc, typename _Resource = memory_resource> |
bfeffbd1 FY |
72 | class __resource_adaptor_imp; |
73 | ||
7a4be380 JW |
74 | // 8.7 Alias template resource_adaptor |
75 | template<typename _Alloc> | |
bfeffbd1 FY |
76 | using resource_adaptor = __resource_adaptor_imp< |
77 | typename allocator_traits<_Alloc>::template rebind_alloc<char>>; | |
78 | ||
7a4be380 | 79 | // 8.8 Global memory resources |
bfeffbd1 FY |
80 | memory_resource* new_delete_resource() noexcept; |
81 | memory_resource* null_memory_resource() noexcept; | |
bfeffbd1 FY |
82 | memory_resource* get_default_resource() noexcept; |
83 | memory_resource* set_default_resource(memory_resource* __r) noexcept; | |
84 | ||
7a4be380 | 85 | // TODO 8.9 Pool resource classes |
bfeffbd1 | 86 | |
bfeffbd1 FY |
87 | class memory_resource |
88 | { | |
89 | static constexpr size_t _S_max_align = alignof(max_align_t); | |
90 | ||
91 | public: | |
7a4be380 JW |
92 | memory_resource() = default; |
93 | memory_resource(const memory_resource&) = default; | |
94 | virtual ~memory_resource() = default; | |
95 | ||
96 | memory_resource& operator=(const memory_resource&) = default; | |
bfeffbd1 | 97 | |
d715f554 | 98 | _GLIBCXX_NODISCARD void* |
bfeffbd1 FY |
99 | allocate(size_t __bytes, size_t __alignment = _S_max_align) |
100 | { return do_allocate(__bytes, __alignment); } | |
101 | ||
102 | void | |
103 | deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align) | |
104 | { return do_deallocate(__p, __bytes, __alignment); } | |
105 | ||
106 | bool | |
107 | is_equal(const memory_resource& __other) const noexcept | |
108 | { return do_is_equal(__other); } | |
109 | ||
110 | protected: | |
111 | virtual void* | |
112 | do_allocate(size_t __bytes, size_t __alignment) = 0; | |
113 | ||
114 | virtual void | |
115 | do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0; | |
116 | ||
117 | virtual bool | |
118 | do_is_equal(const memory_resource& __other) const noexcept = 0; | |
119 | }; | |
120 | ||
121 | inline bool | |
7a4be380 | 122 | operator==(const memory_resource& __a, const memory_resource& __b) noexcept |
bfeffbd1 FY |
123 | { return &__a == &__b || __a.is_equal(__b); } |
124 | ||
125 | inline bool | |
7a4be380 | 126 | operator!=(const memory_resource& __a, const memory_resource& __b) noexcept |
bfeffbd1 FY |
127 | { return !(__a == __b); } |
128 | ||
129 | ||
7a4be380 | 130 | template<typename _Tp> |
bfeffbd1 FY |
131 | class polymorphic_allocator |
132 | { | |
bfeffbd1 FY |
133 | public: |
134 | using value_type = _Tp; | |
135 | ||
136 | polymorphic_allocator() noexcept | |
137 | : _M_resource(get_default_resource()) | |
138 | { } | |
139 | ||
140 | polymorphic_allocator(memory_resource* __r) | |
141 | : _M_resource(__r) | |
142 | { _GLIBCXX_DEBUG_ASSERT(__r); } | |
143 | ||
144 | polymorphic_allocator(const polymorphic_allocator& __other) = default; | |
145 | ||
146 | template <typename _Up> | |
147 | polymorphic_allocator(const polymorphic_allocator<_Up>& | |
148 | __other) noexcept | |
149 | : _M_resource(__other.resource()) | |
150 | { } | |
151 | ||
152 | polymorphic_allocator& | |
153 | operator=(const polymorphic_allocator& __rhs) = default; | |
154 | ||
d715f554 | 155 | _GLIBCXX_NODISCARD _Tp* allocate(size_t __n) |
bfeffbd1 FY |
156 | { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp), |
157 | alignof(_Tp))); } | |
158 | ||
7a4be380 JW |
159 | void |
160 | deallocate(_Tp* __p, size_t __n) | |
bfeffbd1 FY |
161 | { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); } |
162 | ||
163 | template <typename _Tp1, typename... _Args> //used here | |
7a4be380 JW |
164 | void |
165 | construct(_Tp1* __p, _Args&&... __args) | |
bfeffbd1 | 166 | { |
b479fbad JW |
167 | std::__uses_allocator_construct(this->resource(), __p, |
168 | std::forward<_Args>(__args)...); | |
bfeffbd1 FY |
169 | } |
170 | ||
171 | // Specializations for pair using piecewise construction | |
172 | template <typename _Tp1, typename _Tp2, | |
173 | typename... _Args1, typename... _Args2> | |
7a4be380 JW |
174 | void |
175 | construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t, | |
176 | tuple<_Args1...> __x, tuple<_Args2...> __y) | |
bfeffbd1 | 177 | { |
318c48e3 | 178 | memory_resource* const __resource = this->resource(); |
bfeffbd1 | 179 | auto __x_use_tag = |
b479fbad | 180 | std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource); |
bfeffbd1 | 181 | auto __y_use_tag = |
b479fbad | 182 | std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource); |
bfeffbd1 FY |
183 | |
184 | ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct, | |
185 | _M_construct_p(__x_use_tag, __x), | |
186 | _M_construct_p(__y_use_tag, __y)); | |
187 | } | |
188 | ||
189 | template <typename _Tp1, typename _Tp2> | |
7a4be380 JW |
190 | void |
191 | construct(pair<_Tp1,_Tp2>* __p) | |
bfeffbd1 FY |
192 | { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); } |
193 | ||
194 | template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp> | |
7a4be380 JW |
195 | void |
196 | construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y) | |
197 | { | |
198 | this->construct(__p, piecewise_construct, | |
bfeffbd1 | 199 | forward_as_tuple(std::forward<_Up>(__x)), |
7a4be380 JW |
200 | forward_as_tuple(std::forward<_Vp>(__y))); |
201 | } | |
bfeffbd1 FY |
202 | |
203 | template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp> | |
7a4be380 JW |
204 | void |
205 | construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr) | |
206 | { | |
207 | this->construct(__p, piecewise_construct, | |
208 | forward_as_tuple(__pr.first), | |
209 | forward_as_tuple(__pr.second)); | |
210 | } | |
bfeffbd1 FY |
211 | |
212 | template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp> | |
7a4be380 JW |
213 | void |
214 | construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr) | |
215 | { | |
216 | this->construct(__p, piecewise_construct, | |
bfeffbd1 | 217 | forward_as_tuple(std::forward<_Up>(__pr.first)), |
7a4be380 JW |
218 | forward_as_tuple(std::forward<_Vp>(__pr.second))); |
219 | } | |
bfeffbd1 FY |
220 | |
221 | template <typename _Up> | |
7a4be380 JW |
222 | void |
223 | destroy(_Up* __p) | |
bfeffbd1 FY |
224 | { __p->~_Up(); } |
225 | ||
226 | // Return a default-constructed allocator (no allocator propagation) | |
7a4be380 JW |
227 | polymorphic_allocator |
228 | select_on_container_copy_construction() const | |
bfeffbd1 FY |
229 | { return polymorphic_allocator(); } |
230 | ||
7a4be380 | 231 | memory_resource* resource() const { return _M_resource; } |
bfeffbd1 FY |
232 | |
233 | private: | |
b479fbad JW |
234 | using __uses_alloc1_ = __uses_alloc1<memory_resource*>; |
235 | using __uses_alloc2_ = __uses_alloc2<memory_resource*>; | |
236 | ||
bfeffbd1 FY |
237 | template<typename _Tuple> |
238 | _Tuple&& | |
239 | _M_construct_p(__uses_alloc0, _Tuple& __t) | |
240 | { return std::move(__t); } | |
241 | ||
242 | template<typename... _Args> | |
243 | decltype(auto) | |
244 | _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t) | |
245 | { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)), | |
246 | std::move(__t)); } | |
247 | ||
248 | template<typename... _Args> | |
249 | decltype(auto) | |
250 | _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t) | |
251 | { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); } | |
252 | ||
253 | memory_resource* _M_resource; | |
254 | }; | |
255 | ||
256 | template <class _Tp1, class _Tp2> | |
7a4be380 JW |
257 | bool |
258 | operator==(const polymorphic_allocator<_Tp1>& __a, | |
259 | const polymorphic_allocator<_Tp2>& __b) noexcept | |
bfeffbd1 FY |
260 | { return *__a.resource() == *__b.resource(); } |
261 | ||
262 | template <class _Tp1, class _Tp2> | |
7a4be380 JW |
263 | bool |
264 | operator!=(const polymorphic_allocator<_Tp1>& __a, | |
265 | const polymorphic_allocator<_Tp2>& __b) noexcept | |
bfeffbd1 FY |
266 | { return !(__a == __b); } |
267 | ||
7a4be380 | 268 | |
1ababc8b | 269 | /// @cond undocumented |
7956c508 JW |
270 | class __resource_adaptor_common |
271 | { | |
7a4be380 | 272 | template<typename, typename> friend class __resource_adaptor_imp; |
7956c508 JW |
273 | |
274 | struct _AlignMgr | |
275 | { | |
276 | _AlignMgr(size_t __nbytes, size_t __align) | |
277 | : _M_nbytes(__nbytes), _M_align(__align) | |
278 | { } | |
279 | ||
280 | // Total size that needs to be allocated. | |
281 | size_t | |
282 | _M_alloc_size() const { return _M_buf_size() + _M_token_size(); } | |
283 | ||
284 | void* | |
285 | _M_adjust(void* __ptr) const | |
286 | { | |
287 | const auto __orig_ptr = static_cast<char*>(__ptr); | |
288 | size_t __space = _M_buf_size(); | |
289 | // Align the pointer within the buffer: | |
290 | std::align(_M_align, _M_nbytes, __ptr, __space); | |
291 | const auto __aligned_ptr = static_cast<char*>(__ptr); | |
292 | const auto __token_size = _M_token_size(); | |
293 | // Store token immediately after the aligned block: | |
294 | char* const __end = __aligned_ptr + _M_nbytes; | |
295 | if (__token_size == 1) | |
296 | _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr); | |
297 | else if (__token_size == sizeof(short)) | |
298 | _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr); | |
299 | else if (__token_size == sizeof(int) && sizeof(int) < sizeof(char*)) | |
300 | _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr); | |
301 | else // (__token_size == sizeof(char*)) | |
302 | // Just store the original pointer: | |
303 | _S_write<char*>(__end, __orig_ptr); | |
304 | return __aligned_ptr; | |
305 | } | |
306 | ||
307 | char* | |
308 | _M_unadjust(char* __ptr) const | |
309 | { | |
310 | const char* const __end = __ptr + _M_nbytes; | |
311 | char* __orig_ptr; | |
312 | const auto __token_size = _M_token_size(); | |
313 | // Read the token and restore the original pointer: | |
314 | if (__token_size == 1) | |
315 | __orig_ptr = __ptr - _S_read<unsigned char>(__end); | |
316 | else if (__token_size == sizeof(short)) | |
317 | __orig_ptr = __ptr - _S_read<unsigned short>(__end); | |
318 | else if (__token_size == sizeof(int) | |
319 | && sizeof(int) < sizeof(char*)) | |
320 | __orig_ptr = __ptr - _S_read<unsigned int>(__end); | |
321 | else // (__token_size == sizeof(char*)) | |
322 | __orig_ptr = _S_read<char*>(__end); | |
2d6c8eea JW |
323 | // The adjustment is always less than the requested alignment, |
324 | // so if that isn't true now then either the wrong size was passed | |
325 | // to deallocate or the token was overwritten by a buffer overflow: | |
326 | __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align); | |
7956c508 JW |
327 | return __orig_ptr; |
328 | } | |
329 | ||
330 | private: | |
331 | size_t _M_nbytes; | |
332 | size_t _M_align; | |
333 | ||
334 | // Number of bytes needed to fit block of given size and alignment. | |
335 | size_t | |
336 | _M_buf_size() const { return _M_nbytes + _M_align - 1; } | |
337 | ||
338 | // Number of additional bytes needed to write the token. | |
339 | int | |
340 | _M_token_size() const | |
341 | { | |
342 | if (_M_align <= (1ul << __CHAR_BIT__)) | |
343 | return 1; | |
344 | if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__))) | |
345 | return sizeof(short); | |
0b4bc9a1 | 346 | if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__))) |
7956c508 JW |
347 | return sizeof(int); |
348 | return sizeof(char*); | |
349 | } | |
350 | ||
351 | template<typename _Tp> | |
352 | static void | |
353 | _S_write(void* __to, _Tp __val) | |
354 | { __builtin_memcpy(__to, &__val, sizeof(_Tp)); } | |
355 | ||
356 | template<typename _Tp> | |
357 | static _Tp | |
358 | _S_read(const void* __from) | |
359 | { | |
360 | _Tp __val; | |
361 | __builtin_memcpy(&__val, __from, sizeof(_Tp)); | |
362 | return __val; | |
363 | } | |
364 | }; | |
365 | }; | |
1ababc8b | 366 | /// @endcond |
7956c508 | 367 | |
bfeffbd1 | 368 | // 8.7.1 __resource_adaptor_imp |
7a4be380 | 369 | template<typename _Alloc, typename _Resource> |
7956c508 | 370 | class __resource_adaptor_imp |
7a4be380 | 371 | : public _Resource, private __resource_adaptor_common |
bfeffbd1 | 372 | { |
7a4be380 JW |
373 | using memory_resource = _Resource; |
374 | ||
e70359b3 JW |
375 | static_assert(is_same<char, |
376 | typename allocator_traits<_Alloc>::value_type>::value, | |
377 | "Allocator's value_type is char"); | |
378 | static_assert(is_same<char*, | |
379 | typename allocator_traits<_Alloc>::pointer>::value, | |
380 | "Allocator's pointer type is value_type*"); | |
381 | static_assert(is_same<const char*, | |
382 | typename allocator_traits<_Alloc>::const_pointer>::value, | |
383 | "Allocator's const_pointer type is value_type const*"); | |
384 | static_assert(is_same<void*, | |
385 | typename allocator_traits<_Alloc>::void_pointer>::value, | |
386 | "Allocator's void_pointer type is void*"); | |
387 | static_assert(is_same<const void*, | |
388 | typename allocator_traits<_Alloc>::const_void_pointer>::value, | |
389 | "Allocator's const_void_pointer type is void const*"); | |
390 | ||
bfeffbd1 FY |
391 | public: |
392 | using allocator_type = _Alloc; | |
393 | ||
394 | __resource_adaptor_imp() = default; | |
395 | __resource_adaptor_imp(const __resource_adaptor_imp&) = default; | |
396 | __resource_adaptor_imp(__resource_adaptor_imp&&) = default; | |
397 | ||
398 | explicit __resource_adaptor_imp(const _Alloc& __a2) | |
399 | : _M_alloc(__a2) | |
400 | { } | |
401 | ||
402 | explicit __resource_adaptor_imp(_Alloc&& __a2) | |
403 | : _M_alloc(std::move(__a2)) | |
404 | { } | |
405 | ||
406 | __resource_adaptor_imp& | |
407 | operator=(const __resource_adaptor_imp&) = default; | |
408 | ||
e70359b3 | 409 | allocator_type get_allocator() const noexcept { return _M_alloc; } |
bfeffbd1 FY |
410 | |
411 | protected: | |
412 | virtual void* | |
7956c508 | 413 | do_allocate(size_t __bytes, size_t __alignment) override |
bfeffbd1 | 414 | { |
52ea1caf JW |
415 | // Cannot use max_align_t on 32-bit Solaris x86, see PR libstdc++/77691 |
416 | #if ! (defined __sun__ && defined __i386__) | |
417 | if (__alignment == alignof(max_align_t)) | |
418 | return _M_allocate<alignof(max_align_t)>(__bytes); | |
419 | #endif | |
420 | switch (__alignment) | |
49cb790a | 421 | { |
52ea1caf | 422 | case 1: |
49cb790a | 423 | return _M_alloc.allocate(__bytes); |
52ea1caf JW |
424 | case 2: |
425 | return _M_allocate<2>(__bytes); | |
426 | case 4: | |
427 | return _M_allocate<4>(__bytes); | |
428 | case 8: | |
429 | return _M_allocate<8>(__bytes); | |
49cb790a | 430 | } |
7956c508 JW |
431 | const _AlignMgr __mgr(__bytes, __alignment); |
432 | // Assume _M_alloc returns 1-byte aligned memory, so allocate enough | |
433 | // space to fit a block of the right size and alignment, plus some | |
434 | // extra bytes to store a token for retrieving the original pointer. | |
435 | return __mgr._M_adjust(_M_alloc.allocate(__mgr._M_alloc_size())); | |
bfeffbd1 FY |
436 | } |
437 | ||
438 | virtual void | |
52ea1caf | 439 | do_deallocate(void* __ptr, size_t __bytes, size_t __alignment) noexcept |
7956c508 | 440 | override |
bfeffbd1 | 441 | { |
52ea1caf JW |
442 | #if ! (defined __sun__ && defined __i386__) |
443 | if (__alignment == alignof(max_align_t)) | |
444 | return (void) _M_deallocate<alignof(max_align_t)>(__ptr, __bytes); | |
445 | #endif | |
446 | switch (__alignment) | |
e9df6a8f | 447 | { |
52ea1caf JW |
448 | case 1: |
449 | return (void) _M_alloc.deallocate((char*)__ptr, __bytes); | |
450 | case 2: | |
451 | return (void) _M_deallocate<2>(__ptr, __bytes); | |
452 | case 4: | |
453 | return (void) _M_deallocate<4>(__ptr, __bytes); | |
454 | case 8: | |
455 | return (void) _M_deallocate<8>(__ptr, __bytes); | |
e9df6a8f | 456 | } |
7956c508 | 457 | const _AlignMgr __mgr(__bytes, __alignment); |
52ea1caf JW |
458 | // Use the stored token to retrieve the original pointer. |
459 | _M_alloc.deallocate(__mgr._M_unadjust((char*)__ptr), | |
460 | __mgr._M_alloc_size()); | |
bfeffbd1 FY |
461 | } |
462 | ||
463 | virtual bool | |
7956c508 | 464 | do_is_equal(const memory_resource& __other) const noexcept override |
bfeffbd1 | 465 | { |
7956c508 JW |
466 | if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other)) |
467 | return _M_alloc == __p->_M_alloc; | |
468 | return false; | |
bfeffbd1 FY |
469 | } |
470 | ||
471 | private: | |
52ea1caf JW |
472 | template<size_t _Num> |
473 | struct _Aligned_type { alignas(_Num) char __c[_Num]; }; | |
474 | ||
475 | // Rebind the allocator to the specified type and use it to allocate. | |
476 | template<size_t _Num, typename _Tp = _Aligned_type<_Num>> | |
477 | void* | |
478 | _M_allocate(size_t __bytes) | |
479 | { | |
480 | typename allocator_traits<_Alloc>::template | |
481 | rebind_alloc<_Tp> __a2(_M_alloc); | |
482 | const size_t __n = (__bytes + _Num - 1) / _Num; | |
483 | return __a2.allocate(__n); | |
484 | } | |
485 | ||
486 | // Rebind the allocator to the specified type and use it to deallocate. | |
487 | template<size_t _Num, typename _Tp = _Aligned_type<_Num>> | |
488 | void | |
489 | _M_deallocate(void* __ptr, size_t __bytes) noexcept | |
490 | { | |
491 | typename allocator_traits<_Alloc>::template | |
492 | rebind_alloc<_Tp> __a2(_M_alloc); | |
493 | const size_t __n = (__bytes + _Num - 1) / _Num; | |
494 | __a2.deallocate((_Tp*)__ptr, __n); | |
495 | } | |
496 | ||
7956c508 | 497 | _Alloc _M_alloc{}; |
bfeffbd1 FY |
498 | }; |
499 | ||
500 | // Global memory resources | |
bfeffbd1 FY |
501 | |
502 | inline memory_resource* | |
503 | new_delete_resource() noexcept | |
504 | { | |
7956c508 | 505 | using type = resource_adaptor<__gnu_cxx::new_allocator<char>>; |
e70359b3 JW |
506 | alignas(type) static unsigned char __buf[sizeof(type)]; |
507 | static type* __r = new(__buf) type; | |
508 | return __r; | |
bfeffbd1 FY |
509 | } |
510 | ||
e70359b3 JW |
511 | inline memory_resource* |
512 | null_memory_resource() noexcept | |
513 | { | |
514 | class type final : public memory_resource | |
bfeffbd1 | 515 | { |
bfeffbd1 | 516 | void* |
e70359b3 | 517 | do_allocate(size_t, size_t) override |
bfeffbd1 FY |
518 | { std::__throw_bad_alloc(); } |
519 | ||
520 | void | |
e70359b3 | 521 | do_deallocate(void*, size_t, size_t) noexcept override |
bfeffbd1 FY |
522 | { } |
523 | ||
524 | bool | |
e70359b3 | 525 | do_is_equal(const memory_resource& __other) const noexcept override |
d9cb3e75 | 526 | { return this == &__other; } |
bfeffbd1 FY |
527 | }; |
528 | ||
e70359b3 JW |
529 | alignas(type) static unsigned char __buf[sizeof(type)]; |
530 | static type* __r = new(__buf) type; | |
531 | return __r; | |
bfeffbd1 FY |
532 | } |
533 | ||
534 | // The default memory resource | |
0568ade6 | 535 | |
1ababc8b | 536 | /// @cond undocumented |
0568ade6 JW |
537 | inline std::atomic<memory_resource*>& |
538 | __get_default_resource() | |
539 | { | |
540 | using type = atomic<memory_resource*>; | |
541 | alignas(type) static unsigned char __buf[sizeof(type)]; | |
542 | static type* __r = new(__buf) type(new_delete_resource()); | |
543 | return *__r; | |
544 | } | |
1ababc8b | 545 | /// @endcond |
0568ade6 | 546 | |
1ababc8b | 547 | /// Get the current default resource. |
bfeffbd1 FY |
548 | inline memory_resource* |
549 | get_default_resource() noexcept | |
550 | { return __get_default_resource().load(); } | |
551 | ||
1ababc8b | 552 | /// Change the default resource and return the previous one. |
bfeffbd1 FY |
553 | inline memory_resource* |
554 | set_default_resource(memory_resource* __r) noexcept | |
555 | { | |
556 | if (__r == nullptr) | |
557 | __r = new_delete_resource(); | |
558 | return __get_default_resource().exchange(__r); | |
559 | } | |
7a4be380 | 560 | |
bfeffbd1 FY |
561 | } // namespace pmr |
562 | } // namespace fundamentals_v2 | |
563 | } // namespace experimental | |
4a15d842 FD |
564 | |
565 | _GLIBCXX_END_NAMESPACE_VERSION | |
bfeffbd1 | 566 | } // namespace std |
52ea1caf | 567 | #endif // C++14 |
7a4be380 | 568 | #endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE |