1// <experimental/memory_resource> -*- C++ -*-
3// Copyright (C) 2015-2019 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.
29#ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE
30#define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1
32#pragma GCC system_header
34#if __cplusplus >= 201402L
36#include <memory> // align, uses_allocator, __uses_alloc
37#include <experimental/utility> // pair, experimental::erased_type
38#include <atomic> // atomic
39#include <new> // placement new
40#include <cstddef> // max_align_t
41#include <ext/new_allocator.h>
42#include <debug/assertions.h>
44namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
47 template<typename _Tp> class malloc_allocator;
48_GLIBCXX_END_NAMESPACE_VERSION
49} // namespace __gnu_cxx
52_GLIBCXX_BEGIN_NAMESPACE_VERSION
54namespace experimental {
55inline namespace fundamentals_v2 {
57#define __cpp_lib_experimental_memory_resources 201402L
59 // Standard memory resources
61 // 8.5 Class memory_resource
62 class memory_resource;
64 // 8.6 Class template polymorphic_allocator
65 template<typename _Tp>
66 class polymorphic_allocator;
68 template<typename _Alloc, typename _Resource = memory_resource>
69 class __resource_adaptor_imp;
71 // 8.7 Alias template resource_adaptor
72 template<typename _Alloc>
73 using resource_adaptor = __resource_adaptor_imp<
74 typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
76 // 8.8 Global memory resources
77 memory_resource* new_delete_resource() noexcept;
78 memory_resource* null_memory_resource() noexcept;
79 memory_resource* get_default_resource() noexcept;
80 memory_resource* set_default_resource(memory_resource* __r) noexcept;
82 // TODO 8.9 Pool resource classes
86 static constexpr size_t _S_max_align = alignof(max_align_t);
89 memory_resource() = default;
90 memory_resource(const memory_resource&) = default;
91 virtual ~memory_resource() = default;
93 memory_resource& operator=(const memory_resource&) = default;
95 _GLIBCXX_NODISCARD void*
96 allocate(size_t __bytes, size_t __alignment = _S_max_align)
97 { return do_allocate(__bytes, __alignment); }
100 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
101 { return do_deallocate(__p, __bytes, __alignment); }
104 is_equal(const memory_resource& __other) const noexcept
105 { return do_is_equal(__other); }
109 do_allocate(size_t __bytes, size_t __alignment) = 0;
112 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
115 do_is_equal(const memory_resource& __other) const noexcept = 0;
119 operator==(const memory_resource& __a, const memory_resource& __b) noexcept
120 { return &__a == &__b || __a.is_equal(__b); }
123 operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
124 { return !(__a == __b); }
127 template<typename _Tp>
128 class polymorphic_allocator
131 using value_type = _Tp;
133 polymorphic_allocator() noexcept
134 : _M_resource(get_default_resource())
137 polymorphic_allocator(memory_resource* __r)
139 { _GLIBCXX_DEBUG_ASSERT(__r); }
141 polymorphic_allocator(const polymorphic_allocator& __other) = default;
143 template <typename _Up>
144 polymorphic_allocator(const polymorphic_allocator<_Up>&
146 : _M_resource(__other.resource())
149 polymorphic_allocator&
150 operator=(const polymorphic_allocator& __rhs) = default;
152 _GLIBCXX_NODISCARD _Tp* allocate(size_t __n)
153 { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
157 deallocate(_Tp* __p, size_t __n)
158 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
160 template <typename _Tp1, typename... _Args> //used here
162 construct(_Tp1* __p, _Args&&... __args)
164 std::__uses_allocator_construct(this->resource(), __p,
165 std::forward<_Args>(__args)...);
168 // Specializations for pair using piecewise construction
169 template <typename _Tp1, typename _Tp2,
170 typename... _Args1, typename... _Args2>
172 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
173 tuple<_Args1...> __x, tuple<_Args2...> __y)
175 memory_resource* const __resource = this->resource();
177 std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
179 std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
181 ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
182 _M_construct_p(__x_use_tag, __x),
183 _M_construct_p(__y_use_tag, __y));
186 template <typename _Tp1, typename _Tp2>
188 construct(pair<_Tp1,_Tp2>* __p)
189 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
191 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
193 construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
195 this->construct(__p, piecewise_construct,
196 forward_as_tuple(std::forward<_Up>(__x)),
197 forward_as_tuple(std::forward<_Vp>(__y)));
200 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
202 construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
204 this->construct(__p, piecewise_construct,
205 forward_as_tuple(__pr.first),
206 forward_as_tuple(__pr.second));
209 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
211 construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
213 this->construct(__p, piecewise_construct,
214 forward_as_tuple(std::forward<_Up>(__pr.first)),
215 forward_as_tuple(std::forward<_Vp>(__pr.second)));
218 template <typename _Up>
223 // Return a default-constructed allocator (no allocator propagation)
224 polymorphic_allocator
225 select_on_container_copy_construction() const
226 { return polymorphic_allocator(); }
228 memory_resource* resource() const { return _M_resource; }
231 using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
232 using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
234 template<typename _Tuple>
236 _M_construct_p(__uses_alloc0, _Tuple& __t)
237 { return std::move(__t); }
239 template<typename... _Args>
241 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
242 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
245 template<typename... _Args>
247 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
248 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
250 memory_resource* _M_resource;
253 template <class _Tp1, class _Tp2>
255 operator==(const polymorphic_allocator<_Tp1>& __a,
256 const polymorphic_allocator<_Tp2>& __b) noexcept
257 { return *__a.resource() == *__b.resource(); }
259 template <class _Tp1, class _Tp2>
261 operator!=(const polymorphic_allocator<_Tp1>& __a,
262 const polymorphic_allocator<_Tp2>& __b) noexcept
263 { return !(__a == __b); }
266 class __resource_adaptor_common
268 template<typename, typename> friend class __resource_adaptor_imp;
272 _AlignMgr(size_t __nbytes, size_t __align)
273 : _M_nbytes(__nbytes), _M_align(__align)
276 // Total size that needs to be allocated.
278 _M_alloc_size() const { return _M_buf_size() + _M_token_size(); }
281 _M_adjust(void* __ptr) const
283 const auto __orig_ptr = static_cast<char*>(__ptr);
284 size_t __space = _M_buf_size();
285 // Align the pointer within the buffer:
286 std::align(_M_align, _M_nbytes, __ptr, __space);
287 const auto __aligned_ptr = static_cast<char*>(__ptr);
288 const auto __token_size = _M_token_size();
289 // Store token immediately after the aligned block:
290 char* const __end = __aligned_ptr + _M_nbytes;
291 if (__token_size == 1)
292 _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr);
293 else if (__token_size == sizeof(short))
294 _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr);
295 else if (__token_size == sizeof(int) && sizeof(int) < sizeof(char*))
296 _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr);
297 else // (__token_size == sizeof(char*))
298 // Just store the original pointer:
299 _S_write<char*>(__end, __orig_ptr);
300 return __aligned_ptr;
304 _M_unadjust(char* __ptr) const
306 const char* const __end = __ptr + _M_nbytes;
308 const auto __token_size = _M_token_size();
309 // Read the token and restore the original pointer:
310 if (__token_size == 1)
311 __orig_ptr = __ptr - _S_read<unsigned char>(__end);
312 else if (__token_size == sizeof(short))
313 __orig_ptr = __ptr - _S_read<unsigned short>(__end);
314 else if (__token_size == sizeof(int)
315 && sizeof(int) < sizeof(char*))
316 __orig_ptr = __ptr - _S_read<unsigned int>(__end);
317 else // (__token_size == sizeof(char*))
318 __orig_ptr = _S_read<char*>(__end);
319 // The adjustment is always less than the requested alignment,
320 // so if that isn't true now then either the wrong size was passed
321 // to deallocate or the token was overwritten by a buffer overflow:
322 __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align);
330 // Number of bytes needed to fit block of given size and alignment.
332 _M_buf_size() const { return _M_nbytes + _M_align - 1; }
334 // Number of additional bytes needed to write the token.
336 _M_token_size() const
338 if (_M_align <= (1ul << __CHAR_BIT__))
340 if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__)))
341 return sizeof(short);
342 if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
344 return sizeof(char*);
347 template<typename _Tp>
349 _S_write(void* __to, _Tp __val)
350 { __builtin_memcpy(__to, &__val, sizeof(_Tp)); }
352 template<typename _Tp>
354 _S_read(const void* __from)
357 __builtin_memcpy(&__val, __from, sizeof(_Tp));
362 template<typename _Alloc>
363 struct __guaranteed_alignment : std::integral_constant<size_t, 1> { };
365 template<typename _Tp>
366 struct __guaranteed_alignment<__gnu_cxx::new_allocator<_Tp>>
367 : std::alignment_of<std::max_align_t>::type { };
369 template<typename _Tp>
370 struct __guaranteed_alignment<__gnu_cxx::malloc_allocator<_Tp>>
371 : std::alignment_of<std::max_align_t>::type { };
373#if _GLIBCXX_USE_ALLOCATOR_NEW
374 template<typename _Tp>
375 struct __guaranteed_alignment<std::allocator<_Tp>>
376 : std::alignment_of<std::max_align_t>::type { };
380 // 8.7.1 __resource_adaptor_imp
381 template<typename _Alloc, typename _Resource>
382 class __resource_adaptor_imp
383 : public _Resource, private __resource_adaptor_common
385 using memory_resource = _Resource;
387 static_assert(is_same<char,
388 typename allocator_traits<_Alloc>::value_type>::value,
389 "Allocator's value_type is char");
390 static_assert(is_same<char*,
391 typename allocator_traits<_Alloc>::pointer>::value,
392 "Allocator's pointer type is value_type*");
393 static_assert(is_same<const char*,
394 typename allocator_traits<_Alloc>::const_pointer>::value,
395 "Allocator's const_pointer type is value_type const*");
396 static_assert(is_same<void*,
397 typename allocator_traits<_Alloc>::void_pointer>::value,
398 "Allocator's void_pointer type is void*");
399 static_assert(is_same<const void*,
400 typename allocator_traits<_Alloc>::const_void_pointer>::value,
401 "Allocator's const_void_pointer type is void const*");
404 using allocator_type = _Alloc;
406 __resource_adaptor_imp() = default;
407 __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
408 __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
410 explicit __resource_adaptor_imp(const _Alloc& __a2)
414 explicit __resource_adaptor_imp(_Alloc&& __a2)
415 : _M_alloc(std::move(__a2))
418 __resource_adaptor_imp&
419 operator=(const __resource_adaptor_imp&) = default;
421 allocator_type get_allocator() const noexcept { return _M_alloc; }
425 do_allocate(size_t __bytes, size_t __alignment) override
427 if (__alignment <= __guaranteed_alignment<_Alloc>::value)
429 if (__bytes < __alignment)
430 __bytes = __alignment;
431 return _M_alloc.allocate(__bytes);
435 const _AlignMgr __mgr(__bytes, __alignment);
436 // Assume _M_alloc returns 1-byte aligned memory, so allocate enough
437 // space to fit a block of the right size and alignment, plus some
438 // extra bytes to store a token for retrieving the original pointer.
439 return __mgr._M_adjust(_M_alloc.allocate(__mgr._M_alloc_size()));
443 do_deallocate(void* __p, size_t __bytes, size_t __alignment) noexcept
446 auto __ptr = static_cast<char*>(__p);
447 if (__alignment <= __guaranteed_alignment<_Alloc>::value)
449 if (__bytes < __alignment)
450 __bytes = __alignment;
451 _M_alloc.deallocate(__ptr, __bytes);
455 const _AlignMgr __mgr(__bytes, __alignment);
456 // Use the stored token to retrieve the original pointer to deallocate.
457 _M_alloc.deallocate(__mgr._M_unadjust(__ptr), __mgr._M_alloc_size());
461 do_is_equal(const memory_resource& __other) const noexcept override
463 if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
464 return _M_alloc == __p->_M_alloc;
472 // Global memory resources
474 inline memory_resource*
475 new_delete_resource() noexcept
477 using type = resource_adaptor<__gnu_cxx::new_allocator<char>>;
478 alignas(type) static unsigned char __buf[sizeof(type)];
479 static type* __r = new(__buf) type;
483 inline memory_resource*
484 null_memory_resource() noexcept
486 class type final : public memory_resource
489 do_allocate(size_t, size_t) override
490 { std::__throw_bad_alloc(); }
493 do_deallocate(void*, size_t, size_t) noexcept override
497 do_is_equal(const memory_resource& __other) const noexcept override
498 { return this == &__other; }
501 alignas(type) static unsigned char __buf[sizeof(type)];
502 static type* __r = new(__buf) type;
506 // The default memory resource
508 inline std::atomic<memory_resource*>&
509 __get_default_resource()
511 using type = atomic<memory_resource*>;
512 alignas(type) static unsigned char __buf[sizeof(type)];
513 static type* __r = new(__buf) type(new_delete_resource());
517 inline memory_resource*
518 get_default_resource() noexcept
519 { return __get_default_resource().load(); }
521 inline memory_resource*
522 set_default_resource(memory_resource* __r) noexcept
525 __r = new_delete_resource();
526 return __get_default_resource().exchange(__r);
530} // namespace fundamentals_v2
531} // namespace experimental
533_GLIBCXX_END_NAMESPACE_VERSION
536#endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE