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
52 namespace 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;
79 [[nodiscard, __gnu__::__returns_nonnull__, __gnu__::__const__]]
84 [[nodiscard, __gnu__::__returns_nonnull__, __gnu__::__const__]]
86 null_memory_resource() noexcept;
89 [[__gnu__::__returns_nonnull__]]
94 [[__gnu__::__returns_nonnull__]]
96 get_default_resource() noexcept;
100 #ifdef _GLIBCXX_HAS_GTHREADS
104 class monotonic_buffer_resource;
109 static constexpr
size_t _S_max_align =
alignof(max_align_t);
120 allocate(
size_t __bytes,
size_t __alignment = _S_max_align)
121 __attribute__((__returns_nonnull__,__alloc_size__(2),__alloc_align__(3)))
122 { return ::operator
new(__bytes, do_allocate(__bytes, __alignment)); }
125 deallocate(
void* __p,
size_t __bytes,
size_t __alignment = _S_max_align)
126 __attribute__((__nonnull__))
127 {
return do_deallocate(__p, __bytes, __alignment); }
132 {
return do_is_equal(__other); }
136 do_allocate(
size_t __bytes,
size_t __alignment) = 0;
139 do_deallocate(
void* __p,
size_t __bytes,
size_t __alignment) = 0;
148 {
return &__a == &__b || __a.is_equal(__b); }
150 #if __cpp_impl_three_way_comparison < 201907L
153 operator!=(
const memory_resource& __a,
const memory_resource& __b) noexcept
154 {
return !(__a == __b); }
158 template<
typename _Tp>
159 class polymorphic_allocator
163 template<
typename _Up>
164 struct __not_pair {
using type = void; };
166 template<
typename _Up1,
typename _Up2>
167 struct __not_pair<pair<_Up1, _Up2>> { };
170 using value_type = _Tp;
172 polymorphic_allocator() noexcept
173 : _M_resource(get_default_resource())
176 polymorphic_allocator(memory_resource* __r) noexcept
177 __attribute__((__nonnull__))
179 { _GLIBCXX_DEBUG_ASSERT(__r); }
181 polymorphic_allocator(
const polymorphic_allocator& __other) =
default;
183 template<
typename _Up>
184 polymorphic_allocator(
const polymorphic_allocator<_Up>& __x) noexcept
185 : _M_resource(__x.resource())
188 polymorphic_allocator&
189 operator=(
const polymorphic_allocator&) =
delete;
194 __attribute__((__returns_nonnull__))
197 std::__throw_bad_array_new_length();
198 return static_cast<_Tp*
>(_M_resource->allocate(__n *
sizeof(_Tp),
203 deallocate(_Tp* __p,
size_t __n) noexcept
204 __attribute__((__nonnull__))
205 { _M_resource->deallocate(__p, __n *
sizeof(_Tp),
alignof(_Tp)); }
207 #if __cplusplus > 201703L
209 allocate_bytes(
size_t __nbytes,
210 size_t __alignment =
alignof(max_align_t))
211 {
return _M_resource->allocate(__nbytes, __alignment); }
214 deallocate_bytes(
void* __p,
size_t __nbytes,
215 size_t __alignment =
alignof(max_align_t))
216 { _M_resource->deallocate(__p, __nbytes, __alignment); }
218 template<
typename _Up>
220 allocate_object(
size_t __n = 1)
223 std::__throw_bad_array_new_length();
224 return static_cast<_Up*
>(allocate_bytes(__n *
sizeof(_Up),
228 template<
typename _Up>
230 deallocate_object(_Up* __p,
size_t __n = 1)
231 { deallocate_bytes(__p, __n *
sizeof(_Up),
alignof(_Up)); }
233 template<
typename _Up,
typename... _CtorArgs>
235 new_object(_CtorArgs&&... __ctor_args)
237 _Up* __p = allocate_object<_Up>();
240 construct(__p, std::forward<_CtorArgs>(__ctor_args)...);
244 deallocate_object(__p);
245 __throw_exception_again;
250 template<
typename _Up>
252 delete_object(_Up* __p)
255 deallocate_object(__p);
259 #if ! __cpp_lib_make_obj_using_allocator
260 template<
typename _Tp1,
typename... _Args>
261 __attribute__((__nonnull__))
262 typename __not_pair<_Tp1>::type
263 construct(_Tp1* __p, _Args&&... __args)
268 = std::__uses_alloc_t<_Tp1, polymorphic_allocator, _Args...>;
269 if constexpr (is_base_of_v<__uses_alloc0, __use_tag>)
270 ::new(__p) _Tp1(
std::
forward<_Args>(__args)...);
271 else if constexpr (is_base_of_v<__uses_alloc1_, __use_tag>)
272 ::new(__p) _Tp1(allocator_arg, *this,
275 ::new(__p) _Tp1(std::forward<_Args>(__args)..., *
this);
278 template<
typename _Tp1,
typename _Tp2,
279 typename... _Args1,
typename... _Args2>
280 __attribute__((__nonnull__))
282 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
283 tuple<_Args1...> __x, tuple<_Args2...> __y)
286 __use_alloc<_Tp1, polymorphic_allocator, _Args1...>(*this);
288 __use_alloc<_Tp2, polymorphic_allocator, _Args2...>(*this);
293 _S_construct_p(__x_tag, __x_i, __x),
294 _S_construct_p(__y_tag, __y_i, __y));
297 template<
typename _Tp1,
typename _Tp2>
298 __attribute__((__nonnull__))
300 construct(pair<_Tp1, _Tp2>* __p)
303 template<
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
304 __attribute__((__nonnull__))
306 construct(pair<_Tp1, _Tp2>* __p, _Up&& __x, _Vp&& __y)
313 template <
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
314 __attribute__((__nonnull__))
323 template<
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
324 __attribute__((__nonnull__))
326 construct(pair<_Tp1, _Tp2>* __p, pair<_Up, _Vp>&& __pr)
333 template<
typename _Tp1,
typename... _Args>
334 __attribute__((__nonnull__))
336 construct(_Tp1* __p, _Args&&... __args)
338 std::uninitialized_construct_using_allocator(__p, *
this,
339 std::forward<_Args>(__args)...);
343 template<
typename _Up>
344 _GLIBCXX20_DEPRECATED_SUGGEST(
"allocator_traits::destroy")
345 __attribute__((__nonnull__))
350 polymorphic_allocator
351 select_on_container_copy_construction() const noexcept
352 {
return polymorphic_allocator(); }
355 resource() const noexcept
356 __attribute__((__returns_nonnull__))
357 {
return _M_resource; }
363 operator==(
const polymorphic_allocator& __a,
364 const polymorphic_allocator& __b) noexcept
365 {
return *__a.resource() == *__b.resource(); }
367 #if __cpp_impl_three_way_comparison < 201907L
370 operator!=(
const polymorphic_allocator& __a,
371 const polymorphic_allocator& __b) noexcept
372 {
return !(__a == __b); }
376 #if ! __cpp_lib_make_obj_using_allocator
377 using __uses_alloc1_ = __uses_alloc1<polymorphic_allocator>;
378 using __uses_alloc2_ = __uses_alloc2<polymorphic_allocator>;
380 template<
typename _Ind,
typename... _Args>
381 static tuple<_Args&&...>
382 _S_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
385 template<
size_t... _Ind,
typename... _Args>
386 static tuple<allocator_arg_t, polymorphic_allocator, _Args&&...>
387 _S_construct_p(__uses_alloc1_ __ua, index_sequence<_Ind...>,
388 tuple<_Args...>& __t)
391 allocator_arg, *__ua._M_a, std::get<_Ind>(
std::move(__t))...
395 template<
size_t... _Ind,
typename... _Args>
396 static tuple<_Args&&..., polymorphic_allocator>
397 _S_construct_p(__uses_alloc2_ __ua, index_sequence<_Ind...>,
398 tuple<_Args...>& __t)
399 {
return { std::get<_Ind>(
std::move(__t))..., *__ua._M_a }; }
402 memory_resource* _M_resource;
405 template<
typename _Tp1,
typename _Tp2>
408 operator==(
const polymorphic_allocator<_Tp1>& __a,
409 const polymorphic_allocator<_Tp2>& __b) noexcept
410 {
return *__a.resource() == *__b.resource(); }
412 #if __cpp_impl_three_way_comparison < 201907L
413 template<
typename _Tp1,
typename _Tp2>
416 operator!=(
const polymorphic_allocator<_Tp1>& __a,
417 const polymorphic_allocator<_Tp2>& __b) noexcept
418 {
return !(__a == __b); }
424 template<
typename _Tp>
467 template<
typename _Up>
468 using rebind_alloc = pmr::polymorphic_allocator<_Up>;
470 template<
typename _Up>
482 {
return __a.allocate(__n); }
497 {
return __a.allocate(__n); }
509 { __a.deallocate(__p, __n); }
522 template<
typename _Up,
typename... _Args>
525 { __a.construct(__p, std::forward<_Args>(__args)...); }
534 template<
typename _Up>
535 static _GLIBCXX20_CONSTEXPR
void
566 size_t largest_required_pool_block = 0;
570 class __pool_resource
579 __pool_resource(
const __pool_resource&) =
delete;
580 __pool_resource& operator=(
const __pool_resource&) =
delete;
584 allocate(
size_t __bytes,
size_t __alignment);
588 deallocate(
void* __p,
size_t __bytes,
size_t __alignment);
592 void release() noexcept;
595 {
return _M_unpooled.get_allocator().resource(); }
599 _Pool* _M_alloc_pools();
601 const pool_options _M_opts;
606 _GLIBCXX_STD_C::pmr::vector<_BigBlock> _M_unpooled;
611 #ifdef _GLIBCXX_HAS_GTHREADS
618 __attribute__((__nonnull__));
626 __attribute__((__nonnull__))
644 upstream_resource()
const noexcept
645 __attribute__((__returns_nonnull__))
646 {
return _M_impl.resource(); }
648 pool_options options()
const noexcept {
return _M_impl._M_opts; }
652 do_allocate(
size_t __bytes,
size_t __alignment)
override;
655 do_deallocate(
void* __p,
size_t __bytes,
size_t __alignment)
override;
659 {
return this == &__other; }
668 auto _M_thread_specific_pools() noexcept;
670 __pool_resource _M_impl;
671 __gthread_key_t _M_key;
673 _TPools* _M_tpools =
nullptr;
682 [[__gnu__::__nonnull__]]
690 [[__gnu__::__nonnull__]]
709 [[__gnu__::__returns_nonnull__]]
711 upstream_resource()
const noexcept
712 {
return _M_impl.resource(); }
714 pool_options options()
const noexcept {
return _M_impl._M_opts; }
718 do_allocate(
size_t __bytes,
size_t __alignment)
override;
721 do_deallocate(
void* __p,
size_t __bytes,
size_t __alignment)
override;
725 {
return this == &__other; }
728 using _Pool = __pool_resource::_Pool;
730 auto _M_find_pool(
size_t) noexcept;
732 __pool_resource _M_impl;
733 _Pool* _M_pools =
nullptr;
741 __attribute__((__nonnull__))
742 : _M_upstream(__upstream)
743 { _GLIBCXX_DEBUG_ASSERT(__upstream !=
nullptr); }
745 monotonic_buffer_resource(
size_t __initial_size,
746 memory_resource* __upstream) noexcept
747 __attribute__((__nonnull__))
748 : _M_next_bufsiz(__initial_size),
749 _M_upstream(__upstream)
751 _GLIBCXX_DEBUG_ASSERT(__upstream !=
nullptr);
752 _GLIBCXX_DEBUG_ASSERT(__initial_size > 0);
755 monotonic_buffer_resource(
void* __buffer,
size_t __buffer_size,
756 memory_resource* __upstream) noexcept
757 __attribute__((__nonnull__(4)))
758 : _M_current_buf(__buffer), _M_avail(__buffer_size),
759 _M_next_bufsiz(_S_next_bufsize(__buffer_size)),
760 _M_upstream(__upstream),
761 _M_orig_buf(__buffer), _M_orig_size(__buffer_size)
763 _GLIBCXX_DEBUG_ASSERT(__upstream !=
nullptr);
764 _GLIBCXX_DEBUG_ASSERT(__buffer !=
nullptr || __buffer_size == 0);
767 monotonic_buffer_resource() noexcept
768 : monotonic_buffer_resource(get_default_resource())
772 monotonic_buffer_resource(
size_t __initial_size) noexcept
776 monotonic_buffer_resource(
void* __buffer,
size_t __buffer_size) noexcept
780 monotonic_buffer_resource(
const monotonic_buffer_resource&) =
delete;
782 virtual ~monotonic_buffer_resource();
784 monotonic_buffer_resource&
785 operator=(
const monotonic_buffer_resource&) =
delete;
791 _M_release_buffers();
794 if ((_M_current_buf = _M_orig_buf))
796 _M_avail = _M_orig_size;
797 _M_next_bufsiz = _S_next_bufsize(_M_orig_size);
802 _M_next_bufsiz = _M_orig_size;
807 upstream_resource() const noexcept
808 __attribute__((__returns_nonnull__))
809 {
return _M_upstream; }
813 do_allocate(
size_t __bytes,
size_t __alignment)
override
815 if (__builtin_expect(__bytes == 0,
false))
818 void* __p =
std::align(__alignment, __bytes, _M_current_buf, _M_avail);
819 if (__builtin_expect(__p ==
nullptr,
false))
821 _M_new_buffer(__bytes, __alignment);
822 __p = _M_current_buf;
824 _M_current_buf = (
char*)_M_current_buf + __bytes;
830 do_deallocate(
void*,
size_t,
size_t)
override
834 do_is_equal(
const memory_resource& __other)
const noexcept
override
835 {
return this == &__other; }
841 _M_new_buffer(
size_t __bytes,
size_t __alignment);
845 _M_release_buffers() noexcept;
848 _S_next_bufsize(
size_t __buffer_size) noexcept
850 if (__builtin_expect(__buffer_size == 0,
false))
852 return __buffer_size * _S_growth_factor;
855 static constexpr
size_t _S_init_bufsize = 128 *
sizeof(
void*);
856 static constexpr
float _S_growth_factor = 1.5;
858 void* _M_current_buf =
nullptr;
860 size_t _M_next_bufsiz = _S_init_bufsize;
863 memory_resource*
const _M_upstream;
864 void*
const _M_orig_buf =
nullptr;
865 size_t const _M_orig_size = _M_next_bufsiz;
868 _Chunk* _M_head =
nullptr;
872 _GLIBCXX_END_NAMESPACE_VERSION
memory_resource * new_delete_resource() noexcept
A pmr::memory_resource that uses new to allocate memory.
memory_resource * get_default_resource() noexcept
Get the current default memory resource pointer.
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 piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
std::forward_as_tuple
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
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.