libstdc++
new
Go to the documentation of this file.
1// The -*- C++ -*- dynamic memory management header.
2
3// Copyright (C) 1994-2024 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#pragma GCC system_header
39
40#include <bits/c++config.h>
41#include <bits/exception.h>
42
43#define __glibcxx_want_launder
44#define __glibcxx_want_hardware_interference_size
45#define __glibcxx_want_destroying_delete
46#define __glibcxx_want_constexpr_new
47#include <bits/version.h>
48
49#pragma GCC visibility push(default)
50
51extern "C++" {
52
53namespace std
54{
55 /**
56 * @brief Exception possibly thrown by @c new.
57 * @ingroup exceptions
58 *
59 * @c bad_alloc (or classes derived from it) is used to report allocation
60 * errors from the throwing forms of @c new. */
61 class bad_alloc : public exception
62 {
63 public:
64 bad_alloc() throw() { }
65
66#if __cplusplus >= 201103L
67 bad_alloc(const bad_alloc&) = default;
68 bad_alloc& operator=(const bad_alloc&) = default;
69#endif
70
71 // This declaration is not useless:
72 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
73 virtual ~bad_alloc() throw();
74
75 // See comment in eh_exception.cc.
76 virtual const char* what() const throw();
77 };
78
79#if __cplusplus >= 201103L
80 class bad_array_new_length : public bad_alloc
81 {
82 public:
83 bad_array_new_length() throw() { }
84
85 // This declaration is not useless:
86 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
87 virtual ~bad_array_new_length() throw();
88
89 // See comment in eh_exception.cc.
90 virtual const char* what() const throw();
91 };
92#endif
93
94#if __cpp_aligned_new
95 enum class align_val_t: size_t {};
96#endif
97
98 struct nothrow_t
99 {
100#if __cplusplus >= 201103L
101 explicit nothrow_t() = default;
102#endif
103 };
104
105 extern const nothrow_t nothrow;
106
107 /** If you write your own error handler to be called by @c new, it must
108 * be of this type. */
109 typedef void (*new_handler)();
110
111 /// Takes a replacement handler as the argument, returns the
112 /// previous handler.
114
115#if __cplusplus >= 201103L
116 /// Return the current new handler.
118#endif
119} // namespace std
120
121//@{
122/** These are replaceable signatures:
123 * - normal single new and delete (no arguments, throw @c bad_alloc on error)
124 * - normal array new and delete (same)
125 * - @c nothrow single new and delete (take a @c nothrow argument, return
126 * @c NULL on error)
127 * - @c nothrow array new and delete (same)
128 *
129 * Placement new and delete signatures (take a memory address argument,
130 * does nothing) may not be replaced by a user's program.
131*/
132_GLIBCXX_NODISCARD void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc)
133 __attribute__((__externally_visible__));
134_GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
135 __attribute__((__externally_visible__));
136void operator delete(void*) _GLIBCXX_USE_NOEXCEPT
137 __attribute__((__externally_visible__));
138void operator delete[](void*) _GLIBCXX_USE_NOEXCEPT
139 __attribute__((__externally_visible__));
140#if __cpp_sized_deallocation
141void operator delete(void*, std::size_t) _GLIBCXX_USE_NOEXCEPT
142 __attribute__((__externally_visible__));
143void operator delete[](void*, std::size_t) _GLIBCXX_USE_NOEXCEPT
144 __attribute__((__externally_visible__));
145#endif
146_GLIBCXX_NODISCARD void* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
147 __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
148_GLIBCXX_NODISCARD void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
149 __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
150void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
151 __attribute__((__externally_visible__));
152void operator delete[](void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
153 __attribute__((__externally_visible__));
154#if __cpp_aligned_new
155_GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t)
156 __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
157_GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&)
158 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
159void operator delete(void*, std::align_val_t)
160 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
161void operator delete(void*, std::align_val_t, const std::nothrow_t&)
162 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
163_GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t)
164 __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
165_GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&)
166 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__, __alloc_size__ (1), __malloc__));
167void operator delete[](void*, std::align_val_t)
168 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
169void operator delete[](void*, std::align_val_t, const std::nothrow_t&)
170 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
171#if __cpp_sized_deallocation
172void operator delete(void*, std::size_t, std::align_val_t)
173 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
174void operator delete[](void*, std::size_t, std::align_val_t)
175 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
176#endif // __cpp_sized_deallocation
177#endif // __cpp_aligned_new
178
179#if __cpp_lib_constexpr_new >= 202406L
180# define _GLIBCXX_PLACEMENT_CONSTEXPR constexpr
181#else
182# define _GLIBCXX_PLACEMENT_CONSTEXPR inline
183#endif
184
185// Default placement versions of operator new.
186_GLIBCXX_NODISCARD _GLIBCXX_PLACEMENT_CONSTEXPR
187void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
188{ return __p; }
189_GLIBCXX_NODISCARD _GLIBCXX_PLACEMENT_CONSTEXPR
190void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
191{ return __p; }
192
193#undef _GLIBCXX_PLACEMENT_CONSTEXPR
194
195// Default placement versions of operator delete.
196inline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPT { }
197inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { }
198//@}
199} // extern "C++"
200
201#if __cplusplus >= 201703L
202namespace std
203{
204#ifdef __cpp_lib_launder // C++ >= 17 && HAVE_BUILTIN_LAUNDER
205 /// Pointer optimization barrier [ptr.launder]
206 template<typename _Tp>
207 [[nodiscard]] constexpr _Tp*
208 launder(_Tp* __p) noexcept
209 { return __builtin_launder(__p); }
210
211 // The program is ill-formed if T is a function type or
212 // (possibly cv-qualified) void.
213
214 template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
215 void launder(_Ret (*)(_Args...) _GLIBCXX_NOEXCEPT_QUAL) = delete;
216 template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
217 void launder(_Ret (*)(_Args......) _GLIBCXX_NOEXCEPT_QUAL) = delete;
218
219 void launder(void*) = delete;
220 void launder(const void*) = delete;
221 void launder(volatile void*) = delete;
222 void launder(const volatile void*) = delete;
223#endif // __cpp_lib_launder
224
225#ifdef __cpp_lib_hardware_interference_size // C++ >= 17 && defined(gcc_dest_sz)
226 inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
227 inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
228#endif // __cpp_lib_hardware_interference_size
229}
230#endif // C++17
231
232// Emitted despite the FTM potentially being undefined.
233#if __cplusplus > 201703L
234namespace std
235{
236 /// Tag type used to declare a class-specific operator delete that can
237 /// invoke the destructor before deallocating the memory.
239 {
240 explicit destroying_delete_t() = default;
241 };
242 /// Tag variable of type destroying_delete_t.
244}
245#endif // C++20
246
247#pragma GCC visibility pop
248
249#endif
ISO C++ entities toplevel namespace is std.
new_handler set_new_handler(new_handler)
Takes a replacement handler as the argument, returns the previous handler.
new_handler get_new_handler() noexcept
Return the current new handler.
constexpr destroying_delete_t destroying_delete
Tag variable of type destroying_delete_t.
Definition new:243
void(* new_handler)()
Definition new:109
Exception possibly thrown by new.
Definition new:62
virtual const char * what() const
Tag type used to declare a class-specific operator delete that can invoke the destructor before deall...
Definition new:239
Base class for all library exceptions.
Definition exception.h:60