1 // <experimental/memory_resource> -*- C++ -*-
3 // Copyright (C) 2015-2018 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 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 namespace experimental {
43 inline namespace fundamentals_v2 {
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 memory_resource* const __resource = this->resource();
173 = __use_alloc<_Tp1, memory_resource*, _Args...>(__resource);
174 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
177 // Specializations for pair using piecewise construction
178 template <typename _Tp1, typename _Tp2,
179 typename... _Args1, typename... _Args2>
180 void construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
181 tuple<_Args1...> __x,
182 tuple<_Args2...> __y)
184 memory_resource* const __resource = this->resource();
186 __use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
188 __use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
190 ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
191 _M_construct_p(__x_use_tag, __x),
192 _M_construct_p(__y_use_tag, __y));
195 template <typename _Tp1, typename _Tp2>
196 void construct(pair<_Tp1,_Tp2>* __p)
197 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
199 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
200 void construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
201 { this->construct(__p, piecewise_construct,
202 forward_as_tuple(std::forward<_Up>(__x)),
203 forward_as_tuple(std::forward<_Vp>(__y))); }
205 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
206 void construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
207 { this->construct(__p, piecewise_construct, forward_as_tuple(__pr.first),
208 forward_as_tuple(__pr.second)); }
210 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
211 void construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
212 { this->construct(__p, piecewise_construct,
213 forward_as_tuple(std::forward<_Up>(__pr.first)),
214 forward_as_tuple(std::forward<_Vp>(__pr.second))); }
216 template <typename _Up>
217 void destroy(_Up* __p)
220 // Return a default-constructed allocator (no allocator propagation)
221 polymorphic_allocator select_on_container_copy_construction() const
222 { return polymorphic_allocator(); }
224 memory_resource* resource() const
225 { return _M_resource; }
228 template<typename _Tuple>
230 _M_construct_p(__uses_alloc0, _Tuple& __t)
231 { return std::move(__t); }
233 template<typename... _Args>
235 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
236 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
239 template<typename... _Args>
241 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
242 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
244 memory_resource* _M_resource;
247 template <class _Tp1, class _Tp2>
248 bool operator==(const polymorphic_allocator<_Tp1>& __a,
249 const polymorphic_allocator<_Tp2>& __b) noexcept
250 { return *__a.resource() == *__b.resource(); }
252 template <class _Tp1, class _Tp2>
253 bool operator!=(const polymorphic_allocator<_Tp1>& __a,
254 const polymorphic_allocator<_Tp2>& __b) noexcept
255 { return !(__a == __b); }
257 // 8.7.1 __resource_adaptor_imp
258 template <typename _Alloc>
259 class __resource_adaptor_imp : public memory_resource
262 using allocator_type = _Alloc;
264 __resource_adaptor_imp() = default;
265 __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
266 __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
268 explicit __resource_adaptor_imp(const _Alloc& __a2)
272 explicit __resource_adaptor_imp(_Alloc&& __a2)
273 : _M_alloc(std::move(__a2))
276 __resource_adaptor_imp&
277 operator=(const __resource_adaptor_imp&) = default;
279 allocator_type get_allocator() const { return _M_alloc; }
283 do_allocate(size_t __bytes, size_t __alignment)
285 using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
286 size_t __new_size = _S_aligned_size(__bytes,
287 _S_supported(__alignment) ?
288 __alignment : _S_max_align);
289 return _Aligned_alloc(_M_alloc).allocate(__new_size);
293 do_deallocate(void* __p, size_t __bytes, size_t __alignment)
295 using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
296 size_t __new_size = _S_aligned_size(__bytes,
297 _S_supported(__alignment) ?
298 __alignment : _S_max_align);
299 using _Ptr = typename allocator_traits<_Aligned_alloc>::pointer;
300 _Aligned_alloc(_M_alloc).deallocate(static_cast<_Ptr>(__p),
305 do_is_equal(const memory_resource& __other) const noexcept
307 auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other);
308 return __p ? (_M_alloc == __p->_M_alloc) : false;
312 // Calculate Aligned Size
313 // Returns a size that is larger than or equal to __size and divisible
314 // by __alignment, where __alignment is required to be the power of 2.
316 _S_aligned_size(size_t __size, size_t __alignment)
317 { return ((__size - 1)|(__alignment - 1)) + 1; }
319 // Determine whether alignment meets one of those preconditions:
321 // 2. Is power of two
323 _S_supported (size_t __x)
324 { return ((__x != 0) && !(__x & (__x - 1))); }
329 // Global memory resources
330 inline std::atomic<memory_resource*>&
331 __get_default_resource()
333 static atomic<memory_resource*> _S_default_resource(new_delete_resource());
334 return _S_default_resource;
337 inline memory_resource*
338 new_delete_resource() noexcept
340 static resource_adaptor<std::allocator<char>> __r;
341 return static_cast<memory_resource*>(&__r);
344 template <typename _Alloc>
345 class __null_memory_resource : private memory_resource
349 do_allocate(size_t, size_t)
350 { std::__throw_bad_alloc(); }
353 do_deallocate(void*, size_t, size_t) noexcept
357 do_is_equal(const memory_resource& __other) const noexcept
358 { return this == &__other; }
360 friend memory_resource* null_memory_resource() noexcept;
363 inline memory_resource*
364 null_memory_resource() noexcept
366 static __null_memory_resource<void> __r;
367 return static_cast<memory_resource*>(&__r);
370 // The default memory resource
371 inline memory_resource*
372 get_default_resource() noexcept
373 { return __get_default_resource().load(); }
375 inline memory_resource*
376 set_default_resource(memory_resource* __r) noexcept
379 __r = new_delete_resource();
380 return __get_default_resource().exchange(__r);
383 } // namespace fundamentals_v2
384 } // namespace experimental
386 _GLIBCXX_END_NAMESPACE_VERSION