29 #ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE
30 #define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1
40 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 _GLIBCXX_END_NAMESPACE_VERSION
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 namespace experimental {
51 inline namespace fundamentals_v2 {
53 #define __cpp_lib_experimental_memory_resources 201402L
58 class memory_resource;
61 template<
typename _Tp>
62 class polymorphic_allocator;
64 template<
typename _Alloc,
typename _Resource = memory_resource>
65 class __resource_adaptor_imp;
68 template<
typename _Alloc>
69 using resource_adaptor = __resource_adaptor_imp<
70 typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
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;
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;
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>
158 construct(_Tp1* __p, _Args&&... __args)
160 std::__uses_allocator_construct(this->resource(), __p,
161 std::forward<_Args>(__args)...);
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);
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,
196 template <
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
200 this->construct(__p, piecewise_construct,
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,
214 template <
typename _Up>
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)
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();
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();
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);
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();
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);
314 __orig_ptr = _S_read<char*>(__end);
318 __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align);
328 _M_buf_size()
const {
return _M_nbytes + _M_align - 1; }
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>
361 template<
typename _Tp>
362 struct __guaranteed_alignment<__gnu_cxx::new_allocator<_Tp>>
365 template<
typename _Tp>
366 struct __guaranteed_alignment<__gnu_cxx::malloc_allocator<_Tp>>
369 #if _GLIBCXX_USE_ALLOCATOR_NEW
370 template<
typename _Tp>
371 struct __guaranteed_alignment<std::allocator<_Tp>>
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);
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);
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;
470 inline memory_resource*
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*
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;
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*
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);
529 _GLIBCXX_END_NAMESPACE_VERSION
531 #endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE
An allocator that uses malloc.This is precisely the allocator defined in the C++ Standard.
void * align(size_t __align, size_t __size, void *&__ptr, size_t &__space) noexcept
Fit aligned storage in buffer.
Struct holding two objects of arbitrary type.
enable_if< ::__array_traits< _Tp, _Nm >::_Is_swappable::value >::type noexcept(noexcept(__one.swap(__two)))
swap
_GLIBCXX17_INLINE constexpr piecewise_construct_t piecewise_construct
piecewise_construct
_T2 second
first is a copy of the first object
constexpr tuple< _Elements &&...> forward_as_tuple(_Elements &&...__args) noexcept
std::forward_as_tuple
Generic atomic type, primary class template.
_T1 first
second_type is the second bound type