1// The -*- C++ -*- dynamic memory management header.
3// Copyright (C) 1994-2022 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#pragma GCC system_header
40#include <bits/c++config.h>
41#include <bits/exception.h>
43#pragma GCC visibility push(default)
50 * @brief Exception possibly thrown by @c new.
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
58 bad_alloc() throw() { }
60#if __cplusplus >= 201103L
61 bad_alloc(const bad_alloc&) = default;
62 bad_alloc& operator=(const bad_alloc&) = default;
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();
69 // See comment in eh_exception.cc.
70 virtual const char* what() const throw();
73#if __cplusplus >= 201103L
74 class bad_array_new_length : public bad_alloc
77 bad_array_new_length() throw() { }
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();
83 // See comment in eh_exception.cc.
84 virtual const char* what() const throw();
89 enum class align_val_t: size_t {};
94#if __cplusplus >= 201103L
95 explicit nothrow_t() = default;
99 extern const nothrow_t nothrow;
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)();
105 /// Takes a replacement handler as the argument, returns the
106 /// previous handler.
107 new_handler set_new_handler(new_handler) throw();
109#if __cplusplus >= 201103L
110 /// Return the current new handler.
111 new_handler get_new_handler() noexcept;
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
121 * - @c nothrow array new and delete (same)
123 * Placement new and delete signatures (take a memory address argument,
124 * does nothing) may not be replaced by a user's program.
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__));
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__));
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
173// Default placement versions of operator new.
174_GLIBCXX_NODISCARD inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
176_GLIBCXX_NODISCARD inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
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 { }
185#if __cplusplus >= 201703L
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); }
196 // The program is ill-formed if T is a function type or
197 // (possibly cv-qualified) void.
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;
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
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
218#if __cplusplus > 201703L
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
225 explicit destroying_delete_t() = default;
227 /// Tag variable of type destroying_delete_t.
228 inline constexpr destroying_delete_t destroying_delete{};
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
236#pragma GCC visibility pop