You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // The -*- C++ -*- dynamic memory management header.
  2. // Copyright (C) 1994-2020 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. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/dynamic_memory.html
  28. * for more.
  29. */
  30. #ifndef _NEW
  31. #define _NEW
  32. #pragma GCC system_header
  33. #include <bits/c++config.h>
  34. #include <exception>
  35. #pragma GCC visibility push(default)
  36. extern "C++" {
  37. namespace std
  38. {
  39. /**
  40. * @brief Exception possibly thrown by @c new.
  41. * @ingroup exceptions
  42. *
  43. * @c bad_alloc (or classes derived from it) is used to report allocation
  44. * errors from the throwing forms of @c new. */
  45. class bad_alloc : public exception
  46. {
  47. public:
  48. bad_alloc() throw() { }
  49. #if __cplusplus >= 201103L
  50. bad_alloc(const bad_alloc&) = default;
  51. bad_alloc& operator=(const bad_alloc&) = default;
  52. #endif
  53. // This declaration is not useless:
  54. // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
  55. virtual ~bad_alloc() throw();
  56. // See comment in eh_exception.cc.
  57. virtual const char* what() const throw();
  58. };
  59. #if __cplusplus >= 201103L
  60. class bad_array_new_length : public bad_alloc
  61. {
  62. public:
  63. bad_array_new_length() throw() { }
  64. // This declaration is not useless:
  65. // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
  66. virtual ~bad_array_new_length() throw();
  67. // See comment in eh_exception.cc.
  68. virtual const char* what() const throw();
  69. };
  70. #endif
  71. #if __cpp_aligned_new
  72. enum class align_val_t: size_t {};
  73. #endif
  74. struct nothrow_t
  75. {
  76. #if __cplusplus >= 201103L
  77. explicit nothrow_t() = default;
  78. #endif
  79. };
  80. extern const nothrow_t nothrow;
  81. /** If you write your own error handler to be called by @c new, it must
  82. * be of this type. */
  83. typedef void (*new_handler)();
  84. /// Takes a replacement handler as the argument, returns the
  85. /// previous handler.
  86. new_handler set_new_handler(new_handler) throw();
  87. #if __cplusplus >= 201103L
  88. /// Return the current new handler.
  89. new_handler get_new_handler() noexcept;
  90. #endif
  91. } // namespace std
  92. //@{
  93. /** These are replaceable signatures:
  94. * - normal single new and delete (no arguments, throw @c bad_alloc on error)
  95. * - normal array new and delete (same)
  96. * - @c nothrow single new and delete (take a @c nothrow argument, return
  97. * @c NULL on error)
  98. * - @c nothrow array new and delete (same)
  99. *
  100. * Placement new and delete signatures (take a memory address argument,
  101. * does nothing) may not be replaced by a user's program.
  102. */
  103. _GLIBCXX_NODISCARD void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc)
  104. __attribute__((__externally_visible__));
  105. _GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
  106. __attribute__((__externally_visible__));
  107. void operator delete(void*) _GLIBCXX_USE_NOEXCEPT
  108. __attribute__((__externally_visible__));
  109. void operator delete[](void*) _GLIBCXX_USE_NOEXCEPT
  110. __attribute__((__externally_visible__));
  111. #if __cpp_sized_deallocation
  112. void operator delete(void*, std::size_t) _GLIBCXX_USE_NOEXCEPT
  113. __attribute__((__externally_visible__));
  114. void operator delete[](void*, std::size_t) _GLIBCXX_USE_NOEXCEPT
  115. __attribute__((__externally_visible__));
  116. #endif
  117. _GLIBCXX_NODISCARD void* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
  118. __attribute__((__externally_visible__, __malloc__));
  119. _GLIBCXX_NODISCARD void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
  120. __attribute__((__externally_visible__, __malloc__));
  121. void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
  122. __attribute__((__externally_visible__));
  123. void operator delete[](void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
  124. __attribute__((__externally_visible__));
  125. #if __cpp_aligned_new
  126. _GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t)
  127. __attribute__((__externally_visible__));
  128. _GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&)
  129. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__, __malloc__));
  130. void operator delete(void*, std::align_val_t)
  131. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  132. void operator delete(void*, std::align_val_t, const std::nothrow_t&)
  133. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  134. _GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t)
  135. __attribute__((__externally_visible__));
  136. _GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&)
  137. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__, __malloc__));
  138. void operator delete[](void*, std::align_val_t)
  139. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  140. void operator delete[](void*, std::align_val_t, const std::nothrow_t&)
  141. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  142. #if __cpp_sized_deallocation
  143. void operator delete(void*, std::size_t, std::align_val_t)
  144. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  145. void operator delete[](void*, std::size_t, std::align_val_t)
  146. _GLIBCXX_USE_NOEXCEPT __attribute__((__externally_visible__));
  147. #endif // __cpp_sized_deallocation
  148. #endif // __cpp_aligned_new
  149. // Default placement versions of operator new.
  150. _GLIBCXX_NODISCARD inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
  151. { return __p; }
  152. _GLIBCXX_NODISCARD inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
  153. { return __p; }
  154. // Default placement versions of operator delete.
  155. inline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPT { }
  156. inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { }
  157. //@}
  158. } // extern "C++"
  159. #if __cplusplus >= 201703L
  160. #ifdef _GLIBCXX_HAVE_BUILTIN_LAUNDER
  161. namespace std
  162. {
  163. #define __cpp_lib_launder 201606
  164. /// Pointer optimization barrier [ptr.launder]
  165. template<typename _Tp>
  166. [[nodiscard]] constexpr _Tp*
  167. launder(_Tp* __p) noexcept
  168. { return __builtin_launder(__p); }
  169. // The program is ill-formed if T is a function type or
  170. // (possibly cv-qualified) void.
  171. template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
  172. void launder(_Ret (*)(_Args...) _GLIBCXX_NOEXCEPT_QUAL) = delete;
  173. template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
  174. void launder(_Ret (*)(_Args......) _GLIBCXX_NOEXCEPT_QUAL) = delete;
  175. void launder(void*) = delete;
  176. void launder(const void*) = delete;
  177. void launder(volatile void*) = delete;
  178. void launder(const volatile void*) = delete;
  179. }
  180. #endif // _GLIBCXX_HAVE_BUILTIN_LAUNDER
  181. #endif // C++17
  182. #if __cplusplus > 201703L
  183. namespace std
  184. {
  185. /// Tag type used to declare a class-specific operator delete that can
  186. /// invoke the destructor before deallocating the memory.
  187. struct destroying_delete_t
  188. {
  189. explicit destroying_delete_t() = default;
  190. };
  191. /// Tag variable of type destroying_delete_t.
  192. inline constexpr destroying_delete_t destroying_delete{};
  193. }
  194. // Only define the feature test macro if the compiler supports the feature:
  195. #if __cpp_impl_destroying_delete
  196. # define __cpp_lib_destroying_delete 201806L
  197. #endif
  198. #endif // C++20
  199. #pragma GCC visibility pop
  200. #endif