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