]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/libsupc++/new
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / new
CommitLineData
6305f20a 1// The -*- C++ -*- dynamic memory management header.
0c952af3 2
8d9254fc 3// Copyright (C) 1994-2020 Free Software Foundation, Inc.
6305f20a 4
cbecceb9 5// This file is part of GCC.
6305f20a 6//
cbecceb9 7// GCC is free software; you can redistribute it and/or modify
6305f20a 8// it under the terms of the GNU General Public License as published by
748086b7 9// the Free Software Foundation; either version 3, or (at your option)
6305f20a
BK
10// any later version.
11//
cbecceb9 12// GCC is distributed in the hope that it will be useful,
6305f20a
BK
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//
748086b7
JJ
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/>.
6305f20a 25
669f7a03 26/** @file new
78a53887
BK
27 * This is a Standard C++ Library header.
28 *
ffe94f83 29 * The header @c new defines several functions to manage dynamic memory and
669f7a03 30 * handling memory allocation errors; see
302b6996
JW
31 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/dynamic_memory.html
32 * for more.
669f7a03
PE
33 */
34
3d7c150e
BK
35#ifndef _NEW
36#define _NEW
6305f20a 37
584fd146
PC
38#pragma GCC system_header
39
8fc81078 40#include <bits/c++config.h>
6305f20a
BK
41#include <exception>
42
723acbd5
MM
43#pragma GCC visibility push(default)
44
6305f20a
BK
45extern "C++" {
46
0c952af3
BK
47namespace std
48{
aa2d5ba2
PE
49 /**
50 * @brief Exception possibly thrown by @c new.
5b9daa7e 51 * @ingroup exceptions
aa2d5ba2
PE
52 *
53 * @c bad_alloc (or classes derived from it) is used to report allocation
669f7a03 54 * errors from the throwing forms of @c new. */
0c952af3
BK
55 class bad_alloc : public exception
56 {
6305f20a 57 public:
f68147f7 58 bad_alloc() throw() { }
949d9ae1 59
f07c2237
JM
60#if __cplusplus >= 201103L
61 bad_alloc(const bad_alloc&) = default;
62 bad_alloc& operator=(const bad_alloc&) = default;
63#endif
64
e05cd3dd
PC
65 // This declaration is not useless:
66 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
d66ae36a 67 virtual ~bad_alloc() throw();
949d9ae1 68
c3f0f556
PC
69 // See comment in eh_exception.cc.
70 virtual const char* what() const throw();
6305f20a
BK
71 };
72
7d5e76c8
JM
73#if __cplusplus >= 201103L
74 class bad_array_new_length : public bad_alloc
75 {
76 public:
57c51668 77 bad_array_new_length() throw() { }
7d5e76c8
JM
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
af63ba4b
JM
88#if __cpp_aligned_new
89 enum class align_val_t: size_t {};
90#endif
91
269fa2a9
VV
92 struct nothrow_t
93 {
94#if __cplusplus >= 201103L
95 explicit nothrow_t() = default;
96#endif
97 };
949d9ae1 98
6305f20a 99 extern const nothrow_t nothrow;
949d9ae1 100
669f7a03
PE
101 /** If you write your own error handler to be called by @c new, it must
102 * be of this type. */
6305f20a 103 typedef void (*new_handler)();
949d9ae1
BK
104
105 /// Takes a replacement handler as the argument, returns the
106 /// previous handler.
984812cd 107 new_handler set_new_handler(new_handler) throw();
dca77a8a
JW
108
109#if __cplusplus >= 201103L
110 /// Return the current new handler.
111 new_handler get_new_handler() noexcept;
112#endif
6305f20a
BK
113} // namespace std
114
669f7a03
PE
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*/
d715f554 126_GLIBCXX_NODISCARD void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc)
578f0234 127 __attribute__((__externally_visible__));
d715f554 128_GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
578f0234
PC
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__));
34148d68
JM
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
d715f554 140_GLIBCXX_NODISCARD void* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
6eac0600 141 __attribute__((__externally_visible__, __malloc__));
d715f554 142_GLIBCXX_NODISCARD void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
6eac0600 143 __attribute__((__externally_visible__, __malloc__));
578f0234
PC
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__));
af63ba4b 148#if __cpp_aligned_new
d715f554 149_GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t)
af63ba4b 150 __attribute__((__externally_visible__));
d715f554 151_GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&)
6eac0600 152 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__, __malloc__));
af63ba4b
JM
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__));
d715f554 157_GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t)
af63ba4b 158 __attribute__((__externally_visible__));
d715f554 159_GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&)
6eac0600 160 _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__, __malloc__));
af63ba4b
JM
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
6305f20a 172
0c952af3 173// Default placement versions of operator new.
d715f554 174_GLIBCXX_NODISCARD inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
578f0234 175{ return __p; }
d715f554 176_GLIBCXX_NODISCARD inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
578f0234 177{ return __p; }
51937d2c
BK
178
179// Default placement versions of operator delete.
578f0234
PC
180inline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPT { }
181inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { }
669f7a03 182//@}
6305f20a
BK
183} // extern "C++"
184
a8541d90 185#if __cplusplus >= 201703L
afa56c17 186#ifdef _GLIBCXX_HAVE_BUILTIN_LAUNDER
1f5700e9
JW
187namespace std
188{
189#define __cpp_lib_launder 201606
190 /// Pointer optimization barrier [ptr.launder]
191 template<typename _Tp>
a8541d90 192 [[nodiscard]] constexpr _Tp*
1f5700e9
JW
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
51dc6603
JM
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;
1f5700e9
JW
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}
6a8446fa 209#endif // _GLIBCXX_HAVE_BUILTIN_LAUNDER
1f5700e9
JW
210#endif // C++17
211
329c0f89 212#if __cplusplus > 201703L
a6bb6b07
JM
213namespace std
214{
302b6996
JW
215 /// Tag type used to declare a class-specific operator delete that can
216 /// invoke the destructor before deallocating the memory.
a6bb6b07
JM
217 struct destroying_delete_t
218 {
219 explicit destroying_delete_t() = default;
220 };
302b6996 221 /// Tag variable of type destroying_delete_t.
a6bb6b07
JM
222 inline constexpr destroying_delete_t destroying_delete{};
223}
329c0f89
JW
224// Only define the feature test macro if the compiler supports the feature:
225#if __cpp_impl_destroying_delete
226# define __cpp_lib_destroying_delete 201806L
227#endif
228#endif // C++20
a6bb6b07 229
723acbd5
MM
230#pragma GCC visibility pop
231
6305f20a 232#endif