1 // <memory_resource> -*- C++ -*-
3 // Copyright (C) 2018-2019 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/memory_resource
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_MEMORY_RESOURCE
30 #define _GLIBCXX_MEMORY_RESOURCE 1
32 #pragma GCC system_header
34 #if __cplusplus >= 201703L
36 #include <memory> // align, allocator_arg_t, __uses_alloc
37 #include <utility> // pair, index_sequence
38 #include <vector> // vector
39 #include <cstddef> // size_t, max_align_t
40 #include <shared_mutex> // shared_mutex
41 #include <debug/assertions.h>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 #ifdef _GLIBCXX_HAS_GTHREADS
49 // Header and all contents are present.
50 # define __cpp_lib_memory_resource 201603
52 // The pmr::synchronized_pool_resource type is missing.
53 # define __cpp_lib_memory_resource 1
56 class memory_resource;
58 template<typename _Tp>
59 class polymorphic_allocator;
61 // Global memory resources
62 memory_resource* new_delete_resource() noexcept;
63 memory_resource* null_memory_resource() noexcept;
64 memory_resource* set_default_resource(memory_resource* __r) noexcept;
65 memory_resource* get_default_resource() noexcept
66 __attribute__((__returns_nonnull__));
68 // Pool resource classes
70 #ifdef _GLIBCXX_HAS_GTHREADS
71 class synchronized_pool_resource;
73 class unsynchronized_pool_resource;
74 class monotonic_buffer_resource;
76 /// Class memory_resource
79 static constexpr size_t _S_max_align = alignof(max_align_t);
82 memory_resource() = default;
83 memory_resource(const memory_resource&) = default;
84 virtual ~memory_resource() = default;
86 memory_resource& operator=(const memory_resource&) = default;
90 allocate(size_t __bytes, size_t __alignment = _S_max_align)
91 __attribute__((__returns_nonnull__,__alloc_size__(2),__alloc_align__(3)))
92 { return do_allocate(__bytes, __alignment); }
95 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
96 __attribute__((__nonnull__))
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 // C++17 23.12.3 Class template polymorphic_allocator
124 template<typename _Tp>
125 class polymorphic_allocator
127 // _GLIBCXX_RESOLVE_LIB_DEFECTS
128 // 2975. Missing case for pair construction in polymorphic allocators
129 template<typename _Up>
130 struct __not_pair { using type = void; };
132 template<typename _Up1, typename _Up2>
133 struct __not_pair<pair<_Up1, _Up2>> { };
136 using value_type = _Tp;
138 polymorphic_allocator() noexcept
139 : _M_resource(get_default_resource())
142 polymorphic_allocator(memory_resource* __r) noexcept
143 __attribute__((__nonnull__))
145 { _GLIBCXX_DEBUG_ASSERT(__r); }
147 polymorphic_allocator(const polymorphic_allocator& __other) = default;
149 template<typename _Up>
150 polymorphic_allocator(const polymorphic_allocator<_Up>& __x) noexcept
151 : _M_resource(__x.resource())
154 polymorphic_allocator&
155 operator=(const polymorphic_allocator&) = delete;
160 __attribute__((__returns_nonnull__))
162 if (__n > (numeric_limits<size_t>::max() / sizeof(_Tp)))
163 std::__throw_bad_alloc();
164 return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
169 deallocate(_Tp* __p, size_t __n) noexcept
170 __attribute__((__nonnull__))
171 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
173 template<typename _Tp1, typename... _Args>
174 __attribute__((__nonnull__))
175 typename __not_pair<_Tp1>::type
176 construct(_Tp1* __p, _Args&&... __args)
178 // _GLIBCXX_RESOLVE_LIB_DEFECTS
179 // 2969. polymorphic_allocator::construct() shouldn't pass resource()
181 = std::__uses_alloc_t<_Tp1, polymorphic_allocator, _Args...>;
182 if constexpr (is_base_of_v<__uses_alloc0, __use_tag>)
183 ::new(__p) _Tp1(std::forward<_Args>(__args)...);
184 else if constexpr (is_base_of_v<__uses_alloc1_, __use_tag>)
185 ::new(__p) _Tp1(allocator_arg, *this,
186 std::forward<_Args>(__args)...);
188 ::new(__p) _Tp1(std::forward<_Args>(__args)..., *this);
191 template<typename _Tp1, typename _Tp2,
192 typename... _Args1, typename... _Args2>
193 __attribute__((__nonnull__))
195 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
196 tuple<_Args1...> __x, tuple<_Args2...> __y)
199 __use_alloc<_Tp1, polymorphic_allocator, _Args1...>(*this);
201 __use_alloc<_Tp2, polymorphic_allocator, _Args2...>(*this);
202 index_sequence_for<_Args1...> __x_i;
203 index_sequence_for<_Args2...> __y_i;
205 ::new(__p) pair<_Tp1, _Tp2>(piecewise_construct,
206 _S_construct_p(__x_tag, __x_i, __x),
207 _S_construct_p(__y_tag, __y_i, __y));
210 template<typename _Tp1, typename _Tp2>
211 __attribute__((__nonnull__))
213 construct(pair<_Tp1, _Tp2>* __p)
214 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
216 template<typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
217 __attribute__((__nonnull__))
219 construct(pair<_Tp1, _Tp2>* __p, _Up&& __x, _Vp&& __y)
221 this->construct(__p, piecewise_construct,
222 forward_as_tuple(std::forward<_Up>(__x)),
223 forward_as_tuple(std::forward<_Vp>(__y)));
226 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
227 __attribute__((__nonnull__))
229 construct(pair<_Tp1, _Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
231 this->construct(__p, piecewise_construct,
232 forward_as_tuple(__pr.first),
233 forward_as_tuple(__pr.second));
236 template<typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
237 __attribute__((__nonnull__))
239 construct(pair<_Tp1, _Tp2>* __p, pair<_Up, _Vp>&& __pr)
241 this->construct(__p, piecewise_construct,
242 forward_as_tuple(std::forward<_Up>(__pr.first)),
243 forward_as_tuple(std::forward<_Vp>(__pr.second)));
246 template<typename _Up>
247 __attribute__((__nonnull__))
252 polymorphic_allocator
253 select_on_container_copy_construction() const noexcept
254 { return polymorphic_allocator(); }
257 resource() const noexcept
258 __attribute__((__returns_nonnull__))
259 { return _M_resource; }
262 using __uses_alloc1_ = __uses_alloc1<polymorphic_allocator>;
263 using __uses_alloc2_ = __uses_alloc2<polymorphic_allocator>;
265 template<typename _Ind, typename... _Args>
266 static tuple<_Args&&...>
267 _S_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
268 { return std::move(__t); }
270 template<size_t... _Ind, typename... _Args>
271 static tuple<allocator_arg_t, polymorphic_allocator, _Args&&...>
272 _S_construct_p(__uses_alloc1_ __ua, index_sequence<_Ind...>,
273 tuple<_Args...>& __t)
276 allocator_arg, *__ua._M_a, std::get<_Ind>(std::move(__t))...
280 template<size_t... _Ind, typename... _Args>
281 static tuple<_Args&&..., polymorphic_allocator>
282 _S_construct_p(__uses_alloc2_ __ua, index_sequence<_Ind...>,
283 tuple<_Args...>& __t)
284 { return { std::get<_Ind>(std::move(__t))..., *__ua._M_a }; }
286 memory_resource* _M_resource;
289 template<typename _Tp1, typename _Tp2>
291 operator==(const polymorphic_allocator<_Tp1>& __a,
292 const polymorphic_allocator<_Tp2>& __b) noexcept
293 { return *__a.resource() == *__b.resource(); }
295 template<typename _Tp1, typename _Tp2>
297 operator!=(const polymorphic_allocator<_Tp1>& __a,
298 const polymorphic_allocator<_Tp2>& __b) noexcept
299 { return !(__a == __b); }
302 /// Parameters for tuning a pool resource's behaviour.
305 /** @brief Upper limit on number of blocks in a chunk.
307 * A lower value prevents allocating huge chunks that could remain mostly
308 * unused, but means pools will need to replenished more frequently.
310 size_t max_blocks_per_chunk = 0;
312 /* @brief Largest block size (in bytes) that should be served from pools.
314 * Larger allocations will be served directly by the upstream resource,
315 * not from one of the pools managed by the pool resource.
317 size_t largest_required_pool_block = 0;
320 // Common implementation details for un-/synchronized pool resources.
321 class __pool_resource
323 friend class synchronized_pool_resource;
324 friend class unsynchronized_pool_resource;
326 __pool_resource(const pool_options& __opts, memory_resource* __upstream);
330 __pool_resource(const __pool_resource&) = delete;
331 __pool_resource& operator=(const __pool_resource&) = delete;
333 // Allocate a large unpooled block.
335 allocate(size_t __bytes, size_t __alignment);
337 // Deallocate a large unpooled block.
339 deallocate(void* __p, size_t __bytes, size_t __alignment);
342 // Deallocate unpooled memory.
343 void release() noexcept;
345 memory_resource* resource() const noexcept
346 { return _M_unpooled.get_allocator().resource(); }
350 _Pool* _M_alloc_pools();
352 const pool_options _M_opts;
355 // Collection of blocks too big for any pool, sorted by address.
356 // This also stores the only copy of the upstream memory resource pointer.
357 pmr::vector<_BigBlock> _M_unpooled;
362 #ifdef _GLIBCXX_HAS_GTHREADS
363 /// A thread-safe memory resource that manages pools of fixed-size blocks.
364 class synchronized_pool_resource : public memory_resource
367 synchronized_pool_resource(const pool_options& __opts,
368 memory_resource* __upstream)
369 __attribute__((__nonnull__));
371 synchronized_pool_resource()
372 : synchronized_pool_resource(pool_options(), get_default_resource())
376 synchronized_pool_resource(memory_resource* __upstream)
377 __attribute__((__nonnull__))
378 : synchronized_pool_resource(pool_options(), __upstream)
382 synchronized_pool_resource(const pool_options& __opts)
383 : synchronized_pool_resource(__opts, get_default_resource()) { }
385 synchronized_pool_resource(const synchronized_pool_resource&) = delete;
387 virtual ~synchronized_pool_resource();
389 synchronized_pool_resource&
390 operator=(const synchronized_pool_resource&) = delete;
395 upstream_resource() const noexcept
396 __attribute__((__returns_nonnull__))
397 { return _M_impl.resource(); }
399 pool_options options() const noexcept { return _M_impl._M_opts; }
403 do_allocate(size_t __bytes, size_t __alignment) override;
406 do_deallocate(void* __p, size_t __bytes, size_t __alignment) override;
409 do_is_equal(const memory_resource& __other) const noexcept override
410 { return this == &__other; }
413 // Thread-specific pools (only public for access by implementation details)
417 _TPools* _M_alloc_tpools(lock_guard<shared_mutex>&);
418 _TPools* _M_alloc_shared_tpools(lock_guard<shared_mutex>&);
419 auto _M_thread_specific_pools() noexcept;
421 __pool_resource _M_impl;
422 __gthread_key_t _M_key;
423 // Linked list of thread-specific pools. All threads share _M_tpools[0].
424 _TPools* _M_tpools = nullptr;
425 mutable shared_mutex _M_mx;
429 /// A non-thread-safe memory resource that manages pools of fixed-size blocks.
430 class unsynchronized_pool_resource : public memory_resource
433 [[__gnu__::__nonnull__]]
434 unsynchronized_pool_resource(const pool_options& __opts,
435 memory_resource* __upstream);
437 unsynchronized_pool_resource()
438 : unsynchronized_pool_resource(pool_options(), get_default_resource())
441 [[__gnu__::__nonnull__]]
443 unsynchronized_pool_resource(memory_resource* __upstream)
444 : unsynchronized_pool_resource(pool_options(), __upstream)
448 unsynchronized_pool_resource(const pool_options& __opts)
449 : unsynchronized_pool_resource(__opts, get_default_resource()) { }
451 unsynchronized_pool_resource(const unsynchronized_pool_resource&) = delete;
453 virtual ~unsynchronized_pool_resource();
455 unsynchronized_pool_resource&
456 operator=(const unsynchronized_pool_resource&) = delete;
460 [[__gnu__::__returns_nonnull__]]
462 upstream_resource() const noexcept
463 { return _M_impl.resource(); }
465 pool_options options() const noexcept { return _M_impl._M_opts; }
469 do_allocate(size_t __bytes, size_t __alignment) override;
472 do_deallocate(void* __p, size_t __bytes, size_t __alignment) override;
475 do_is_equal(const memory_resource& __other) const noexcept override
476 { return this == &__other; }
479 using _Pool = __pool_resource::_Pool;
481 auto _M_find_pool(size_t) noexcept;
483 __pool_resource _M_impl;
484 _Pool* _M_pools = nullptr;
487 class monotonic_buffer_resource : public memory_resource
491 monotonic_buffer_resource(memory_resource* __upstream) noexcept
492 __attribute__((__nonnull__))
493 : _M_upstream(__upstream)
494 { _GLIBCXX_DEBUG_ASSERT(__upstream != nullptr); }
496 monotonic_buffer_resource(size_t __initial_size,
497 memory_resource* __upstream) noexcept
498 __attribute__((__nonnull__))
499 : _M_next_bufsiz(__initial_size),
500 _M_upstream(__upstream)
502 _GLIBCXX_DEBUG_ASSERT(__upstream != nullptr);
503 _GLIBCXX_DEBUG_ASSERT(__initial_size > 0);
506 monotonic_buffer_resource(void* __buffer, size_t __buffer_size,
507 memory_resource* __upstream) noexcept
508 __attribute__((__nonnull__(4)))
509 : _M_current_buf(__buffer), _M_avail(__buffer_size),
510 _M_next_bufsiz(_S_next_bufsize(__buffer_size)),
511 _M_upstream(__upstream),
512 _M_orig_buf(__buffer), _M_orig_size(__buffer_size)
514 _GLIBCXX_DEBUG_ASSERT(__upstream != nullptr);
515 _GLIBCXX_DEBUG_ASSERT(__buffer != nullptr || __buffer_size == 0);
518 monotonic_buffer_resource() noexcept
519 : monotonic_buffer_resource(get_default_resource())
523 monotonic_buffer_resource(size_t __initial_size) noexcept
524 : monotonic_buffer_resource(__initial_size, get_default_resource())
527 monotonic_buffer_resource(void* __buffer, size_t __buffer_size) noexcept
528 : monotonic_buffer_resource(__buffer, __buffer_size, get_default_resource())
531 monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
533 virtual ~monotonic_buffer_resource() { release(); }
535 monotonic_buffer_resource&
536 operator=(const monotonic_buffer_resource&) = delete;
542 _M_release_buffers();
544 // reset to initial state at contruction:
545 if ((_M_current_buf = _M_orig_buf))
547 _M_avail = _M_orig_size;
548 _M_next_bufsiz = _S_next_bufsize(_M_orig_size);
553 _M_next_bufsiz = _M_orig_size;
558 upstream_resource() const noexcept
559 __attribute__((__returns_nonnull__))
560 { return _M_upstream; }
564 do_allocate(size_t __bytes, size_t __alignment) override
567 __bytes = 1; // Ensures we don't return the same pointer twice.
569 void* __p = std::align(__alignment, __bytes, _M_current_buf, _M_avail);
572 _M_new_buffer(__bytes, __alignment);
573 __p = _M_current_buf;
575 _M_current_buf = (char*)_M_current_buf + __bytes;
581 do_deallocate(void*, size_t, size_t) override
585 do_is_equal(const memory_resource& __other) const noexcept override
586 { return this == &__other; }
589 // Update _M_current_buf and _M_avail to refer to a new buffer with
590 // at least the specified size and alignment, allocated from upstream.
592 _M_new_buffer(size_t __bytes, size_t __alignment);
594 // Deallocate all buffers obtained from upstream.
596 _M_release_buffers() noexcept;
599 _S_next_bufsize(size_t __buffer_size) noexcept
601 if (__buffer_size == 0)
603 return __buffer_size * _S_growth_factor;
606 static constexpr size_t _S_init_bufsize = 128 * sizeof(void*);
607 static constexpr float _S_growth_factor = 1.5;
609 void* _M_current_buf = nullptr;
611 size_t _M_next_bufsiz = _S_init_bufsize;
613 // Initial values set at construction and reused by release():
614 memory_resource* const _M_upstream;
615 void* const _M_orig_buf = nullptr;
616 size_t const _M_orig_size = _M_next_bufsiz;
619 _Chunk* _M_head = nullptr;
623 _GLIBCXX_END_NAMESPACE_VERSION
627 #endif // _GLIBCXX_MEMORY_RESOURCE