30#ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE
31#define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1
33#pragma GCC system_header
37#if __cplusplus >= 201402L
48namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
51 template<
typename _Tp>
class malloc_allocator;
52_GLIBCXX_END_NAMESPACE_VERSION
56_GLIBCXX_BEGIN_NAMESPACE_VERSION
58namespace experimental {
59inline namespace fundamentals_v2 {
61#define __cpp_lib_experimental_memory_resources 201402L
66 class memory_resource;
69 template<
typename _Tp>
70 class polymorphic_allocator;
72 template<
typename _Alloc,
typename _Resource = memory_resource>
73 class __resource_adaptor_imp;
76 template<
typename _Alloc>
77 using resource_adaptor = __resource_adaptor_imp<
78 typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
82 memory_resource* null_memory_resource() noexcept;
83 memory_resource* get_default_resource() noexcept;
84 memory_resource* set_default_resource(memory_resource* __r) noexcept;
90 static constexpr size_t _S_max_align =
alignof(max_align_t);
93 memory_resource() =
default;
94 memory_resource(
const memory_resource&) =
default;
95 virtual ~memory_resource() =
default;
97 memory_resource& operator=(
const memory_resource&) =
default;
99 _GLIBCXX_NODISCARD
void*
100 allocate(
size_t __bytes,
size_t __alignment = _S_max_align)
101 {
return do_allocate(__bytes, __alignment); }
104 deallocate(
void* __p,
size_t __bytes,
size_t __alignment = _S_max_align)
105 {
return do_deallocate(__p, __bytes, __alignment); }
108 is_equal(
const memory_resource& __other)
const noexcept
109 {
return do_is_equal(__other); }
113 do_allocate(
size_t __bytes,
size_t __alignment) = 0;
116 do_deallocate(
void* __p,
size_t __bytes,
size_t __alignment) = 0;
119 do_is_equal(
const memory_resource& __other)
const noexcept = 0;
123 operator==(
const memory_resource& __a,
const memory_resource& __b)
noexcept
124 {
return &__a == &__b || __a.is_equal(__b); }
127 operator!=(
const memory_resource& __a,
const memory_resource& __b)
noexcept
128 {
return !(__a == __b); }
131 template<
typename _Tp>
132 class polymorphic_allocator
135 using value_type = _Tp;
137 polymorphic_allocator() noexcept
138 : _M_resource(get_default_resource())
141 polymorphic_allocator(memory_resource* __r)
143 { _GLIBCXX_DEBUG_ASSERT(__r); }
145 polymorphic_allocator(
const polymorphic_allocator& __other) =
default;
147 template <
typename _Up>
148 polymorphic_allocator(
const polymorphic_allocator<_Up>&
150 : _M_resource(__other.resource())
153 polymorphic_allocator&
154 operator=(
const polymorphic_allocator& __rhs) =
default;
156 _GLIBCXX_NODISCARD _Tp* allocate(
size_t __n)
157 {
return static_cast<_Tp*
>(_M_resource->allocate(__n *
sizeof(_Tp),
161 deallocate(_Tp* __p,
size_t __n)
162 { _M_resource->deallocate(__p, __n *
sizeof(_Tp),
alignof(_Tp)); }
164 template <
typename _Tp1,
typename... _Args>
166 construct(_Tp1* __p, _Args&&... __args)
168 std::__uses_allocator_construct(this->resource(), __p,
169 std::forward<_Args>(__args)...);
173 template <
typename _Tp1,
typename _Tp2,
174 typename... _Args1,
typename... _Args2>
176 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
177 tuple<_Args1...> __x, tuple<_Args2...> __y)
179 memory_resource*
const __resource = this->resource();
181 std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
183 std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
186 _M_construct_p(__x_use_tag, __x),
187 _M_construct_p(__y_use_tag, __y));
190 template <
typename _Tp1,
typename _Tp2>
192 construct(pair<_Tp1,_Tp2>* __p)
195 template <
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
197 construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
204 template <
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
213 template <
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
215 construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
222 template <
typename _Up>
228 polymorphic_allocator
229 select_on_container_copy_construction()
const
230 {
return polymorphic_allocator(); }
232 memory_resource* resource()
const {
return _M_resource; }
235 using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
236 using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
238 template<
typename _Tuple>
240 _M_construct_p(__uses_alloc0, _Tuple& __t)
243 template<
typename... _Args>
245 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
249 template<
typename... _Args>
251 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
254 memory_resource* _M_resource;
257 template <
class _Tp1,
class _Tp2>
259 operator==(
const polymorphic_allocator<_Tp1>& __a,
260 const polymorphic_allocator<_Tp2>& __b)
noexcept
261 {
return *__a.resource() == *__b.resource(); }
263 template <
class _Tp1,
class _Tp2>
265 operator!=(
const polymorphic_allocator<_Tp1>& __a,
266 const polymorphic_allocator<_Tp2>& __b)
noexcept
267 {
return !(__a == __b); }
271 class __resource_adaptor_common
273 template<
typename,
typename>
friend class __resource_adaptor_imp;
277 _AlignMgr(
size_t __nbytes,
size_t __align)
278 : _M_nbytes(__nbytes), _M_align(__align)
283 _M_alloc_size()
const {
return _M_buf_size() + _M_token_size(); }
286 _M_adjust(
void* __ptr)
const
288 const auto __orig_ptr =
static_cast<char*
>(__ptr);
289 size_t __space = _M_buf_size();
291 std::align(_M_align, _M_nbytes, __ptr, __space);
292 const auto __aligned_ptr =
static_cast<char*
>(__ptr);
293 const auto __token_size = _M_token_size();
295 char*
const __end = __aligned_ptr + _M_nbytes;
296 if (__token_size == 1)
297 _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr);
298 else if (__token_size ==
sizeof(
short))
299 _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr);
300 else if (__token_size ==
sizeof(
int) &&
sizeof(
int) <
sizeof(
char*))
301 _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr);
304 _S_write<char*>(__end, __orig_ptr);
305 return __aligned_ptr;
309 _M_unadjust(
char* __ptr)
const
311 const char*
const __end = __ptr + _M_nbytes;
313 const auto __token_size = _M_token_size();
315 if (__token_size == 1)
316 __orig_ptr = __ptr - _S_read<unsigned char>(__end);
317 else if (__token_size ==
sizeof(
short))
318 __orig_ptr = __ptr - _S_read<unsigned short>(__end);
319 else if (__token_size ==
sizeof(
int)
320 &&
sizeof(
int) <
sizeof(
char*))
321 __orig_ptr = __ptr - _S_read<unsigned int>(__end);
323 __orig_ptr = _S_read<char*>(__end);
327 __glibcxx_assert(
static_cast<size_t>(__ptr - __orig_ptr) < _M_align);
337 _M_buf_size()
const {
return _M_nbytes + _M_align - 1; }
341 _M_token_size()
const
343 if (_M_align <= (1ul << __CHAR_BIT__))
345 if (_M_align <= (1ul << (
sizeof(
short) * __CHAR_BIT__)))
346 return sizeof(short);
347 if (_M_align <= (1ull << (
sizeof(
int) * __CHAR_BIT__)))
349 return sizeof(
char*);
352 template<
typename _Tp>
354 _S_write(
void* __to, _Tp __val)
355 { __builtin_memcpy(__to, &__val,
sizeof(_Tp)); }
357 template<
typename _Tp>
359 _S_read(
const void* __from)
362 __builtin_memcpy(&__val, __from,
sizeof(_Tp));
370 template<
typename _Alloc,
typename _Resource>
371 class __resource_adaptor_imp
372 :
public _Resource,
private __resource_adaptor_common
374 using memory_resource = _Resource;
376 static_assert(is_same<char,
378 "Allocator's value_type is char");
379 static_assert(is_same<
char*,
381 "Allocator's pointer type is value_type*");
382 static_assert(is_same<
const char*,
384 "Allocator's const_pointer type is value_type const*");
385 static_assert(is_same<
void*,
387 "Allocator's void_pointer type is void*");
388 static_assert(is_same<
const void*,
390 "Allocator's const_void_pointer type is void const*");
393 using allocator_type = _Alloc;
395 __resource_adaptor_imp() =
default;
396 __resource_adaptor_imp(
const __resource_adaptor_imp&) =
default;
397 __resource_adaptor_imp(__resource_adaptor_imp&&) =
default;
399 explicit __resource_adaptor_imp(
const _Alloc& __a2)
403 explicit __resource_adaptor_imp(_Alloc&& __a2)
407 __resource_adaptor_imp&
408 operator=(
const __resource_adaptor_imp&) =
default;
410 allocator_type get_allocator() const noexcept {
return _M_alloc; }
413#if (defined __sun__ || defined __VXWORKS__) && defined __i386__
415# define _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC 0
416#elif defined __hpux__ && defined __hppa__ && defined __LP64__
418# define _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC 0
420# define _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC 1
424 do_allocate(
size_t __bytes,
size_t __alignment)
override
426#if _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC
427 if (__alignment ==
alignof(max_align_t))
428 return _M_allocate<alignof(max_align_t)>(__bytes);
433 return _M_alloc.allocate(__bytes);
435 return _M_allocate<2>(__bytes);
437 return _M_allocate<4>(__bytes);
439 return _M_allocate<8>(__bytes);
441 const _AlignMgr __mgr(__bytes, __alignment);
445 return __mgr._M_adjust(_M_alloc.allocate(__mgr._M_alloc_size()));
449 do_deallocate(
void* __ptr,
size_t __bytes,
size_t __alignment)
noexcept
452#if _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC
453 if (__alignment ==
alignof(max_align_t))
454 return (
void) _M_deallocate<alignof(max_align_t)>(__ptr, __bytes);
459 return (
void) _M_alloc.deallocate((
char*)__ptr, __bytes);
461 return (
void) _M_deallocate<2>(__ptr, __bytes);
463 return (
void) _M_deallocate<4>(__ptr, __bytes);
465 return (
void) _M_deallocate<8>(__ptr, __bytes);
467 const _AlignMgr __mgr(__bytes, __alignment);
469 _M_alloc.deallocate(__mgr._M_unadjust((
char*)__ptr),
470 __mgr._M_alloc_size());
474 do_is_equal(
const memory_resource& __other)
const noexcept override
477 if (
auto __p =
dynamic_cast<const __resource_adaptor_imp*
>(&__other))
478 return _M_alloc == __p->_M_alloc;
480 if (
this == &__other)
487 template<
size_t _Num>
488 struct _Aligned_type {
alignas(_Num)
char __c[_Num]; };
491 template<
size_t _Num,
typename _Tp = _Aligned_type<_Num>>
493 _M_allocate(
size_t __bytes)
495 typename allocator_traits<_Alloc>::template
496 rebind_alloc<_Tp> __a2(_M_alloc);
497 const size_t __n = (__bytes + _Num - 1) / _Num;
498 return __a2.allocate(__n);
502 template<
size_t _Num,
typename _Tp = _Aligned_type<_Num>>
504 _M_deallocate(
void* __ptr,
size_t __bytes)
noexcept
506 typename allocator_traits<_Alloc>::template
507 rebind_alloc<_Tp> __a2(_M_alloc);
508 const size_t __n = (__bytes + _Num - 1) / _Num;
509 __a2.deallocate((_Tp*)__ptr, __n);
517 inline memory_resource*
520 using type = resource_adaptor<std::__new_allocator<char>>;
521 alignas(type)
static unsigned char __buf[
sizeof(type)];
522 static type* __r =
new(__buf) type;
526 inline memory_resource*
529 class type final :
public memory_resource
532 do_allocate(
size_t,
size_t)
override
533 { std::__throw_bad_alloc(); }
536 do_deallocate(
void*,
size_t,
size_t)
noexcept override
540 do_is_equal(
const memory_resource& __other)
const noexcept override
541 {
return this == &__other; }
544 alignas(type)
static unsigned char __buf[
sizeof(type)];
545 static type* __r =
new(__buf) type;
553 __get_default_resource()
555#ifndef _GLIBCXX_HAS_GTHREADS
557 using value_type = memory_resource*;
558 explicit type(value_type __r) : _M_r(__r) { }
560 value_type load()
const {
return _M_r; }
561 value_type exchange(value_type __r) {
return std::__exchange(_M_r, __r); }
564 using type = atomic<memory_resource*>;
566 alignas(type)
static unsigned char __buf[
sizeof(type)];
573 inline memory_resource*
574 get_default_resource() noexcept
575 {
return __get_default_resource().load(); }
578 inline memory_resource*
579 set_default_resource(memory_resource* __r)
noexcept
582 __r = new_delete_resource();
583 return __get_default_resource().exchange(__r);
590_GLIBCXX_END_NAMESPACE_VERSION
memory_resource * null_memory_resource() noexcept
A pmr::memory_resource that always throws bad_alloc
void * align(size_t __align, size_t __size, void *&__ptr, size_t &__space) noexcept
Fit aligned storage in buffer.
memory_resource * new_delete_resource() noexcept
A pmr::memory_resource that uses new to allocate memory.
constexpr auto tuple_cat(_Tpls &&... __tpls) -> typename __tuple_cat_result< _Tpls... >::__type
Create a tuple containing all elements from multiple tuple-like objects.
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
Create a tuple of lvalue or rvalue references to the arguments.
constexpr tuple< typename __decay_and_strip< _Elements >::__type... > make_tuple(_Elements &&... __args)
Create a tuple containing copies of the arguments.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
ISO C++ entities toplevel namespace is std.
GNU extensions for public use.
typename _Ptr< __v_pointer, void >::type void_pointer
The allocator's void pointer type.
__detected_or_t< value_type *, __pointer, _Alloc > pointer
The allocator's pointer type.
typename _Ptr< __cv_pointer, const void >::type const_void_pointer
The allocator's const void pointer type.
typename _Ptr< __c_pointer, const value_type >::type const_pointer
The allocator's const pointer type.
_Alloc::value_type value_type
The allocated type.
Struct holding two objects of arbitrary type.
_T1 first
The first member.
_T2 second
The second member.