1*38fd1498Szrj// The -*- C++ -*- dynamic memory management header. 2*38fd1498Szrj 3*38fd1498Szrj// Copyright (C) 1994-2018 Free Software Foundation, Inc. 4*38fd1498Szrj 5*38fd1498Szrj// This file is part of GCC. 6*38fd1498Szrj// 7*38fd1498Szrj// GCC is free software; you can redistribute it and/or modify 8*38fd1498Szrj// it under the terms of the GNU General Public License as published by 9*38fd1498Szrj// the Free Software Foundation; either version 3, or (at your option) 10*38fd1498Szrj// any later version. 11*38fd1498Szrj// 12*38fd1498Szrj// GCC is distributed in the hope that it will be useful, 13*38fd1498Szrj// but WITHOUT ANY WARRANTY; without even the implied warranty of 14*38fd1498Szrj// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*38fd1498Szrj// GNU General Public License for more details. 16*38fd1498Szrj// 17*38fd1498Szrj// Under Section 7 of GPL version 3, you are granted additional 18*38fd1498Szrj// permissions described in the GCC Runtime Library Exception, version 19*38fd1498Szrj// 3.1, as published by the Free Software Foundation. 20*38fd1498Szrj 21*38fd1498Szrj// You should have received a copy of the GNU General Public License and 22*38fd1498Szrj// a copy of the GCC Runtime Library Exception along with this program; 23*38fd1498Szrj// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24*38fd1498Szrj// <http://www.gnu.org/licenses/>. 25*38fd1498Szrj 26*38fd1498Szrj/** @file new 27*38fd1498Szrj * This is a Standard C++ Library header. 28*38fd1498Szrj * 29*38fd1498Szrj * The header @c new defines several functions to manage dynamic memory and 30*38fd1498Szrj * handling memory allocation errors; see 31*38fd1498Szrj * http://gcc.gnu.org/onlinedocs/libstdc++/18_support/howto.html#4 for more. 32*38fd1498Szrj */ 33*38fd1498Szrj 34*38fd1498Szrj#ifndef _NEW 35*38fd1498Szrj#define _NEW 36*38fd1498Szrj 37*38fd1498Szrj#pragma GCC system_header 38*38fd1498Szrj 39*38fd1498Szrj#include <bits/c++config.h> 40*38fd1498Szrj#include <exception> 41*38fd1498Szrj 42*38fd1498Szrj#pragma GCC visibility push(default) 43*38fd1498Szrj 44*38fd1498Szrjextern "C++" { 45*38fd1498Szrj 46*38fd1498Szrjnamespace std 47*38fd1498Szrj{ 48*38fd1498Szrj /** 49*38fd1498Szrj * @brief Exception possibly thrown by @c new. 50*38fd1498Szrj * @ingroup exceptions 51*38fd1498Szrj * 52*38fd1498Szrj * @c bad_alloc (or classes derived from it) is used to report allocation 53*38fd1498Szrj * errors from the throwing forms of @c new. */ 54*38fd1498Szrj class bad_alloc : public exception 55*38fd1498Szrj { 56*38fd1498Szrj public: 57*38fd1498Szrj bad_alloc() throw() { } 58*38fd1498Szrj 59*38fd1498Szrj // This declaration is not useless: 60*38fd1498Szrj // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118 61*38fd1498Szrj virtual ~bad_alloc() throw(); 62*38fd1498Szrj 63*38fd1498Szrj // See comment in eh_exception.cc. 64*38fd1498Szrj virtual const char* what() const throw(); 65*38fd1498Szrj }; 66*38fd1498Szrj 67*38fd1498Szrj#if __cplusplus >= 201103L 68*38fd1498Szrj class bad_array_new_length : public bad_alloc 69*38fd1498Szrj { 70*38fd1498Szrj public: 71*38fd1498Szrj bad_array_new_length() throw() { } 72*38fd1498Szrj 73*38fd1498Szrj // This declaration is not useless: 74*38fd1498Szrj // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118 75*38fd1498Szrj virtual ~bad_array_new_length() throw(); 76*38fd1498Szrj 77*38fd1498Szrj // See comment in eh_exception.cc. 78*38fd1498Szrj virtual const char* what() const throw(); 79*38fd1498Szrj }; 80*38fd1498Szrj#endif 81*38fd1498Szrj 82*38fd1498Szrj#if __cpp_aligned_new 83*38fd1498Szrj enum class align_val_t: size_t {}; 84*38fd1498Szrj#endif 85*38fd1498Szrj 86*38fd1498Szrj struct nothrow_t 87*38fd1498Szrj { 88*38fd1498Szrj#if __cplusplus >= 201103L 89*38fd1498Szrj explicit nothrow_t() = default; 90*38fd1498Szrj#endif 91*38fd1498Szrj }; 92*38fd1498Szrj 93*38fd1498Szrj extern const nothrow_t nothrow; 94*38fd1498Szrj 95*38fd1498Szrj /** If you write your own error handler to be called by @c new, it must 96*38fd1498Szrj * be of this type. */ 97*38fd1498Szrj typedef void (*new_handler)(); 98*38fd1498Szrj 99*38fd1498Szrj /// Takes a replacement handler as the argument, returns the 100*38fd1498Szrj /// previous handler. 101*38fd1498Szrj new_handler set_new_handler(new_handler) throw(); 102*38fd1498Szrj 103*38fd1498Szrj#if __cplusplus >= 201103L 104*38fd1498Szrj /// Return the current new handler. 105*38fd1498Szrj new_handler get_new_handler() noexcept; 106*38fd1498Szrj#endif 107*38fd1498Szrj} // namespace std 108*38fd1498Szrj 109*38fd1498Szrj//@{ 110*38fd1498Szrj/** These are replaceable signatures: 111*38fd1498Szrj * - normal single new and delete (no arguments, throw @c bad_alloc on error) 112*38fd1498Szrj * - normal array new and delete (same) 113*38fd1498Szrj * - @c nothrow single new and delete (take a @c nothrow argument, return 114*38fd1498Szrj * @c NULL on error) 115*38fd1498Szrj * - @c nothrow array new and delete (same) 116*38fd1498Szrj * 117*38fd1498Szrj * Placement new and delete signatures (take a memory address argument, 118*38fd1498Szrj * does nothing) may not be replaced by a user's program. 119*38fd1498Szrj*/ 120*38fd1498Szrjvoid* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc) 121*38fd1498Szrj __attribute__((__externally_visible__)); 122*38fd1498Szrjvoid* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc) 123*38fd1498Szrj __attribute__((__externally_visible__)); 124*38fd1498Szrjvoid operator delete(void*) _GLIBCXX_USE_NOEXCEPT 125*38fd1498Szrj __attribute__((__externally_visible__)); 126*38fd1498Szrjvoid operator delete[](void*) _GLIBCXX_USE_NOEXCEPT 127*38fd1498Szrj __attribute__((__externally_visible__)); 128*38fd1498Szrj#if __cpp_sized_deallocation 129*38fd1498Szrjvoid operator delete(void*, std::size_t) _GLIBCXX_USE_NOEXCEPT 130*38fd1498Szrj __attribute__((__externally_visible__)); 131*38fd1498Szrjvoid operator delete[](void*, std::size_t) _GLIBCXX_USE_NOEXCEPT 132*38fd1498Szrj __attribute__((__externally_visible__)); 133*38fd1498Szrj#endif 134*38fd1498Szrjvoid* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 135*38fd1498Szrj __attribute__((__externally_visible__)); 136*38fd1498Szrjvoid* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 137*38fd1498Szrj __attribute__((__externally_visible__)); 138*38fd1498Szrjvoid operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 139*38fd1498Szrj __attribute__((__externally_visible__)); 140*38fd1498Szrjvoid operator delete[](void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 141*38fd1498Szrj __attribute__((__externally_visible__)); 142*38fd1498Szrj#if __cpp_aligned_new 143*38fd1498Szrjvoid* operator new(std::size_t, std::align_val_t) 144*38fd1498Szrj __attribute__((__externally_visible__)); 145*38fd1498Szrjvoid* operator new(std::size_t, std::align_val_t, const std::nothrow_t&) 146*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 147*38fd1498Szrjvoid operator delete(void*, std::align_val_t) 148*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 149*38fd1498Szrjvoid operator delete(void*, std::align_val_t, const std::nothrow_t&) 150*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 151*38fd1498Szrjvoid* operator new[](std::size_t, std::align_val_t) 152*38fd1498Szrj __attribute__((__externally_visible__)); 153*38fd1498Szrjvoid* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&) 154*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 155*38fd1498Szrjvoid operator delete[](void*, std::align_val_t) 156*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 157*38fd1498Szrjvoid operator delete[](void*, std::align_val_t, const std::nothrow_t&) 158*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 159*38fd1498Szrj#if __cpp_sized_deallocation 160*38fd1498Szrjvoid operator delete(void*, std::size_t, std::align_val_t) 161*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 162*38fd1498Szrjvoid operator delete[](void*, std::size_t, std::align_val_t) 163*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__)); 164*38fd1498Szrj#endif // __cpp_sized_deallocation 165*38fd1498Szrj#endif // __cpp_aligned_new 166*38fd1498Szrj 167*38fd1498Szrj// Default placement versions of operator new. 168*38fd1498Szrjinline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT 169*38fd1498Szrj{ return __p; } 170*38fd1498Szrjinline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT 171*38fd1498Szrj{ return __p; } 172*38fd1498Szrj 173*38fd1498Szrj// Default placement versions of operator delete. 174*38fd1498Szrjinline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPT { } 175*38fd1498Szrjinline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { } 176*38fd1498Szrj//@} 177*38fd1498Szrj} // extern "C++" 178*38fd1498Szrj 179*38fd1498Szrj#if __cplusplus >= 201703L 180*38fd1498Szrj#if __GNUC__ >= 7 181*38fd1498Szrj# define _GLIBCXX_HAVE_BUILTIN_LAUNDER 1 182*38fd1498Szrj#elif defined __has_builtin 183*38fd1498Szrj// For non-GNU compilers: 184*38fd1498Szrj# if __has_builtin(__builtin_launder) 185*38fd1498Szrj# define _GLIBCXX_HAVE_BUILTIN_LAUNDER 1 186*38fd1498Szrj# endif 187*38fd1498Szrj#endif 188*38fd1498Szrj 189*38fd1498Szrj#ifdef _GLIBCXX_HAVE_BUILTIN_LAUNDER 190*38fd1498Szrjnamespace std 191*38fd1498Szrj{ 192*38fd1498Szrj#define __cpp_lib_launder 201606 193*38fd1498Szrj /// Pointer optimization barrier [ptr.launder] 194*38fd1498Szrj template<typename _Tp> 195*38fd1498Szrj [[nodiscard]] constexpr _Tp* 196*38fd1498Szrj launder(_Tp* __p) noexcept 197*38fd1498Szrj { return __builtin_launder(__p); } 198*38fd1498Szrj 199*38fd1498Szrj // The program is ill-formed if T is a function type or 200*38fd1498Szrj // (possibly cv-qualified) void. 201*38fd1498Szrj 202*38fd1498Szrj template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM> 203*38fd1498Szrj void launder(_Ret (*)(_Args...) _GLIBCXX_NOEXCEPT_QUAL) = delete; 204*38fd1498Szrj template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM> 205*38fd1498Szrj void launder(_Ret (*)(_Args......) _GLIBCXX_NOEXCEPT_QUAL) = delete; 206*38fd1498Szrj 207*38fd1498Szrj void launder(void*) = delete; 208*38fd1498Szrj void launder(const void*) = delete; 209*38fd1498Szrj void launder(volatile void*) = delete; 210*38fd1498Szrj void launder(const volatile void*) = delete; 211*38fd1498Szrj} 212*38fd1498Szrj#endif // _GLIBCXX_HAVE_BUILTIN_LAUNDER 213*38fd1498Szrj#undef _GLIBCXX_HAVE_BUILTIN_LAUNDER 214*38fd1498Szrj#endif // C++17 215*38fd1498Szrj 216*38fd1498Szrj#pragma GCC visibility pop 217*38fd1498Szrj 218*38fd1498Szrj#endif 219