new 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_NEW
  10. #define _LIBCPP_NEW
  11. /*
  12. new synopsis
  13. namespace std
  14. {
  15. class bad_alloc
  16. : public exception
  17. {
  18. public:
  19. bad_alloc() noexcept;
  20. bad_alloc(const bad_alloc&) noexcept;
  21. bad_alloc& operator=(const bad_alloc&) noexcept;
  22. virtual const char* what() const noexcept;
  23. };
  24. class bad_array_new_length : public bad_alloc // C++14
  25. {
  26. public:
  27. bad_array_new_length() noexcept;
  28. };
  29. enum class align_val_t : size_t {}; // C++17
  30. struct destroying_delete_t { // C++20
  31. explicit destroying_delete_t() = default;
  32. };
  33. inline constexpr destroying_delete_t destroying_delete{}; // C++20
  34. struct nothrow_t { explicit nothrow_t() = default; };
  35. extern const nothrow_t nothrow;
  36. typedef void (*new_handler)();
  37. new_handler set_new_handler(new_handler new_p) noexcept;
  38. new_handler get_new_handler() noexcept;
  39. // 21.6.4, pointer optimization barrier
  40. template <class T> constexpr T* launder(T* p) noexcept; // C++17
  41. } // std
  42. void* operator new(std::size_t size); // replaceable, nodiscard in C++20
  43. void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++20
  44. void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
  45. void* operator new(std::size_t size, std::align_val_t alignment,
  46. const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
  47. void operator delete(void* ptr) noexcept; // replaceable
  48. void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
  49. void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17
  50. void operator delete(void* ptr, std::size_t size,
  51. std::align_val_t alignment) noexcept; // replaceable, C++17
  52. void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
  53. void operator delete(void* ptr, std:align_val_t alignment,
  54. const std::nothrow_t&) noexcept; // replaceable, C++17
  55. void* operator new[](std::size_t size); // replaceable, nodiscard in C++20
  56. void* operator new[](std::size_t size,
  57. std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++20
  58. void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
  59. void* operator new[](std::size_t size, std::align_val_t alignment,
  60. const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
  61. void operator delete[](void* ptr) noexcept; // replaceable
  62. void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
  63. void operator delete[](void* ptr,
  64. std::align_val_t alignment) noexcept; // replaceable, C++17
  65. void operator delete[](void* ptr, std::size_t size,
  66. std::align_val_t alignment) noexcept; // replaceable, C++17
  67. void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
  68. void operator delete[](void* ptr, std::align_val_t alignment,
  69. const std::nothrow_t&) noexcept; // replaceable, C++17
  70. void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++20
  71. void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++20
  72. void operator delete (void* ptr, void*) noexcept;
  73. void operator delete[](void* ptr, void*) noexcept;
  74. */
  75. #include <__assert> // all public C++ headers provide the assertion handler
  76. #include <__availability>
  77. #include <__config>
  78. #include <__exception/exception.h>
  79. #include <__type_traits/alignment_of.h>
  80. #include <__type_traits/is_function.h>
  81. #include <__type_traits/is_same.h>
  82. #include <__type_traits/remove_cv.h>
  83. #include <__verbose_abort>
  84. #include <cstddef>
  85. #include <cstdlib>
  86. #include <version>
  87. #if defined(_LIBCPP_ABI_VCRUNTIME)
  88. #include <new.h>
  89. #endif
  90. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  91. # pragma GCC system_header
  92. #endif
  93. #if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
  94. #define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
  95. #endif
  96. #if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
  97. defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
  98. # define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  99. #endif
  100. #if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
  101. defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
  102. # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
  103. #endif
  104. namespace std // purposefully not using versioning namespace
  105. {
  106. #if !defined(_LIBCPP_ABI_VCRUNTIME)
  107. struct _LIBCPP_EXPORTED_FROM_ABI nothrow_t { explicit nothrow_t() = default; };
  108. extern _LIBCPP_EXPORTED_FROM_ABI const nothrow_t nothrow;
  109. class _LIBCPP_EXPORTED_FROM_ABI bad_alloc
  110. : public exception
  111. {
  112. public:
  113. bad_alloc() _NOEXCEPT;
  114. _LIBCPP_HIDE_FROM_ABI bad_alloc(const bad_alloc&) _NOEXCEPT = default;
  115. _LIBCPP_HIDE_FROM_ABI bad_alloc& operator=(const bad_alloc&) _NOEXCEPT = default;
  116. ~bad_alloc() _NOEXCEPT override;
  117. const char* what() const _NOEXCEPT override;
  118. };
  119. class _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length
  120. : public bad_alloc
  121. {
  122. public:
  123. bad_array_new_length() _NOEXCEPT;
  124. _LIBCPP_HIDE_FROM_ABI bad_array_new_length(const bad_array_new_length&) _NOEXCEPT = default;
  125. _LIBCPP_HIDE_FROM_ABI bad_array_new_length& operator=(const bad_array_new_length&) _NOEXCEPT = default;
  126. ~bad_array_new_length() _NOEXCEPT override;
  127. const char* what() const _NOEXCEPT override;
  128. };
  129. typedef void (*new_handler)();
  130. _LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
  131. _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
  132. #elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
  133. // When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
  134. // since they would normally be provided in vcruntime_exception.h
  135. class bad_alloc : public exception {
  136. public:
  137. bad_alloc() noexcept : exception("bad allocation") {}
  138. private:
  139. friend class bad_array_new_length;
  140. bad_alloc(char const* const __message) noexcept : exception(__message) {}
  141. };
  142. class bad_array_new_length : public bad_alloc {
  143. public:
  144. bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
  145. };
  146. #endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
  147. _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_bad_alloc(); // not in C++ spec
  148. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  149. void __throw_bad_array_new_length()
  150. {
  151. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  152. throw bad_array_new_length();
  153. #else
  154. _LIBCPP_VERBOSE_ABORT("bad_array_new_length was thrown in -fno-exceptions mode");
  155. #endif
  156. }
  157. #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
  158. !defined(_LIBCPP_ABI_VCRUNTIME)
  159. #ifndef _LIBCPP_CXX03_LANG
  160. enum class align_val_t : size_t { };
  161. #else
  162. enum align_val_t { __zero = 0, __max = (size_t)-1 };
  163. #endif
  164. #endif
  165. #if _LIBCPP_STD_VER >= 20
  166. // Enable the declaration even if the compiler doesn't support the language
  167. // feature.
  168. struct destroying_delete_t {
  169. explicit destroying_delete_t() = default;
  170. };
  171. inline constexpr destroying_delete_t destroying_delete{};
  172. #endif // _LIBCPP_STD_VER >= 20
  173. } // namespace std
  174. #if defined(_LIBCPP_CXX03_LANG)
  175. #define _THROW_BAD_ALLOC throw(std::bad_alloc)
  176. #else
  177. #define _THROW_BAD_ALLOC
  178. #endif
  179. #if !defined(_LIBCPP_ABI_VCRUNTIME)
  180. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
  181. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
  182. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
  183. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
  184. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  185. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
  186. #endif
  187. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
  188. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
  189. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
  190. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
  191. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  192. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
  193. #endif
  194. #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
  195. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
  196. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
  197. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
  198. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
  199. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  200. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
  201. #endif
  202. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
  203. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
  204. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
  205. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
  206. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  207. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
  208. #endif
  209. #endif
  210. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
  211. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
  212. inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
  213. inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
  214. #endif // !_LIBCPP_ABI_VCRUNTIME
  215. _LIBCPP_BEGIN_NAMESPACE_STD
  216. _LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
  217. #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
  218. return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
  219. #else
  220. return __align > alignment_of<max_align_t>::value;
  221. #endif
  222. }
  223. template <class ..._Args>
  224. _LIBCPP_INLINE_VISIBILITY
  225. void* __libcpp_operator_new(_Args ...__args) {
  226. #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
  227. return __builtin_operator_new(__args...);
  228. #else
  229. return ::operator new(__args...);
  230. #endif
  231. }
  232. template <class ..._Args>
  233. _LIBCPP_INLINE_VISIBILITY
  234. void __libcpp_operator_delete(_Args ...__args) {
  235. #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
  236. __builtin_operator_delete(__args...);
  237. #else
  238. ::operator delete(__args...);
  239. #endif
  240. }
  241. inline _LIBCPP_INLINE_VISIBILITY
  242. void *__libcpp_allocate(size_t __size, size_t __align) {
  243. #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
  244. if (__is_overaligned_for_new(__align)) {
  245. const align_val_t __align_val = static_cast<align_val_t>(__align);
  246. return __libcpp_operator_new(__size, __align_val);
  247. }
  248. #endif
  249. (void)__align;
  250. return __libcpp_operator_new(__size);
  251. }
  252. template <class ..._Args>
  253. _LIBCPP_INLINE_VISIBILITY
  254. void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
  255. #ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
  256. (void)__size;
  257. return std::__libcpp_operator_delete(__ptr, __args...);
  258. #else
  259. return std::__libcpp_operator_delete(__ptr, __size, __args...);
  260. #endif
  261. }
  262. inline _LIBCPP_INLINE_VISIBILITY
  263. void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
  264. #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
  265. (void)__align;
  266. return __do_deallocate_handle_size(__ptr, __size);
  267. #else
  268. if (__is_overaligned_for_new(__align)) {
  269. const align_val_t __align_val = static_cast<align_val_t>(__align);
  270. return __do_deallocate_handle_size(__ptr, __size, __align_val);
  271. } else {
  272. return __do_deallocate_handle_size(__ptr, __size);
  273. }
  274. #endif
  275. }
  276. inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
  277. #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
  278. (void)__align;
  279. return __libcpp_operator_delete(__ptr);
  280. #else
  281. if (__is_overaligned_for_new(__align)) {
  282. const align_val_t __align_val = static_cast<align_val_t>(__align);
  283. return __libcpp_operator_delete(__ptr, __align_val);
  284. } else {
  285. return __libcpp_operator_delete(__ptr);
  286. }
  287. #endif
  288. }
  289. template <class _Tp>
  290. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
  291. _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
  292. {
  293. static_assert (!(is_function<_Tp>::value), "can't launder functions" );
  294. static_assert (!(is_same<void, __remove_cv_t<_Tp> >::value), "can't launder cv-void" );
  295. return __builtin_launder(__p);
  296. }
  297. #if _LIBCPP_STD_VER >= 17
  298. template <class _Tp>
  299. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
  300. constexpr _Tp* launder(_Tp* __p) noexcept
  301. {
  302. return _VSTD::__launder(__p);
  303. }
  304. #endif
  305. #if _LIBCPP_STD_VER >= 17
  306. #if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
  307. inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
  308. inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
  309. #endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
  310. #endif // _LIBCPP_STD_VER >= 17
  311. _LIBCPP_END_NAMESPACE_STD
  312. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  313. # include <exception>
  314. # include <type_traits>
  315. #endif
  316. #endif // _LIBCPP_NEW