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 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 <__availability> 90#include <__config> 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_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY 153void __throw_bad_array_new_length() 154{ 155#ifndef _LIBCPP_NO_EXCEPTIONS 156 throw bad_array_new_length(); 157#else 158 _VSTD::abort(); 159#endif 160} 161 162#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \ 163 !defined(_LIBCPP_ABI_VCRUNTIME) 164#ifndef _LIBCPP_CXX03_LANG 165enum class _LIBCPP_ENUM_VIS align_val_t : size_t { }; 166#else 167enum align_val_t { __zero = 0, __max = (size_t)-1 }; 168#endif 169#endif 170 171#if _LIBCPP_STD_VER > 17 172// Enable the declaration even if the compiler doesn't support the language 173// feature. 174struct destroying_delete_t { 175 explicit destroying_delete_t() = default; 176}; 177inline constexpr destroying_delete_t destroying_delete{}; 178#endif // _LIBCPP_STD_VER > 17 179 180} // namespace std 181 182#if defined(_LIBCPP_CXX03_LANG) 183#define _THROW_BAD_ALLOC throw(std::bad_alloc) 184#else 185#define _THROW_BAD_ALLOC 186#endif 187 188#if !defined(_LIBCPP_ABI_VCRUNTIME) 189 190_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC; 191_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; 192_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT; 193_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT; 194#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 195_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT; 196#endif 197 198_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC; 199_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; 200_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT; 201_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT; 202#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 203_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT; 204#endif 205 206#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION 207_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; 208_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; 209_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT; 210_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT; 211#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 212_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT; 213#endif 214 215_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; 216_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; 217_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT; 218_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT; 219#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 220_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT; 221#endif 222#endif 223 224_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;} 225_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;} 226inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {} 227inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {} 228 229#endif // !_LIBCPP_ABI_VCRUNTIME 230 231_LIBCPP_BEGIN_NAMESPACE_STD 232 233_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT { 234#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ 235 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__; 236#else 237 return __align > alignment_of<max_align_t>::value; 238#endif 239} 240 241template <class ..._Args> 242_LIBCPP_INLINE_VISIBILITY 243void* __libcpp_operator_new(_Args ...__args) { 244#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete) 245 return __builtin_operator_new(__args...); 246#else 247 return ::operator new(__args...); 248#endif 249} 250 251template <class ..._Args> 252_LIBCPP_INLINE_VISIBILITY 253void __libcpp_operator_delete(_Args ...__args) { 254#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete) 255 __builtin_operator_delete(__args...); 256#else 257 ::operator delete(__args...); 258#endif 259} 260 261inline _LIBCPP_INLINE_VISIBILITY 262void *__libcpp_allocate(size_t __size, size_t __align) { 263#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION 264 if (__is_overaligned_for_new(__align)) { 265 const align_val_t __align_val = static_cast<align_val_t>(__align); 266 return __libcpp_operator_new(__size, __align_val); 267 } 268#endif 269 270 (void)__align; 271 return __libcpp_operator_new(__size); 272} 273 274template <class ..._Args> 275_LIBCPP_INLINE_VISIBILITY 276void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) { 277#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 278 (void)__size; 279 return __libcpp_operator_delete(__ptr, __args...); 280#else 281 return __libcpp_operator_delete(__ptr, __size, __args...); 282#endif 283} 284 285inline _LIBCPP_INLINE_VISIBILITY 286void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) { 287#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) 288 (void)__align; 289 return __do_deallocate_handle_size(__ptr, __size); 290#else 291 if (__is_overaligned_for_new(__align)) { 292 const align_val_t __align_val = static_cast<align_val_t>(__align); 293 return __do_deallocate_handle_size(__ptr, __size, __align_val); 294 } else { 295 return __do_deallocate_handle_size(__ptr, __size); 296 } 297#endif 298} 299 300inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) { 301#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) 302 (void)__align; 303 return __libcpp_operator_delete(__ptr); 304#else 305 if (__is_overaligned_for_new(__align)) { 306 const align_val_t __align_val = static_cast<align_val_t>(__align); 307 return __libcpp_operator_delete(__ptr, __align_val); 308 } else { 309 return __libcpp_operator_delete(__ptr); 310 } 311#endif 312} 313 314#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) 315// Low-level helpers to call the aligned allocation and deallocation functions 316// on the target platform. This is used to implement libc++'s own memory 317// allocation routines -- if you need to allocate memory inside the library, 318// chances are that you want to use `__libcpp_allocate` instead. 319// 320// Returns the allocated memory, or `nullptr` on failure. 321inline _LIBCPP_INLINE_VISIBILITY 322void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) { 323#if defined(_LIBCPP_MSVCRT_LIKE) 324 return ::_aligned_malloc(__size, __alignment); 325#else 326 void* __result = nullptr; 327 (void)::posix_memalign(&__result, __alignment, __size); 328 // If posix_memalign fails, __result is unmodified so we still return `nullptr`. 329 return __result; 330#endif 331} 332 333inline _LIBCPP_INLINE_VISIBILITY 334void __libcpp_aligned_free(void* __ptr) { 335#if defined(_LIBCPP_MSVCRT_LIKE) 336 ::_aligned_free(__ptr); 337#else 338 ::free(__ptr); 339#endif 340} 341#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION 342 343 344template <class _Tp> 345_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI 346_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT 347{ 348 static_assert (!(is_function<_Tp>::value), "can't launder functions" ); 349 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" ); 350 return __builtin_launder(__p); 351} 352 353#if _LIBCPP_STD_VER > 14 354template <class _Tp> 355_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI 356constexpr _Tp* launder(_Tp* __p) noexcept 357{ 358 return _VSTD::__launder(__p); 359} 360#endif 361 362_LIBCPP_END_NAMESPACE_STD 363 364#endif // _LIBCPP_NEW 365