libstdc++
bits/new_allocator.h
Go to the documentation of this file.
1// Allocator that wraps operator new -*- C++ -*-
2
3// Copyright (C) 2001-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 bits/new_allocator.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory}
28 */
29
30#ifndef _STD_NEW_ALLOCATOR_H
31#define _STD_NEW_ALLOCATOR_H 1
32
33#include <bits/c++config.h>
34#include <new>
35#include <bits/new_throw.h>
36#include <bits/move.h>
37#if __cplusplus >= 201103L
38#include <type_traits>
39#endif
40#include <bits/memoryfwd.h>
41
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
45
46 /**
47 * @brief An allocator that uses global `new`, as per C++03 [20.4.1].
48 * @ingroup allocators
49 *
50 * This is precisely the allocator defined in the C++ Standard.
51 * - all allocation calls `operator new`
52 * - all deallocation calls `operator delete`
53 *
54 * This is the default base-class implementation of `std::allocator`,
55 * and is also the base-class of the `__gnu_cxx::new_allocator` extension.
56 * You should use either `std::allocator` or `__gnu_cxx::new_allocator`
57 * instead of using this directly.
58 *
59 * @tparam _Tp Type of allocated object.
60 *
61 * @headerfile memory
62 */
63 template<typename _Tp>
64 class __new_allocator
65 {
66 public:
67 typedef _Tp value_type;
68 typedef std::size_t size_type;
69 typedef std::ptrdiff_t difference_type;
70#if __cplusplus <= 201703L
71 typedef _Tp* pointer;
72 typedef const _Tp* const_pointer;
73 typedef _Tp& reference;
74 typedef const _Tp& const_reference;
75
76 template<typename _Tp1>
77 struct rebind
78 { typedef __new_allocator<_Tp1> other; };
79#endif
80
81#if __cplusplus >= 201103L
82 // _GLIBCXX_RESOLVE_LIB_DEFECTS
83 // 2103. propagate_on_container_move_assignment
84 typedef std::true_type propagate_on_container_move_assignment;
85#endif
86
87 __attribute__((__always_inline__))
88 _GLIBCXX20_CONSTEXPR
89 __new_allocator() _GLIBCXX_USE_NOEXCEPT { }
90
91 __attribute__((__always_inline__))
92 _GLIBCXX20_CONSTEXPR
93 __new_allocator(const __new_allocator&) _GLIBCXX_USE_NOEXCEPT { }
94
95 template<typename _Tp1>
96 __attribute__((__always_inline__))
97 _GLIBCXX20_CONSTEXPR
98 __new_allocator(const __new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { }
99
100#if __cplusplus >= 201103L
101 __new_allocator& operator=(const __new_allocator&) = default;
102#endif
103
104#if __cplusplus <= 201703L
105 ~__new_allocator() _GLIBCXX_USE_NOEXCEPT { }
106
107 pointer
108 address(reference __x) const _GLIBCXX_NOEXCEPT
109 { return std::__addressof(__x); }
110
111 const_pointer
112 address(const_reference __x) const _GLIBCXX_NOEXCEPT
113 { return std::__addressof(__x); }
114#endif
115
116#if __has_builtin(__builtin_operator_new) >= 201802L
117# define _GLIBCXX_OPERATOR_NEW __builtin_operator_new
118# define _GLIBCXX_OPERATOR_DELETE __builtin_operator_delete
119#else
120# define _GLIBCXX_OPERATOR_NEW ::operator new
121# define _GLIBCXX_OPERATOR_DELETE ::operator delete
122#endif
123
124#pragma GCC diagnostic push
125#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
126
127 __attribute__((__always_inline__))
128 _GLIBCXX20_CONSTEXPR static void
129 _S_check_allocation_limit(size_t __n)
130 {
131 if (__builtin_expect(__n > _S_max_size(), false))
132 {
133 // _GLIBCXX_RESOLVE_LIB_DEFECTS
134 // 3190. allocator::allocate sometimes returns too little storage
135 if (__n > (std::size_t(-1) / sizeof(_Tp)))
136 std::__throw_bad_array_new_length();
137 std::__throw_bad_alloc();
138 }
139 }
140
141 // NB: __n is permitted to be 0. The C++ standard says nothing
142 // about what the return value is when __n == 0.
143 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR _Tp*
144 allocate(size_type __n, const void* = static_cast<const void*>(0))
145 {
146#if __cplusplus >= 201103L
147 // _GLIBCXX_RESOLVE_LIB_DEFECTS
148 // 3308. std::allocator<void>().allocate(n)
149#if ! __cpp_concepts
150 static_assert(sizeof(_Tp) != 0, "cannot allocate incomplete types");
151#else
152 static_assert(requires { sizeof(_Tp); },
153 "cannot allocate incomplete types");
154
155 if constexpr (!requires { sizeof(_Tp); })
156 return nullptr; // static_assert already failed
157 else
158#endif
159#endif
160 {
161 _S_check_allocation_limit(__n);
162#if __cpp_aligned_new && __cplusplus >= 201103L
163 if constexpr (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
164 {
165 std::align_val_t __al = std::align_val_t(alignof(_Tp));
166 return static_cast<_Tp*>(
167 _GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp), __al));
168 }
169 else
170#endif
171 return static_cast<_Tp*>(
172 _GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));
173 }
174 }
175
176#ifdef __glibcxx_allocate_at_least // C++23
177 [[nodiscard]] constexpr std::allocation_result<_Tp*, size_t>
178 allocate_at_least(size_t __n)
179 {
180 if ! consteval
181 {
182 if constexpr (requires { sizeof(_Tp); })
183 if constexpr (alignof(_Tp) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
184 if constexpr ( sizeof(_Tp) < __STDCPP_DEFAULT_NEW_ALIGNMENT__)
185 {
186 _S_check_allocation_limit(__n);
187 const size_t __need = __n * sizeof(_Tp);
188 const size_t __mask = __STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1;
189 size_t __ask = (__need + __mask) & ~__mask;
190 // Avoid rounding up to and asking for 2^63 bytes (PR108377):
191 __ask -= __ask >> (__SIZE_WIDTH__ - 1);
192 auto* __p = static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__ask));
193 using _U8 = const unsigned char;
194 static_assert(sizeof(_Tp) <= ~_U8());
195 // Use 8-bit division for minimal latency:
196 _U8 __spare = __ask - __need, __size = sizeof(_Tp);
197 return { __p , __n + __spare / __size };
198 }
199 }
200 return { allocate(__n), __n };
201 }
202#endif
203
204 // __p is not permitted to be a null pointer.
205 _GLIBCXX20_CONSTEXPR void
206 deallocate(_Tp* __p, size_type __n __attribute__ ((__unused__)))
207 {
208#if __cpp_sized_deallocation
209# define _GLIBCXX_SIZED_DEALLOC(p, n) (p), (n) * sizeof(_Tp)
210#else
211# define _GLIBCXX_SIZED_DEALLOC(p, n) (p)
212#endif
213
214#if __cpp_aligned_new && __cplusplus >= 201103L
215 if constexpr (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
216 {
217 _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n),
218 std::align_val_t(alignof(_Tp)));
219 }
220 else
221#endif
222 _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n));
223 }
224
225#pragma GCC diagnostic pop
226#undef _GLIBCXX_SIZED_DEALLOC
227#undef _GLIBCXX_OPERATOR_DELETE
228#undef _GLIBCXX_OPERATOR_NEW
229
230#if __cplusplus <= 201703L
231 __attribute__((__always_inline__))
232 size_type
233 max_size() const _GLIBCXX_USE_NOEXCEPT
234 { return _S_max_size(); }
235
236#if __cplusplus >= 201103L
237 template<typename _Up, typename... _Args>
238 __attribute__((__always_inline__))
239 void
240 construct(_Up* __p, _Args&&... __args)
241 noexcept(__is_nothrow_new_constructible<_Up, _Args...>)
242 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
243
244 template<typename _Up>
245 __attribute__((__always_inline__))
246 void
247 destroy(_Up* __p)
249 { __p->~_Up(); }
250#else
251 // _GLIBCXX_RESOLVE_LIB_DEFECTS
252 // 402. wrong new expression in [some_] allocator::construct
253 __attribute__((__always_inline__))
254 void
255 construct(pointer __p, const _Tp& __val)
256 { ::new((void *)__p) _Tp(__val); }
257
258 __attribute__((__always_inline__))
259 void
260 destroy(pointer __p) { __p->~_Tp(); }
261#endif
262#endif // ! C++20
263
264 template<typename _Up>
265 friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR bool
266 operator==(const __new_allocator&, const __new_allocator<_Up>&)
267 _GLIBCXX_NOTHROW
268 { return true; }
269
270#if __cpp_impl_three_way_comparison < 201907L
271 template<typename _Up>
272 friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR bool
273 operator!=(const __new_allocator&, const __new_allocator<_Up>&)
274 _GLIBCXX_NOTHROW
275 { return false; }
276#endif
277
278 private:
279 __attribute__((__always_inline__))
280 _GLIBCXX_CONSTEXPR static size_type
281 _S_max_size() _GLIBCXX_USE_NOEXCEPT
282 {
283#if __PTRDIFF_MAX__ < __SIZE_MAX__
284 return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
285#else
286 return std::size_t(-1) / sizeof(_Tp);
287#endif
288 }
289 };
290
291_GLIBCXX_END_NAMESPACE_VERSION
292} // namespace
293
294#endif
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:119
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
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.
is_nothrow_destructible
Definition type_traits:1198