1// <experimental/memory_resource> -*- C++ -*-
3// Copyright (C) 2015-2023 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#include <bits/requires_hosted.h> // experimental is currently omitted
37#if __cplusplus >= 201402L
39#include <memory> // align, uses_allocator, __uses_alloc
40#include <experimental/utility> // pair, experimental::erased_type
41#include <tuple> // tuple, forward_as_tuple
42#include <atomic> // atomic
43#include <new> // placement new
44#include <cstddef> // max_align_t
45#include <bits/new_allocator.h>
46#include <debug/assertions.h>
49namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
51_GLIBCXX_BEGIN_NAMESPACE_VERSION
52 template<typename _Tp> class malloc_allocator;
53_GLIBCXX_END_NAMESPACE_VERSION
54} // namespace __gnu_cxx
58_GLIBCXX_BEGIN_NAMESPACE_VERSION
60namespace experimental {
61inline namespace fundamentals_v2 {
63#define __cpp_lib_experimental_memory_resources 201402L
65 // Standard memory resources
67 // 8.5 Class memory_resource
68 class memory_resource;
70 // 8.6 Class template polymorphic_allocator
71 template<typename _Tp>
72 class polymorphic_allocator;
74 template<typename _Alloc, typename _Resource = memory_resource>
75 class __resource_adaptor_imp;
77 // 8.7 Alias template resource_adaptor
78 template<typename _Alloc>
79 using resource_adaptor = __resource_adaptor_imp<
80 typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
82 // 8.8 Global memory resources
83 memory_resource* new_delete_resource() noexcept;
84 memory_resource* null_memory_resource() noexcept;
85 memory_resource* get_default_resource() noexcept;
86 memory_resource* set_default_resource(memory_resource* __r) noexcept;
88 // TODO 8.9 Pool resource classes
92 static constexpr size_t _S_max_align = alignof(max_align_t);
95 memory_resource() = default;
96 memory_resource(const memory_resource&) = default;
97 virtual ~memory_resource() = default;
99 memory_resource& operator=(const memory_resource&) = default;
101 _GLIBCXX_NODISCARD void*
102 allocate(size_t __bytes, size_t __alignment = _S_max_align)
103 { return do_allocate(__bytes, __alignment); }
106 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
107 { return do_deallocate(__p, __bytes, __alignment); }
110 is_equal(const memory_resource& __other) const noexcept
111 { return do_is_equal(__other); }
115 do_allocate(size_t __bytes, size_t __alignment) = 0;
118 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
121 do_is_equal(const memory_resource& __other) const noexcept = 0;
125 operator==(const memory_resource& __a, const memory_resource& __b) noexcept
126 { return &__a == &__b || __a.is_equal(__b); }
129 operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
130 { return !(__a == __b); }
133 template<typename _Tp>
134 class polymorphic_allocator
137 using value_type = _Tp;
139 polymorphic_allocator() noexcept
140 : _M_resource(get_default_resource())
143 polymorphic_allocator(memory_resource* __r)
145 { _GLIBCXX_DEBUG_ASSERT(__r); }
147 polymorphic_allocator(const polymorphic_allocator& __other) = default;
149 template <typename _Up>
150 polymorphic_allocator(const polymorphic_allocator<_Up>&
152 : _M_resource(__other.resource())
155 polymorphic_allocator&
156 operator=(const polymorphic_allocator& __rhs) = default;
158 _GLIBCXX_NODISCARD _Tp* allocate(size_t __n)
159 { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
163 deallocate(_Tp* __p, size_t __n)
164 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
166 template <typename _Tp1, typename... _Args> //used here
168 construct(_Tp1* __p, _Args&&... __args)
170 std::__uses_allocator_construct(this->resource(), __p,
171 std::forward<_Args>(__args)...);
174 // Specializations for pair using piecewise construction
175 template <typename _Tp1, typename _Tp2,
176 typename... _Args1, typename... _Args2>
178 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
179 tuple<_Args1...> __x, tuple<_Args2...> __y)
181 memory_resource* const __resource = this->resource();
183 std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
185 std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
187 ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
188 _M_construct_p(__x_use_tag, __x),
189 _M_construct_p(__y_use_tag, __y));
192 template <typename _Tp1, typename _Tp2>
194 construct(pair<_Tp1,_Tp2>* __p)
195 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
197 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
199 construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
201 this->construct(__p, piecewise_construct,
202 std::forward_as_tuple(std::forward<_Up>(__x)),
203 std::forward_as_tuple(std::forward<_Vp>(__y)));
206 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
208 construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
210 this->construct(__p, piecewise_construct,
211 std::forward_as_tuple(__pr.first),
212 std::forward_as_tuple(__pr.second));
215 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
217 construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
219 this->construct(__p, piecewise_construct,
220 std::forward_as_tuple(std::forward<_Up>(__pr.first)),
221 std::forward_as_tuple(std::forward<_Vp>(__pr.second)));
224 template <typename _Up>
229 // Return a default-constructed allocator (no allocator propagation)
230 polymorphic_allocator
231 select_on_container_copy_construction() const
232 { return polymorphic_allocator(); }
234 memory_resource* resource() const { return _M_resource; }
237 using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
238 using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
240 template<typename _Tuple>
242 _M_construct_p(__uses_alloc0, _Tuple& __t)
243 { return std::move(__t); }
245 template<typename... _Args>
247 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
248 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
251 template<typename... _Args>
253 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
254 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
256 memory_resource* _M_resource;
259 template <class _Tp1, class _Tp2>
261 operator==(const polymorphic_allocator<_Tp1>& __a,
262 const polymorphic_allocator<_Tp2>& __b) noexcept
263 { return *__a.resource() == *__b.resource(); }
265 template <class _Tp1, class _Tp2>
267 operator!=(const polymorphic_allocator<_Tp1>& __a,
268 const polymorphic_allocator<_Tp2>& __b) noexcept
269 { return !(__a == __b); }
272 /// @cond undocumented
273 class __resource_adaptor_common
275 template<typename, typename> friend class __resource_adaptor_imp;
279 _AlignMgr(size_t __nbytes, size_t __align)
280 : _M_nbytes(__nbytes), _M_align(__align)
283 // Total size that needs to be allocated.
285 _M_alloc_size() const { return _M_buf_size() + _M_token_size(); }
288 _M_adjust(void* __ptr) const
290 const auto __orig_ptr = static_cast<char*>(__ptr);
291 size_t __space = _M_buf_size();
292 // Align the pointer within the buffer:
293 std::align(_M_align, _M_nbytes, __ptr, __space);
294 const auto __aligned_ptr = static_cast<char*>(__ptr);
295 const auto __token_size = _M_token_size();
296 // Store token immediately after the aligned block:
297 char* const __end = __aligned_ptr + _M_nbytes;
298 if (__token_size == 1)
299 _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr);
300 else if (__token_size == sizeof(short))
301 _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr);
302 else if (__token_size == sizeof(int) && sizeof(int) < sizeof(char*))
303 _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr);
304 else // (__token_size == sizeof(char*))
305 // Just store the original pointer:
306 _S_write<char*>(__end, __orig_ptr);
307 return __aligned_ptr;
311 _M_unadjust(char* __ptr) const
313 const char* const __end = __ptr + _M_nbytes;
315 const auto __token_size = _M_token_size();
316 // Read the token and restore the original pointer:
317 if (__token_size == 1)
318 __orig_ptr = __ptr - _S_read<unsigned char>(__end);
319 else if (__token_size == sizeof(short))
320 __orig_ptr = __ptr - _S_read<unsigned short>(__end);
321 else if (__token_size == sizeof(int)
322 && sizeof(int) < sizeof(char*))
323 __orig_ptr = __ptr - _S_read<unsigned int>(__end);
324 else // (__token_size == sizeof(char*))
325 __orig_ptr = _S_read<char*>(__end);
326 // The adjustment is always less than the requested alignment,
327 // so if that isn't true now then either the wrong size was passed
328 // to deallocate or the token was overwritten by a buffer overflow:
329 __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align);
337 // Number of bytes needed to fit block of given size and alignment.
339 _M_buf_size() const { return _M_nbytes + _M_align - 1; }
341 // Number of additional bytes needed to write the token.
343 _M_token_size() const
345 if (_M_align <= (1ul << __CHAR_BIT__))
347 if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__)))
348 return sizeof(short);
349 if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
351 return sizeof(char*);
354 template<typename _Tp>
356 _S_write(void* __to, _Tp __val)
357 { __builtin_memcpy(__to, &__val, sizeof(_Tp)); }
359 template<typename _Tp>
361 _S_read(const void* __from)
364 __builtin_memcpy(&__val, __from, sizeof(_Tp));
371 // 8.7.1 __resource_adaptor_imp
372 template<typename _Alloc, typename _Resource>
373 class __resource_adaptor_imp
374 : public _Resource, private __resource_adaptor_common
376 using memory_resource = _Resource;
378 static_assert(is_same<char,
379 typename allocator_traits<_Alloc>::value_type>::value,
380 "Allocator's value_type is char");
381 static_assert(is_same<char*,
382 typename allocator_traits<_Alloc>::pointer>::value,
383 "Allocator's pointer type is value_type*");
384 static_assert(is_same<const char*,
385 typename allocator_traits<_Alloc>::const_pointer>::value,
386 "Allocator's const_pointer type is value_type const*");
387 static_assert(is_same<void*,
388 typename allocator_traits<_Alloc>::void_pointer>::value,
389 "Allocator's void_pointer type is void*");
390 static_assert(is_same<const void*,
391 typename allocator_traits<_Alloc>::const_void_pointer>::value,
392 "Allocator's const_void_pointer type is void const*");
395 using allocator_type = _Alloc;
397 __resource_adaptor_imp() = default;
398 __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
399 __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
401 explicit __resource_adaptor_imp(const _Alloc& __a2)
405 explicit __resource_adaptor_imp(_Alloc&& __a2)
406 : _M_alloc(std::move(__a2))
409 __resource_adaptor_imp&
410 operator=(const __resource_adaptor_imp&) = default;
412 allocator_type get_allocator() const noexcept { return _M_alloc; }
415#if (defined __sun__ || defined __VXWORKS__) && defined __i386__
416// Cannot use max_align_t on 32-bit Solaris x86, see PR libstdc++/77691
417# define _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC 0
418#elif defined __hpux__ && defined __hppa__ && defined __LP64__
419// Ignore inconsistent long double and malloc alignment (libstdc++/77691)
420# define _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC 0
422# define _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC 1
426 do_allocate(size_t __bytes, size_t __alignment) override
428#if _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC
429 if (__alignment == alignof(max_align_t))
430 return _M_allocate<alignof(max_align_t)>(__bytes);
435 return _M_alloc.allocate(__bytes);
437 return _M_allocate<2>(__bytes);
439 return _M_allocate<4>(__bytes);
441 return _M_allocate<8>(__bytes);
443 const _AlignMgr __mgr(__bytes, __alignment);
444 // Assume _M_alloc returns 1-byte aligned memory, so allocate enough
445 // space to fit a block of the right size and alignment, plus some
446 // extra bytes to store a token for retrieving the original pointer.
447 return __mgr._M_adjust(_M_alloc.allocate(__mgr._M_alloc_size()));
451 do_deallocate(void* __ptr, size_t __bytes, size_t __alignment) noexcept
454#if _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC
455 if (__alignment == alignof(max_align_t))
456 return (void) _M_deallocate<alignof(max_align_t)>(__ptr, __bytes);
461 return (void) _M_alloc.deallocate((char*)__ptr, __bytes);
463 return (void) _M_deallocate<2>(__ptr, __bytes);
465 return (void) _M_deallocate<4>(__ptr, __bytes);
467 return (void) _M_deallocate<8>(__ptr, __bytes);
469 const _AlignMgr __mgr(__bytes, __alignment);
470 // Use the stored token to retrieve the original pointer.
471 _M_alloc.deallocate(__mgr._M_unadjust((char*)__ptr),
472 __mgr._M_alloc_size());
476 do_is_equal(const memory_resource& __other) const noexcept override
479 if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
480 return _M_alloc == __p->_M_alloc;
482 if (this == &__other) // Need RTTI to do better than this.
489 template<size_t _Num>
490 struct _Aligned_type { alignas(_Num) char __c[_Num]; };
492 // Rebind the allocator to the specified type and use it to allocate.
493 template<size_t _Num, typename _Tp = _Aligned_type<_Num>>
495 _M_allocate(size_t __bytes)
497 typename allocator_traits<_Alloc>::template
498 rebind_alloc<_Tp> __a2(_M_alloc);
499 const size_t __n = (__bytes + _Num - 1) / _Num;
500 return __a2.allocate(__n);
503 // Rebind the allocator to the specified type and use it to deallocate.
504 template<size_t _Num, typename _Tp = _Aligned_type<_Num>>
506 _M_deallocate(void* __ptr, size_t __bytes) noexcept
508 typename allocator_traits<_Alloc>::template
509 rebind_alloc<_Tp> __a2(_M_alloc);
510 const size_t __n = (__bytes + _Num - 1) / _Num;
511 __a2.deallocate((_Tp*)__ptr, __n);
517 // Global memory resources
519 inline memory_resource*
520 new_delete_resource() noexcept
522 using type = resource_adaptor<std::__new_allocator<char>>;
523 alignas(type) static unsigned char __buf[sizeof(type)];
524 static type* __r = new(__buf) type;
528 inline memory_resource*
529 null_memory_resource() noexcept
531 class type final : public memory_resource
534 do_allocate(size_t, size_t) override
535 { std::__throw_bad_alloc(); }
538 do_deallocate(void*, size_t, size_t) noexcept override
542 do_is_equal(const memory_resource& __other) const noexcept override
543 { return this == &__other; }
546 alignas(type) static unsigned char __buf[sizeof(type)];
547 static type* __r = new(__buf) type;
551 // The default memory resource
553 /// @cond undocumented
554 inline std::atomic<memory_resource*>&
555 __get_default_resource()
557 using type = atomic<memory_resource*>;
558 alignas(type) static unsigned char __buf[sizeof(type)];
559 static type* __r = new(__buf) type(new_delete_resource());
564 /// Get the current default resource.
565 inline memory_resource*
566 get_default_resource() noexcept
567 { return __get_default_resource().load(); }
569 /// Change the default resource and return the previous one.
570 inline memory_resource*
571 set_default_resource(memory_resource* __r) noexcept
574 __r = new_delete_resource();
575 return __get_default_resource().exchange(__r);
579} // namespace fundamentals_v2
580} // namespace experimental
582_GLIBCXX_END_NAMESPACE_VERSION
585#endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE