libstdc++
new
Go to the documentation of this file.
00001 // The -*- C++ -*- dynamic memory management header.
00002 
00003 // Copyright (C) 1994-2019 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 #if __cplusplus >= 201103L
00060     bad_alloc(const bad_alloc&) = default;
00061     bad_alloc& operator=(const bad_alloc&) = default;
00062 #endif
00063 
00064     // This declaration is not useless:
00065     // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
00066     virtual ~bad_alloc() throw();
00067 
00068     // See comment in eh_exception.cc.
00069     virtual const char* what() const throw();
00070   };
00071 
00072 #if __cplusplus >= 201103L
00073   class bad_array_new_length : public bad_alloc
00074   {
00075   public:
00076     bad_array_new_length() throw() { }
00077 
00078     // This declaration is not useless:
00079     // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
00080     virtual ~bad_array_new_length() throw();
00081 
00082     // See comment in eh_exception.cc.
00083     virtual const char* what() const throw();
00084   };
00085 #endif
00086 
00087 #if __cpp_aligned_new
00088   enum class align_val_t: size_t {};
00089 #endif
00090 
00091   struct nothrow_t
00092   {
00093 #if __cplusplus >= 201103L
00094     explicit nothrow_t() = default;
00095 #endif
00096   };
00097 
00098   extern const nothrow_t nothrow;
00099 
00100   /** If you write your own error handler to be called by @c new, it must
00101    *  be of this type.  */
00102   typedef void (*new_handler)();
00103 
00104   /// Takes a replacement handler as the argument, returns the
00105   /// previous handler.
00106   new_handler set_new_handler(new_handler) throw();
00107 
00108 #if __cplusplus >= 201103L
00109   /// Return the current new handler.
00110   new_handler get_new_handler() noexcept;
00111 #endif
00112 } // namespace std
00113 
00114 //@{
00115 /** These are replaceable signatures:
00116  *  - normal single new and delete (no arguments, throw @c bad_alloc on error)
00117  *  - normal array new and delete (same)
00118  *  - @c nothrow single new and delete (take a @c nothrow argument, return
00119  *    @c NULL on error)
00120  *  - @c nothrow array new and delete (same)
00121  *
00122  *  Placement new and delete signatures (take a memory address argument,
00123  *  does nothing) may not be replaced by a user's program.
00124 */
00125 _GLIBCXX_NODISCARD void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc)
00126   __attribute__((__externally_visible__));
00127 _GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
00128   __attribute__((__externally_visible__));
00129 void operator delete(void*) _GLIBCXX_USE_NOEXCEPT
00130   __attribute__((__externally_visible__));
00131 void operator delete[](void*) _GLIBCXX_USE_NOEXCEPT
00132   __attribute__((__externally_visible__));
00133 #if __cpp_sized_deallocation
00134 void operator delete(void*, std::size_t) _GLIBCXX_USE_NOEXCEPT
00135   __attribute__((__externally_visible__));
00136 void operator delete[](void*, std::size_t) _GLIBCXX_USE_NOEXCEPT
00137   __attribute__((__externally_visible__));
00138 #endif
00139 _GLIBCXX_NODISCARD void* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
00140   __attribute__((__externally_visible__, __malloc__));
00141 _GLIBCXX_NODISCARD void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
00142   __attribute__((__externally_visible__, __malloc__));
00143 void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
00144   __attribute__((__externally_visible__));
00145 void operator delete[](void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
00146   __attribute__((__externally_visible__));
00147 #if __cpp_aligned_new
00148 _GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t)
00149   __attribute__((__externally_visible__));
00150 _GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&)
00151   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__, __malloc__));
00152 void operator delete(void*, std::align_val_t)
00153   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
00154 void operator delete(void*, std::align_val_t, const std::nothrow_t&)
00155   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
00156 _GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t)
00157   __attribute__((__externally_visible__));
00158 _GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&)
00159   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__, __malloc__));
00160 void operator delete[](void*, std::align_val_t)
00161   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
00162 void operator delete[](void*, std::align_val_t, const std::nothrow_t&)
00163   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
00164 #if __cpp_sized_deallocation
00165 void operator delete(void*, std::size_t, std::align_val_t)
00166   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
00167 void operator delete[](void*, std::size_t, std::align_val_t)
00168   _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
00169 #endif // __cpp_sized_deallocation
00170 #endif // __cpp_aligned_new
00171 
00172 // Default placement versions of operator new.
00173 _GLIBCXX_NODISCARD inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
00174 { return __p; }
00175 _GLIBCXX_NODISCARD inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
00176 { return __p; }
00177 
00178 // Default placement versions of operator delete.
00179 inline void operator delete  (void*, void*) _GLIBCXX_USE_NOEXCEPT { }
00180 inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { }
00181 //@}
00182 } // extern "C++"
00183 
00184 #if __cplusplus >= 201703L
00185 #ifdef _GLIBCXX_HAVE_BUILTIN_LAUNDER
00186 namespace std
00187 {
00188 #define __cpp_lib_launder 201606
00189   /// Pointer optimization barrier [ptr.launder]
00190   template<typename _Tp>
00191     [[nodiscard]] constexpr _Tp*
00192     launder(_Tp* __p) noexcept
00193     { return __builtin_launder(__p); }
00194 
00195   // The program is ill-formed if T is a function type or
00196   // (possibly cv-qualified) void.
00197 
00198   template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
00199     void launder(_Ret (*)(_Args...) _GLIBCXX_NOEXCEPT_QUAL) = delete;
00200   template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
00201     void launder(_Ret (*)(_Args......) _GLIBCXX_NOEXCEPT_QUAL) = delete;
00202 
00203   void launder(void*) = delete;
00204   void launder(const void*) = delete;
00205   void launder(volatile void*) = delete;
00206   void launder(const volatile void*) = delete;
00207 }
00208 #endif // _GLIBCXX_HAVE_BUILTIN_LAUNDER
00209 #endif // C++17
00210 
00211 #if __cplusplus > 201703L
00212 namespace std
00213 {
00214   struct destroying_delete_t
00215   {
00216     explicit destroying_delete_t() = default;
00217   };
00218   inline constexpr destroying_delete_t destroying_delete{};
00219 }
00220 // Only define the feature test macro if the compiler supports the feature:
00221 #if __cpp_impl_destroying_delete
00222 # define __cpp_lib_destroying_delete 201806L
00223 #endif
00224 #endif // C++20
00225 
00226 #pragma GCC visibility pop
00227 
00228 #endif