libstdc++
new
Go to the documentation of this file.
1// The -*- C++ -*- dynamic memory management header.
2
3// Copyright (C) 1994-2025 Free Software Foundation, Inc.
4
5// This file is part of GCC.
6//
7// GCC is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 3, or (at your option)
10// any later version.
11//
12// GCC is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
20
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
25
26/** @file new
27 * This is a Standard C++ Library header.
28 *
29 * The header @c new defines several functions to manage dynamic memory and
30 * handling memory allocation errors; see
31 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/dynamic_memory.html
32 * for more.
33 */
34
35#ifndef _NEW
36#define _NEW
37
38#ifdef _GLIBCXX_SYSHDR
39#pragma GCC system_header
40#endif
41
42#include <bits/c++config.h>
43#include <bits/exception.h>
44
45#define __glibcxx_want_launder
46#define __glibcxx_want_hardware_interference_size
47#define __glibcxx_want_destroying_delete
48#define __glibcxx_want_constexpr_new
49#include <bits/version.h>
50
51#pragma GCC diagnostic push
52#pragma GCC diagnostic ignored "-Wc++11-extensions" // scoped enum
53
54#pragma GCC visibility push(default)
55
56extern "C++" {
57
58namespace std
59{
60 /**
61 * @brief Exception possibly thrown by @c new.
62 * @ingroup exceptions
63 *
64 * @c bad_alloc (or classes derived from it) is used to report allocation
65 * errors from the throwing forms of @c new. */
66 class bad_alloc : public exception
67 {
68 public:
69 _GLIBCXX26_CONSTEXPR bad_alloc() throw() { }
70
71#if __cplusplus >= 201103L
72 _GLIBCXX26_CONSTEXPR bad_alloc(const bad_alloc&) = default;
73 _GLIBCXX26_CONSTEXPR bad_alloc& operator=(const bad_alloc&) = default;
74#endif
75
76#if __cplusplus >= 202400L
77 constexpr virtual ~bad_alloc() noexcept {}
78
79 constexpr virtual const char* what() const noexcept
80 {
81 return "std::bad_alloc";
82 }
83#else
84 // This declaration is not useless:
85 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
86 virtual ~bad_alloc() throw();
87
88 // See comment in eh_exception.cc.
89 virtual const char* what() const throw();
90#endif
91 };
92
93#if __cplusplus >= 201103L
94 class bad_array_new_length : public bad_alloc
95 {
96 public:
97 _GLIBCXX26_CONSTEXPR bad_array_new_length() throw() { }
98
99#if __cplusplus >= 202400L
100 constexpr virtual ~bad_array_new_length() noexcept {}
101
102 constexpr virtual const char* what() const noexcept
103 {
104 return "std::bad_array_new_length";
105 }
106#else
107 // This declaration is not useless:
108 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
109 virtual ~bad_array_new_length() throw();
110
111 // See comment in eh_exception.cc.
112 virtual const char* what() const throw();
113#endif
114 };
115#endif
116
117#if __cpp_aligned_new
118 enum class align_val_t: size_t {};
119#endif
120
121 struct nothrow_t
122 {
123#if __cplusplus >= 201103L
124 explicit nothrow_t() = default;
125#endif
126 };
127
128 extern const nothrow_t nothrow;
129
130 /** If you write your own error handler to be called by @c new, it must
131 * be of this type. */
132 typedef void (*new_handler)();
133
134 /// Takes a replacement handler as the argument, returns the
135 /// previous handler.
136 new_handler set_new_handler(new_handler) throw();
137
138#if __cplusplus >= 201103L
139 /// Return the current new handler.
140 new_handler get_new_handler() noexcept;
141#endif
142} // namespace std
143
144//@{
145/** These are replaceable signatures:
146 * - normal single new and delete (no arguments, throw @c bad_alloc on error)
147 * - normal array new and delete (same)
148 * - @c nothrow single new and delete (take a @c nothrow argument, return
149 * @c NULL on error)
150 * - @c nothrow array new and delete (same)
151 *
152 * Placement new and delete signatures (take a memory address argument,
153 * does nothing) may not be replaced by a user's program.
154*/
155_GLIBCXX_NODISCARD void* operator new(std::size_t)
156 _GLIBCXX_TXN_SAFE _GLIBCXX_THROW (std::bad_alloc)
157 __attribute__((__externally_visible__, __malloc__));
158_GLIBCXX_NODISCARD void* operator new[](std::size_t)
159 _GLIBCXX_TXN_SAFE _GLIBCXX_THROW (std::bad_alloc)
160 __attribute__((__externally_visible__, __malloc__));
161void operator delete(void*) _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
162 __attribute__((__externally_visible__));
163void operator delete[](void*) _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
164 __attribute__((__externally_visible__));
165#if __cpp_sized_deallocation
166void operator delete(void*, std::size_t)
167 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
168 __attribute__((__externally_visible__));
169void operator delete[](void*, std::size_t)
170 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
171 __attribute__((__externally_visible__));
172#endif
173_GLIBCXX_NODISCARD void* operator new(std::size_t, const std::nothrow_t&)
174 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
175 __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
176_GLIBCXX_NODISCARD void* operator new[](std::size_t, const std::nothrow_t&)
177 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
178 __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
179void operator delete(void*, const std::nothrow_t&)
180 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
181 __attribute__((__externally_visible__));
182void operator delete[](void*, const std::nothrow_t&)
183 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
184 __attribute__((__externally_visible__));
185#if __cpp_aligned_new
186_GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t)
187 _GLIBCXX_TXN_SAFE
188 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
189_GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&)
190 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
191 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
192void operator delete(void*, std::align_val_t) _GLIBCXX_TXN_SAFE
193 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
194void operator delete(void*, std::align_val_t, const std::nothrow_t&)
195 _GLIBCXX_TXN_SAFE
196 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
197_GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t)
198 _GLIBCXX_TXN_SAFE
199 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
200_GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&)
201 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
202 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
203void operator delete[](void*, std::align_val_t) _GLIBCXX_TXN_SAFE
204 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
205void operator delete[](void*, std::align_val_t, const std::nothrow_t&)
206 _GLIBCXX_TXN_SAFE
207 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
208#if __cpp_sized_deallocation
209void operator delete(void*, std::size_t, std::align_val_t) _GLIBCXX_TXN_SAFE
210 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
211void operator delete[](void*, std::size_t, std::align_val_t) _GLIBCXX_TXN_SAFE
212 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
213#endif // __cpp_sized_deallocation
214#endif // __cpp_aligned_new
215
216#if __cpp_lib_constexpr_new >= 202406L
217# define _GLIBCXX_PLACEMENT_CONSTEXPR constexpr
218#else
219# define _GLIBCXX_PLACEMENT_CONSTEXPR inline
220#endif
221
222// Default placement versions of operator new.
223_GLIBCXX_NODISCARD _GLIBCXX_PLACEMENT_CONSTEXPR
224void* operator new(std::size_t, void* __p)
225 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
226{ return __p; }
227_GLIBCXX_NODISCARD _GLIBCXX_PLACEMENT_CONSTEXPR
228void* operator new[](std::size_t, void* __p)
229 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
230{ return __p; }
231
232#undef _GLIBCXX_PLACEMENT_CONSTEXPR
233
234// Default placement versions of operator delete.
235inline void operator delete (void*, void*)
236 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
237{ }
238inline void operator delete[](void*, void*)
239 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
240{ }
241//@}
242} // extern "C++"
243
244namespace std
245{
246#ifdef __cpp_lib_launder // C++ >= 17 && HAVE_BUILTIN_LAUNDER
247 /// Pointer optimization barrier [ptr.launder]
248 template<typename _Tp>
249 [[nodiscard]] constexpr _Tp*
250 launder(_Tp* __p) noexcept
251 {
252 if constexpr (__is_same(const volatile _Tp, const volatile void))
253 static_assert(!__is_same(const volatile _Tp, const volatile void),
254 "std::launder argument must not be a void pointer");
255#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function)
256 else if constexpr (__is_function(_Tp))
257 static_assert(!__is_function(_Tp),
258 "std::launder argument must not be a function pointer");
259#endif
260 else
261 return __builtin_launder(__p);
262 return nullptr;
263 }
264#endif // __cpp_lib_launder
265
266#ifdef __cpp_lib_hardware_interference_size // C++ >= 17 && defined(gcc_dest_sz)
267 inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
268 inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
269#endif // __cpp_lib_hardware_interference_size
270
271// Emitted despite the FTM potentially being undefined.
272#if __cplusplus >= 202002L
273 /// Tag type used to declare a class-specific operator delete that can
274 /// invoke the destructor before deallocating the memory.
275 struct destroying_delete_t
276 {
277 explicit destroying_delete_t() = default;
278 };
279 /// Tag variable of type destroying_delete_t.
280 inline constexpr destroying_delete_t destroying_delete{};
281#endif // C++20
282}
283
284#pragma GCC visibility pop
285#pragma GCC diagnostic pop
286
287#endif