1// -*- C++ -*- 2//===----------------------------- new ------------------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 9//===----------------------------------------------------------------------===// 10 11#ifndef _LIBCPP_NEW 12#define _LIBCPP_NEW 13 14/* 15 new synopsis 16 17namespace std 18{ 19 20class bad_alloc 21 : public exception 22{ 23public: 24 bad_alloc() noexcept; 25 bad_alloc(const bad_alloc&) noexcept; 26 bad_alloc& operator=(const bad_alloc&) noexcept; 27 virtual const char* what() const noexcept; 28}; 29 30class bad_array_length : public bad_alloc // FIXME: Not part of C++ 31{ 32public: 33 bad_array_length() noexcept; 34}; 35 36class bad_array_new_length : public bad_alloc // C++14 37{ 38public: 39 bad_array_new_length() noexcept; 40}; 41 42enum class align_val_t : size_t {}; // C++17 43struct nothrow_t {}; 44extern const nothrow_t nothrow; 45typedef void (*new_handler)(); 46new_handler set_new_handler(new_handler new_p) noexcept; 47new_handler get_new_handler() noexcept; 48 49// 21.6.4, pointer optimization barrier 50template <class T> constexpr T* launder(T* p) noexcept; // C++17 51} // std 52 53void* operator new(std::size_t size); // replaceable, nodiscard in C++2a 54void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++2a 55void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a 56void* operator new(std::size_t size, std::align_val_t alignment, 57 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a 58void operator delete(void* ptr) noexcept; // replaceable 59void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14 60void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17 61void operator delete(void* ptr, std::size_t size, 62 std::align_val_t alignment) noexcept; // replaceable, C++17 63void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable 64void operator delete(void* ptr, std:align_val_t alignment, 65 const std::nothrow_t&) noexcept; // replaceable, C++17 66 67void* operator new[](std::size_t size); // replaceable, nodiscard in C++2a 68void* operator new[](std::size_t size, 69 std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++2a 70void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a 71void* operator new[](std::size_t size, std::align_val_t alignment, 72 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a 73void operator delete[](void* ptr) noexcept; // replaceable 74void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14 75void operator delete[](void* ptr, 76 std::align_val_t alignment) noexcept; // replaceable, C++17 77void operator delete[](void* ptr, std::size_t size, 78 std::align_val_t alignment) noexcept; // replaceable, C++17 79void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable 80void operator delete[](void* ptr, std::align_val_t alignment, 81 const std::nothrow_t&) noexcept; // replaceable, C++17 82 83void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++2a 84void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++2a 85void operator delete (void* ptr, void*) noexcept; 86void operator delete[](void* ptr, void*) noexcept; 87 88*/ 89 90#include <__config> 91#include <exception> 92#include <type_traits> 93#include <cstddef> 94#ifdef _LIBCPP_NO_EXCEPTIONS 95#include <cstdlib> 96#endif 97 98#if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_NO_VCRUNTIME) 99#include <new.h> 100#endif 101 102#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 103#pragma GCC system_header 104#endif 105 106#if !(defined(_LIBCPP_BUILDING_LIBRARY) || _LIBCPP_STD_VER >= 14 || \ 107 (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)) 108# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION 109#endif 110 111#if !__has_builtin(__builtin_operator_new) || \ 112 __has_builtin(__builtin_operator_new) < 201802L || \ 113 defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \ 114 !defined(__cpp_aligned_new) || __cpp_aligned_new < 201606 115#define _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE 116#endif 117 118namespace std // purposefully not using versioning namespace 119{ 120 121#if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME) 122struct _LIBCPP_TYPE_VIS nothrow_t {}; 123extern _LIBCPP_FUNC_VIS const nothrow_t nothrow; 124 125class _LIBCPP_EXCEPTION_ABI bad_alloc 126 : public exception 127{ 128public: 129 bad_alloc() _NOEXCEPT; 130 virtual ~bad_alloc() _NOEXCEPT; 131 virtual const char* what() const _NOEXCEPT; 132}; 133 134class _LIBCPP_EXCEPTION_ABI bad_array_new_length 135 : public bad_alloc 136{ 137public: 138 bad_array_new_length() _NOEXCEPT; 139 virtual ~bad_array_new_length() _NOEXCEPT; 140 virtual const char* what() const _NOEXCEPT; 141}; 142 143typedef void (*new_handler)(); 144_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT; 145_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT; 146 147#endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME 148 149_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec 150 151#if defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11) 152 153class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH 154 bad_array_length : public bad_alloc { 155public: 156 bad_array_length() _NOEXCEPT; 157 virtual ~bad_array_length() _NOEXCEPT; 158 virtual const char* what() const _NOEXCEPT; 159}; 160 161#define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED 162 163#endif // defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11) 164 165#if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME) 166#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || _LIBCPP_STD_VER > 14 167#ifndef _LIBCPP_CXX03_LANG 168enum class _LIBCPP_ENUM_VIS align_val_t : size_t { }; 169#else 170enum align_val_t { __zero = 0, __max = (size_t)-1 }; 171#endif 172#endif 173#endif 174 175} // std 176 177#if defined(_LIBCPP_CXX03_LANG) 178#define _THROW_BAD_ALLOC throw(std::bad_alloc) 179#else 180#define _THROW_BAD_ALLOC 181#endif 182 183#if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME) 184 185_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC; 186_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS; 187_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT; 188_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT; 189#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 190_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT; 191#endif 192 193_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC; 194_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS; 195_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT; 196_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT; 197#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 198_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT; 199#endif 200 201#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION 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 _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_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 210_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; 211_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS; 212_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT; 213_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT; 214#ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 215_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT; 216#endif 217#endif 218 219_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;} 220_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;} 221inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {} 222inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {} 223 224#endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME 225 226_LIBCPP_BEGIN_NAMESPACE_STD 227 228_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT { 229#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ 230 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__; 231#else 232 return __align > alignment_of<max_align_t>::value; 233#endif 234} 235 236inline _LIBCPP_INLINE_VISIBILITY 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# ifdef _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE 241 return ::operator new(__size, __align_val); 242# else 243 return __builtin_operator_new(__size, __align_val); 244# endif 245 } 246#else 247 ((void)__align); 248#endif 249#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE 250 return ::operator new(__size); 251#else 252 return __builtin_operator_new(__size); 253#endif 254} 255 256inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void* __ptr, size_t __align) { 257#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION 258 if (__is_overaligned_for_new(__align)) { 259 const align_val_t __align_val = static_cast<align_val_t>(__align); 260# ifdef _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE 261 return ::operator delete(__ptr, __align_val); 262# else 263 return __builtin_operator_delete(__ptr, __align_val); 264# endif 265 } 266#else 267 ((void)__align); 268#endif 269#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE 270 return ::operator delete(__ptr); 271#else 272 return __builtin_operator_delete(__ptr); 273#endif 274} 275 276#ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED 277_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY 278#ifndef _LIBCPP_NO_EXCEPTIONS 279_LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH 280#endif 281void __throw_bad_array_length() 282{ 283#ifndef _LIBCPP_NO_EXCEPTIONS 284 throw bad_array_length(); 285#else 286 _VSTD::abort(); 287#endif 288} 289#endif 290 291template <class _Tp> 292_LIBCPP_NODISCARD_AFTER_CXX17 inline 293_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT 294{ 295 static_assert (!(is_function<_Tp>::value), "can't launder functions" ); 296 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" ); 297#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER 298 return __builtin_launder(__p); 299#else 300 return __p; 301#endif 302} 303 304 305#if _LIBCPP_STD_VER > 14 306template <class _Tp> 307_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY 308constexpr _Tp* launder(_Tp* __p) noexcept 309{ 310 return _VSTD::__launder(__p); 311} 312#endif 313 314_LIBCPP_END_NAMESPACE_STD 315 316#endif // _LIBCPP_NEW 317