libstdc++
memory_resource.h
Go to the documentation of this file.
1// <memory_resource> -*- C++ -*-
2
3// Copyright (C) 2018-2026 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/** @file include/bits/memory_resource.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory_resource}
28 */
29
30#ifndef _GLIBCXX_MEMORY_RESOURCE_H
31#define _GLIBCXX_MEMORY_RESOURCE_H 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#if __cplusplus >= 201703L
38
39#include <new> // operator new(size_t, void*)
40#include <cstddef> // size_t, max_align_t, byte
41#include <bits/new_throw.h> // __throw_bad_array_new_length
42#include <bits/uses_allocator.h> // allocator_arg_t, __use_alloc
43#include <bits/uses_allocator_args.h> // uninitialized_construct_using_alloc
44#include <ext/numeric_traits.h> // __int_traits
45#include <debug/assertions.h>
46
47#if ! __glibcxx_make_obj_using_allocator
48# include <bits/utility.h> // index_sequence
49# include <tuple> // tuple, forward_as_tuple
50#endif
51#include <bits/memoryfwd.h>
52
53namespace std _GLIBCXX_VISIBILITY(default)
54{
55_GLIBCXX_BEGIN_NAMESPACE_VERSION
56namespace pmr
57{
58 /// Class `memory_resource`
59 /**
60 * @ingroup pmr
61 * @headerfile memory_resource
62 * @since C++17
63 */
64 class memory_resource
65 {
66 static constexpr size_t _S_max_align = alignof(max_align_t);
67
68 public:
69 memory_resource() = default;
70 memory_resource(const memory_resource&) = default;
71 virtual ~memory_resource(); // key function
72
73 memory_resource& operator=(const memory_resource&) = default;
74
75 [[nodiscard]]
76 void*
77 allocate(size_t __bytes, size_t __alignment = _S_max_align)
78 __attribute__((__returns_nonnull__,__alloc_size__(2),__alloc_align__(3)))
79 { return ::operator new(__bytes, do_allocate(__bytes, __alignment)); }
80
81 void
82 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
83 __attribute__((__nonnull__))
84 { return do_deallocate(__p, __bytes, __alignment); }
85
86 [[nodiscard]]
87 bool
88 is_equal(const memory_resource& __other) const noexcept
89 { return do_is_equal(__other); }
90
91 private:
92 virtual void*
93 do_allocate(size_t __bytes, size_t __alignment) = 0;
94
95 virtual void
96 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
97
98 virtual bool
99 do_is_equal(const memory_resource& __other) const noexcept = 0;
100 };
101
102 [[nodiscard]]
103 inline bool
104 operator==(const memory_resource& __a, const memory_resource& __b) noexcept
105 { return &__a == &__b || __a.is_equal(__b); }
106
107#if __cpp_impl_three_way_comparison < 201907L
108 [[nodiscard]]
109 inline bool
110 operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
111 { return !(__a == __b); }
112#endif
113
114 // C++17 23.12.3 Class template polymorphic_allocator
115
116 /// Class template polymorphic_allocator
117 /**
118 * @ingroup pmr
119 * @headerfile memory_resource
120 * @since C++17
121 */
122 template<typename _Tp>
123 class polymorphic_allocator
124 {
125 // _GLIBCXX_RESOLVE_LIB_DEFECTS
126 // 2975. Missing case for pair construction in polymorphic allocators
127 template<typename _Up>
128 struct __not_pair { using type = void; };
129
130 template<typename _Up1, typename _Up2>
131 struct __not_pair<pair<_Up1, _Up2>> { };
132
133 public:
134 using value_type = _Tp;
135
136 polymorphic_allocator() noexcept
137 {
138 extern memory_resource* get_default_resource() noexcept
139 __attribute__((__returns_nonnull__));
140 _M_resource = get_default_resource();
141 }
142
143 polymorphic_allocator(memory_resource* __r) noexcept
144 __attribute__((__nonnull__))
145 : _M_resource(__r)
146 { _GLIBCXX_DEBUG_ASSERT(__r); }
147
148 polymorphic_allocator(const polymorphic_allocator& __other) = default;
149
150 template<typename _Up>
151 polymorphic_allocator(const polymorphic_allocator<_Up>& __x) noexcept
152 : _M_resource(__x.resource())
153 { }
154
155 polymorphic_allocator&
156 operator=(const polymorphic_allocator&) = delete;
157
158 [[nodiscard]]
159 _Tp*
160 allocate(size_t __n)
161 __attribute__((__returns_nonnull__))
162 {
163 if ((__gnu_cxx::__int_traits<size_t>::__max / sizeof(_Tp)) < __n)
164 std::__throw_bad_array_new_length();
165 return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
166 alignof(_Tp)));
167 }
168
169 void
170 deallocate(_Tp* __p, size_t __n) noexcept
171 __attribute__((__nonnull__))
172 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
173
174#ifdef __glibcxx_polymorphic_allocator // >= C++20
175 [[nodiscard]] void*
176 allocate_bytes(size_t __nbytes,
177 size_t __alignment = alignof(max_align_t))
178 { return _M_resource->allocate(__nbytes, __alignment); }
179
180 void
181 deallocate_bytes(void* __p, size_t __nbytes,
182 size_t __alignment = alignof(max_align_t))
183 { _M_resource->deallocate(__p, __nbytes, __alignment); }
184
185 template<typename _Up>
186 [[nodiscard]] _Up*
187 allocate_object(size_t __n = 1)
188 {
189 if ((__gnu_cxx::__int_traits<size_t>::__max / sizeof(_Up)) < __n)
190 std::__throw_bad_array_new_length();
191 return static_cast<_Up*>(allocate_bytes(__n * sizeof(_Up),
192 alignof(_Up)));
193 }
194
195 template<typename _Up>
196 void
197 deallocate_object(_Up* __p, size_t __n = 1)
198 { deallocate_bytes(__p, __n * sizeof(_Up), alignof(_Up)); }
199
200 template<typename _Up, typename... _CtorArgs>
201 [[nodiscard]] _Up*
202 new_object(_CtorArgs&&... __ctor_args)
203 {
204 _Up* __p = allocate_object<_Up>();
205 __try
206 {
207 construct(__p, std::forward<_CtorArgs>(__ctor_args)...);
208 }
209 __catch (...)
210 {
211 deallocate_object(__p);
212 __throw_exception_again;
213 }
214 return __p;
215 }
216
217 template<typename _Up>
218 void
219 delete_object(_Up* __p)
220 {
221 __p->~_Up();
222 deallocate_object(__p);
223 }
224#endif // C++20
225
226#if ! __glibcxx_make_obj_using_allocator // >= C++20
227 template<typename _Tp1, typename... _Args>
228 __attribute__((__nonnull__))
229 typename __not_pair<_Tp1>::type
230 construct(_Tp1* __p, _Args&&... __args)
231 {
232 // _GLIBCXX_RESOLVE_LIB_DEFECTS
233 // 2969. polymorphic_allocator::construct() shouldn't pass resource()
234 using __use_tag
235 = std::__uses_alloc_t<_Tp1, polymorphic_allocator, _Args...>;
236 if constexpr (is_base_of_v<__uses_alloc0, __use_tag>)
237 ::new(__p) _Tp1(std::forward<_Args>(__args)...);
238 else if constexpr (is_base_of_v<__uses_alloc1_, __use_tag>)
239 ::new(__p) _Tp1(allocator_arg, *this,
240 std::forward<_Args>(__args)...);
241 else
242 ::new(__p) _Tp1(std::forward<_Args>(__args)..., *this);
243 }
244
245 template<typename _Tp1, typename _Tp2,
246 typename... _Args1, typename... _Args2>
247 __attribute__((__nonnull__))
248 void
251 {
252 auto __x_tag =
253 __use_alloc<_Tp1, polymorphic_allocator, _Args1...>(*this);
254 auto __y_tag =
255 __use_alloc<_Tp2, polymorphic_allocator, _Args2...>(*this);
256 index_sequence_for<_Args1...> __x_i;
257 index_sequence_for<_Args2...> __y_i;
258
260 _S_construct_p(__x_tag, __x_i, __x),
261 _S_construct_p(__y_tag, __y_i, __y));
262 }
263
264 template<typename _Tp1, typename _Tp2>
265 __attribute__((__nonnull__))
266 void
267 construct(pair<_Tp1, _Tp2>* __p)
268 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
269
270 template<typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
271 __attribute__((__nonnull__))
272 void
273 construct(pair<_Tp1, _Tp2>* __p, _Up&& __x, _Vp&& __y)
274 {
275 this->construct(__p, piecewise_construct,
278 }
279
280 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
281 __attribute__((__nonnull__))
282 void
283 construct(pair<_Tp1, _Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
284 {
285 this->construct(__p, piecewise_construct,
288 }
289
290 template<typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
291 __attribute__((__nonnull__))
292 void
293 construct(pair<_Tp1, _Tp2>* __p, pair<_Up, _Vp>&& __pr)
294 {
295 this->construct(__p, piecewise_construct,
298 }
299#else // make_obj_using_allocator
300 template<typename _Tp1, typename... _Args>
301 __attribute__((__nonnull__))
302 void
303 construct(_Tp1* __p, _Args&&... __args)
304 {
305 std::uninitialized_construct_using_allocator(__p, *this,
306 std::forward<_Args>(__args)...);
307 }
308#endif
309
310 template<typename _Up>
311 __attribute__((__nonnull__))
312 void
313 destroy(_Up* __p)
314 { __p->~_Up(); }
315
316 polymorphic_allocator
317 select_on_container_copy_construction() const noexcept
318 { return polymorphic_allocator(); }
319
321 resource() const noexcept
322 __attribute__((__returns_nonnull__))
323 { return _M_resource; }
324
325 // _GLIBCXX_RESOLVE_LIB_DEFECTS
326 // 3683. operator== for polymorphic_allocator cannot deduce template arg
327 [[nodiscard]]
328 friend bool
329 operator==(const polymorphic_allocator& __a,
330 const polymorphic_allocator& __b) noexcept
331 { return *__a.resource() == *__b.resource(); }
332
333#if __cpp_impl_three_way_comparison < 201907L
334 [[nodiscard]]
335 friend bool
336 operator!=(const polymorphic_allocator& __a,
337 const polymorphic_allocator& __b) noexcept
338 { return !(__a == __b); }
339#endif
340
341 private:
342#if ! __glibcxx_make_obj_using_allocator // >= C++20
343 using __uses_alloc1_ = __uses_alloc1<polymorphic_allocator>;
344 using __uses_alloc2_ = __uses_alloc2<polymorphic_allocator>;
345
346 template<typename _Ind, typename... _Args>
347 static tuple<_Args&&...>
348 _S_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
349 { return std::move(__t); }
350
351 template<size_t... _Ind, typename... _Args>
352 static tuple<allocator_arg_t, polymorphic_allocator, _Args&&...>
353 _S_construct_p(__uses_alloc1_ __ua, index_sequence<_Ind...>,
354 tuple<_Args...>& __t)
355 {
356 return {
357 allocator_arg, *__ua._M_a, std::get<_Ind>(std::move(__t))...
358 };
359 }
360
361 template<size_t... _Ind, typename... _Args>
362 static tuple<_Args&&..., polymorphic_allocator>
363 _S_construct_p(__uses_alloc2_ __ua, index_sequence<_Ind...>,
364 tuple<_Args...>& __t)
365 { return { std::get<_Ind>(std::move(__t))..., *__ua._M_a }; }
366#endif
367
368 memory_resource* _M_resource;
369 };
370
371 template<typename _Tp1, typename _Tp2>
372 [[nodiscard]]
373 inline bool
374 operator==(const polymorphic_allocator<_Tp1>& __a,
375 const polymorphic_allocator<_Tp2>& __b) noexcept
376 { return *__a.resource() == *__b.resource(); }
377
378#if __cpp_impl_three_way_comparison < 201907L
379 template<typename _Tp1, typename _Tp2>
380 [[nodiscard]]
381 inline bool
382 operator!=(const polymorphic_allocator<_Tp1>& __a,
383 const polymorphic_allocator<_Tp2>& __b) noexcept
384 { return !(__a == __b); }
385#endif
386
387} // namespace pmr
388
389 template<typename _Alloc> struct allocator_traits;
390
391 /// Partial specialization for `std::pmr::polymorphic_allocator`
392 /**
393 * @ingroup pmr
394 * @headerfile memory_resource
395 * @since C++17
396 */
397 template<typename _Tp>
398 struct allocator_traits<pmr::polymorphic_allocator<_Tp>>
399 {
400 /// The allocator type
402
403 /// The allocated type
404 using value_type = _Tp;
405
406 /// The allocator's pointer type.
407 using pointer = _Tp*;
408
409 /// The allocator's const pointer type.
410 using const_pointer = const _Tp*;
411
412 /// The allocator's void pointer type.
413 using void_pointer = void*;
414
415 /// The allocator's const void pointer type.
416 using const_void_pointer = const void*;
417
418 /// The allocator's difference type
419 using difference_type = std::ptrdiff_t;
420
421 /// The allocator's size type
422 using size_type = std::size_t;
423
424 /** @{
425 * A `polymorphic_allocator` does not propagate when a
426 * container is copied, moved, or swapped.
427 */
431
432 static allocator_type
435 /// @}
436
437 /// Whether all instances of the allocator type compare equal.
439
440 template<typename _Up>
441 using rebind_alloc = pmr::polymorphic_allocator<_Up>;
442
443 template<typename _Up>
445
446 /**
447 * @brief Allocate memory.
448 * @param __a An allocator.
449 * @param __n The number of objects to allocate space for.
450 *
451 * Calls `a.allocate(n)`.
452 */
453 [[nodiscard]] static pointer
455 { return __a.allocate(__n); }
456
457 /**
458 * @brief Allocate memory.
459 * @param __a An allocator.
460 * @param __n The number of objects to allocate space for.
461 * @return Memory of suitable size and alignment for `n` objects
462 * of type `value_type`.
463 *
464 * The third parameter is ignored..
465 *
466 * Returns `a.allocate(n)`.
467 */
468 [[nodiscard]] static pointer
470 { return __a.allocate(__n); }
471
472#ifdef __glibcxx_allocate_at_least
473 /**
474 * @brief Allocate memory.
475 * @param __a An allocator.
476 * @param __n The number of objects to allocate space for.
477 * @return Memory of suitable size and alignment for `n` objects
478 * of type `value_type`.
479 *
480 * Just returns `{ a.allocate(n), n }`: `polymorphic_allocator`
481 * cannot be extended without breaking ABI.
482 */
483 [[nodiscard]] static std::allocation_result<pointer, size_type>
485 { return { __a.allocate(__n), __n }; }
486#endif
487
488 /**
489 * @brief Deallocate memory.
490 * @param __a An allocator.
491 * @param __p Pointer to the memory to deallocate.
492 * @param __n The number of objects space was allocated for.
493 *
494 * Calls `a.deallocate(p, n)`.
495 */
496 static void
498 { __a.deallocate(__p, __n); }
499
500 /**
501 * @brief Construct an object of type `_Up`
502 * @param __a An allocator.
503 * @param __p Pointer to memory of suitable size and alignment for
504 * an object of type `_Up`.
505 * @param __args Constructor arguments.
506 *
507 * Calls `__a.construct(__p, std::forward<_Args>(__args)...)`
508 * in C++11, C++14 and C++17. Changed in C++20 to call
509 * `std::construct_at(__p, std::forward<_Args>(__args)...)` instead.
510 */
511 template<typename _Up, typename... _Args>
512 static void
513 construct(allocator_type& __a, _Up* __p, _Args&&... __args)
514 { __a.construct(__p, std::forward<_Args>(__args)...); }
515
516 /**
517 * @brief Destroy an object of type `_Up`
518 * @param __a An allocator.
519 * @param __p Pointer to the object to destroy
520 *
521 * Calls `p->_Up()`.
522 */
523 template<typename _Up>
524 static _GLIBCXX20_CONSTEXPR void
527 { __p->~_Up(); }
528
529 /**
530 * @brief The maximum supported allocation size
531 * @return `numeric_limits<size_t>::max() / sizeof(value_type)`
532 */
533 static _GLIBCXX20_CONSTEXPR size_type
534 max_size(const allocator_type&) noexcept
535 { return size_t(-1) / sizeof(value_type); }
536 };
537
538_GLIBCXX_END_NAMESPACE_VERSION
539} // namespace std
540
541#endif // C++17
542#endif // _GLIBCXX_MEMORY_RESOURCE_H
memory_resource * get_default_resource() noexcept
Get the current default memory resource pointer.
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:122
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
Create a tuple of lvalue or rvalue references to the arguments.
Definition tuple:2698
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
Definition stl_pair.h:82
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
ISO C++ entities toplevel namespace is std.
integer_sequence< size_t, _Idx... > index_sequence
Alias template index_sequence.
Definition utility.h:555
make_index_sequence< sizeof...(_Types)> index_sequence_for
Alias template index_sequence_for.
Definition utility.h:563
Primary class template, tuple.
Definition tuple:795
is_nothrow_destructible
Definition type_traits:1198
Uniform interface to all allocator types.
_Alloc allocator_type
The allocator type.
Class memory_resource.
Class template polymorphic_allocator.
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.
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.
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 std::allocation_result< pointer, size_type > allocate_at_least(allocator_type &__a, size_type __n)
Allocate memory.
static allocator_type select_on_container_copy_construction(const allocator_type &) noexcept
void * void_pointer
The allocator's void pointer type.
false_type is_always_equal
Whether all instances of the allocator type compare equal.
pmr::polymorphic_allocator< _Tp > allocator_type
The allocator type.
Struct holding two objects (or references) of arbitrary type.
Definition stl_pair.h:307
_T1 first
The first member.
Definition stl_pair.h:311
_T2 second
The second member.
Definition stl_pair.h:312
Tag type for piecewise construction of std::pair objects.
Definition stl_pair.h:79