1 // The -*- C++ -*- dynamic memory management header.
3 // Copyright (C) 1994-2026 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>
50 #include <bits/new_except.h> // std::bad_alloc, std::bad_array_new_length
52 #pragma GCC diagnostic push
53 #pragma GCC diagnostic ignored "-Wc++11-extensions" // scoped enum
55 #pragma GCC visibility push(default)
62 enum class align_val_t: size_t {};
67 #if __cplusplus >= 201103L
68 explicit nothrow_t() = default;
72 extern const nothrow_t nothrow;
74 /** If you write your own error handler to be called by @c new, it must
76 typedef void (*new_handler)();
78 /// Takes a replacement handler as the argument, returns the
80 new_handler set_new_handler(new_handler) throw();
82 #if __cplusplus >= 201103L
83 /// Return the current new handler.
84 new_handler get_new_handler() noexcept;
89 /** These are replaceable signatures:
90 * - normal single new and delete (no arguments, throw @c bad_alloc on error)
91 * - normal array new and delete (same)
92 * - @c nothrow single new and delete (take a @c nothrow argument, return
94 * - @c nothrow array new and delete (same)
96 * Placement new and delete signatures (take a memory address argument,
97 * does nothing) may not be replaced by a user's program.
99 _GLIBCXX_NODISCARD void* operator new(std::size_t)
100 _GLIBCXX_TXN_SAFE _GLIBCXX_THROW (std::bad_alloc)
101 __attribute__((__externally_visible__, __malloc__));
102 _GLIBCXX_NODISCARD void* operator new[](std::size_t)
103 _GLIBCXX_TXN_SAFE _GLIBCXX_THROW (std::bad_alloc)
104 __attribute__((__externally_visible__, __malloc__));
105 void operator delete(void*) _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
106 __attribute__((__externally_visible__));
107 void operator delete[](void*) _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
108 __attribute__((__externally_visible__));
109 #if __cpp_sized_deallocation
110 void operator delete(void*, std::size_t)
111 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
112 __attribute__((__externally_visible__));
113 void operator delete[](void*, std::size_t)
114 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
115 __attribute__((__externally_visible__));
117 _GLIBCXX_NODISCARD void* operator new(std::size_t, const std::nothrow_t&)
118 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
119 __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
120 _GLIBCXX_NODISCARD void* operator new[](std::size_t, const std::nothrow_t&)
121 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
122 __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
123 void operator delete(void*, const std::nothrow_t&)
124 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
125 __attribute__((__externally_visible__));
126 void operator delete[](void*, const std::nothrow_t&)
127 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
128 __attribute__((__externally_visible__));
129 #if __cpp_aligned_new
130 _GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t)
132 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
133 _GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&)
134 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
135 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
136 void operator delete(void*, std::align_val_t) _GLIBCXX_TXN_SAFE
137 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
138 void operator delete(void*, std::align_val_t, const std::nothrow_t&)
140 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
141 _GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t)
143 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
144 _GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&)
145 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
146 __attribute__((__externally_visible__, __alloc_size__ (1), __alloc_align__ (2), __malloc__));
147 void operator delete[](void*, std::align_val_t) _GLIBCXX_TXN_SAFE
148 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
149 void operator delete[](void*, std::align_val_t, const std::nothrow_t&)
151 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
152 #if __cpp_sized_deallocation
153 void operator delete(void*, std::size_t, std::align_val_t) _GLIBCXX_TXN_SAFE
154 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
155 void operator delete[](void*, std::size_t, std::align_val_t) _GLIBCXX_TXN_SAFE
156 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
157 #endif // __cpp_sized_deallocation
158 #endif // __cpp_aligned_new
160 #if __cpp_lib_constexpr_new >= 202406L
161 # define _GLIBCXX_PLACEMENT_CONSTEXPR constexpr
163 # define _GLIBCXX_PLACEMENT_CONSTEXPR inline
166 // Default placement versions of operator new.
167 _GLIBCXX_NODISCARD _GLIBCXX_PLACEMENT_CONSTEXPR
168 void* operator new(std::size_t, void* __p)
169 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
171 _GLIBCXX_NODISCARD _GLIBCXX_PLACEMENT_CONSTEXPR
172 void* operator new[](std::size_t, void* __p)
173 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
176 // Default placement versions of operator delete.
177 _GLIBCXX_PLACEMENT_CONSTEXPR void operator delete (void*, void*)
178 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
180 _GLIBCXX_PLACEMENT_CONSTEXPR void operator delete[](void*, void*)
181 _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
184 #undef _GLIBCXX_PLACEMENT_CONSTEXPR
191 #ifdef __cpp_lib_launder // C++ >= 17 && HAVE_BUILTIN_LAUNDER
192 /// Pointer optimization barrier [ptr.launder]
193 template<typename _Tp>
194 [[nodiscard]] constexpr _Tp*
195 launder(_Tp* __p) noexcept
197 if constexpr (__is_same(const volatile _Tp, const volatile void))
198 static_assert(!__is_same(const volatile _Tp, const volatile void),
199 "std::launder argument must not be a void pointer");
200 #if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function)
201 else if constexpr (__is_function(_Tp))
202 static_assert(!__is_function(_Tp),
203 "std::launder argument must not be a function pointer");
206 return __builtin_launder(__p);
209 #endif // __cpp_lib_launder
211 #ifdef __cpp_lib_hardware_interference_size // C++ >= 17 && defined(gcc_dest_sz)
212 inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
213 inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
214 #endif // __cpp_lib_hardware_interference_size
216 // Emitted despite the FTM potentially being undefined.
217 #if __cplusplus >= 202002L
218 /// Tag type used to declare a class-specific operator delete that can
219 /// invoke the destructor before deallocating the memory.
220 struct destroying_delete_t
222 explicit destroying_delete_t() = default;
224 /// Tag variable of type destroying_delete_t.
225 inline constexpr destroying_delete_t destroying_delete{};
229 #pragma GCC visibility pop
230 #pragma GCC diagnostic pop