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 
56 extern "C++" {
57 
58 namespace 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  bad_alloc() throw() { }
70 
71 #if __cplusplus >= 201103L
72  bad_alloc(const bad_alloc&) = default;
73  bad_alloc& operator=(const bad_alloc&) = default;
74 #endif
75 
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();
79 
80  // See comment in eh_exception.cc.
81  virtual const char* what() const throw();
82  };
83 
84 #if __cplusplus >= 201103L
85  class bad_array_new_length : public bad_alloc
86  {
87  public:
88  bad_array_new_length() throw() { }
89 
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();
93 
94  // See comment in eh_exception.cc.
95  virtual const char* what() const throw();
96  };
97 #endif
98 
99 #if __cpp_aligned_new
100  enum class align_val_t: size_t {};
101 #endif
102 
103  struct nothrow_t
104  {
105 #if __cplusplus >= 201103L
106  explicit nothrow_t() = default;
107 #endif
108  };
109 
110  extern const nothrow_t nothrow;
111 
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)();
115 
116  /// Takes a replacement handler as the argument, returns the
117  /// previous handler.
118  new_handler set_new_handler(new_handler) throw();
119 
120 #if __cplusplus >= 201103L
121  /// Return the current new handler.
122  new_handler get_new_handler() noexcept;
123 #endif
124 } // namespace std
125 
126 //@{
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
131  * @c NULL on error)
132  * - @c nothrow array new and delete (same)
133  *
134  * Placement new and delete signatures (take a memory address argument,
135  * does nothing) may not be replaced by a user's program.
136 */
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__));
154 #endif
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)
169  _GLIBCXX_TXN_SAFE
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&)
177  _GLIBCXX_TXN_SAFE
178  _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
179 _GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t)
180  _GLIBCXX_TXN_SAFE
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&)
188  _GLIBCXX_TXN_SAFE
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
197 
198 #if __cpp_lib_constexpr_new >= 202406L
199 # define _GLIBCXX_PLACEMENT_CONSTEXPR constexpr
200 #else
201 # define _GLIBCXX_PLACEMENT_CONSTEXPR inline
202 #endif
203 
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
208 { return __p; }
209 _GLIBCXX_NODISCARD _GLIBCXX_PLACEMENT_CONSTEXPR
210 void* operator new[](std::size_t, void* __p)
211  _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
212 { return __p; }
213 
214 #undef _GLIBCXX_PLACEMENT_CONSTEXPR
215 
216 // Default placement versions of operator delete.
217 inline void operator delete (void*, void*)
218  _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
219 { }
220 inline void operator delete[](void*, void*)
221  _GLIBCXX_TXN_SAFE _GLIBCXX_USE_NOEXCEPT
222 { }
223 //@}
224 } // extern "C++"
225 
226 namespace std
227 {
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
233  {
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");
241 #endif
242  else
243  return __builtin_launder(__p);
244  return nullptr;
245  }
246 #endif // __cpp_lib_launder
247 
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
252 
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
258  {
259  explicit destroying_delete_t() = default;
260  };
261  /// Tag variable of type destroying_delete_t.
262  inline constexpr destroying_delete_t destroying_delete{};
263 #endif // C++20
264 }
265 
266 #pragma GCC visibility pop
267 #pragma GCC diagnostic pop
268 
269 #endif