libstdc++
|
00001 // The -*- C++ -*- dynamic memory management header. 00002 00003 // Copyright (C) 1994-2016 Free Software Foundation, Inc. 00004 00005 // This file is part of GCC. 00006 // 00007 // GCC is free software; you can redistribute it and/or modify 00008 // it under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation; either version 3, or (at your option) 00010 // any later version. 00011 // 00012 // GCC is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // Under Section 7 of GPL version 3, you are granted additional 00018 // permissions described in the GCC Runtime Library Exception, version 00019 // 3.1, as published by the Free Software Foundation. 00020 00021 // You should have received a copy of the GNU General Public License and 00022 // a copy of the GCC Runtime Library Exception along with this program; 00023 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00024 // <http://www.gnu.org/licenses/>. 00025 00026 /** @file new 00027 * This is a Standard C++ Library header. 00028 * 00029 * The header @c new defines several functions to manage dynamic memory and 00030 * handling memory allocation errors; see 00031 * http://gcc.gnu.org/onlinedocs/libstdc++/18_support/howto.html#4 for more. 00032 */ 00033 00034 #ifndef _NEW 00035 #define _NEW 00036 00037 #pragma GCC system_header 00038 00039 #include <bits/c++config.h> 00040 #include <exception> 00041 00042 #pragma GCC visibility push(default) 00043 00044 extern "C++" { 00045 00046 namespace std 00047 { 00048 /** 00049 * @brief Exception possibly thrown by @c new. 00050 * @ingroup exceptions 00051 * 00052 * @c bad_alloc (or classes derived from it) is used to report allocation 00053 * errors from the throwing forms of @c new. */ 00054 class bad_alloc : public exception 00055 { 00056 public: 00057 bad_alloc() throw() { } 00058 00059 // This declaration is not useless: 00060 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118 00061 virtual ~bad_alloc() throw(); 00062 00063 // See comment in eh_exception.cc. 00064 virtual const char* what() const throw(); 00065 }; 00066 00067 #if __cplusplus >= 201103L 00068 class bad_array_new_length : public bad_alloc 00069 { 00070 public: 00071 bad_array_new_length() throw() { }; 00072 00073 // This declaration is not useless: 00074 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118 00075 virtual ~bad_array_new_length() throw(); 00076 00077 // See comment in eh_exception.cc. 00078 virtual const char* what() const throw(); 00079 }; 00080 #endif 00081 00082 struct nothrow_t 00083 { 00084 #if __cplusplus >= 201103L 00085 explicit nothrow_t() = default; 00086 #endif 00087 }; 00088 00089 extern const nothrow_t nothrow; 00090 00091 /** If you write your own error handler to be called by @c new, it must 00092 * be of this type. */ 00093 typedef void (*new_handler)(); 00094 00095 /// Takes a replacement handler as the argument, returns the 00096 /// previous handler. 00097 new_handler set_new_handler(new_handler) throw(); 00098 00099 #if __cplusplus >= 201103L 00100 /// Return the current new handler. 00101 new_handler get_new_handler() noexcept; 00102 #endif 00103 } // namespace std 00104 00105 //@{ 00106 /** These are replaceable signatures: 00107 * - normal single new and delete (no arguments, throw @c bad_alloc on error) 00108 * - normal array new and delete (same) 00109 * - @c nothrow single new and delete (take a @c nothrow argument, return 00110 * @c NULL on error) 00111 * - @c nothrow array new and delete (same) 00112 * 00113 * Placement new and delete signatures (take a memory address argument, 00114 * does nothing) may not be replaced by a user's program. 00115 */ 00116 void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc) 00117 __attribute__((__externally_visible__)); 00118 void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc) 00119 __attribute__((__externally_visible__)); 00120 void operator delete(void*) _GLIBCXX_USE_NOEXCEPT 00121 __attribute__((__externally_visible__)); 00122 void operator delete[](void*) _GLIBCXX_USE_NOEXCEPT 00123 __attribute__((__externally_visible__)); 00124 #if __cpp_sized_deallocation 00125 void operator delete(void*, std::size_t) _GLIBCXX_USE_NOEXCEPT 00126 __attribute__((__externally_visible__)); 00127 void operator delete[](void*, std::size_t) _GLIBCXX_USE_NOEXCEPT 00128 __attribute__((__externally_visible__)); 00129 #endif 00130 void* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 00131 __attribute__((__externally_visible__)); 00132 void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 00133 __attribute__((__externally_visible__)); 00134 void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 00135 __attribute__((__externally_visible__)); 00136 void operator delete[](void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 00137 __attribute__((__externally_visible__)); 00138 #if __cpp_sized_deallocation 00139 void operator delete(void*, std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 00140 __attribute__((__externally_visible__)); 00141 void operator delete[](void*, std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT 00142 __attribute__((__externally_visible__)); 00143 #endif 00144 00145 // Default placement versions of operator new. 00146 inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT 00147 { return __p; } 00148 inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT 00149 { return __p; } 00150 00151 // Default placement versions of operator delete. 00152 inline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPT { } 00153 inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { } 00154 //@} 00155 } // extern "C++" 00156 00157 #pragma GCC visibility pop 00158 00159 #endif