new 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <__availability>
  76. #include <__config>
  77. #include <cstddef>
  78. #include <cstdlib>
  79. #include <exception>
  80. #include <type_traits>
  81. #include <version>
  82. #if defined(_LIBCPP_ABI_VCRUNTIME)
  83. #include <new.h>
  84. #endif
  85. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  86. # pragma GCC system_header
  87. #endif
  88. #if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
  89. #define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
  90. #endif
  91. #if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
  92. defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
  93. # define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  94. #endif
  95. #if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
  96. defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
  97. # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
  98. #endif
  99. namespace std // purposefully not using versioning namespace
  100. {
  101. #if !defined(_LIBCPP_ABI_VCRUNTIME)
  102. struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
  103. extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
  104. class _LIBCPP_EXCEPTION_ABI bad_alloc
  105. : public exception
  106. {
  107. public:
  108. bad_alloc() _NOEXCEPT;
  109. virtual ~bad_alloc() _NOEXCEPT;
  110. virtual const char* what() const _NOEXCEPT;
  111. };
  112. class _LIBCPP_EXCEPTION_ABI bad_array_new_length
  113. : public bad_alloc
  114. {
  115. public:
  116. bad_array_new_length() _NOEXCEPT;
  117. virtual ~bad_array_new_length() _NOEXCEPT;
  118. virtual const char* what() const _NOEXCEPT;
  119. };
  120. typedef void (*new_handler)();
  121. _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
  122. _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
  123. #endif // !_LIBCPP_ABI_VCRUNTIME
  124. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
  125. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  126. void __throw_bad_array_new_length()
  127. {
  128. #ifndef _LIBCPP_NO_EXCEPTIONS
  129. throw bad_array_new_length();
  130. #else
  131. _VSTD::abort();
  132. #endif
  133. }
  134. #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
  135. !defined(_LIBCPP_ABI_VCRUNTIME)
  136. #ifndef _LIBCPP_CXX03_LANG
  137. enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
  138. #else
  139. enum align_val_t { __zero = 0, __max = (size_t)-1 };
  140. #endif
  141. #endif
  142. #if _LIBCPP_STD_VER > 17
  143. // Enable the declaration even if the compiler doesn't support the language
  144. // feature.
  145. struct destroying_delete_t {
  146. explicit destroying_delete_t() = default;
  147. };
  148. inline constexpr destroying_delete_t destroying_delete{};
  149. #endif // _LIBCPP_STD_VER > 17
  150. } // namespace std
  151. #if defined(_LIBCPP_CXX03_LANG)
  152. #define _THROW_BAD_ALLOC throw(std::bad_alloc)
  153. #else
  154. #define _THROW_BAD_ALLOC
  155. #endif
  156. #if !defined(_LIBCPP_ABI_VCRUNTIME)
  157. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
  158. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
  159. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
  160. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
  161. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  162. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
  163. #endif
  164. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
  165. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
  166. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
  167. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
  168. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  169. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
  170. #endif
  171. #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
  172. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
  173. _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;
  174. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
  175. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
  176. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  177. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
  178. #endif
  179. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
  180. _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;
  181. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
  182. _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
  183. #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
  184. _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
  185. #endif
  186. #endif
  187. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
  188. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
  189. inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
  190. inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
  191. #endif // !_LIBCPP_ABI_VCRUNTIME
  192. _LIBCPP_BEGIN_NAMESPACE_STD
  193. _LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
  194. #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
  195. return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
  196. #else
  197. return __align > alignment_of<max_align_t>::value;
  198. #endif
  199. }
  200. template <class ..._Args>
  201. _LIBCPP_INLINE_VISIBILITY
  202. void* __libcpp_operator_new(_Args ...__args) {
  203. #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
  204. return __builtin_operator_new(__args...);
  205. #else
  206. return ::operator new(__args...);
  207. #endif
  208. }
  209. template <class ..._Args>
  210. _LIBCPP_INLINE_VISIBILITY
  211. void __libcpp_operator_delete(_Args ...__args) {
  212. #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
  213. __builtin_operator_delete(__args...);
  214. #else
  215. ::operator delete(__args...);
  216. #endif
  217. }
  218. inline _LIBCPP_INLINE_VISIBILITY
  219. void *__libcpp_allocate(size_t __size, size_t __align) {
  220. #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
  221. if (__is_overaligned_for_new(__align)) {
  222. const align_val_t __align_val = static_cast<align_val_t>(__align);
  223. return __libcpp_operator_new(__size, __align_val);
  224. }
  225. #endif
  226. (void)__align;
  227. return __libcpp_operator_new(__size);
  228. }
  229. template <class ..._Args>
  230. _LIBCPP_INLINE_VISIBILITY
  231. void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
  232. #ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
  233. (void)__size;
  234. return __libcpp_operator_delete(__ptr, __args...);
  235. #else
  236. return __libcpp_operator_delete(__ptr, __size, __args...);
  237. #endif
  238. }
  239. inline _LIBCPP_INLINE_VISIBILITY
  240. void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
  241. #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
  242. (void)__align;
  243. return __do_deallocate_handle_size(__ptr, __size);
  244. #else
  245. if (__is_overaligned_for_new(__align)) {
  246. const align_val_t __align_val = static_cast<align_val_t>(__align);
  247. return __do_deallocate_handle_size(__ptr, __size, __align_val);
  248. } else {
  249. return __do_deallocate_handle_size(__ptr, __size);
  250. }
  251. #endif
  252. }
  253. inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
  254. #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
  255. (void)__align;
  256. return __libcpp_operator_delete(__ptr);
  257. #else
  258. if (__is_overaligned_for_new(__align)) {
  259. const align_val_t __align_val = static_cast<align_val_t>(__align);
  260. return __libcpp_operator_delete(__ptr, __align_val);
  261. } else {
  262. return __libcpp_operator_delete(__ptr);
  263. }
  264. #endif
  265. }
  266. #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
  267. // Low-level helpers to call the aligned allocation and deallocation functions
  268. // on the target platform. This is used to implement libc++'s own memory
  269. // allocation routines -- if you need to allocate memory inside the library,
  270. // chances are that you want to use `__libcpp_allocate` instead.
  271. //
  272. // Returns the allocated memory, or `nullptr` on failure.
  273. inline _LIBCPP_INLINE_VISIBILITY
  274. void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
  275. #if defined(_LIBCPP_MSVCRT_LIKE)
  276. return ::_aligned_malloc(__size, __alignment);
  277. #else
  278. void* __result = nullptr;
  279. (void)::posix_memalign(&__result, __alignment, __size);
  280. // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
  281. return __result;
  282. #endif
  283. }
  284. inline _LIBCPP_INLINE_VISIBILITY
  285. void __libcpp_aligned_free(void* __ptr) {
  286. #if defined(_LIBCPP_MSVCRT_LIKE)
  287. ::_aligned_free(__ptr);
  288. #else
  289. ::free(__ptr);
  290. #endif
  291. }
  292. #endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
  293. template <class _Tp>
  294. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
  295. _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
  296. {
  297. static_assert (!(is_function<_Tp>::value), "can't launder functions" );
  298. static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
  299. return __builtin_launder(__p);
  300. }
  301. #if _LIBCPP_STD_VER > 14
  302. template <class _Tp>
  303. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
  304. constexpr _Tp* launder(_Tp* __p) noexcept
  305. {
  306. return _VSTD::__launder(__p);
  307. }
  308. #endif
  309. _LIBCPP_END_NAMESPACE_STD
  310. #endif // _LIBCPP_NEW