1// -*- C++ -*- 2//===----------------------------- new ------------------------------------===// 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 10#ifndef _LIBCPP_NEW 11#define _LIBCPP_NEW 12 13/* 14 new synopsis 15 16namespace std 17{ 18 19class bad_alloc 20 : public exception 21{ 22public: 23 bad_alloc() noexcept; 24 bad_alloc(const bad_alloc&) noexcept; 25 bad_alloc& operator=(const bad_alloc&) noexcept; 26 virtual const char* what() const noexcept; 27}; 28 29class bad_array_new_length : public bad_alloc // C++14 30{ 31public: 32 bad_array_new_length() noexcept; 33}; 34 35enum class align_val_t : size_t {}; // C++17 36 37struct destroying_delete_t { // C++20 38 explicit destroying_delete_t() = default; 39}; 40inline constexpr destroying_delete_t destroying_delete{}; // C++20 41 42struct nothrow_t { explicit nothrow_t() = default; }; 43extern const nothrow_t nothrow; 44typedef void (*new_handler)(); 45new_handler set_new_handler(new_handler new_p) noexcept; 46new_handler get_new_handler() noexcept; 47 48// 21.6.4, pointer optimization barrier 49template <class T> constexpr T* launder(T* p) noexcept; // C++17 50} // std 51 52void* operator new(std::size_t size); // replaceable, nodiscard in C++20 53void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++20 54void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20 55void* operator new(std::size_t size, std::align_val_t alignment, 56 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20 57void operator delete(void* ptr) noexcept; // replaceable 58void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14 59void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17 60void operator delete(void* ptr, std::size_t size, 61 std::align_val_t alignment) noexcept; // replaceable, C++17 62void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable 63void operator delete(void* ptr, std:align_val_t alignment, 64 const std::nothrow_t&) noexcept; // replaceable, C++17 65 66void* operator new[](std::size_t size); // replaceable, nodiscard in C++20 67void* operator new[](std::size_t size, 68 std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++20 69void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20 70void* operator new[](std::size_t size, std::align_val_t alignment, 71 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20 72void operator delete[](void* ptr) noexcept; // replaceable 73void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14 74void operator delete[](void* ptr, 75 std::align_val_t alignment) noexcept; // replaceable, C++17 76void operator delete[](void* ptr, std::size_t size, 77 std::align_val_t alignment) noexcept; // replaceable, C++17 78void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable 79void operator delete[](void* ptr, std::align_val_t alignment, 80 const std::nothrow_t&) noexcept; // replaceable, C++17 81 82void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++20 83void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++20 84void operator delete (void* ptr, void*) noexcept; 85void operator delete[](void* ptr, void*) noexcept; 86 87*/ 88 89#include <__config> 90#include <__availability> 91#include <cstddef> 92#include <cstdlib> 93#include <exception> 94#include <type_traits> 95#include <version> 96 97#if defined(_LIBCPP_ABI_VCRUNTIME) 98#include <new.h> 99#endif 100 101#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 102#pragma GCC system_header 103#endif 104 105#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L 106#define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION 107#endif 108 109#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \ 110 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION) 111# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 112#endif 113 114#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \ 115 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION) 116# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION 117#endif 118 119namespace std // purposefully not using versioning namespace 120{ 121 122#if !defined(_LIBCPP_ABI_VCRUNTIME) 123struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; }; 124extern _LIBCPP_FUNC_VIS const nothrow_t nothrow; 125 126class _LIBCPP_EXCEPTION_ABI bad_alloc 127 : public exception 128{ 129public: 130 bad_alloc() _NOEXCEPT; 131 virtual ~bad_alloc() _NOEXCEPT; 132 virtual const char* what() const _NOEXCEPT; 133}; 134 135class _LIBCPP_EXCEPTION_ABI bad_array_new_length 136 : public bad_alloc 137{ 138public: 139 bad_array_new_length() _NOEXCEPT; 140 virtual ~bad_array_new_length() _NOEXCEPT; 141 virtual const char* what() const _NOEXCEPT; 142}; 143 144typedef void (*new_handler)(); 145_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT; 146_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT; 147 148#endif // !_LIBCPP_ABI_VCRUNTIME 149 150_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec 151 152#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \ 153 !defined(_LIBCPP_ABI_VCRUNTIME) 154#ifndef _LIBCPP_CXX03_LANG 155enum class _LIBCPP_ENUM_VIS align_val_t : size_t { }; 156#else 157enum align_val_t { __zero = 0, __max = (size_t)-1 }; 158#endif 159#endif 160 161#if _LIBCPP_STD_VER > 17 162// Enable the declaration even if the compiler doesn't support the language 163// feature. 164struct destroying_delete_t { 165 explicit destroying_delete_t() = default; 166}; 167_LIBCPP_INLINE_VAR constexpr destroying_delete_t destroying_delete{}; 168#endif // _LIBCPP_STD_VER > 17 169 170} // std 171 172#if defined(_LIBCPP_CXX03_LANG) 173#define _THROW_BAD_ALLOC throw(std::bad_alloc) 174#else 175#define _THROW_BAD_ALLOC 176#endif 177 178#if !defined(_LIBCPP_ABI_VCRUNTIME) 179 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 188_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC; 189_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; 190_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT; 191_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT; 192#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 193_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT; 194#endif 195 196#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION 197_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; 198_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; 199_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT; 200_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT; 201#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 202_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT; 203#endif 204 205_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; 206_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; 207_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT; 208_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT; 209#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 210_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT; 211#endif 212#endif 213 214_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;} 215_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;} 216inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {} 217inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {} 218 219#endif // !_LIBCPP_ABI_VCRUNTIME 220 221_LIBCPP_BEGIN_NAMESPACE_STD 222 223_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT { 224#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ 225 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__; 226#else 227 return __align > alignment_of<max_align_t>::value; 228#endif 229} 230 231template <class ..._Args> 232_LIBCPP_INLINE_VISIBILITY 233void* __libcpp_operator_new(_Args ...__args) { 234#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete) 235 return __builtin_operator_new(__args...); 236#else 237 return ::operator new(__args...); 238#endif 239} 240 241template <class ..._Args> 242_LIBCPP_INLINE_VISIBILITY 243void __libcpp_operator_delete(_Args ...__args) { 244#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete) 245 __builtin_operator_delete(__args...); 246#else 247 ::operator delete(__args...); 248#endif 249} 250 251inline _LIBCPP_INLINE_VISIBILITY 252void *__libcpp_allocate(size_t __size, size_t __align) { 253#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION 254 if (__is_overaligned_for_new(__align)) { 255 const align_val_t __align_val = static_cast<align_val_t>(__align); 256 return __libcpp_operator_new(__size, __align_val); 257 } 258#endif 259 260 (void)__align; 261 return __libcpp_operator_new(__size); 262} 263 264template <class ..._Args> 265_LIBCPP_INLINE_VISIBILITY 266void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) { 267#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 268 (void)__size; 269 return __libcpp_operator_delete(__ptr, __args...); 270#else 271 return __libcpp_operator_delete(__ptr, __size, __args...); 272#endif 273} 274 275inline _LIBCPP_INLINE_VISIBILITY 276void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) { 277#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) 278 (void)__align; 279 return __do_deallocate_handle_size(__ptr, __size); 280#else 281 if (__is_overaligned_for_new(__align)) { 282 const align_val_t __align_val = static_cast<align_val_t>(__align); 283 return __do_deallocate_handle_size(__ptr, __size, __align_val); 284 } else { 285 return __do_deallocate_handle_size(__ptr, __size); 286 } 287#endif 288} 289 290inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) { 291#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) 292 (void)__align; 293 return __libcpp_operator_delete(__ptr); 294#else 295 if (__is_overaligned_for_new(__align)) { 296 const align_val_t __align_val = static_cast<align_val_t>(__align); 297 return __libcpp_operator_delete(__ptr, __align_val); 298 } else { 299 return __libcpp_operator_delete(__ptr); 300 } 301#endif 302} 303 304#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) 305// Low-level helpers to call the aligned allocation and deallocation functions 306// on the target platform. This is used to implement libc++'s own memory 307// allocation routines -- if you need to allocate memory inside the library, 308// chances are that you want to use `__libcpp_allocate` instead. 309// 310// Returns the allocated memory, or `nullptr` on failure. 311inline _LIBCPP_INLINE_VISIBILITY 312void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) { 313#if defined(_LIBCPP_MSVCRT_LIKE) 314 return ::_aligned_malloc(__size, __alignment); 315#else 316 void* __result = nullptr; 317 ::posix_memalign(&__result, __alignment, __size); 318 // If posix_memalign fails, __result is unmodified so we still return `nullptr`. 319 return __result; 320#endif 321} 322 323inline _LIBCPP_INLINE_VISIBILITY 324void __libcpp_aligned_free(void* __ptr) { 325#if defined(_LIBCPP_MSVCRT_LIKE) 326 ::_aligned_free(__ptr); 327#else 328 ::free(__ptr); 329#endif 330} 331#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION 332 333 334template <class _Tp> 335_LIBCPP_NODISCARD_AFTER_CXX17 inline 336_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT 337{ 338 static_assert (!(is_function<_Tp>::value), "can't launder functions" ); 339 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" ); 340#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER 341 return __builtin_launder(__p); 342#else 343 return __p; 344#endif 345} 346 347 348#if _LIBCPP_STD_VER > 14 349template <class _Tp> 350_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY 351constexpr _Tp* launder(_Tp* __p) noexcept 352{ 353 return _VSTD::__launder(__p); 354} 355#endif 356 357_LIBCPP_END_NAMESPACE_STD 358 359#endif // _LIBCPP_NEW 360