new 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 <__type_traits/is_function.h>
  79. #include <__type_traits/is_same.h>
  80. #include <__type_traits/remove_cv.h>
  81. #include <cstddef>
  82. #include <cstdlib>
  83. #include <exception>
  84. #include <version>
  85. #if defined(_LIBCPP_ABI_VCRUNTIME)
  86. #include <new.h>
  87. #endif
  88. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  89. # pragma GCC system_header
  90. #endif
  91. #if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
  92. #define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
  93. #endif
  94. #if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
  95. defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
  96. # define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  97. #endif
  98. #if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
  99. defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
  100. # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
  101. #endif
  102. namespace std // purposefully not using versioning namespace
  103. {
  104. #if !defined(_LIBCPP_ABI_VCRUNTIME)
  105. struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
  106. extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
  107. class _LIBCPP_EXCEPTION_ABI bad_alloc
  108. : public exception
  109. {
  110. public:
  111. bad_alloc() _NOEXCEPT;
  112. ~bad_alloc() _NOEXCEPT override;
  113. const char* what() const _NOEXCEPT override;
  114. };
  115. class _LIBCPP_EXCEPTION_ABI bad_array_new_length
  116. : public bad_alloc
  117. {
  118. public:
  119. bad_array_new_length() _NOEXCEPT;
  120. ~bad_array_new_length() _NOEXCEPT override;
  121. const char* what() const _NOEXCEPT override;
  122. };
  123. typedef void (*new_handler)();
  124. _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
  125. _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
  126. #elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
  127. // When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
  128. // since they would normally be provided in vcruntime_exception.h
  129. class bad_alloc : public exception {
  130. public:
  131. bad_alloc() noexcept : exception("bad allocation") {}
  132. private:
  133. friend class bad_array_new_length;
  134. bad_alloc(char const* const __message) noexcept : exception(__message) {}
  135. };
  136. class bad_array_new_length : public bad_alloc {
  137. public:
  138. bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
  139. };
  140. #endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
  141. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
  142. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  143. void __throw_bad_array_new_length()
  144. {
  145. #ifndef _LIBCPP_NO_EXCEPTIONS
  146. throw bad_array_new_length();
  147. #else
  148. _VSTD::abort();
  149. #endif
  150. }
  151. #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
  152. !defined(_LIBCPP_ABI_VCRUNTIME)
  153. #ifndef _LIBCPP_CXX03_LANG
  154. enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
  155. #else
  156. enum align_val_t { __zero = 0, __max = (size_t)-1 };
  157. #endif
  158. #endif
  159. #if _LIBCPP_STD_VER > 17
  160. // Enable the declaration even if the compiler doesn't support the language
  161. // feature.
  162. struct destroying_delete_t {
  163. explicit destroying_delete_t() = default;
  164. };
  165. inline constexpr destroying_delete_t destroying_delete{};
  166. #endif // _LIBCPP_STD_VER > 17
  167. } // namespace std
  168. #if defined(_LIBCPP_CXX03_LANG)
  169. #define _THROW_BAD_ALLOC throw(std::bad_alloc)
  170. #else
  171. #define _THROW_BAD_ALLOC
  172. #endif
  173. #if !defined(_LIBCPP_ABI_VCRUNTIME)
  174. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
  175. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
  176. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
  177. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
  178. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  179. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
  180. #endif
  181. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
  182. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
  183. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
  184. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
  185. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  186. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
  187. #endif
  188. #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
  189. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
  190. _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;
  191. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
  192. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
  193. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  194. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
  195. #endif
  196. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
  197. _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;
  198. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
  199. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
  200. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  201. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
  202. #endif
  203. #endif
  204. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
  205. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
  206. inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
  207. inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
  208. #endif // !_LIBCPP_ABI_VCRUNTIME
  209. _LIBCPP_BEGIN_NAMESPACE_STD
  210. _LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
  211. #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
  212. return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
  213. #else
  214. return __align > alignment_of<max_align_t>::value;
  215. #endif
  216. }
  217. template <class ..._Args>
  218. _LIBCPP_INLINE_VISIBILITY
  219. void* __libcpp_operator_new(_Args ...__args) {
  220. #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
  221. return __builtin_operator_new(__args...);
  222. #else
  223. return ::operator new(__args...);
  224. #endif
  225. }
  226. template <class ..._Args>
  227. _LIBCPP_INLINE_VISIBILITY
  228. void __libcpp_operator_delete(_Args ...__args) {
  229. #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
  230. __builtin_operator_delete(__args...);
  231. #else
  232. ::operator delete(__args...);
  233. #endif
  234. }
  235. inline _LIBCPP_INLINE_VISIBILITY
  236. void *__libcpp_allocate(size_t __size, size_t __align) {
  237. #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
  238. if (__is_overaligned_for_new(__align)) {
  239. const align_val_t __align_val = static_cast<align_val_t>(__align);
  240. return __libcpp_operator_new(__size, __align_val);
  241. }
  242. #endif
  243. (void)__align;
  244. return __libcpp_operator_new(__size);
  245. }
  246. template <class ..._Args>
  247. _LIBCPP_INLINE_VISIBILITY
  248. void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
  249. #ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
  250. (void)__size;
  251. return std::__libcpp_operator_delete(__ptr, __args...);
  252. #else
  253. return std::__libcpp_operator_delete(__ptr, __size, __args...);
  254. #endif
  255. }
  256. inline _LIBCPP_INLINE_VISIBILITY
  257. void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
  258. #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
  259. (void)__align;
  260. return __do_deallocate_handle_size(__ptr, __size);
  261. #else
  262. if (__is_overaligned_for_new(__align)) {
  263. const align_val_t __align_val = static_cast<align_val_t>(__align);
  264. return __do_deallocate_handle_size(__ptr, __size, __align_val);
  265. } else {
  266. return __do_deallocate_handle_size(__ptr, __size);
  267. }
  268. #endif
  269. }
  270. inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
  271. #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
  272. (void)__align;
  273. return __libcpp_operator_delete(__ptr);
  274. #else
  275. if (__is_overaligned_for_new(__align)) {
  276. const align_val_t __align_val = static_cast<align_val_t>(__align);
  277. return __libcpp_operator_delete(__ptr, __align_val);
  278. } else {
  279. return __libcpp_operator_delete(__ptr);
  280. }
  281. #endif
  282. }
  283. #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
  284. // Low-level helpers to call the aligned allocation and deallocation functions
  285. // on the target platform. This is used to implement libc++'s own memory
  286. // allocation routines -- if you need to allocate memory inside the library,
  287. // chances are that you want to use `__libcpp_allocate` instead.
  288. //
  289. // Returns the allocated memory, or `nullptr` on failure.
  290. inline _LIBCPP_INLINE_VISIBILITY void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
  291. # if defined(_LIBCPP_MSVCRT_LIKE)
  292. return ::_aligned_malloc(__size, __alignment);
  293. // Use posix_memalign instead of ::aligned_alloc to fix the musl and some of the tests
  294. # elif _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_C11_ALIGNED_ALLOC) && false
  295. // aligned_alloc() requires that __size is a multiple of __alignment,
  296. // but for C++ [new.delete.general], only states "if the value of an
  297. // alignment argument passed to any of these functions is not a valid
  298. // alignment value, the behavior is undefined".
  299. // To handle calls such as ::operator new(1, std::align_val_t(128)), we
  300. // round __size up to the next multiple of __alignment.
  301. size_t __rounded_size = (__size + __alignment - 1) & ~(__alignment - 1);
  302. // Rounding up could have wrapped around to zero, so we have to add another
  303. // max() ternary to the actual call site to avoid succeeded in that case.
  304. return ::aligned_alloc(__alignment, __size > __rounded_size ? __size : __rounded_size);
  305. # else
  306. void* __result = nullptr;
  307. (void)::posix_memalign(&__result, __alignment, __size);
  308. // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
  309. return __result;
  310. # endif
  311. }
  312. inline _LIBCPP_INLINE_VISIBILITY
  313. void __libcpp_aligned_free(void* __ptr) {
  314. #if defined(_LIBCPP_MSVCRT_LIKE)
  315. ::_aligned_free(__ptr);
  316. #else
  317. ::free(__ptr);
  318. #endif
  319. }
  320. #endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
  321. template <class _Tp>
  322. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
  323. _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
  324. {
  325. static_assert (!(is_function<_Tp>::value), "can't launder functions" );
  326. static_assert (!(is_same<void, __remove_cv_t<_Tp> >::value), "can't launder cv-void" );
  327. return __builtin_launder(__p);
  328. }
  329. #if _LIBCPP_STD_VER > 14
  330. template <class _Tp>
  331. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
  332. constexpr _Tp* launder(_Tp* __p) noexcept
  333. {
  334. return _VSTD::__launder(__p);
  335. }
  336. #endif
  337. #if _LIBCPP_STD_VER > 14
  338. #if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
  339. inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
  340. inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
  341. #endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
  342. #endif // _LIBCPP_STD_VER > 14
  343. _LIBCPP_END_NAMESPACE_STD
  344. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  345. # include <type_traits>
  346. #endif
  347. #endif // _LIBCPP_NEW