29#ifndef _GLIBCXX_MEMORY_RESOURCE
30#define _GLIBCXX_MEMORY_RESOURCE 1
32#pragma GCC system_header
34#if __cplusplus >= 201703L
42#include <bits/uses_allocator.h>
47#if ! __cpp_lib_make_obj_using_allocator
52namespace std _GLIBCXX_VISIBILITY(default)
54_GLIBCXX_BEGIN_NAMESPACE_VERSION
57#ifdef _GLIBCXX_HAS_GTHREADS
59# define __cpp_lib_memory_resource 201603L
62# define __cpp_lib_memory_resource 1
65 class memory_resource;
67#if __cplusplus == 201703L
68 template<
typename _Tp>
69 class polymorphic_allocator;
71# define __cpp_lib_polymorphic_allocator 201902L
72 template<
typename _Tp = std::
byte>
73 class polymorphic_allocator;
77 memory_resource* new_delete_resource() noexcept;
78 memory_resource* null_memory_resource() noexcept;
79 memory_resource* set_default_resource(memory_resource* __r) noexcept;
80 memory_resource* get_default_resource() noexcept
81 __attribute__((__returns_nonnull__));
85#ifdef _GLIBCXX_HAS_GTHREADS
86 class synchronized_pool_resource;
88 class unsynchronized_pool_resource;
89 class monotonic_buffer_resource;
94 static constexpr size_t _S_max_align =
alignof(max_align_t);
105 allocate(
size_t __bytes,
size_t __alignment = _S_max_align)
106 __attribute__((__returns_nonnull__,__alloc_size__(2),__alloc_align__(3)))
107 { return ::operator
new(__bytes, do_allocate(__bytes, __alignment)); }
110 deallocate(
void* __p,
size_t __bytes,
size_t __alignment = _S_max_align)
111 __attribute__((__nonnull__))
112 {
return do_deallocate(__p, __bytes, __alignment); }
116 {
return do_is_equal(__other); }
120 do_allocate(
size_t __bytes,
size_t __alignment) = 0;
123 do_deallocate(
void* __p,
size_t __bytes,
size_t __alignment) = 0;
131 {
return &__a == &__b || __a.is_equal(__b); }
133#if __cpp_impl_three_way_comparison < 201907L
135 operator!=(
const memory_resource& __a,
const memory_resource& __b)
noexcept
136 {
return !(__a == __b); }
140 template<
typename _Tp>
141 class polymorphic_allocator
145 template<
typename _Up>
146 struct __not_pair {
using type = void; };
148 template<
typename _Up1,
typename _Up2>
149 struct __not_pair<pair<_Up1, _Up2>> { };
152 using value_type = _Tp;
154 polymorphic_allocator() noexcept
155 : _M_resource(get_default_resource())
158 polymorphic_allocator(memory_resource* __r)
noexcept
159 __attribute__((__nonnull__))
161 { _GLIBCXX_DEBUG_ASSERT(__r); }
163 polymorphic_allocator(
const polymorphic_allocator& __other) =
default;
165 template<
typename _Up>
166 polymorphic_allocator(
const polymorphic_allocator<_Up>& __x) noexcept
167 : _M_resource(__x.resource())
170 polymorphic_allocator&
171 operator=(
const polymorphic_allocator&) =
delete;
176 __attribute__((__returns_nonnull__))
179 std::__throw_bad_array_new_length();
180 return static_cast<_Tp*
>(_M_resource->allocate(__n *
sizeof(_Tp),
185 deallocate(_Tp* __p,
size_t __n)
noexcept
186 __attribute__((__nonnull__))
187 { _M_resource->deallocate(__p, __n *
sizeof(_Tp),
alignof(_Tp)); }
189#if __cplusplus > 201703L
191 allocate_bytes(
size_t __nbytes,
192 size_t __alignment =
alignof(max_align_t))
193 {
return _M_resource->allocate(__nbytes, __alignment); }
196 deallocate_bytes(
void* __p,
size_t __nbytes,
197 size_t __alignment =
alignof(max_align_t))
198 { _M_resource->deallocate(__p, __nbytes, __alignment); }
200 template<
typename _Up>
202 allocate_object(
size_t __n = 1)
205 std::__throw_bad_array_new_length();
206 return static_cast<_Up*
>(allocate_bytes(__n *
sizeof(_Up),
210 template<
typename _Up>
212 deallocate_object(_Up* __p,
size_t __n = 1)
213 { deallocate_bytes(__p, __n *
sizeof(_Up),
alignof(_Up)); }
215 template<
typename _Up,
typename... _CtorArgs>
217 new_object(_CtorArgs&&... __ctor_args)
219 _Up* __p = allocate_object<_Up>();
222 construct(__p, std::forward<_CtorArgs>(__ctor_args)...);
226 deallocate_object(__p);
227 __throw_exception_again;
232 template<
typename _Up>
234 delete_object(_Up* __p)
237 deallocate_object(__p);
241#if ! __cpp_lib_make_obj_using_allocator
242 template<
typename _Tp1,
typename... _Args>
243 __attribute__((__nonnull__))
244 typename __not_pair<_Tp1>::type
245 construct(_Tp1* __p, _Args&&... __args)
250 = std::__uses_alloc_t<_Tp1, polymorphic_allocator, _Args...>;
251 if constexpr (is_base_of_v<__uses_alloc0, __use_tag>)
252 ::new(__p) _Tp1(std::forward<_Args>(__args)...);
253 else if constexpr (is_base_of_v<__uses_alloc1_, __use_tag>)
254 ::new(__p) _Tp1(allocator_arg, *
this,
255 std::forward<_Args>(__args)...);
257 ::new(__p) _Tp1(std::forward<_Args>(__args)..., *
this);
260 template<
typename _Tp1,
typename _Tp2,
261 typename... _Args1,
typename... _Args2>
262 __attribute__((__nonnull__))
264 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
265 tuple<_Args1...> __x, tuple<_Args2...> __y)
268 __use_alloc<_Tp1, polymorphic_allocator, _Args1...>(*this);
270 __use_alloc<_Tp2, polymorphic_allocator, _Args2...>(*this);
275 _S_construct_p(__x_tag, __x_i, __x),
276 _S_construct_p(__y_tag, __y_i, __y));
279 template<
typename _Tp1,
typename _Tp2>
280 __attribute__((__nonnull__))
282 construct(pair<_Tp1, _Tp2>* __p)
285 template<
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
286 __attribute__((__nonnull__))
288 construct(pair<_Tp1, _Tp2>* __p, _Up&& __x, _Vp&& __y)
295 template <
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
296 __attribute__((__nonnull__))
305 template<
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
306 __attribute__((__nonnull__))
308 construct(pair<_Tp1, _Tp2>* __p, pair<_Up, _Vp>&& __pr)
315 template<
typename _Tp1,
typename... _Args>
316 __attribute__((__nonnull__))
318 construct(_Tp1* __p, _Args&&... __args)
320 std::uninitialized_construct_using_allocator(__p, *
this,
321 std::forward<_Args>(__args)...);
325 template<
typename _Up>
326 _GLIBCXX20_DEPRECATED_SUGGEST(
"allocator_traits::destroy")
327 __attribute__((__nonnull__))
332 polymorphic_allocator
333 select_on_container_copy_construction() const noexcept
334 {
return polymorphic_allocator(); }
337 resource() const noexcept
338 __attribute__((__returns_nonnull__))
339 {
return _M_resource; }
345 operator==(
const polymorphic_allocator& __a,
346 const polymorphic_allocator& __b)
noexcept
347 {
return *__a.resource() == *__b.resource(); }
349#if __cpp_impl_three_way_comparison < 201907L
352 operator!=(
const polymorphic_allocator& __a,
353 const polymorphic_allocator& __b)
noexcept
354 {
return !(__a == __b); }
358#if ! __cpp_lib_make_obj_using_allocator
359 using __uses_alloc1_ = __uses_alloc1<polymorphic_allocator>;
360 using __uses_alloc2_ = __uses_alloc2<polymorphic_allocator>;
362 template<
typename _Ind,
typename... _Args>
363 static tuple<_Args&&...>
364 _S_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
367 template<
size_t... _Ind,
typename... _Args>
368 static tuple<allocator_arg_t, polymorphic_allocator, _Args&&...>
369 _S_construct_p(__uses_alloc1_ __ua, index_sequence<_Ind...>,
370 tuple<_Args...>& __t)
373 allocator_arg, *__ua._M_a, std::get<_Ind>(
std::move(__t))...
377 template<
size_t... _Ind,
typename... _Args>
378 static tuple<_Args&&..., polymorphic_allocator>
379 _S_construct_p(__uses_alloc2_ __ua, index_sequence<_Ind...>,
380 tuple<_Args...>& __t)
381 {
return { std::get<_Ind>(
std::move(__t))..., *__ua._M_a }; }
384 memory_resource* _M_resource;
387 template<
typename _Tp1,
typename _Tp2>
389 operator==(
const polymorphic_allocator<_Tp1>& __a,
390 const polymorphic_allocator<_Tp2>& __b)
noexcept
391 {
return *__a.resource() == *__b.resource(); }
393#if __cpp_impl_three_way_comparison < 201907L
394 template<
typename _Tp1,
typename _Tp2>
396 operator!=(
const polymorphic_allocator<_Tp1>& __a,
397 const polymorphic_allocator<_Tp2>& __b)
noexcept
398 {
return !(__a == __b); }
404 template<
typename _Tp>
447 template<
typename _Up>
448 using rebind_alloc = pmr::polymorphic_allocator<_Up>;
450 template<
typename _Up>
462 {
return __a.allocate(__n); }
477 {
return __a.allocate(__n); }
489 { __a.deallocate(__p, __n); }
502 template<
typename _Up,
typename... _Args>
505 { __a.construct(__p, std::forward<_Args>(__args)...); }
514 template<
typename _Up>
515 static _GLIBCXX20_CONSTEXPR
void
546 size_t largest_required_pool_block = 0;
550 class __pool_resource
559 __pool_resource(
const __pool_resource&) =
delete;
560 __pool_resource& operator=(
const __pool_resource&) =
delete;
564 allocate(
size_t __bytes,
size_t __alignment);
568 deallocate(
void* __p,
size_t __bytes,
size_t __alignment);
572 void release()
noexcept;
575 {
return _M_unpooled.get_allocator().resource(); }
579 _Pool* _M_alloc_pools();
581 const pool_options _M_opts;
586 _GLIBCXX_STD_C::pmr::vector<_BigBlock> _M_unpooled;
591#ifdef _GLIBCXX_HAS_GTHREADS
598 __attribute__((__nonnull__));
606 __attribute__((__nonnull__))
624 upstream_resource()
const noexcept
625 __attribute__((__returns_nonnull__))
626 {
return _M_impl.resource(); }
628 pool_options options()
const noexcept {
return _M_impl._M_opts; }
632 do_allocate(
size_t __bytes,
size_t __alignment)
override;
635 do_deallocate(
void* __p,
size_t __bytes,
size_t __alignment)
override;
639 {
return this == &__other; }
648 auto _M_thread_specific_pools()
noexcept;
650 __pool_resource _M_impl;
651 __gthread_key_t _M_key;
653 _TPools* _M_tpools =
nullptr;
662 [[__gnu__::__nonnull__]]
670 [[__gnu__::__nonnull__]]
689 [[__gnu__::__returns_nonnull__]]
691 upstream_resource()
const noexcept
692 {
return _M_impl.resource(); }
694 pool_options options()
const noexcept {
return _M_impl._M_opts; }
698 do_allocate(
size_t __bytes,
size_t __alignment)
override;
701 do_deallocate(
void* __p,
size_t __bytes,
size_t __alignment)
override;
705 {
return this == &__other; }
708 using _Pool = __pool_resource::_Pool;
710 auto _M_find_pool(
size_t)
noexcept;
712 __pool_resource _M_impl;
713 _Pool* _M_pools =
nullptr;
721 __attribute__((__nonnull__))
722 : _M_upstream(__upstream)
723 { _GLIBCXX_DEBUG_ASSERT(__upstream !=
nullptr); }
725 monotonic_buffer_resource(
size_t __initial_size,
726 memory_resource* __upstream)
noexcept
727 __attribute__((__nonnull__))
728 : _M_next_bufsiz(__initial_size),
729 _M_upstream(__upstream)
731 _GLIBCXX_DEBUG_ASSERT(__upstream !=
nullptr);
732 _GLIBCXX_DEBUG_ASSERT(__initial_size > 0);
735 monotonic_buffer_resource(
void* __buffer,
size_t __buffer_size,
736 memory_resource* __upstream)
noexcept
737 __attribute__((__nonnull__(4)))
738 : _M_current_buf(__buffer), _M_avail(__buffer_size),
739 _M_next_bufsiz(_S_next_bufsize(__buffer_size)),
740 _M_upstream(__upstream),
741 _M_orig_buf(__buffer), _M_orig_size(__buffer_size)
743 _GLIBCXX_DEBUG_ASSERT(__upstream !=
nullptr);
744 _GLIBCXX_DEBUG_ASSERT(__buffer !=
nullptr || __buffer_size == 0);
747 monotonic_buffer_resource() noexcept
748 : monotonic_buffer_resource(get_default_resource())
752 monotonic_buffer_resource(
size_t __initial_size) noexcept
753 : monotonic_buffer_resource(__initial_size, get_default_resource())
756 monotonic_buffer_resource(
void* __buffer,
size_t __buffer_size) noexcept
757 : monotonic_buffer_resource(__buffer, __buffer_size, get_default_resource())
760 monotonic_buffer_resource(
const monotonic_buffer_resource&) =
delete;
762 virtual ~monotonic_buffer_resource();
764 monotonic_buffer_resource&
765 operator=(
const monotonic_buffer_resource&) =
delete;
771 _M_release_buffers();
774 if ((_M_current_buf = _M_orig_buf))
776 _M_avail = _M_orig_size;
777 _M_next_bufsiz = _S_next_bufsize(_M_orig_size);
782 _M_next_bufsiz = _M_orig_size;
787 upstream_resource() const noexcept
788 __attribute__((__returns_nonnull__))
789 {
return _M_upstream; }
793 do_allocate(
size_t __bytes,
size_t __alignment)
override
795 if (__builtin_expect(__bytes == 0,
false))
798 void* __p =
std::align(__alignment, __bytes, _M_current_buf, _M_avail);
799 if (__builtin_expect(__p ==
nullptr,
false))
801 _M_new_buffer(__bytes, __alignment);
802 __p = _M_current_buf;
804 _M_current_buf = (
char*)_M_current_buf + __bytes;
810 do_deallocate(
void*,
size_t,
size_t)
override
814 do_is_equal(
const memory_resource& __other)
const noexcept override
815 {
return this == &__other; }
821 _M_new_buffer(
size_t __bytes,
size_t __alignment);
825 _M_release_buffers() noexcept;
828 _S_next_bufsize(
size_t __buffer_size) noexcept
830 if (__builtin_expect(__buffer_size == 0,
false))
832 return __buffer_size * _S_growth_factor;
835 static constexpr size_t _S_init_bufsize = 128 *
sizeof(
void*);
836 static constexpr float _S_growth_factor = 1.5;
838 void* _M_current_buf =
nullptr;
840 size_t _M_next_bufsiz = _S_init_bufsize;
843 memory_resource*
const _M_upstream;
844 void*
const _M_orig_buf =
nullptr;
845 size_t const _M_orig_size = _M_next_bufsiz;
848 _Chunk* _M_head =
nullptr;
852_GLIBCXX_END_NAMESPACE_VERSION
void * align(size_t __align, size_t __size, void *&__ptr, size_t &__space) noexcept
Fit aligned storage in buffer.
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
std::forward_as_tuple
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.
make_index_sequence< sizeof...(_Types)> index_sequence_for
Alias template index_sequence_for.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
static void construct(allocator_type &__a, _Up *__p, _Args &&... __args)
Construct an object of type _Up
std::ptrdiff_t difference_type
The allocator's difference type.
static void deallocate(allocator_type &__a, pointer __p, size_type __n)
Deallocate memory.
_Tp value_type
The allocated type.
static constexpr void destroy(allocator_type &, _Up *__p) noexcept(is_nothrow_destructible< _Up >::value)
Destroy an object of type _Up
static constexpr size_type max_size(const allocator_type &) noexcept
The maximum supported allocation size.
static pointer allocate(allocator_type &__a, size_type __n)
Allocate memory.
_Tp * pointer
The allocator's pointer type.
const _Tp * const_pointer
The allocator's const pointer type.
static pointer allocate(allocator_type &__a, size_type __n, const_void_pointer)
Allocate memory.
const void * const_void_pointer
The allocator's const void pointer type.
static allocator_type select_on_container_copy_construction(const allocator_type &) noexcept
void * void_pointer
The allocator's void pointer type.
std::size_t size_type
The allocator's size type.
pmr::polymorphic_allocator< _Tp > allocator_type
The allocator type.
Parameters for tuning a pool resource's behaviour.
size_t max_blocks_per_chunk
Upper limit on number of blocks in a chunk.
A thread-safe memory resource that manages pools of fixed-size blocks.
A non-thread-safe memory resource that manages pools of fixed-size blocks.
The standard shared mutex type.
Uniform interface to all allocator types.
__detected_or_t< value_type *, __pointer, _Alloc > pointer
The allocator's pointer type.
typename _Size< _Alloc, difference_type >::type size_type
The allocator's size type.
_Alloc::value_type value_type
The allocated type.
_Alloc allocator_type
The allocator type.
A simple scoped lock type.
Struct holding two objects of arbitrary type.
_T1 first
The first member.
_T2 second
The second member.