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 36struct nothrow_t {}; 37extern const nothrow_t nothrow; 38typedef void (*new_handler)(); 39new_handler set_new_handler(new_handler new_p) noexcept; 40new_handler get_new_handler() noexcept; 41 42// 21.6.4, pointer optimization barrier 43template <class T> constexpr T* launder(T* p) noexcept; // C++17 44} // std 45 46void* operator new(std::size_t size); // replaceable, nodiscard in C++2a 47void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++2a 48void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a 49void* operator new(std::size_t size, std::align_val_t alignment, 50 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a 51void operator delete(void* ptr) noexcept; // replaceable 52void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14 53void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17 54void operator delete(void* ptr, std::size_t size, 55 std::align_val_t alignment) noexcept; // replaceable, C++17 56void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable 57void operator delete(void* ptr, std:align_val_t alignment, 58 const std::nothrow_t&) noexcept; // replaceable, C++17 59 60void* operator new[](std::size_t size); // replaceable, nodiscard in C++2a 61void* operator new[](std::size_t size, 62 std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++2a 63void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a 64void* operator new[](std::size_t size, std::align_val_t alignment, 65 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a 66void operator delete[](void* ptr) noexcept; // replaceable 67void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14 68void operator delete[](void* ptr, 69 std::align_val_t alignment) noexcept; // replaceable, C++17 70void operator delete[](void* ptr, std::size_t size, 71 std::align_val_t alignment) noexcept; // replaceable, C++17 72void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable 73void operator delete[](void* ptr, std::align_val_t alignment, 74 const std::nothrow_t&) noexcept; // replaceable, C++17 75 76void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++2a 77void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++2a 78void operator delete (void* ptr, void*) noexcept; 79void operator delete[](void* ptr, void*) noexcept; 80 81*/ 82 83#include <__config> 84#include <exception> 85#include <type_traits> 86#include <cstddef> 87#include <version> 88#ifdef _LIBCPP_NO_EXCEPTIONS 89#include <cstdlib> 90#endif 91 92#if defined(_LIBCPP_ABI_VCRUNTIME) 93#include <new.h> 94#endif 95 96#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 97#pragma GCC system_header 98#endif 99 100#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L 101#define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION 102#endif 103 104#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \ 105 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION) 106# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 107#endif 108 109#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \ 110 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION) 111# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION 112#endif 113 114#if !__has_builtin(__builtin_operator_new) || \ 115 __has_builtin(__builtin_operator_new) < 201802L 116#define _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE 117#endif 118 119namespace std // purposefully not using versioning namespace 120{ 121 122#if !defined(_LIBCPP_ABI_VCRUNTIME) 123struct _LIBCPP_TYPE_VIS nothrow_t {}; 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} // std 162 163#if defined(_LIBCPP_CXX03_LANG) 164#define _THROW_BAD_ALLOC throw(std::bad_alloc) 165#else 166#define _THROW_BAD_ALLOC 167#endif 168 169#if !defined(_LIBCPP_ABI_VCRUNTIME) 170 171_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC; 172_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; 173_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT; 174_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT; 175#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 176_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT; 177#endif 178 179_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC; 180_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; 181_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT; 182_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, 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) _NOEXCEPT; 185#endif 186 187#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION 188_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; 189_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; 190_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT; 191_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, 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, std::align_val_t) _NOEXCEPT; 194#endif 195 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 205_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;} 206_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;} 207inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {} 208inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {} 209 210#endif // !_LIBCPP_ABI_VCRUNTIME 211 212_LIBCPP_BEGIN_NAMESPACE_STD 213 214_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT { 215#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ 216 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__; 217#else 218 return __align > alignment_of<max_align_t>::value; 219#endif 220} 221 222inline _LIBCPP_INLINE_VISIBILITY void *__libcpp_allocate(size_t __size, size_t __align) { 223#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION 224 if (__is_overaligned_for_new(__align)) { 225 const align_val_t __align_val = static_cast<align_val_t>(__align); 226# ifdef _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE 227 return ::operator new(__size, __align_val); 228# else 229 return __builtin_operator_new(__size, __align_val); 230# endif 231 } 232#else 233 ((void)__align); 234#endif 235#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE 236 return ::operator new(__size); 237#else 238 return __builtin_operator_new(__size); 239#endif 240} 241 242struct _DeallocateCaller { 243 static inline _LIBCPP_INLINE_VISIBILITY 244 void __do_deallocate_handle_size_align(void *__ptr, size_t __size, size_t __align) { 245#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) 246 ((void)__align); 247 return __do_deallocate_handle_size(__ptr, __size); 248#else 249 if (__is_overaligned_for_new(__align)) { 250 const align_val_t __align_val = static_cast<align_val_t>(__align); 251 return __do_deallocate_handle_size(__ptr, __size, __align_val); 252 } else { 253 return __do_deallocate_handle_size(__ptr, __size); 254 } 255#endif 256 } 257 258 static inline _LIBCPP_INLINE_VISIBILITY 259 void __do_deallocate_handle_align(void *__ptr, size_t __align) { 260#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) 261 ((void)__align); 262 return __do_call(__ptr); 263#else 264 if (__is_overaligned_for_new(__align)) { 265 const align_val_t __align_val = static_cast<align_val_t>(__align); 266 return __do_call(__ptr, __align_val); 267 } else { 268 return __do_call(__ptr); 269 } 270#endif 271 } 272 273 private: 274 static inline void __do_deallocate_handle_size(void *__ptr, size_t __size) { 275#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 276 ((void)__size); 277 return __do_call(__ptr); 278#else 279 return __do_call(__ptr, __size); 280#endif 281 } 282 283#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION 284 static inline void __do_deallocate_handle_size(void *__ptr, size_t __size, align_val_t __align) { 285#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 286 ((void)__size); 287 return __do_call(__ptr, __align); 288#else 289 return __do_call(__ptr, __size, __align); 290#endif 291 } 292#endif 293 294private: 295 template <class _A1, class _A2> 296 static inline void __do_call(void *__ptr, _A1 __a1, _A2 __a2) { 297#if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \ 298 defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE) 299 return ::operator delete(__ptr, __a1, __a2); 300#else 301 return __builtin_operator_delete(__ptr, __a1, __a2); 302#endif 303 } 304 305 template <class _A1> 306 static inline void __do_call(void *__ptr, _A1 __a1) { 307#if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \ 308 defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE) 309 return ::operator delete(__ptr, __a1); 310#else 311 return __builtin_operator_delete(__ptr, __a1); 312#endif 313 } 314 315 static inline void __do_call(void *__ptr) { 316#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE 317 return ::operator delete(__ptr); 318#else 319 return __builtin_operator_delete(__ptr); 320#endif 321 } 322}; 323 324inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) { 325 _DeallocateCaller::__do_deallocate_handle_size_align(__ptr, __size, __align); 326} 327 328inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) { 329 _DeallocateCaller::__do_deallocate_handle_align(__ptr, __align); 330} 331 332template <class _Tp> 333_LIBCPP_NODISCARD_AFTER_CXX17 inline 334_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT 335{ 336 static_assert (!(is_function<_Tp>::value), "can't launder functions" ); 337 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" ); 338#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER 339 return __builtin_launder(__p); 340#else 341 return __p; 342#endif 343} 344 345 346#if _LIBCPP_STD_VER > 14 347template <class _Tp> 348_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY 349constexpr _Tp* launder(_Tp* __p) noexcept 350{ 351 return _VSTD::__launder(__p); 352} 353#endif 354 355_LIBCPP_END_NAMESPACE_STD 356 357#endif // _LIBCPP_NEW 358