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 __cpp_aligned_new
181 if ! consteval
182 {
183 if constexpr (requires { sizeof(_Tp); })
184 if constexpr (alignof(_Tp) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
185 if constexpr ( sizeof(_Tp) < __STDCPP_DEFAULT_NEW_ALIGNMENT__)
186 {
187 _S_check_allocation_limit(__n);
188 size_t __bytes = __n * sizeof(_Tp);
189 const size_t __mask = __STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1;
190 size_t __max = (__bytes + __mask) & ~__mask;
191 // Avoid seeming to ask for 2^63 bytes (PR108377):
192 __max -= __max >> (__SIZE_WIDTH__ - 1);
193 auto __spare = static_cast<unsigned>(__max - __bytes);
194 if constexpr (sizeof(_Tp) < (__mask + 1) / 2)
195 {
196 auto __bonus = __spare / sizeof(_Tp);
197 __n += __bonus;
198 __bytes += __bonus * sizeof(_Tp);
199 }
200 else if (sizeof(_Tp) <= __spare)
201 {
202 __n += 1;
203 __bytes += sizeof(_Tp);
204 }
205 void* __p = _GLIBCXX_OPERATOR_NEW(__bytes);
206 return { static_cast<_Tp*>(__p), __n };
207 }
208 }
209#endif
210 return { allocate(__n), __n };
211 }
212#endif
213
214 // __p is not permitted to be a null pointer.
215 _GLIBCXX20_CONSTEXPR void
216 deallocate(_Tp* __p, size_type __n __attribute__ ((__unused__)))
217 {
218#if __cpp_sized_deallocation
219# define _GLIBCXX_SIZED_DEALLOC(p, n) (p), (n) * sizeof(_Tp)
220#else
221# define _GLIBCXX_SIZED_DEALLOC(p, n) (p)
222#endif
223
224#if __cpp_aligned_new && __cplusplus >= 201103L
225 if constexpr (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
226 {
227 _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n),
228 std::align_val_t(alignof(_Tp)));
229 }
230 else
231#endif
232 _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n));
233 }
234
235#pragma GCC diagnostic pop
236#undef _GLIBCXX_SIZED_DEALLOC
237#undef _GLIBCXX_OPERATOR_DELETE
238#undef _GLIBCXX_OPERATOR_NEW
239
240#if __cplusplus <= 201703L
241 __attribute__((__always_inline__))
242 size_type
243 max_size() const _GLIBCXX_USE_NOEXCEPT
244 { return _S_max_size(); }
245
246#if __cplusplus >= 201103L
247 template<typename _Up, typename... _Args>
248 __attribute__((__always_inline__))
249 void
250 construct(_Up* __p, _Args&&... __args)
251 noexcept(__is_nothrow_new_constructible<_Up, _Args...>)
252 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
253
254 template<typename _Up>
255 __attribute__((__always_inline__))
256 void
257 destroy(_Up* __p)
259 { __p->~_Up(); }
260#else
261 // _GLIBCXX_RESOLVE_LIB_DEFECTS
262 // 402. wrong new expression in [some_] allocator::construct
263 __attribute__((__always_inline__))
264 void
265 construct(pointer __p, const _Tp& __val)
266 { ::new((void *)__p) _Tp(__val); }
267
268 __attribute__((__always_inline__))
269 void
270 destroy(pointer __p) { __p->~_Tp(); }
271#endif
272#endif // ! C++20
273
274 template<typename _Up>
275 friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR bool
276 operator==(const __new_allocator&, const __new_allocator<_Up>&)
277 _GLIBCXX_NOTHROW
278 { return true; }
279
280#if __cpp_impl_three_way_comparison < 201907L
281 template<typename _Up>
282 friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR bool
283 operator!=(const __new_allocator&, const __new_allocator<_Up>&)
284 _GLIBCXX_NOTHROW
285 { return false; }
286#endif
287
288 private:
289 __attribute__((__always_inline__))
290 _GLIBCXX_CONSTEXPR static size_type
291 _S_max_size() _GLIBCXX_USE_NOEXCEPT
292 {
293#if __PTRDIFF_MAX__ < __SIZE_MAX__
294 return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
295#else
296 return std::size_t(-1) / sizeof(_Tp);
297#endif
298 }
299 };
300
301_GLIBCXX_END_NAMESPACE_VERSION
302} // namespace
303
304#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