1 // The -*- C++ -*- dynamic memory management header.
3 // Copyright (C) 1994-2025 Free Software Foundation, Inc.
5 // This file is part of GCC.
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)
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.
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.
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/>.
27 * This is a Standard C++ Library header.
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
38 #ifdef _GLIBCXX_SYSHDR
39 #pragma GCC system_header
42 #include <bits/c++config.h>
43 #include <bits/exception.h>
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>
51 #pragma GCC diagnostic push
52 #pragma GCC diagnostic ignored "-Wc++11-extensions" // scoped enum
54 #pragma GCC visibility push(default)
61 * @brief Exception possibly thrown by @c new.
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
69 bad_alloc() throw() { }
71 #if __cplusplus >= 201103L
72 bad_alloc(const bad_alloc&) = default;
73 bad_alloc& operator=(const bad_alloc&) = default;
76 // This declaration is not useless:
77 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
78 virtual ~bad_alloc() throw();
80 // See comment in eh_exception.cc.
81 virtual const char* what() const throw();
84 #if __cplusplus >= 201103L
85 class bad_array_new_length : public bad_alloc
88 bad_array_new_length() throw() { }
90 // This declaration is not useless:
91 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
92 virtual ~bad_array_new_length() throw();
94 // See comment in eh_exception.cc.
95 virtual const char* what() const throw();
100 enum class align_val_t: size_t {};
105 #if __cplusplus >= 201103L
106 explicit nothrow_t() = default;
110 extern const nothrow_t nothrow;
112 /** If you write your own error handler to be called by @c new, it must
113 * be of this type. */
114 typedef void (*new_handler)();
116 /// Takes a replacement handler as the argument, returns the
117 /// previous handler.
118 new_handler set_new_handler(new_handler) throw();
120 #if __cplusplus >= 201103L
121 /// Return the current new handler.
122 new_handler get_new_handler() noexcept;
127 /** These are replaceable signatures:
128 * - normal single new and delete (no arguments, throw @c bad_alloc on error)
129 * - normal array new and delete (same)
130 * - @c nothrow single new and delete (take a @c nothrow argument, return
132 * - @c nothrow array new and delete (same)
134 * Placement new and delete signatures (take a memory address argument,
135 * does nothing) may not be replaced by a user's program.
137 _GLIBCXX_NODISCARD void* operator new(std::size_t)
138 _GLIBCXX_TXN_SAFE _GLIBCXX_THROW (std::bad_alloc)
139 __attribute__((__externally_visible__, __malloc__));
140 _GLIBCXX_NODISCARD void* operator new[](std::size_t)
141 _GLIBCXX_TXN_SAFE _GLIBCXX_THROW (std::bad_alloc)
142 __attribute__((__externally_visible__, __malloc__));
143 void operator delete(void*) _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
144 __attribute__((__externally_visible__));
145 void operator delete[](void*) _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
146 __attribute__((__externally_visible__));
147 #if __cpp_sized_deallocation
148 void operator delete(void*, std::size_t)
149 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
150 __attribute__((__externally_visible__));
151 void operator delete[](void*, std::size_t)
152 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
153 __attribute__((__externally_visible__));
155 _GLIBCXX_NODISCARD void* operator new(std::size_t, const std::nothrow_t&)
156 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
157 __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
158 _GLIBCXX_NODISCARD void* operator new[](std::size_t, const std::nothrow_t&)
159 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
160 __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
161 void operator delete(void*, const std::nothrow_t&)
162 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
163 __attribute__((__externally_visible__));
164 void operator delete[](void*, const std::nothrow_t&)
165 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
166 __attribute__((__externally_visible__));
167 #if __cpp_aligned_new
168 _GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t)
170 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
171 _GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&)
172 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
173 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
174 void operator delete(void*, std::align_val_t) _GLIBCXX_TXN_SAFE
175 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
176 void operator delete(void*, std::align_val_t, const std::nothrow_t&)
178 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
179 _GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t)
181 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
182 _GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&)
183 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
184 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
185 void operator delete[](void*, std::align_val_t) _GLIBCXX_TXN_SAFE
186 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
187 void operator delete[](void*, std::align_val_t, const std::nothrow_t&)
189 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
190 #if __cpp_sized_deallocation
191 void operator delete(void*, std::size_t, std::align_val_t) _GLIBCXX_TXN_SAFE
192 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
193 void operator delete[](void*, std::size_t, std::align_val_t) _GLIBCXX_TXN_SAFE
194 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
195 #endif // __cpp_sized_deallocation
196 #endif // __cpp_aligned_new
198 #if __cpp_lib_constexpr_new >= 202406L
199 # define _GLIBCXX_PLACEMENT_CONSTEXPR constexpr
201 # define _GLIBCXX_PLACEMENT_CONSTEXPR inline
204 // Default placement versions of operator new.
205 _GLIBCXX_NODISCARD _GLIBCXX_PLACEMENT_CONSTEXPR
206 void* operator new(std::size_t, void* __p)
207 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
209 _GLIBCXX_NODISCARD _GLIBCXX_PLACEMENT_CONSTEXPR
210 void* operator new[](std::size_t, void* __p)
211 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
214 #undef _GLIBCXX_PLACEMENT_CONSTEXPR
216 // Default placement versions of operator delete.
217 inline void operator delete (void*, void*)
218 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
220 inline void operator delete[](void*, void*)
221 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
228 #ifdef __cpp_lib_launder // C++ >= 17 && HAVE_BUILTIN_LAUNDER
229 /// Pointer optimization barrier [ptr.launder]
230 template<typename _Tp>
231 [[nodiscard]] constexpr _Tp*
232 launder(_Tp* __p) noexcept
234 if constexpr (__is_same(const volatile _Tp, const volatile void))
235 static_assert(!__is_same(const volatile _Tp, const volatile void),
236 "std::launder argument must not be a void pointer");
237 #if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function)
238 else if constexpr (__is_function(_Tp))
239 static_assert(!__is_function(_Tp),
240 "std::launder argument must not be a function pointer");
243 return __builtin_launder(__p);
246 #endif // __cpp_lib_launder
248 #ifdef __cpp_lib_hardware_interference_size // C++ >= 17 && defined(gcc_dest_sz)
249 inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
250 inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
251 #endif // __cpp_lib_hardware_interference_size
253 // Emitted despite the FTM potentially being undefined.
254 #if __cplusplus >= 202002L
255 /// Tag type used to declare a class-specific operator delete that can
256 /// invoke the destructor before deallocating the memory.
257 struct destroying_delete_t
259 explicit destroying_delete_t() = default;
261 /// Tag variable of type destroying_delete_t.
262 inline constexpr destroying_delete_t destroying_delete{};
266 #pragma GCC visibility pop
267 #pragma GCC diagnostic pop