11debfc3dSmrg// The -*- C++ -*- dynamic memory management header. 21debfc3dSmrg 3*8feb0f0bSmrg// Copyright (C) 1994-2020 Free Software Foundation, Inc. 41debfc3dSmrg 51debfc3dSmrg// This file is part of GCC. 61debfc3dSmrg// 71debfc3dSmrg// GCC is free software; you can redistribute it and/or modify 81debfc3dSmrg// it under the terms of the GNU General Public License as published by 91debfc3dSmrg// the Free Software Foundation; either version 3, or (at your option) 101debfc3dSmrg// any later version. 111debfc3dSmrg// 121debfc3dSmrg// GCC is distributed in the hope that it will be useful, 131debfc3dSmrg// but WITHOUT ANY WARRANTY; without even the implied warranty of 141debfc3dSmrg// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 151debfc3dSmrg// GNU General Public License for more details. 161debfc3dSmrg// 171debfc3dSmrg// Under Section 7 of GPL version 3, you are granted additional 181debfc3dSmrg// permissions described in the GCC Runtime Library Exception, version 191debfc3dSmrg// 3.1, as published by the Free Software Foundation. 201debfc3dSmrg 211debfc3dSmrg// You should have received a copy of the GNU General Public License and 221debfc3dSmrg// a copy of the GCC Runtime Library Exception along with this program; 231debfc3dSmrg// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 241debfc3dSmrg// <http://www.gnu.org/licenses/>. 251debfc3dSmrg 261debfc3dSmrg/** @file new 271debfc3dSmrg * This is a Standard C++ Library header. 281debfc3dSmrg * 291debfc3dSmrg * The header @c new defines several functions to manage dynamic memory and 301debfc3dSmrg * handling memory allocation errors; see 31*8feb0f0bSmrg * https://gcc.gnu.org/onlinedocs/libstdc++/manual/dynamic_memory.html 32*8feb0f0bSmrg * for more. 331debfc3dSmrg */ 341debfc3dSmrg 351debfc3dSmrg#ifndef _NEW 361debfc3dSmrg#define _NEW 371debfc3dSmrg 381debfc3dSmrg#pragma GCC system_header 391debfc3dSmrg 401debfc3dSmrg#include <bits/c++config.h> 411debfc3dSmrg#include <exception> 421debfc3dSmrg 431debfc3dSmrg#pragma GCC visibility push(default) 441debfc3dSmrg 451debfc3dSmrgextern "C++" { 461debfc3dSmrg 471debfc3dSmrgnamespace std 481debfc3dSmrg{ 491debfc3dSmrg /** 501debfc3dSmrg * @brief Exception possibly thrown by @c new. 511debfc3dSmrg * @ingroup exceptions 521debfc3dSmrg * 531debfc3dSmrg * @c bad_alloc (or classes derived from it) is used to report allocation 541debfc3dSmrg * errors from the throwing forms of @c new. */ 551debfc3dSmrg class bad_alloc : public exception 561debfc3dSmrg { 571debfc3dSmrg public: 581debfc3dSmrg bad_alloc() throw() { } 591debfc3dSmrg 60c0a68be4Smrg#if __cplusplus >= 201103L 61c0a68be4Smrg bad_alloc(const bad_alloc&) = default; 62c0a68be4Smrg bad_alloc& operator=(const bad_alloc&) = default; 63c0a68be4Smrg#endif 64c0a68be4Smrg 651debfc3dSmrg // This declaration is not useless: 661debfc3dSmrg // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118 671debfc3dSmrg virtual ~bad_alloc() throw(); 681debfc3dSmrg 691debfc3dSmrg // See comment in eh_exception.cc. 701debfc3dSmrg virtual const char* what() const throw(); 711debfc3dSmrg }; 721debfc3dSmrg 731debfc3dSmrg#if __cplusplus >= 201103L 741debfc3dSmrg class bad_array_new_length : public bad_alloc 751debfc3dSmrg { 761debfc3dSmrg public: 77a2dc1f3fSmrg bad_array_new_length() throw() { } 781debfc3dSmrg 791debfc3dSmrg // This declaration is not useless: 801debfc3dSmrg // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118 811debfc3dSmrg virtual ~bad_array_new_length() throw(); 821debfc3dSmrg 831debfc3dSmrg // See comment in eh_exception.cc. 841debfc3dSmrg virtual const char* what() const throw(); 851debfc3dSmrg }; 861debfc3dSmrg#endif 871debfc3dSmrg 881debfc3dSmrg#if __cpp_aligned_new 891debfc3dSmrg enum class align_val_t: size_t {}; 901debfc3dSmrg#endif 911debfc3dSmrg 921debfc3dSmrg struct nothrow_t 931debfc3dSmrg { 941debfc3dSmrg#if __cplusplus >= 201103L 951debfc3dSmrg explicit nothrow_t() = default; 961debfc3dSmrg#endif 971debfc3dSmrg }; 981debfc3dSmrg 991debfc3dSmrg extern const nothrow_t nothrow; 1001debfc3dSmrg 1011debfc3dSmrg /** If you write your own error handler to be called by @c new, it must 1021debfc3dSmrg * be of this type. */ 1031debfc3dSmrg typedef void (*new_handler)(); 1041debfc3dSmrg 1051debfc3dSmrg /// Takes a replacement handler as the argument, returns the 1061debfc3dSmrg /// previous handler. 1071debfc3dSmrg new_handler set_new_handler(new_handler) throw(); 1081debfc3dSmrg 1091debfc3dSmrg#if __cplusplus >= 201103L 1101debfc3dSmrg /// Return the current new handler. 1111debfc3dSmrg new_handler get_new_handler() noexcept; 1121debfc3dSmrg#endif 1131debfc3dSmrg} // namespace std 1141debfc3dSmrg 1151debfc3dSmrg//@{ 1161debfc3dSmrg/** These are replaceable signatures: 1171debfc3dSmrg * - normal single new and delete (no arguments, throw @c bad_alloc on error) 1181debfc3dSmrg * - normal array new and delete (same) 1191debfc3dSmrg * - @c nothrow single new and delete (take a @c nothrow argument, return 1201debfc3dSmrg * @c NULL on error) 1211debfc3dSmrg * - @c nothrow array new and delete (same) 1221debfc3dSmrg * 1231debfc3dSmrg * Placement new and delete signatures (take a memory address argument, 1241debfc3dSmrg * does nothing) may not be replaced by a user's program. 1251debfc3dSmrg*/ 126c0a68be4Smrg_GLIBCXX_NODISCARD void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc) 1271debfc3dSmrg __attribute__((__externally_visible__)); 128c0a68be4Smrg_GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc) 1291debfc3dSmrg __attribute__((__externally_visible__)); 1301debfc3dSmrgvoid operator delete(void*) _GLIBCXX_USE_NOEXCEPT 1311debfc3dSmrg __attribute__((__externally_visible__)); 1321debfc3dSmrgvoid operator delete[](void*) _GLIBCXX_USE_NOEXCEPT 1331debfc3dSmrg __attribute__((__externally_visible__)); 1341debfc3dSmrg#if __cpp_sized_deallocation 1351debfc3dSmrgvoid operator delete(void*, std::size_t) _GLIBCXX_USE_NOEXCEPT 1361debfc3dSmrg __attribute__((__externally_visible__)); 1371debfc3dSmrgvoid operator delete[](void*, std::size_t) _GLIBCXX_USE_NOEXCEPT 1381debfc3dSmrg __attribute__((__externally_visible__)); 1391debfc3dSmrg#endif 140c0a68be4Smrg_GLIBCXX_NODISCARD void* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 141c0a68be4Smrg __attribute__((__externally_visible__, __malloc__)); 142c0a68be4Smrg_GLIBCXX_NODISCARD void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 143c0a68be4Smrg __attribute__((__externally_visible__, __malloc__)); 1441debfc3dSmrgvoid operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 1451debfc3dSmrg __attribute__((__externally_visible__)); 1461debfc3dSmrgvoid operator delete[](void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 1471debfc3dSmrg __attribute__((__externally_visible__)); 1481debfc3dSmrg#if __cpp_aligned_new 149c0a68be4Smrg_GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t) 1501debfc3dSmrg __attribute__((__externally_visible__)); 151c0a68be4Smrg_GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&) 152c0a68be4Smrg _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__, __malloc__)); 1531debfc3dSmrgvoid operator delete(void*, std::align_val_t) 1541debfc3dSmrg _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 1551debfc3dSmrgvoid operator delete(void*, std::align_val_t, const std::nothrow_t&) 1561debfc3dSmrg _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 157c0a68be4Smrg_GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t) 1581debfc3dSmrg __attribute__((__externally_visible__)); 159c0a68be4Smrg_GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&) 160c0a68be4Smrg _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__, __malloc__)); 1611debfc3dSmrgvoid operator delete[](void*, std::align_val_t) 1621debfc3dSmrg _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 1631debfc3dSmrgvoid operator delete[](void*, std::align_val_t, const std::nothrow_t&) 1641debfc3dSmrg _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 1651debfc3dSmrg#if __cpp_sized_deallocation 1661debfc3dSmrgvoid operator delete(void*, std::size_t, std::align_val_t) 1671debfc3dSmrg _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 1681debfc3dSmrgvoid operator delete[](void*, std::size_t, std::align_val_t) 1691debfc3dSmrg _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 1701debfc3dSmrg#endif // __cpp_sized_deallocation 1711debfc3dSmrg#endif // __cpp_aligned_new 1721debfc3dSmrg 1731debfc3dSmrg// Default placement versions of operator new. 174c0a68be4Smrg_GLIBCXX_NODISCARD inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT 1751debfc3dSmrg{ return __p; } 176c0a68be4Smrg_GLIBCXX_NODISCARD inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT 1771debfc3dSmrg{ return __p; } 1781debfc3dSmrg 1791debfc3dSmrg// Default placement versions of operator delete. 1801debfc3dSmrginline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPT { } 1811debfc3dSmrginline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { } 1821debfc3dSmrg//@} 1831debfc3dSmrg} // extern "C++" 1841debfc3dSmrg 185a2dc1f3fSmrg#if __cplusplus >= 201703L 1861debfc3dSmrg#ifdef _GLIBCXX_HAVE_BUILTIN_LAUNDER 1871debfc3dSmrgnamespace std 1881debfc3dSmrg{ 1891debfc3dSmrg#define __cpp_lib_launder 201606 1901debfc3dSmrg /// Pointer optimization barrier [ptr.launder] 1911debfc3dSmrg template<typename _Tp> 192a2dc1f3fSmrg [[nodiscard]] constexpr _Tp* 1931debfc3dSmrg launder(_Tp* __p) noexcept 1941debfc3dSmrg { return __builtin_launder(__p); } 1951debfc3dSmrg 1961debfc3dSmrg // The program is ill-formed if T is a function type or 1971debfc3dSmrg // (possibly cv-qualified) void. 1981debfc3dSmrg 1991debfc3dSmrg template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM> 2001debfc3dSmrg void launder(_Ret (*)(_Args...) _GLIBCXX_NOEXCEPT_QUAL) = delete; 2011debfc3dSmrg template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM> 2021debfc3dSmrg void launder(_Ret (*)(_Args......) _GLIBCXX_NOEXCEPT_QUAL) = delete; 2031debfc3dSmrg 2041debfc3dSmrg void launder(void*) = delete; 2051debfc3dSmrg void launder(const void*) = delete; 2061debfc3dSmrg void launder(volatile void*) = delete; 2071debfc3dSmrg void launder(const volatile void*) = delete; 2081debfc3dSmrg} 2091debfc3dSmrg#endif // _GLIBCXX_HAVE_BUILTIN_LAUNDER 2101debfc3dSmrg#endif // C++17 2111debfc3dSmrg 212c0a68be4Smrg#if __cplusplus > 201703L 213c0a68be4Smrgnamespace std 214c0a68be4Smrg{ 215*8feb0f0bSmrg /// Tag type used to declare a class-specific operator delete that can 216*8feb0f0bSmrg /// invoke the destructor before deallocating the memory. 217c0a68be4Smrg struct destroying_delete_t 218c0a68be4Smrg { 219c0a68be4Smrg explicit destroying_delete_t() = default; 220c0a68be4Smrg }; 221*8feb0f0bSmrg /// Tag variable of type destroying_delete_t. 222c0a68be4Smrg inline constexpr destroying_delete_t destroying_delete{}; 223c0a68be4Smrg} 224c0a68be4Smrg// Only define the feature test macro if the compiler supports the feature: 225c0a68be4Smrg#if __cpp_impl_destroying_delete 226c0a68be4Smrg# define __cpp_lib_destroying_delete 201806L 227c0a68be4Smrg#endif 228c0a68be4Smrg#endif // C++20 229c0a68be4Smrg 2301debfc3dSmrg#pragma GCC visibility pop 2311debfc3dSmrg 2321debfc3dSmrg#endif 233