1 // <experimental/memory_resource> -*- C++ -*-
3 // Copyright (C) 2015-2020 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file experimental/memory_resource
26 * This is a TS C++ Library header.
30 #ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE
31 #define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1
33 #pragma GCC system_header
35 #if __cplusplus >= 201402L
37 #include <memory> // align, uses_allocator, __uses_alloc
38 #include <experimental/utility> // pair, experimental::erased_type
39 #include <atomic> // atomic
40 #include <new> // placement new
41 #include <cstddef> // max_align_t
42 #include <ext/new_allocator.h>
43 #include <debug/assertions.h>
46 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 template<typename _Tp> class malloc_allocator;
50 _GLIBCXX_END_NAMESPACE_VERSION
51 } // namespace __gnu_cxx
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
57 namespace experimental {
58 inline namespace fundamentals_v2 {
60 #define __cpp_lib_experimental_memory_resources 201402L
62 // Standard memory resources
64 // 8.5 Class memory_resource
65 class memory_resource;
67 // 8.6 Class template polymorphic_allocator
68 template<typename _Tp>
69 class polymorphic_allocator;
71 template<typename _Alloc, typename _Resource = memory_resource>
72 class __resource_adaptor_imp;
74 // 8.7 Alias template resource_adaptor
75 template<typename _Alloc>
76 using resource_adaptor = __resource_adaptor_imp<
77 typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
79 // 8.8 Global memory resources
80 memory_resource* new_delete_resource() noexcept;
81 memory_resource* null_memory_resource() noexcept;
82 memory_resource* get_default_resource() noexcept;
83 memory_resource* set_default_resource(memory_resource* __r) noexcept;
85 // TODO 8.9 Pool resource classes
89 static constexpr size_t _S_max_align = alignof(max_align_t);
92 memory_resource() = default;
93 memory_resource(const memory_resource&) = default;
94 virtual ~memory_resource() = default;
96 memory_resource& operator=(const memory_resource&) = default;
98 _GLIBCXX_NODISCARD void*
99 allocate(size_t __bytes, size_t __alignment = _S_max_align)
100 { return do_allocate(__bytes, __alignment); }
103 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
104 { return do_deallocate(__p, __bytes, __alignment); }
107 is_equal(const memory_resource& __other) const noexcept
108 { return do_is_equal(__other); }
112 do_allocate(size_t __bytes, size_t __alignment) = 0;
115 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
118 do_is_equal(const memory_resource& __other) const noexcept = 0;
122 operator==(const memory_resource& __a, const memory_resource& __b) noexcept
123 { return &__a == &__b || __a.is_equal(__b); }
126 operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
127 { return !(__a == __b); }
130 template<typename _Tp>
131 class polymorphic_allocator
134 using value_type = _Tp;
136 polymorphic_allocator() noexcept
137 : _M_resource(get_default_resource())
140 polymorphic_allocator(memory_resource* __r)
142 { _GLIBCXX_DEBUG_ASSERT(__r); }
144 polymorphic_allocator(const polymorphic_allocator& __other) = default;
146 template <typename _Up>
147 polymorphic_allocator(const polymorphic_allocator<_Up>&
149 : _M_resource(__other.resource())
152 polymorphic_allocator&
153 operator=(const polymorphic_allocator& __rhs) = default;
155 _GLIBCXX_NODISCARD _Tp* allocate(size_t __n)
156 { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
160 deallocate(_Tp* __p, size_t __n)
161 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
163 template <typename _Tp1, typename... _Args> //used here
165 construct(_Tp1* __p, _Args&&... __args)
167 std::__uses_allocator_construct(this->resource(), __p,
168 std::forward<_Args>(__args)...);
171 // Specializations for pair using piecewise construction
172 template <typename _Tp1, typename _Tp2,
173 typename... _Args1, typename... _Args2>
175 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
176 tuple<_Args1...> __x, tuple<_Args2...> __y)
178 memory_resource* const __resource = this->resource();
180 std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
182 std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
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));
189 template <typename _Tp1, typename _Tp2>
191 construct(pair<_Tp1,_Tp2>* __p)
192 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
194 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
196 construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
198 this->construct(__p, piecewise_construct,
199 forward_as_tuple(std::forward<_Up>(__x)),
200 forward_as_tuple(std::forward<_Vp>(__y)));
203 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
205 construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
207 this->construct(__p, piecewise_construct,
208 forward_as_tuple(__pr.first),
209 forward_as_tuple(__pr.second));
212 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
214 construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
216 this->construct(__p, piecewise_construct,
217 forward_as_tuple(std::forward<_Up>(__pr.first)),
218 forward_as_tuple(std::forward<_Vp>(__pr.second)));
221 template <typename _Up>
226 // Return a default-constructed allocator (no allocator propagation)
227 polymorphic_allocator
228 select_on_container_copy_construction() const
229 { return polymorphic_allocator(); }
231 memory_resource* resource() const { return _M_resource; }
234 using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
235 using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
237 template<typename _Tuple>
239 _M_construct_p(__uses_alloc0, _Tuple& __t)
240 { return std::move(__t); }
242 template<typename... _Args>
244 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
245 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
248 template<typename... _Args>
250 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
251 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
253 memory_resource* _M_resource;
256 template <class _Tp1, class _Tp2>
258 operator==(const polymorphic_allocator<_Tp1>& __a,
259 const polymorphic_allocator<_Tp2>& __b) noexcept
260 { return *__a.resource() == *__b.resource(); }
262 template <class _Tp1, class _Tp2>
264 operator!=(const polymorphic_allocator<_Tp1>& __a,
265 const polymorphic_allocator<_Tp2>& __b) noexcept
266 { return !(__a == __b); }
269 /// @cond undocumented
270 class __resource_adaptor_common
272 template<typename, typename> friend class __resource_adaptor_imp;
276 _AlignMgr(size_t __nbytes, size_t __align)
277 : _M_nbytes(__nbytes), _M_align(__align)
280 // Total size that needs to be allocated.
282 _M_alloc_size() const { return _M_buf_size() + _M_token_size(); }
285 _M_adjust(void* __ptr) const
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;
308 _M_unadjust(char* __ptr) const
310 const char* const __end = __ptr + _M_nbytes;
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);
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);
334 // Number of bytes needed to fit block of given size and alignment.
336 _M_buf_size() const { return _M_nbytes + _M_align - 1; }
338 // Number of additional bytes needed to write the token.
340 _M_token_size() const
342 if (_M_align <= (1ul << __CHAR_BIT__))
344 if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__)))
345 return sizeof(short);
346 if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
348 return sizeof(char*);
351 template<typename _Tp>
353 _S_write(void* __to, _Tp __val)
354 { __builtin_memcpy(__to, &__val, sizeof(_Tp)); }
356 template<typename _Tp>
358 _S_read(const void* __from)
361 __builtin_memcpy(&__val, __from, sizeof(_Tp));
368 // 8.7.1 __resource_adaptor_imp
369 template<typename _Alloc, typename _Resource>
370 class __resource_adaptor_imp
371 : public _Resource, private __resource_adaptor_common
373 using memory_resource = _Resource;
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*");
392 using allocator_type = _Alloc;
394 __resource_adaptor_imp() = default;
395 __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
396 __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
398 explicit __resource_adaptor_imp(const _Alloc& __a2)
402 explicit __resource_adaptor_imp(_Alloc&& __a2)
403 : _M_alloc(std::move(__a2))
406 __resource_adaptor_imp&
407 operator=(const __resource_adaptor_imp&) = default;
409 allocator_type get_allocator() const noexcept { return _M_alloc; }
413 do_allocate(size_t __bytes, size_t __alignment) override
415 // Cannot use max_align_t on 32-bit Solaris x86, see PR libstdc++/77691
416 #if ! ((defined __sun__ || defined __VXWORKS__) && defined __i386__)
417 if (__alignment == alignof(max_align_t))
418 return _M_allocate<alignof(max_align_t)>(__bytes);
423 return _M_alloc.allocate(__bytes);
425 return _M_allocate<2>(__bytes);
427 return _M_allocate<4>(__bytes);
429 return _M_allocate<8>(__bytes);
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()));
439 do_deallocate(void* __ptr, size_t __bytes, size_t __alignment) noexcept
442 #if ! ((defined __sun__ || defined __VXWORKS__) && defined __i386__)
443 if (__alignment == alignof(max_align_t))
444 return (void) _M_deallocate<alignof(max_align_t)>(__ptr, __bytes);
449 return (void) _M_alloc.deallocate((char*)__ptr, __bytes);
451 return (void) _M_deallocate<2>(__ptr, __bytes);
453 return (void) _M_deallocate<4>(__ptr, __bytes);
455 return (void) _M_deallocate<8>(__ptr, __bytes);
457 const _AlignMgr __mgr(__bytes, __alignment);
458 // Use the stored token to retrieve the original pointer.
459 _M_alloc.deallocate(__mgr._M_unadjust((char*)__ptr),
460 __mgr._M_alloc_size());
464 do_is_equal(const memory_resource& __other) const noexcept override
466 if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
467 return _M_alloc == __p->_M_alloc;
472 template<size_t _Num>
473 struct _Aligned_type { alignas(_Num) char __c[_Num]; };
475 // Rebind the allocator to the specified type and use it to allocate.
476 template<size_t _Num, typename _Tp = _Aligned_type<_Num>>
478 _M_allocate(size_t __bytes)
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);
486 // Rebind the allocator to the specified type and use it to deallocate.
487 template<size_t _Num, typename _Tp = _Aligned_type<_Num>>
489 _M_deallocate(void* __ptr, size_t __bytes) noexcept
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);
500 // Global memory resources
502 inline memory_resource*
503 new_delete_resource() noexcept
505 using type = resource_adaptor<__gnu_cxx::new_allocator<char>>;
506 alignas(type) static unsigned char __buf[sizeof(type)];
507 static type* __r = new(__buf) type;
511 inline memory_resource*
512 null_memory_resource() noexcept
514 class type final : public memory_resource
517 do_allocate(size_t, size_t) override
518 { std::__throw_bad_alloc(); }
521 do_deallocate(void*, size_t, size_t) noexcept override
525 do_is_equal(const memory_resource& __other) const noexcept override
526 { return this == &__other; }
529 alignas(type) static unsigned char __buf[sizeof(type)];
530 static type* __r = new(__buf) type;
534 // The default memory resource
536 /// @cond undocumented
537 inline std::atomic<memory_resource*>&
538 __get_default_resource()
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());
547 /// Get the current default resource.
548 inline memory_resource*
549 get_default_resource() noexcept
550 { return __get_default_resource().load(); }
552 /// Change the default resource and return the previous one.
553 inline memory_resource*
554 set_default_resource(memory_resource* __r) noexcept
557 __r = new_delete_resource();
558 return __get_default_resource().exchange(__r);
562 } // namespace fundamentals_v2
563 } // namespace experimental
565 _GLIBCXX_END_NAMESPACE_VERSION
568 #endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE