29#ifndef _GLIBCXX_MEMORY_RESOURCE
30#define _GLIBCXX_MEMORY_RESOURCE 1
32#pragma GCC system_header
34#if __cplusplus >= 201703L
41#include <bits/uses_allocator.h>
46#if ! __cpp_lib_make_obj_using_allocator
51namespace std _GLIBCXX_VISIBILITY(default)
53_GLIBCXX_BEGIN_NAMESPACE_VERSION
56#ifdef _GLIBCXX_HAS_GTHREADS
58# define __cpp_lib_memory_resource 201603L
61# define __cpp_lib_memory_resource 1
64 class memory_resource;
66#if __cplusplus == 201703L
67 template<
typename _Tp>
68 class polymorphic_allocator;
70# define __cpp_lib_polymorphic_allocator 201902L
71 template<
typename _Tp = std::
byte>
72 class polymorphic_allocator;
76 memory_resource* new_delete_resource() noexcept;
77 memory_resource* null_memory_resource() noexcept;
78 memory_resource* set_default_resource(memory_resource* __r) noexcept;
79 memory_resource* get_default_resource() noexcept
80 __attribute__((__returns_nonnull__));
84#ifdef _GLIBCXX_HAS_GTHREADS
85 class synchronized_pool_resource;
87 class unsynchronized_pool_resource;
88 class monotonic_buffer_resource;
93 static constexpr size_t _S_max_align =
alignof(max_align_t);
104 allocate(
size_t __bytes,
size_t __alignment = _S_max_align)
105 __attribute__((__returns_nonnull__,__alloc_size__(2),__alloc_align__(3)))
106 {
return do_allocate(__bytes, __alignment); }
109 deallocate(
void* __p,
size_t __bytes,
size_t __alignment = _S_max_align)
110 __attribute__((__nonnull__))
111 {
return do_deallocate(__p, __bytes, __alignment); }
115 {
return do_is_equal(__other); }
119 do_allocate(
size_t __bytes,
size_t __alignment) = 0;
122 do_deallocate(
void* __p,
size_t __bytes,
size_t __alignment) = 0;
130 {
return &__a == &__b || __a.is_equal(__b); }
132#if __cpp_impl_three_way_comparison < 201907L
134 operator!=(
const memory_resource& __a,
const memory_resource& __b)
noexcept
135 {
return !(__a == __b); }
139 template<
typename _Tp>
140 class polymorphic_allocator
144 template<
typename _Up>
145 struct __not_pair {
using type = void; };
147 template<
typename _Up1,
typename _Up2>
148 struct __not_pair<pair<_Up1, _Up2>> { };
151 using value_type = _Tp;
153 polymorphic_allocator() noexcept
154 : _M_resource(get_default_resource())
157 polymorphic_allocator(memory_resource* __r)
noexcept
158 __attribute__((__nonnull__))
160 { _GLIBCXX_DEBUG_ASSERT(__r); }
162 polymorphic_allocator(
const polymorphic_allocator& __other) =
default;
164 template<
typename _Up>
165 polymorphic_allocator(
const polymorphic_allocator<_Up>& __x) noexcept
166 : _M_resource(__x.resource())
169 polymorphic_allocator&
170 operator=(
const polymorphic_allocator&) =
delete;
175 __attribute__((__returns_nonnull__))
178 std::__throw_bad_array_new_length();
179 return static_cast<_Tp*
>(_M_resource->allocate(__n *
sizeof(_Tp),
184 deallocate(_Tp* __p,
size_t __n)
noexcept
185 __attribute__((__nonnull__))
186 { _M_resource->deallocate(__p, __n *
sizeof(_Tp),
alignof(_Tp)); }
188#if __cplusplus > 201703L
190 allocate_bytes(
size_t __nbytes,
191 size_t __alignment =
alignof(max_align_t))
192 {
return _M_resource->allocate(__nbytes, __alignment); }
195 deallocate_bytes(
void* __p,
size_t __nbytes,
196 size_t __alignment =
alignof(max_align_t))
197 { _M_resource->deallocate(__p, __nbytes, __alignment); }
199 template<
typename _Up>
201 allocate_object(
size_t __n = 1)
204 std::__throw_bad_array_new_length();
205 return static_cast<_Up*
>(allocate_bytes(__n *
sizeof(_Up),
209 template<
typename _Up>
211 deallocate_object(_Up* __p,
size_t __n = 1)
212 { deallocate_bytes(__p, __n *
sizeof(_Up),
alignof(_Up)); }
214 template<
typename _Up,
typename... _CtorArgs>
216 new_object(_CtorArgs&&... __ctor_args)
218 _Up* __p = allocate_object<_Up>();
221 construct(__p, std::forward<_CtorArgs>(__ctor_args)...);
225 deallocate_object(__p);
226 __throw_exception_again;
231 template<
typename _Up>
233 delete_object(_Up* __p)
236 deallocate_object(__p);
240#if ! __cpp_lib_make_obj_using_allocator
241 template<
typename _Tp1,
typename... _Args>
242 __attribute__((__nonnull__))
243 typename __not_pair<_Tp1>::type
244 construct(_Tp1* __p, _Args&&... __args)
249 = std::__uses_alloc_t<_Tp1, polymorphic_allocator, _Args...>;
250 if constexpr (is_base_of_v<__uses_alloc0, __use_tag>)
251 ::new(__p) _Tp1(std::forward<_Args>(__args)...);
252 else if constexpr (is_base_of_v<__uses_alloc1_, __use_tag>)
253 ::new(__p) _Tp1(allocator_arg, *
this,
254 std::forward<_Args>(__args)...);
256 ::new(__p) _Tp1(std::forward<_Args>(__args)..., *
this);
259 template<
typename _Tp1,
typename _Tp2,
260 typename... _Args1,
typename... _Args2>
261 __attribute__((__nonnull__))
263 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
264 tuple<_Args1...> __x, tuple<_Args2...> __y)
267 __use_alloc<_Tp1, polymorphic_allocator, _Args1...>(*this);
269 __use_alloc<_Tp2, polymorphic_allocator, _Args2...>(*this);
274 _S_construct_p(__x_tag, __x_i, __x),
275 _S_construct_p(__y_tag, __y_i, __y));
278 template<
typename _Tp1,
typename _Tp2>
279 __attribute__((__nonnull__))
281 construct(pair<_Tp1, _Tp2>* __p)
284 template<
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
285 __attribute__((__nonnull__))
287 construct(pair<_Tp1, _Tp2>* __p, _Up&& __x, _Vp&& __y)
294 template <
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
295 __attribute__((__nonnull__))
304 template<
typename _Tp1,
typename _Tp2,
typename _Up,
typename _Vp>
305 __attribute__((__nonnull__))
307 construct(pair<_Tp1, _Tp2>* __p, pair<_Up, _Vp>&& __pr)
314 template<
typename _Tp1,
typename... _Args>
315 __attribute__((__nonnull__))
317 construct(_Tp1* __p, _Args&&... __args)
319 std::uninitialized_construct_using_allocator(__p, *
this,
320 std::forward<_Args>(__args)...);
324 template<
typename _Up>
325 _GLIBCXX20_DEPRECATED_SUGGEST(
"allocator_traits::destroy")
326 __attribute__((__nonnull__))
331 polymorphic_allocator
332 select_on_container_copy_construction() const noexcept
333 {
return polymorphic_allocator(); }
336 resource() const noexcept
337 __attribute__((__returns_nonnull__))
338 {
return _M_resource; }
341 using __uses_alloc1_ = __uses_alloc1<polymorphic_allocator>;
342 using __uses_alloc2_ = __uses_alloc2<polymorphic_allocator>;
344#if ! __cpp_lib_make_obj_using_allocator
345 template<
typename _Ind,
typename... _Args>
346 static tuple<_Args&&...>
347 _S_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
350 template<
size_t... _Ind,
typename... _Args>
351 static tuple<allocator_arg_t, polymorphic_allocator, _Args&&...>
352 _S_construct_p(__uses_alloc1_ __ua, index_sequence<_Ind...>,
353 tuple<_Args...>& __t)
356 allocator_arg, *__ua._M_a, std::get<_Ind>(
std::move(__t))...
360 template<
size_t... _Ind,
typename... _Args>
361 static tuple<_Args&&..., polymorphic_allocator>
362 _S_construct_p(__uses_alloc2_ __ua, index_sequence<_Ind...>,
363 tuple<_Args...>& __t)
364 {
return { std::get<_Ind>(
std::move(__t))..., *__ua._M_a }; }
367 memory_resource* _M_resource;
370 template<
typename _Tp1,
typename _Tp2>
372 operator==(
const polymorphic_allocator<_Tp1>& __a,
373 const polymorphic_allocator<_Tp2>& __b)
noexcept
374 {
return *__a.resource() == *__b.resource(); }
376#if __cpp_impl_three_way_comparison < 201907L
377 template<
typename _Tp1,
typename _Tp2>
379 operator!=(
const polymorphic_allocator<_Tp1>& __a,
380 const polymorphic_allocator<_Tp2>& __b)
noexcept
381 {
return !(__a == __b); }
399 size_t largest_required_pool_block = 0;
403 class __pool_resource
412 __pool_resource(
const __pool_resource&) =
delete;
413 __pool_resource& operator=(
const __pool_resource&) =
delete;
417 allocate(
size_t __bytes,
size_t __alignment);
421 deallocate(
void* __p,
size_t __bytes,
size_t __alignment);
425 void release()
noexcept;
428 {
return _M_unpooled.get_allocator().resource(); }
432 _Pool* _M_alloc_pools();
434 const pool_options _M_opts;
439 _GLIBCXX_STD_C::pmr::vector<_BigBlock> _M_unpooled;
444#ifdef _GLIBCXX_HAS_GTHREADS
451 __attribute__((__nonnull__));
459 __attribute__((__nonnull__))
477 upstream_resource()
const noexcept
478 __attribute__((__returns_nonnull__))
479 {
return _M_impl.resource(); }
481 pool_options options()
const noexcept {
return _M_impl._M_opts; }
485 do_allocate(
size_t __bytes,
size_t __alignment)
override;
488 do_deallocate(
void* __p,
size_t __bytes,
size_t __alignment)
override;
492 {
return this == &__other; }
501 auto _M_thread_specific_pools()
noexcept;
503 __pool_resource _M_impl;
504 __gthread_key_t _M_key;
506 _TPools* _M_tpools =
nullptr;
515 [[__gnu__::__nonnull__]]
523 [[__gnu__::__nonnull__]]
542 [[__gnu__::__returns_nonnull__]]
544 upstream_resource()
const noexcept
545 {
return _M_impl.resource(); }
547 pool_options options()
const noexcept {
return _M_impl._M_opts; }
551 do_allocate(
size_t __bytes,
size_t __alignment)
override;
554 do_deallocate(
void* __p,
size_t __bytes,
size_t __alignment)
override;
558 {
return this == &__other; }
561 using _Pool = __pool_resource::_Pool;
563 auto _M_find_pool(
size_t)
noexcept;
565 __pool_resource _M_impl;
566 _Pool* _M_pools =
nullptr;
574 __attribute__((__nonnull__))
575 : _M_upstream(__upstream)
576 { _GLIBCXX_DEBUG_ASSERT(__upstream !=
nullptr); }
578 monotonic_buffer_resource(
size_t __initial_size,
579 memory_resource* __upstream)
noexcept
580 __attribute__((__nonnull__))
581 : _M_next_bufsiz(__initial_size),
582 _M_upstream(__upstream)
584 _GLIBCXX_DEBUG_ASSERT(__upstream !=
nullptr);
585 _GLIBCXX_DEBUG_ASSERT(__initial_size > 0);
588 monotonic_buffer_resource(
void* __buffer,
size_t __buffer_size,
589 memory_resource* __upstream)
noexcept
590 __attribute__((__nonnull__(4)))
591 : _M_current_buf(__buffer), _M_avail(__buffer_size),
592 _M_next_bufsiz(_S_next_bufsize(__buffer_size)),
593 _M_upstream(__upstream),
594 _M_orig_buf(__buffer), _M_orig_size(__buffer_size)
596 _GLIBCXX_DEBUG_ASSERT(__upstream !=
nullptr);
597 _GLIBCXX_DEBUG_ASSERT(__buffer !=
nullptr || __buffer_size == 0);
600 monotonic_buffer_resource() noexcept
601 : monotonic_buffer_resource(get_default_resource())
605 monotonic_buffer_resource(
size_t __initial_size) noexcept
606 : monotonic_buffer_resource(__initial_size, get_default_resource())
609 monotonic_buffer_resource(
void* __buffer,
size_t __buffer_size) noexcept
610 : monotonic_buffer_resource(__buffer, __buffer_size, get_default_resource())
613 monotonic_buffer_resource(
const monotonic_buffer_resource&) =
delete;
615 virtual ~monotonic_buffer_resource();
617 monotonic_buffer_resource&
618 operator=(
const monotonic_buffer_resource&) =
delete;
624 _M_release_buffers();
627 if ((_M_current_buf = _M_orig_buf))
629 _M_avail = _M_orig_size;
630 _M_next_bufsiz = _S_next_bufsize(_M_orig_size);
635 _M_next_bufsiz = _M_orig_size;
640 upstream_resource() const noexcept
641 __attribute__((__returns_nonnull__))
642 {
return _M_upstream; }
646 do_allocate(
size_t __bytes,
size_t __alignment)
override
648 if (__builtin_expect(__bytes == 0,
false))
651 void* __p =
std::align(__alignment, __bytes, _M_current_buf, _M_avail);
652 if (__builtin_expect(__p ==
nullptr,
false))
654 _M_new_buffer(__bytes, __alignment);
655 __p = _M_current_buf;
657 _M_current_buf = (
char*)_M_current_buf + __bytes;
663 do_deallocate(
void*,
size_t,
size_t)
override
667 do_is_equal(
const memory_resource& __other)
const noexcept override
668 {
return this == &__other; }
674 _M_new_buffer(
size_t __bytes,
size_t __alignment);
678 _M_release_buffers() noexcept;
681 _S_next_bufsize(
size_t __buffer_size) noexcept
683 if (__builtin_expect(__buffer_size == 0,
false))
685 return __buffer_size * _S_growth_factor;
688 static constexpr size_t _S_init_bufsize = 128 *
sizeof(
void*);
689 static constexpr float _S_growth_factor = 1.5;
691 void* _M_current_buf =
nullptr;
693 size_t _M_next_bufsiz = _S_init_bufsize;
696 memory_resource*
const _M_upstream;
697 void*
const _M_orig_buf =
nullptr;
698 size_t const _M_orig_size = _M_next_bufsiz;
701 _Chunk* _M_head =
nullptr;
705_GLIBCXX_END_NAMESPACE_VERSION
void * align(size_t __align, size_t __size, void *&__ptr, size_t &__space) noexcept
Fit aligned storage in buffer.
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>.
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.
A simple scoped lock type.
Struct holding two objects of arbitrary type.
_T1 first
The first member.
_T2 second
The second member.