1 // <experimental/memory_resource> -*- C++ -*-
3 // Copyright (C) 2015-2017 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
36 #include <bits/alloc_traits.h>
37 #include <experimental/bits/lfts_config.h>
40 namespace experimental {
41 inline namespace fundamentals_v2 {
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 #define __cpp_lib_experimental_memory_resources 201402L
47 class memory_resource;
49 template <typename _Tp>
50 class polymorphic_allocator;
52 template <typename _Alloc>
53 class __resource_adaptor_imp;
55 template <typename _Alloc>
56 using resource_adaptor = __resource_adaptor_imp<
57 typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
59 template <typename _Tp>
60 struct __uses_allocator_construction_helper;
62 // Global memory resources
63 memory_resource* new_delete_resource() noexcept;
64 memory_resource* null_memory_resource() noexcept;
66 // The default memory resource
67 memory_resource* get_default_resource() noexcept;
68 memory_resource* set_default_resource(memory_resource* __r) noexcept;
70 // Standard memory resources
72 // 8.5 Class memory_resource
76 static constexpr size_t _S_max_align = alignof(max_align_t);
79 virtual ~memory_resource() { }
82 allocate(size_t __bytes, size_t __alignment = _S_max_align)
83 { return do_allocate(__bytes, __alignment); }
86 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
87 { return do_deallocate(__p, __bytes, __alignment); }
90 is_equal(const memory_resource& __other) const noexcept
91 { return do_is_equal(__other); }
95 do_allocate(size_t __bytes, size_t __alignment) = 0;
98 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
101 do_is_equal(const memory_resource& __other) const noexcept = 0;
105 operator==(const memory_resource& __a,
106 const memory_resource& __b) noexcept
107 { return &__a == &__b || __a.is_equal(__b); }
110 operator!=(const memory_resource& __a,
111 const memory_resource& __b) noexcept
112 { return !(__a == __b); }
115 // 8.6 Class template polymorphic_allocator
117 class polymorphic_allocator
119 using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
120 using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
122 template<typename _Tp1, typename... _Args>
124 _M_construct(__uses_alloc0, _Tp1* __p, _Args&&... __args)
125 { ::new(__p) _Tp1(std::forward<_Args>(__args)...); }
127 template<typename _Tp1, typename... _Args>
129 _M_construct(__uses_alloc1_, _Tp1* __p, _Args&&... __args)
130 { ::new(__p) _Tp1(allocator_arg, this->resource(),
131 std::forward<_Args>(__args)...); }
133 template<typename _Tp1, typename... _Args>
135 _M_construct(__uses_alloc2_, _Tp1* __p, _Args&&... __args)
136 { ::new(__p) _Tp1(std::forward<_Args>(__args)...,
140 using value_type = _Tp;
142 polymorphic_allocator() noexcept
143 : _M_resource(get_default_resource())
146 polymorphic_allocator(memory_resource* __r)
148 { _GLIBCXX_DEBUG_ASSERT(__r); }
150 polymorphic_allocator(const polymorphic_allocator& __other) = default;
152 template <typename _Up>
153 polymorphic_allocator(const polymorphic_allocator<_Up>&
155 : _M_resource(__other.resource())
158 polymorphic_allocator&
159 operator=(const polymorphic_allocator& __rhs) = default;
161 _Tp* allocate(size_t __n)
162 { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
165 void deallocate(_Tp* __p, size_t __n)
166 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
168 template <typename _Tp1, typename... _Args> //used here
169 void construct(_Tp1* __p, _Args&&... __args)
171 auto __use_tag = __use_alloc<_Tp1, memory_resource*,
172 _Args...>(this->resource());
173 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
176 // Specializations for pair using piecewise construction
177 template <typename _Tp1, typename _Tp2,
178 typename... _Args1, typename... _Args2>
179 void construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
180 tuple<_Args1...> __x,
181 tuple<_Args2...> __y)
184 __use_alloc<_Tp1, memory_resource*, _Args1...>(this->resource());
186 __use_alloc<_Tp2, memory_resource*, _Args2...>(this->resource());
188 ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
189 _M_construct_p(__x_use_tag, __x),
190 _M_construct_p(__y_use_tag, __y));
193 template <typename _Tp1, typename _Tp2>
194 void construct(pair<_Tp1,_Tp2>* __p)
195 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
197 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
198 void construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
199 { this->construct(__p, piecewise_construct,
200 forward_as_tuple(std::forward<_Up>(__x)),
201 forward_as_tuple(std::forward<_Vp>(__y))); }
203 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
204 void construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
205 { this->construct(__p, piecewise_construct, forward_as_tuple(__pr.first),
206 forward_as_tuple(__pr.second)); }
208 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
209 void construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
210 { this->construct(__p, piecewise_construct,
211 forward_as_tuple(std::forward<_Up>(__pr.first)),
212 forward_as_tuple(std::forward<_Vp>(__pr.second))); }
214 template <typename _Up>
215 void destroy(_Up* __p)
218 // Return a default-constructed allocator (no allocator propagation)
219 polymorphic_allocator select_on_container_copy_construction() const
220 { return polymorphic_allocator(); }
222 memory_resource* resource() const
223 { return _M_resource; }
226 template<typename _Tuple>
228 _M_construct_p(__uses_alloc0, _Tuple& __t)
229 { return std::move(__t); }
231 template<typename... _Args>
233 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
234 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
237 template<typename... _Args>
239 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
240 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
242 memory_resource* _M_resource;
245 template <class _Tp1, class _Tp2>
246 bool operator==(const polymorphic_allocator<_Tp1>& __a,
247 const polymorphic_allocator<_Tp2>& __b) noexcept
248 { return *__a.resource() == *__b.resource(); }
250 template <class _Tp1, class _Tp2>
251 bool operator!=(const polymorphic_allocator<_Tp1>& __a,
252 const polymorphic_allocator<_Tp2>& __b) noexcept
253 { return !(__a == __b); }
255 // 8.7.1 __resource_adaptor_imp
256 template <typename _Alloc>
257 class __resource_adaptor_imp : public memory_resource
260 using allocator_type = _Alloc;
262 __resource_adaptor_imp() = default;
263 __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
264 __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
266 explicit __resource_adaptor_imp(const _Alloc& __a2)
270 explicit __resource_adaptor_imp(_Alloc&& __a2)
271 : _M_alloc(std::move(__a2))
274 __resource_adaptor_imp&
275 operator=(const __resource_adaptor_imp&) = default;
277 allocator_type get_allocator() const { return _M_alloc; }
281 do_allocate(size_t __bytes, size_t __alignment)
283 using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
284 size_t __new_size = _S_aligned_size(__bytes,
285 _S_supported(__alignment) ?
286 __alignment : _S_max_align);
287 return _Aligned_alloc(_M_alloc).allocate(__new_size);
291 do_deallocate(void* __p, size_t __bytes, size_t __alignment)
293 using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
294 size_t __new_size = _S_aligned_size(__bytes,
295 _S_supported(__alignment) ?
296 __alignment : _S_max_align);
297 using _Ptr = typename allocator_traits<_Aligned_alloc>::pointer;
298 _Aligned_alloc(_M_alloc).deallocate(static_cast<_Ptr>(__p),
303 do_is_equal(const memory_resource& __other) const noexcept
305 auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other);
306 return __p ? (_M_alloc == __p->_M_alloc) : false;
310 // Calculate Aligned Size
311 // Returns a size that is larger than or equal to __size and divisible
312 // by __alignment, where __alignment is required to be the power of 2.
314 _S_aligned_size(size_t __size, size_t __alignment)
315 { return ((__size - 1)|(__alignment - 1)) + 1; }
317 // Determine whether alignment meets one of those preconditions:
319 // 2. Is power of two
321 _S_supported (size_t __x)
322 { return ((__x != 0) && !(__x & (__x - 1))); }
327 // Global memory resources
328 inline std::atomic<memory_resource*>&
329 __get_default_resource()
331 static atomic<memory_resource*> _S_default_resource(new_delete_resource());
332 return _S_default_resource;
335 inline memory_resource*
336 new_delete_resource() noexcept
338 static resource_adaptor<std::allocator<char>> __r;
339 return static_cast<memory_resource*>(&__r);
342 template <typename _Alloc>
343 class __null_memory_resource : private memory_resource
347 do_allocate(size_t, size_t)
348 { std::__throw_bad_alloc(); }
351 do_deallocate(void*, size_t, size_t) noexcept
355 do_is_equal(const memory_resource& __other) const noexcept
356 { return this == &__other; }
358 friend memory_resource* null_memory_resource() noexcept;
361 inline memory_resource*
362 null_memory_resource() noexcept
364 static __null_memory_resource<void> __r;
365 return static_cast<memory_resource*>(&__r);
368 // The default memory resource
369 inline memory_resource*
370 get_default_resource() noexcept
371 { return __get_default_resource().load(); }
373 inline memory_resource*
374 set_default_resource(memory_resource* __r) noexcept
377 __r = new_delete_resource();
378 return __get_default_resource().exchange(__r);
381 _GLIBCXX_END_NAMESPACE_VERSION
383 } // namespace fundamentals_v2
384 } // namespace experimental