1 // <experimental/memory_resource> -*- C++ -*-
3 // Copyright (C) 2015-2022 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 <tuple> // tuple, forward_as_tuple
40 #include <atomic> // atomic
41 #include <new> // placement new
42 #include <cstddef> // max_align_t
43 #include <bits/new_allocator.h>
44 #include <debug/assertions.h>
47 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 template<typename _Tp> class malloc_allocator;
51 _GLIBCXX_END_NAMESPACE_VERSION
52 } // namespace __gnu_cxx
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 namespace experimental {
59 inline namespace fundamentals_v2 {
61 #define __cpp_lib_experimental_memory_resources 201402L
63 // Standard memory resources
65 // 8.5 Class memory_resource
66 class memory_resource;
68 // 8.6 Class template polymorphic_allocator
69 template<typename _Tp>
70 class polymorphic_allocator;
72 template<typename _Alloc, typename _Resource = memory_resource>
73 class __resource_adaptor_imp;
75 // 8.7 Alias template resource_adaptor
76 template<typename _Alloc>
77 using resource_adaptor = __resource_adaptor_imp<
78 typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
80 // 8.8 Global memory resources
81 memory_resource* new_delete_resource() noexcept;
82 memory_resource* null_memory_resource() noexcept;
83 memory_resource* get_default_resource() noexcept;
84 memory_resource* set_default_resource(memory_resource* __r) noexcept;
86 // TODO 8.9 Pool resource classes
90 static constexpr size_t _S_max_align = alignof(max_align_t);
93 memory_resource() = default;
94 memory_resource(const memory_resource&) = default;
95 virtual ~memory_resource() = default;
97 memory_resource& operator=(const memory_resource&) = default;
99 _GLIBCXX_NODISCARD void*
100 allocate(size_t __bytes, size_t __alignment = _S_max_align)
101 { return do_allocate(__bytes, __alignment); }
104 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
105 { return do_deallocate(__p, __bytes, __alignment); }
108 is_equal(const memory_resource& __other) const noexcept
109 { return do_is_equal(__other); }
113 do_allocate(size_t __bytes, size_t __alignment) = 0;
116 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
119 do_is_equal(const memory_resource& __other) const noexcept = 0;
123 operator==(const memory_resource& __a, const memory_resource& __b) noexcept
124 { return &__a == &__b || __a.is_equal(__b); }
127 operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
128 { return !(__a == __b); }
131 template<typename _Tp>
132 class polymorphic_allocator
135 using value_type = _Tp;
137 polymorphic_allocator() noexcept
138 : _M_resource(get_default_resource())
141 polymorphic_allocator(memory_resource* __r)
143 { _GLIBCXX_DEBUG_ASSERT(__r); }
145 polymorphic_allocator(const polymorphic_allocator& __other) = default;
147 template <typename _Up>
148 polymorphic_allocator(const polymorphic_allocator<_Up>&
150 : _M_resource(__other.resource())
153 polymorphic_allocator&
154 operator=(const polymorphic_allocator& __rhs) = default;
156 _GLIBCXX_NODISCARD _Tp* allocate(size_t __n)
157 { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
161 deallocate(_Tp* __p, size_t __n)
162 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
164 template <typename _Tp1, typename... _Args> //used here
166 construct(_Tp1* __p, _Args&&... __args)
168 std::__uses_allocator_construct(this->resource(), __p,
169 std::forward<_Args>(__args)...);
172 // Specializations for pair using piecewise construction
173 template <typename _Tp1, typename _Tp2,
174 typename... _Args1, typename... _Args2>
176 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
177 tuple<_Args1...> __x, tuple<_Args2...> __y)
179 memory_resource* const __resource = this->resource();
181 std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
183 std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
185 ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
186 _M_construct_p(__x_use_tag, __x),
187 _M_construct_p(__y_use_tag, __y));
190 template <typename _Tp1, typename _Tp2>
192 construct(pair<_Tp1,_Tp2>* __p)
193 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
195 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
197 construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
199 this->construct(__p, piecewise_construct,
200 std::forward_as_tuple(std::forward<_Up>(__x)),
201 std::forward_as_tuple(std::forward<_Vp>(__y)));
204 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
206 construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
208 this->construct(__p, piecewise_construct,
209 std::forward_as_tuple(__pr.first),
210 std::forward_as_tuple(__pr.second));
213 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
215 construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
217 this->construct(__p, piecewise_construct,
218 std::forward_as_tuple(std::forward<_Up>(__pr.first)),
219 std::forward_as_tuple(std::forward<_Vp>(__pr.second)));
222 template <typename _Up>
227 // Return a default-constructed allocator (no allocator propagation)
228 polymorphic_allocator
229 select_on_container_copy_construction() const
230 { return polymorphic_allocator(); }
232 memory_resource* resource() const { return _M_resource; }
235 using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
236 using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
238 template<typename _Tuple>
240 _M_construct_p(__uses_alloc0, _Tuple& __t)
241 { return std::move(__t); }
243 template<typename... _Args>
245 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
246 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
249 template<typename... _Args>
251 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
252 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
254 memory_resource* _M_resource;
257 template <class _Tp1, class _Tp2>
259 operator==(const polymorphic_allocator<_Tp1>& __a,
260 const polymorphic_allocator<_Tp2>& __b) noexcept
261 { return *__a.resource() == *__b.resource(); }
263 template <class _Tp1, class _Tp2>
265 operator!=(const polymorphic_allocator<_Tp1>& __a,
266 const polymorphic_allocator<_Tp2>& __b) noexcept
267 { return !(__a == __b); }
270 /// @cond undocumented
271 class __resource_adaptor_common
273 template<typename, typename> friend class __resource_adaptor_imp;
277 _AlignMgr(size_t __nbytes, size_t __align)
278 : _M_nbytes(__nbytes), _M_align(__align)
281 // Total size that needs to be allocated.
283 _M_alloc_size() const { return _M_buf_size() + _M_token_size(); }
286 _M_adjust(void* __ptr) const
288 const auto __orig_ptr = static_cast<char*>(__ptr);
289 size_t __space = _M_buf_size();
290 // Align the pointer within the buffer:
291 std::align(_M_align, _M_nbytes, __ptr, __space);
292 const auto __aligned_ptr = static_cast<char*>(__ptr);
293 const auto __token_size = _M_token_size();
294 // Store token immediately after the aligned block:
295 char* const __end = __aligned_ptr + _M_nbytes;
296 if (__token_size == 1)
297 _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr);
298 else if (__token_size == sizeof(short))
299 _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr);
300 else if (__token_size == sizeof(int) && sizeof(int) < sizeof(char*))
301 _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr);
302 else // (__token_size == sizeof(char*))
303 // Just store the original pointer:
304 _S_write<char*>(__end, __orig_ptr);
305 return __aligned_ptr;
309 _M_unadjust(char* __ptr) const
311 const char* const __end = __ptr + _M_nbytes;
313 const auto __token_size = _M_token_size();
314 // Read the token and restore the original pointer:
315 if (__token_size == 1)
316 __orig_ptr = __ptr - _S_read<unsigned char>(__end);
317 else if (__token_size == sizeof(short))
318 __orig_ptr = __ptr - _S_read<unsigned short>(__end);
319 else if (__token_size == sizeof(int)
320 && sizeof(int) < sizeof(char*))
321 __orig_ptr = __ptr - _S_read<unsigned int>(__end);
322 else // (__token_size == sizeof(char*))
323 __orig_ptr = _S_read<char*>(__end);
324 // The adjustment is always less than the requested alignment,
325 // so if that isn't true now then either the wrong size was passed
326 // to deallocate or the token was overwritten by a buffer overflow:
327 __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align);
335 // Number of bytes needed to fit block of given size and alignment.
337 _M_buf_size() const { return _M_nbytes + _M_align - 1; }
339 // Number of additional bytes needed to write the token.
341 _M_token_size() const
343 if (_M_align <= (1ul << __CHAR_BIT__))
345 if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__)))
346 return sizeof(short);
347 if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
349 return sizeof(char*);
352 template<typename _Tp>
354 _S_write(void* __to, _Tp __val)
355 { __builtin_memcpy(__to, &__val, sizeof(_Tp)); }
357 template<typename _Tp>
359 _S_read(const void* __from)
362 __builtin_memcpy(&__val, __from, sizeof(_Tp));
369 // 8.7.1 __resource_adaptor_imp
370 template<typename _Alloc, typename _Resource>
371 class __resource_adaptor_imp
372 : public _Resource, private __resource_adaptor_common
374 using memory_resource = _Resource;
376 static_assert(is_same<char,
377 typename allocator_traits<_Alloc>::value_type>::value,
378 "Allocator's value_type is char");
379 static_assert(is_same<char*,
380 typename allocator_traits<_Alloc>::pointer>::value,
381 "Allocator's pointer type is value_type*");
382 static_assert(is_same<const char*,
383 typename allocator_traits<_Alloc>::const_pointer>::value,
384 "Allocator's const_pointer type is value_type const*");
385 static_assert(is_same<void*,
386 typename allocator_traits<_Alloc>::void_pointer>::value,
387 "Allocator's void_pointer type is void*");
388 static_assert(is_same<const void*,
389 typename allocator_traits<_Alloc>::const_void_pointer>::value,
390 "Allocator's const_void_pointer type is void const*");
393 using allocator_type = _Alloc;
395 __resource_adaptor_imp() = default;
396 __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
397 __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
399 explicit __resource_adaptor_imp(const _Alloc& __a2)
403 explicit __resource_adaptor_imp(_Alloc&& __a2)
404 : _M_alloc(std::move(__a2))
407 __resource_adaptor_imp&
408 operator=(const __resource_adaptor_imp&) = default;
410 allocator_type get_allocator() const noexcept { return _M_alloc; }
414 do_allocate(size_t __bytes, size_t __alignment) override
416 // Cannot use max_align_t on 32-bit Solaris x86, see PR libstdc++/77691
417 #if ! ((defined __sun__ || defined __VXWORKS__) && defined __i386__)
418 if (__alignment == alignof(max_align_t))
419 return _M_allocate<alignof(max_align_t)>(__bytes);
424 return _M_alloc.allocate(__bytes);
426 return _M_allocate<2>(__bytes);
428 return _M_allocate<4>(__bytes);
430 return _M_allocate<8>(__bytes);
432 const _AlignMgr __mgr(__bytes, __alignment);
433 // Assume _M_alloc returns 1-byte aligned memory, so allocate enough
434 // space to fit a block of the right size and alignment, plus some
435 // extra bytes to store a token for retrieving the original pointer.
436 return __mgr._M_adjust(_M_alloc.allocate(__mgr._M_alloc_size()));
440 do_deallocate(void* __ptr, size_t __bytes, size_t __alignment) noexcept
443 #if ! ((defined __sun__ || defined __VXWORKS__) && defined __i386__)
444 if (__alignment == alignof(max_align_t))
445 return (void) _M_deallocate<alignof(max_align_t)>(__ptr, __bytes);
450 return (void) _M_alloc.deallocate((char*)__ptr, __bytes);
452 return (void) _M_deallocate<2>(__ptr, __bytes);
454 return (void) _M_deallocate<4>(__ptr, __bytes);
456 return (void) _M_deallocate<8>(__ptr, __bytes);
458 const _AlignMgr __mgr(__bytes, __alignment);
459 // Use the stored token to retrieve the original pointer.
460 _M_alloc.deallocate(__mgr._M_unadjust((char*)__ptr),
461 __mgr._M_alloc_size());
465 do_is_equal(const memory_resource& __other) const noexcept override
467 if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
468 return _M_alloc == __p->_M_alloc;
473 template<size_t _Num>
474 struct _Aligned_type { alignas(_Num) char __c[_Num]; };
476 // Rebind the allocator to the specified type and use it to allocate.
477 template<size_t _Num, typename _Tp = _Aligned_type<_Num>>
479 _M_allocate(size_t __bytes)
481 typename allocator_traits<_Alloc>::template
482 rebind_alloc<_Tp> __a2(_M_alloc);
483 const size_t __n = (__bytes + _Num - 1) / _Num;
484 return __a2.allocate(__n);
487 // Rebind the allocator to the specified type and use it to deallocate.
488 template<size_t _Num, typename _Tp = _Aligned_type<_Num>>
490 _M_deallocate(void* __ptr, size_t __bytes) noexcept
492 typename allocator_traits<_Alloc>::template
493 rebind_alloc<_Tp> __a2(_M_alloc);
494 const size_t __n = (__bytes + _Num - 1) / _Num;
495 __a2.deallocate((_Tp*)__ptr, __n);
501 // Global memory resources
503 inline memory_resource*
504 new_delete_resource() noexcept
506 using type = resource_adaptor<std::__new_allocator<char>>;
507 alignas(type) static unsigned char __buf[sizeof(type)];
508 static type* __r = new(__buf) type;
512 inline memory_resource*
513 null_memory_resource() noexcept
515 class type final : public memory_resource
518 do_allocate(size_t, size_t) override
519 { std::__throw_bad_alloc(); }
522 do_deallocate(void*, size_t, size_t) noexcept override
526 do_is_equal(const memory_resource& __other) const noexcept override
527 { return this == &__other; }
530 alignas(type) static unsigned char __buf[sizeof(type)];
531 static type* __r = new(__buf) type;
535 // The default memory resource
537 /// @cond undocumented
538 inline std::atomic<memory_resource*>&
539 __get_default_resource()
541 using type = atomic<memory_resource*>;
542 alignas(type) static unsigned char __buf[sizeof(type)];
543 static type* __r = new(__buf) type(new_delete_resource());
548 /// Get the current default resource.
549 inline memory_resource*
550 get_default_resource() noexcept
551 { return __get_default_resource().load(); }
553 /// Change the default resource and return the previous one.
554 inline memory_resource*
555 set_default_resource(memory_resource* __r) noexcept
558 __r = new_delete_resource();
559 return __get_default_resource().exchange(__r);
563 } // namespace fundamentals_v2
564 } // namespace experimental
566 _GLIBCXX_END_NAMESPACE_VERSION
569 #endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE