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 <experimental/bits/lfts_config.h>
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 namespace experimental {
42 inline namespace fundamentals_v2 {
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
75 static constexpr size_t _S_max_align = alignof(max_align_t);
78 virtual ~memory_resource() { }
81 allocate(size_t __bytes, size_t __alignment = _S_max_align)
82 { return do_allocate(__bytes, __alignment); }
85 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
86 { return do_deallocate(__p, __bytes, __alignment); }
89 is_equal(const memory_resource& __other) const noexcept
90 { return do_is_equal(__other); }
94 do_allocate(size_t __bytes, size_t __alignment) = 0;
97 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
100 do_is_equal(const memory_resource& __other) const noexcept = 0;
104 operator==(const memory_resource& __a,
105 const memory_resource& __b) noexcept
106 { return &__a == &__b || __a.is_equal(__b); }
109 operator!=(const memory_resource& __a,
110 const memory_resource& __b) noexcept
111 { return !(__a == __b); }
114 // 8.6 Class template polymorphic_allocator
116 class polymorphic_allocator
118 using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
119 using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
121 template<typename _Tp1, typename... _Args>
123 _M_construct(__uses_alloc0, _Tp1* __p, _Args&&... __args)
124 { ::new(__p) _Tp1(std::forward<_Args>(__args)...); }
126 template<typename _Tp1, typename... _Args>
128 _M_construct(__uses_alloc1_, _Tp1* __p, _Args&&... __args)
129 { ::new(__p) _Tp1(allocator_arg, this->resource(),
130 std::forward<_Args>(__args)...); }
132 template<typename _Tp1, typename... _Args>
134 _M_construct(__uses_alloc2_, _Tp1* __p, _Args&&... __args)
135 { ::new(__p) _Tp1(std::forward<_Args>(__args)...,
139 using value_type = _Tp;
141 polymorphic_allocator() noexcept
142 : _M_resource(get_default_resource())
145 polymorphic_allocator(memory_resource* __r)
147 { _GLIBCXX_DEBUG_ASSERT(__r); }
149 polymorphic_allocator(const polymorphic_allocator& __other) = default;
151 template <typename _Up>
152 polymorphic_allocator(const polymorphic_allocator<_Up>&
154 : _M_resource(__other.resource())
157 polymorphic_allocator&
158 operator=(const polymorphic_allocator& __rhs) = default;
160 _Tp* allocate(size_t __n)
161 { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
164 void deallocate(_Tp* __p, size_t __n)
165 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
167 template <typename _Tp1, typename... _Args> //used here
168 void construct(_Tp1* __p, _Args&&... __args)
170 memory_resource* const __resource = this->resource();
172 = __use_alloc<_Tp1, memory_resource*, _Args...>(__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)
183 memory_resource* const __resource = this->resource();
185 __use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
187 __use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
189 ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
190 _M_construct_p(__x_use_tag, __x),
191 _M_construct_p(__y_use_tag, __y));
194 template <typename _Tp1, typename _Tp2>
195 void construct(pair<_Tp1,_Tp2>* __p)
196 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
198 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
199 void construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
200 { this->construct(__p, piecewise_construct,
201 forward_as_tuple(std::forward<_Up>(__x)),
202 forward_as_tuple(std::forward<_Vp>(__y))); }
204 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
205 void construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
206 { this->construct(__p, piecewise_construct, forward_as_tuple(__pr.first),
207 forward_as_tuple(__pr.second)); }
209 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
210 void construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
211 { this->construct(__p, piecewise_construct,
212 forward_as_tuple(std::forward<_Up>(__pr.first)),
213 forward_as_tuple(std::forward<_Vp>(__pr.second))); }
215 template <typename _Up>
216 void destroy(_Up* __p)
219 // Return a default-constructed allocator (no allocator propagation)
220 polymorphic_allocator select_on_container_copy_construction() const
221 { return polymorphic_allocator(); }
223 memory_resource* resource() const
224 { return _M_resource; }
227 template<typename _Tuple>
229 _M_construct_p(__uses_alloc0, _Tuple& __t)
230 { return std::move(__t); }
232 template<typename... _Args>
234 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
235 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
238 template<typename... _Args>
240 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
241 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
243 memory_resource* _M_resource;
246 template <class _Tp1, class _Tp2>
247 bool operator==(const polymorphic_allocator<_Tp1>& __a,
248 const polymorphic_allocator<_Tp2>& __b) noexcept
249 { return *__a.resource() == *__b.resource(); }
251 template <class _Tp1, class _Tp2>
252 bool operator!=(const polymorphic_allocator<_Tp1>& __a,
253 const polymorphic_allocator<_Tp2>& __b) noexcept
254 { return !(__a == __b); }
256 // 8.7.1 __resource_adaptor_imp
257 template <typename _Alloc>
258 class __resource_adaptor_imp : public memory_resource
260 static_assert(is_same<char,
261 typename allocator_traits<_Alloc>::value_type>::value,
262 "Allocator's value_type is char");
263 static_assert(is_same<char*,
264 typename allocator_traits<_Alloc>::pointer>::value,
265 "Allocator's pointer type is value_type*");
266 static_assert(is_same<const char*,
267 typename allocator_traits<_Alloc>::const_pointer>::value,
268 "Allocator's const_pointer type is value_type const*");
269 static_assert(is_same<void*,
270 typename allocator_traits<_Alloc>::void_pointer>::value,
271 "Allocator's void_pointer type is void*");
272 static_assert(is_same<const void*,
273 typename allocator_traits<_Alloc>::const_void_pointer>::value,
274 "Allocator's const_void_pointer type is void const*");
277 using allocator_type = _Alloc;
279 __resource_adaptor_imp() = default;
280 __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
281 __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
283 explicit __resource_adaptor_imp(const _Alloc& __a2)
287 explicit __resource_adaptor_imp(_Alloc&& __a2)
288 : _M_alloc(std::move(__a2))
291 __resource_adaptor_imp&
292 operator=(const __resource_adaptor_imp&) = default;
294 allocator_type get_allocator() const noexcept { return _M_alloc; }
298 do_allocate(size_t __bytes, size_t __alignment)
300 using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
301 size_t __new_size = _S_aligned_size(__bytes,
302 _S_supported(__alignment) ?
303 __alignment : _S_max_align);
304 return _Aligned_alloc(_M_alloc).allocate(__new_size);
308 do_deallocate(void* __p, size_t __bytes, size_t __alignment)
310 using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
311 size_t __new_size = _S_aligned_size(__bytes,
312 _S_supported(__alignment) ?
313 __alignment : _S_max_align);
314 using _Ptr = typename allocator_traits<_Aligned_alloc>::pointer;
315 _Aligned_alloc(_M_alloc).deallocate(static_cast<_Ptr>(__p),
320 do_is_equal(const memory_resource& __other) const noexcept
322 auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other);
323 return __p ? (_M_alloc == __p->_M_alloc) : false;
327 // Calculate Aligned Size
328 // Returns a size that is larger than or equal to __size and divisible
329 // by __alignment, where __alignment is required to be a power of 2.
331 _S_aligned_size(size_t __size, size_t __alignment)
332 { return ((__size - 1)|(__alignment - 1)) + 1; }
334 // Determine whether alignment meets one of those preconditions:
336 // 2. Is power of two
338 _S_supported (size_t __x)
339 { return ((__x != 0) && !(__x & (__x - 1))); }
344 // Global memory resources
346 inline memory_resource*
347 new_delete_resource() noexcept
349 using type = resource_adaptor<std::allocator<char>>;
350 alignas(type) static unsigned char __buf[sizeof(type)];
351 static type* __r = new(__buf) type;
355 inline memory_resource*
356 null_memory_resource() noexcept
358 class type final : public memory_resource
361 do_allocate(size_t, size_t) override
362 { std::__throw_bad_alloc(); }
365 do_deallocate(void*, size_t, size_t) noexcept override
369 do_is_equal(const memory_resource& __other) const noexcept override
370 { return this == &__other; }
373 alignas(type) static unsigned char __buf[sizeof(type)];
374 static type* __r = new(__buf) type;
378 // The default memory resource
380 inline std::atomic<memory_resource*>&
381 __get_default_resource()
383 using type = atomic<memory_resource*>;
384 alignas(type) static unsigned char __buf[sizeof(type)];
385 static type* __r = new(__buf) type(new_delete_resource());
389 inline memory_resource*
390 get_default_resource() noexcept
391 { return __get_default_resource().load(); }
393 inline memory_resource*
394 set_default_resource(memory_resource* __r) noexcept
397 __r = new_delete_resource();
398 return __get_default_resource().exchange(__r);
401 } // namespace fundamentals_v2
402 } // namespace experimental
404 _GLIBCXX_END_NAMESPACE_VERSION