1 // <experimental/memory_resource> -*- C++ -*-
3 // Copyright (C) 2015 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>
39 namespace experimental {
40 inline namespace fundamentals_v2 {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 #define __cpp_lib_experimental_memory_resources 201402L
46 class memory_resource;
48 template <typename _Tp>
49 class polymorphic_allocator;
51 template <typename _Alloc>
52 class __resource_adaptor_imp;
54 template <typename _Alloc>
55 using resource_adaptor = __resource_adaptor_imp<
56 typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
58 template <typename _Tp>
59 struct __uses_allocator_construction_helper;
61 // Global memory resources
62 memory_resource* new_delete_resource() noexcept;
63 memory_resource* null_memory_resource() noexcept;
65 // The default memory resource
66 memory_resource* get_default_resource() noexcept;
67 memory_resource* set_default_resource(memory_resource* __r) noexcept;
69 // Standard memory resources
71 // 8.5 Class memory_resource
74 static constexpr size_t _S_max_align = alignof(max_align_t);
77 virtual ~memory_resource() { }
80 allocate(size_t __bytes, size_t __alignment = _S_max_align)
81 { return do_allocate(__bytes, __alignment); }
84 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
85 { return do_deallocate(__p, __bytes, __alignment); }
88 is_equal(const memory_resource& __other) const noexcept
89 { return do_is_equal(__other); }
93 do_allocate(size_t __bytes, size_t __alignment) = 0;
96 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
99 do_is_equal(const memory_resource& __other) const noexcept = 0;
103 operator==(const memory_resource& __a,
104 const memory_resource& __b) noexcept
105 { return &__a == &__b || __a.is_equal(__b); }
108 operator!=(const memory_resource& __a,
109 const memory_resource& __b) noexcept
110 { return !(__a == __b); }
113 // 8.6 Class template polymorphic_allocator
115 class polymorphic_allocator
117 using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
118 using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
120 template<typename _Tp1, typename... _Args>
122 _M_construct(__uses_alloc0, _Tp1* __p, _Args&&... __args)
123 { ::new(__p) _Tp1(std::forward<_Args>(__args)...); }
125 template<typename _Tp1, typename... _Args>
127 _M_construct(__uses_alloc1_, _Tp1* __p, _Args&&... __args)
128 { ::new(__p) _Tp1(allocator_arg, this->resource(),
129 std::forward<_Args>(__args)...); }
131 template<typename _Tp1, typename... _Args>
133 _M_construct(__uses_alloc2_, _Tp1* __p, _Args&&... __args)
134 { ::new(__p) _Tp1(std::forward<_Args>(__args)...,
138 using value_type = _Tp;
140 polymorphic_allocator() noexcept
141 : _M_resource(get_default_resource())
144 polymorphic_allocator(memory_resource* __r)
146 { _GLIBCXX_DEBUG_ASSERT(__r); }
148 polymorphic_allocator(const polymorphic_allocator& __other) = default;
150 template <typename _Up>
151 polymorphic_allocator(const polymorphic_allocator<_Up>&
153 : _M_resource(__other.resource())
156 polymorphic_allocator&
157 operator=(const polymorphic_allocator& __rhs) = default;
159 _Tp* allocate(size_t __n)
160 { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
163 void deallocate(_Tp* __p, size_t __n)
164 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
166 template <typename _Tp1, typename... _Args> //used here
167 void construct(_Tp1* __p, _Args&&... __args)
169 auto __use_tag = __use_alloc<_Tp1, memory_resource*,
170 _Args...>(this->resource());
171 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
174 // Specializations for pair using piecewise construction
175 template <typename _Tp1, typename _Tp2,
176 typename... _Args1, typename... _Args2>
177 void construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
178 tuple<_Args1...> __x,
179 tuple<_Args2...> __y)
182 __use_alloc<_Tp1, memory_resource*, _Args1...>(this->resource());
184 __use_alloc<_Tp2, memory_resource*, _Args2...>(this->resource());
186 ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
187 _M_construct_p(__x_use_tag, __x),
188 _M_construct_p(__y_use_tag, __y));
191 template <typename _Tp1, typename _Tp2>
192 void construct(pair<_Tp1,_Tp2>* __p)
193 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
195 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
196 void construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
197 { this->construct(__p, piecewise_construct,
198 forward_as_tuple(std::forward<_Up>(__x)),
199 forward_as_tuple(std::forward<_Vp>(__y))); }
201 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
202 void construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
203 { this->construct(__p, piecewise_construct, forward_as_tuple(__pr.first),
204 forward_as_tuple(__pr.second)); }
206 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
207 void construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
208 { this->construct(__p, piecewise_construct,
209 forward_as_tuple(std::forward<_Up>(__pr.first)),
210 forward_as_tuple(std::forward<_Vp>(__pr.second))); }
212 template <typename _Up>
213 void destroy(_Up* __p)
216 // Return a default-constructed allocator (no allocator propagation)
217 polymorphic_allocator select_on_container_copy_construction() const
218 { return polymorphic_allocator(); }
220 memory_resource* resource() const
221 { return _M_resource; }
224 template<typename _Tuple>
226 _M_construct_p(__uses_alloc0, _Tuple& __t)
227 { return std::move(__t); }
229 template<typename... _Args>
231 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
232 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
235 template<typename... _Args>
237 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
238 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
240 memory_resource* _M_resource;
243 template <class _Tp1, class _Tp2>
244 bool operator==(const polymorphic_allocator<_Tp1>& __a,
245 const polymorphic_allocator<_Tp2>& __b) noexcept
246 { return *__a.resource() == *__b.resource(); }
248 template <class _Tp1, class _Tp2>
249 bool operator!=(const polymorphic_allocator<_Tp1>& __a,
250 const polymorphic_allocator<_Tp2>& __b) noexcept
251 { return !(__a == __b); }
253 // 8.7.1 __resource_adaptor_imp
254 template <typename _Alloc>
255 class __resource_adaptor_imp : public memory_resource
258 using allocator_type = _Alloc;
260 __resource_adaptor_imp() = default;
261 __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
262 __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
264 explicit __resource_adaptor_imp(const _Alloc& __a2)
268 explicit __resource_adaptor_imp(_Alloc&& __a2)
269 : _M_alloc(std::move(__a2))
272 __resource_adaptor_imp&
273 operator=(const __resource_adaptor_imp&) = default;
275 allocator_type get_allocator() const { return _M_alloc; }
279 do_allocate(size_t __bytes, size_t __alignment)
281 using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
282 size_t __new_size = _S_aligned_size(__bytes,
283 _S_supported(__alignment) ?
284 __alignment : _S_max_align);
285 return _Aligned_alloc().allocate(__new_size);
289 do_deallocate(void* __p, size_t __bytes, size_t __alignment)
291 using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
292 size_t __new_size = _S_aligned_size(__bytes,
293 _S_supported(__alignment) ?
294 __alignment : _S_max_align);
295 _Aligned_alloc().deallocate(static_cast<typename
296 _Aligned_alloc::pointer>(__p),
301 do_is_equal(const memory_resource& __other) const noexcept
303 auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other);
304 return __p ? (_M_alloc == __p->_M_alloc) : false;
308 // Calculate Aligned Size
309 // Returns a size that is larger than or equal to __size and divided by
310 // __alignment, where __alignment is required to be the power of 2.
312 _S_aligned_size(size_t __size, size_t __alignment)
313 { return ((__size - 1)|(__alignment - 1)) + 1; }
315 // Determine whether alignment meets one of those preconditions:
317 // 2. Is power of two
319 _S_supported (size_t __x)
320 { return ((__x != 0) && !(__x & (__x - 1))); }
325 // Global memory resources
326 inline std::atomic<memory_resource*>&
327 __get_default_resource()
329 static atomic<memory_resource*> _S_default_resource(new_delete_resource());
330 return _S_default_resource;
333 inline memory_resource*
334 new_delete_resource() noexcept
336 static resource_adaptor<std::allocator<char>> __r;
337 return static_cast<memory_resource*>(&__r);
340 template <typename _Alloc>
341 class __null_memory_resource : private memory_resource
345 do_allocate(size_t __bytes, size_t __alignment)
346 { std::__throw_bad_alloc(); }
349 do_deallocate(void* __p, size_t __bytes, size_t __alignment)
353 do_is_equal(const memory_resource& __other) const noexcept
356 friend memory_resource* null_memory_resource() noexcept;
359 inline memory_resource*
360 null_memory_resource() noexcept
362 static __null_memory_resource<void> __r;
363 return static_cast<memory_resource*>(&__r);
366 // The default memory resource
367 inline memory_resource*
368 get_default_resource() noexcept
369 { return __get_default_resource().load(); }
371 inline memory_resource*
372 set_default_resource(memory_resource* __r) noexcept
375 __r = new_delete_resource();
376 return __get_default_resource().exchange(__r);
379 _GLIBCXX_END_NAMESPACE_VERSION
381 } // namespace fundamentals_v2
382 } // namespace experimental