new 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // The -*- C++ -*- dynamic memory management header.
  2. // Copyright (C) 1994-2018 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. // This declaration is not useless:
  49. // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
  50. virtual ~bad_alloc() throw();
  51. // See comment in eh_exception.cc.
  52. virtual const char* what() const throw();
  53. };
  54. #if __cplusplus >= 201103L
  55. class bad_array_new_length : public bad_alloc
  56. {
  57. public:
  58. bad_array_new_length() throw() { }
  59. // This declaration is not useless:
  60. // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
  61. virtual ~bad_array_new_length() throw();
  62. // See comment in eh_exception.cc.
  63. virtual const char* what() const throw();
  64. };
  65. #endif
  66. #if __cpp_aligned_new
  67. enum class align_val_t: size_t {};
  68. #endif
  69. struct nothrow_t
  70. {
  71. #if __cplusplus >= 201103L
  72. explicit nothrow_t() = default;
  73. #endif
  74. };
  75. extern const nothrow_t nothrow;
  76. /** If you write your own error handler to be called by @c new, it must
  77. * be of this type. */
  78. typedef void (*new_handler)();
  79. /// Takes a replacement handler as the argument, returns the
  80. /// previous handler.
  81. new_handler set_new_handler(new_handler) throw();
  82. #if __cplusplus >= 201103L
  83. /// Return the current new handler.
  84. new_handler get_new_handler() noexcept;
  85. #endif
  86. } // namespace std
  87. //@{
  88. /** These are replaceable signatures:
  89. * - normal single new and delete (no arguments, throw @c bad_alloc on error)
  90. * - normal array new and delete (same)
  91. * - @c nothrow single new and delete (take a @c nothrow argument, return
  92. * @c NULL on error)
  93. * - @c nothrow array new and delete (same)
  94. *
  95. * Placement new and delete signatures (take a memory address argument,
  96. * does nothing) may not be replaced by a user's program.
  97. */
  98. void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc)
  99. __attribute__((__externally_visible__));
  100. void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
  101. __attribute__((__externally_visible__));
  102. void operator delete(void*) _GLIBCXX_USE_NOEXCEPT
  103. __attribute__((__externally_visible__));
  104. void operator delete[](void*) _GLIBCXX_USE_NOEXCEPT
  105. __attribute__((__externally_visible__));
  106. #if __cpp_sized_deallocation
  107. void operator delete(void*, std::size_t) _GLIBCXX_USE_NOEXCEPT
  108. __attribute__((__externally_visible__));
  109. void operator delete[](void*, std::size_t) _GLIBCXX_USE_NOEXCEPT
  110. __attribute__((__externally_visible__));
  111. #endif
  112. void* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
  113. __attribute__((__externally_visible__));
  114. void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
  115. __attribute__((__externally_visible__));
  116. void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
  117. __attribute__((__externally_visible__));
  118. void operator delete[](void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
  119. __attribute__((__externally_visible__));
  120. #if __cpp_aligned_new
  121. void* operator new(std::size_t, std::align_val_t)
  122. __attribute__((__externally_visible__));
  123. void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&)
  124. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  125. void operator delete(void*, std::align_val_t)
  126. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  127. void operator delete(void*, std::align_val_t, const std::nothrow_t&)
  128. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  129. void* operator new[](std::size_t, std::align_val_t)
  130. __attribute__((__externally_visible__));
  131. void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&)
  132. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  133. void operator delete[](void*, std::align_val_t)
  134. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  135. void operator delete[](void*, std::align_val_t, const std::nothrow_t&)
  136. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  137. #if __cpp_sized_deallocation
  138. void operator delete(void*, std::size_t, std::align_val_t)
  139. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  140. void operator delete[](void*, std::size_t, std::align_val_t)
  141. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  142. #endif // __cpp_sized_deallocation
  143. #endif // __cpp_aligned_new
  144. // Default placement versions of operator new.
  145. inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
  146. { return __p; }
  147. inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
  148. { return __p; }
  149. // Default placement versions of operator delete.
  150. inline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPT { }
  151. inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { }
  152. //@}
  153. } // extern "C++"
  154. #if __cplusplus >= 201703L
  155. #if __GNUC__ >= 7
  156. # define _GLIBCXX_HAVE_BUILTIN_LAUNDER 1
  157. #elif defined __has_builtin
  158. // For non-GNU compilers:
  159. # if __has_builtin(__builtin_launder)
  160. # define _GLIBCXX_HAVE_BUILTIN_LAUNDER 1
  161. # endif
  162. #endif
  163. #ifdef _GLIBCXX_HAVE_BUILTIN_LAUNDER
  164. namespace std
  165. {
  166. #define __cpp_lib_launder 201606
  167. /// Pointer optimization barrier [ptr.launder]
  168. template<typename _Tp>
  169. [[nodiscard]] constexpr _Tp*
  170. launder(_Tp* __p) noexcept
  171. { return __builtin_launder(__p); }
  172. // The program is ill-formed if T is a function type or
  173. // (possibly cv-qualified) void.
  174. template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
  175. void launder(_Ret (*)(_Args...) _GLIBCXX_NOEXCEPT_QUAL) = delete;
  176. template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
  177. void launder(_Ret (*)(_Args......) _GLIBCXX_NOEXCEPT_QUAL) = delete;
  178. void launder(void*) = delete;
  179. void launder(const void*) = delete;
  180. void launder(volatile void*) = delete;
  181. void launder(const volatile void*) = delete;
  182. }
  183. #endif // _GLIBCXX_HAVE_BUILTIN_LAUNDER
  184. #undef _GLIBCXX_HAVE_BUILTIN_LAUNDER
  185. #endif // C++17
  186. #pragma GCC visibility pop
  187. #endif