new 7.8 KB

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