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 #include <memory> // align, uses_allocator, __uses_alloc
33 #include <experimental/utility> // pair, experimental::erased_type
34 #include <atomic> // atomic
35 #include <new> // placement new
36 #include <cstddef> // max_align_t
37 #include <ext/new_allocator.h>
38 #include <debug/assertions.h>
40 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 template<typename _Tp> class malloc_allocator;
44 _GLIBCXX_END_NAMESPACE_VERSION
45 } // namespace __gnu_cxx
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 namespace experimental {
51 inline namespace fundamentals_v2 {
53 #define __cpp_lib_experimental_memory_resources 201402L
55 // Standard memory resources
57 // 8.5 Class memory_resource
58 class memory_resource;
60 // 8.6 Class template polymorphic_allocator
61 template<typename _Tp>
62 class polymorphic_allocator;
64 template<typename _Alloc, typename _Resource = memory_resource>
65 class __resource_adaptor_imp;
67 // 8.7 Alias template resource_adaptor
68 template<typename _Alloc>
69 using resource_adaptor = __resource_adaptor_imp<
70 typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
72 // 8.8 Global memory resources
73 memory_resource* new_delete_resource() noexcept;
74 memory_resource* null_memory_resource() noexcept;
75 memory_resource* get_default_resource() noexcept;
76 memory_resource* set_default_resource(memory_resource* __r) noexcept;
78 // TODO 8.9 Pool resource classes
82 static constexpr size_t _S_max_align = alignof(max_align_t);
85 memory_resource() = default;
86 memory_resource(const memory_resource&) = default;
87 virtual ~memory_resource() = default;
89 memory_resource& operator=(const memory_resource&) = default;
91 _GLIBCXX_NODISCARD void*
92 allocate(size_t __bytes, size_t __alignment = _S_max_align)
93 { return do_allocate(__bytes, __alignment); }
96 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
97 { return do_deallocate(__p, __bytes, __alignment); }
100 is_equal(const memory_resource& __other) const noexcept
101 { return do_is_equal(__other); }
105 do_allocate(size_t __bytes, size_t __alignment) = 0;
108 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
111 do_is_equal(const memory_resource& __other) const noexcept = 0;
115 operator==(const memory_resource& __a, const memory_resource& __b) noexcept
116 { return &__a == &__b || __a.is_equal(__b); }
119 operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
120 { return !(__a == __b); }
123 template<typename _Tp>
124 class polymorphic_allocator
127 using value_type = _Tp;
129 polymorphic_allocator() noexcept
130 : _M_resource(get_default_resource())
133 polymorphic_allocator(memory_resource* __r)
135 { _GLIBCXX_DEBUG_ASSERT(__r); }
137 polymorphic_allocator(const polymorphic_allocator& __other) = default;
139 template <typename _Up>
140 polymorphic_allocator(const polymorphic_allocator<_Up>&
142 : _M_resource(__other.resource())
145 polymorphic_allocator&
146 operator=(const polymorphic_allocator& __rhs) = default;
148 _GLIBCXX_NODISCARD _Tp* allocate(size_t __n)
149 { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
153 deallocate(_Tp* __p, size_t __n)
154 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
156 template <typename _Tp1, typename... _Args> //used here
158 construct(_Tp1* __p, _Args&&... __args)
160 std::__uses_allocator_construct(this->resource(), __p,
161 std::forward<_Args>(__args)...);
164 // Specializations for pair using piecewise construction
165 template <typename _Tp1, typename _Tp2,
166 typename... _Args1, typename... _Args2>
168 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
169 tuple<_Args1...> __x, tuple<_Args2...> __y)
171 memory_resource* const __resource = this->resource();
173 std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
175 std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
177 ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
178 _M_construct_p(__x_use_tag, __x),
179 _M_construct_p(__y_use_tag, __y));
182 template <typename _Tp1, typename _Tp2>
184 construct(pair<_Tp1,_Tp2>* __p)
185 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
187 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
189 construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
191 this->construct(__p, piecewise_construct,
192 forward_as_tuple(std::forward<_Up>(__x)),
193 forward_as_tuple(std::forward<_Vp>(__y)));
196 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
198 construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
200 this->construct(__p, piecewise_construct,
201 forward_as_tuple(__pr.first),
202 forward_as_tuple(__pr.second));
205 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
207 construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
209 this->construct(__p, piecewise_construct,
210 forward_as_tuple(std::forward<_Up>(__pr.first)),
211 forward_as_tuple(std::forward<_Vp>(__pr.second)));
214 template <typename _Up>
219 // Return a default-constructed allocator (no allocator propagation)
220 polymorphic_allocator
221 select_on_container_copy_construction() const
222 { return polymorphic_allocator(); }
224 memory_resource* resource() const { return _M_resource; }
227 using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
228 using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
230 template<typename _Tuple>
232 _M_construct_p(__uses_alloc0, _Tuple& __t)
233 { return std::move(__t); }
235 template<typename... _Args>
237 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
238 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
241 template<typename... _Args>
243 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
244 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
246 memory_resource* _M_resource;
249 template <class _Tp1, class _Tp2>
251 operator==(const polymorphic_allocator<_Tp1>& __a,
252 const polymorphic_allocator<_Tp2>& __b) noexcept
253 { return *__a.resource() == *__b.resource(); }
255 template <class _Tp1, class _Tp2>
257 operator!=(const polymorphic_allocator<_Tp1>& __a,
258 const polymorphic_allocator<_Tp2>& __b) noexcept
259 { return !(__a == __b); }
262 class __resource_adaptor_common
264 template<typename, typename> friend class __resource_adaptor_imp;
268 _AlignMgr(size_t __nbytes, size_t __align)
269 : _M_nbytes(__nbytes), _M_align(__align)
272 // Total size that needs to be allocated.
274 _M_alloc_size() const { return _M_buf_size() + _M_token_size(); }
277 _M_adjust(void* __ptr) const
279 const auto __orig_ptr = static_cast<char*>(__ptr);
280 size_t __space = _M_buf_size();
281 // Align the pointer within the buffer:
282 std::align(_M_align, _M_nbytes, __ptr, __space);
283 const auto __aligned_ptr = static_cast<char*>(__ptr);
284 const auto __token_size = _M_token_size();
285 // Store token immediately after the aligned block:
286 char* const __end = __aligned_ptr + _M_nbytes;
287 if (__token_size == 1)
288 _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr);
289 else if (__token_size == sizeof(short))
290 _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr);
291 else if (__token_size == sizeof(int) && sizeof(int) < sizeof(char*))
292 _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr);
293 else // (__token_size == sizeof(char*))
294 // Just store the original pointer:
295 _S_write<char*>(__end, __orig_ptr);
296 return __aligned_ptr;
300 _M_unadjust(char* __ptr) const
302 const char* const __end = __ptr + _M_nbytes;
304 const auto __token_size = _M_token_size();
305 // Read the token and restore the original pointer:
306 if (__token_size == 1)
307 __orig_ptr = __ptr - _S_read<unsigned char>(__end);
308 else if (__token_size == sizeof(short))
309 __orig_ptr = __ptr - _S_read<unsigned short>(__end);
310 else if (__token_size == sizeof(int)
311 && sizeof(int) < sizeof(char*))
312 __orig_ptr = __ptr - _S_read<unsigned int>(__end);
313 else // (__token_size == sizeof(char*))
314 __orig_ptr = _S_read<char*>(__end);
315 // The adjustment is always less than the requested alignment,
316 // so if that isn't true now then either the wrong size was passed
317 // to deallocate or the token was overwritten by a buffer overflow:
318 __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align);
326 // Number of bytes needed to fit block of given size and alignment.
328 _M_buf_size() const { return _M_nbytes + _M_align - 1; }
330 // Number of additional bytes needed to write the token.
332 _M_token_size() const
334 if (_M_align <= (1ul << __CHAR_BIT__))
336 if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__)))
337 return sizeof(short);
338 if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
340 return sizeof(char*);
343 template<typename _Tp>
345 _S_write(void* __to, _Tp __val)
346 { __builtin_memcpy(__to, &__val, sizeof(_Tp)); }
348 template<typename _Tp>
350 _S_read(const void* __from)
353 __builtin_memcpy(&__val, __from, sizeof(_Tp));
358 template<typename _Alloc>
359 struct __guaranteed_alignment : std::integral_constant<size_t, 1> { };
361 template<typename _Tp>
362 struct __guaranteed_alignment<__gnu_cxx::new_allocator<_Tp>>
363 : std::alignment_of<std::max_align_t>::type { };
365 template<typename _Tp>
366 struct __guaranteed_alignment<__gnu_cxx::malloc_allocator<_Tp>>
367 : std::alignment_of<std::max_align_t>::type { };
369 #if _GLIBCXX_USE_ALLOCATOR_NEW
370 template<typename _Tp>
371 struct __guaranteed_alignment<std::allocator<_Tp>>
372 : std::alignment_of<std::max_align_t>::type { };
376 // 8.7.1 __resource_adaptor_imp
377 template<typename _Alloc, typename _Resource>
378 class __resource_adaptor_imp
379 : public _Resource, private __resource_adaptor_common
381 using memory_resource = _Resource;
383 static_assert(is_same<char,
384 typename allocator_traits<_Alloc>::value_type>::value,
385 "Allocator's value_type is char");
386 static_assert(is_same<char*,
387 typename allocator_traits<_Alloc>::pointer>::value,
388 "Allocator's pointer type is value_type*");
389 static_assert(is_same<const char*,
390 typename allocator_traits<_Alloc>::const_pointer>::value,
391 "Allocator's const_pointer type is value_type const*");
392 static_assert(is_same<void*,
393 typename allocator_traits<_Alloc>::void_pointer>::value,
394 "Allocator's void_pointer type is void*");
395 static_assert(is_same<const void*,
396 typename allocator_traits<_Alloc>::const_void_pointer>::value,
397 "Allocator's const_void_pointer type is void const*");
400 using allocator_type = _Alloc;
402 __resource_adaptor_imp() = default;
403 __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
404 __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
406 explicit __resource_adaptor_imp(const _Alloc& __a2)
410 explicit __resource_adaptor_imp(_Alloc&& __a2)
411 : _M_alloc(std::move(__a2))
414 __resource_adaptor_imp&
415 operator=(const __resource_adaptor_imp&) = default;
417 allocator_type get_allocator() const noexcept { return _M_alloc; }
421 do_allocate(size_t __bytes, size_t __alignment) override
423 if (__alignment <= __guaranteed_alignment<_Alloc>::value)
425 if (__bytes < __alignment)
426 __bytes = __alignment;
427 return _M_alloc.allocate(__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* __p, size_t __bytes, size_t __alignment) noexcept
442 auto __ptr = static_cast<char*>(__p);
443 if (__alignment <= __guaranteed_alignment<_Alloc>::value)
445 if (__bytes < __alignment)
446 __bytes = __alignment;
447 _M_alloc.deallocate(__ptr, __bytes);
451 const _AlignMgr __mgr(__bytes, __alignment);
452 // Use the stored token to retrieve the original pointer to deallocate.
453 _M_alloc.deallocate(__mgr._M_unadjust(__ptr), __mgr._M_alloc_size());
457 do_is_equal(const memory_resource& __other) const noexcept override
459 if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
460 return _M_alloc == __p->_M_alloc;
468 // Global memory resources
470 inline memory_resource*
471 new_delete_resource() noexcept
473 using type = resource_adaptor<__gnu_cxx::new_allocator<char>>;
474 alignas(type) static unsigned char __buf[sizeof(type)];
475 static type* __r = new(__buf) type;
479 inline memory_resource*
480 null_memory_resource() noexcept
482 class type final : public memory_resource
485 do_allocate(size_t, size_t) override
486 { std::__throw_bad_alloc(); }
489 do_deallocate(void*, size_t, size_t) noexcept override
493 do_is_equal(const memory_resource& __other) const noexcept override
494 { return this == &__other; }
497 alignas(type) static unsigned char __buf[sizeof(type)];
498 static type* __r = new(__buf) type;
502 // The default memory resource
504 inline std::atomic<memory_resource*>&
505 __get_default_resource()
507 using type = atomic<memory_resource*>;
508 alignas(type) static unsigned char __buf[sizeof(type)];
509 static type* __r = new(__buf) type(new_delete_resource());
513 inline memory_resource*
514 get_default_resource() noexcept
515 { return __get_default_resource().load(); }
517 inline memory_resource*
518 set_default_resource(memory_resource* __r) noexcept
521 __r = new_delete_resource();
522 return __get_default_resource().exchange(__r);
526 } // namespace fundamentals_v2
527 } // namespace experimental
529 _GLIBCXX_END_NAMESPACE_VERSION
531 #endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE