10b57cec5SDimitry Andric// -*- C++ -*- 2349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 30b57cec5SDimitry Andric// 40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 70b57cec5SDimitry Andric// 80b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 90b57cec5SDimitry Andric 100b57cec5SDimitry Andric#ifndef _LIBCPP_NEW 110b57cec5SDimitry Andric#define _LIBCPP_NEW 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric/* 140b57cec5SDimitry Andric new synopsis 150b57cec5SDimitry Andric 160b57cec5SDimitry Andricnamespace std 170b57cec5SDimitry Andric{ 180b57cec5SDimitry Andric 190b57cec5SDimitry Andricclass bad_alloc 200b57cec5SDimitry Andric : public exception 210b57cec5SDimitry Andric{ 220b57cec5SDimitry Andricpublic: 230b57cec5SDimitry Andric bad_alloc() noexcept; 240b57cec5SDimitry Andric bad_alloc(const bad_alloc&) noexcept; 250b57cec5SDimitry Andric bad_alloc& operator=(const bad_alloc&) noexcept; 260b57cec5SDimitry Andric virtual const char* what() const noexcept; 270b57cec5SDimitry Andric}; 280b57cec5SDimitry Andric 290b57cec5SDimitry Andricclass bad_array_new_length : public bad_alloc // C++14 300b57cec5SDimitry Andric{ 310b57cec5SDimitry Andricpublic: 320b57cec5SDimitry Andric bad_array_new_length() noexcept; 330b57cec5SDimitry Andric}; 340b57cec5SDimitry Andric 350b57cec5SDimitry Andricenum class align_val_t : size_t {}; // C++17 360b57cec5SDimitry Andric 370b57cec5SDimitry Andricstruct destroying_delete_t { // C++20 380b57cec5SDimitry Andric explicit destroying_delete_t() = default; 390b57cec5SDimitry Andric}; 400b57cec5SDimitry Andricinline constexpr destroying_delete_t destroying_delete{}; // C++20 410b57cec5SDimitry Andric 42e40139ffSDimitry Andricstruct nothrow_t { explicit nothrow_t() = default; }; 430b57cec5SDimitry Andricextern const nothrow_t nothrow; 440b57cec5SDimitry Andrictypedef void (*new_handler)(); 450b57cec5SDimitry Andricnew_handler set_new_handler(new_handler new_p) noexcept; 460b57cec5SDimitry Andricnew_handler get_new_handler() noexcept; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric// 21.6.4, pointer optimization barrier 495f757f3fSDimitry Andrictemplate <class T> [[nodiscard]] constexpr T* launder(T* p) noexcept; // C++17, nodiscard since C++20 500b57cec5SDimitry Andric} // std 510b57cec5SDimitry Andric 52e8d8bef9SDimitry Andricvoid* operator new(std::size_t size); // replaceable, nodiscard in C++20 53e8d8bef9SDimitry Andricvoid* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++20 54e8d8bef9SDimitry Andricvoid* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20 550b57cec5SDimitry Andricvoid* operator new(std::size_t size, std::align_val_t alignment, 56e8d8bef9SDimitry Andric const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20 570b57cec5SDimitry Andricvoid operator delete(void* ptr) noexcept; // replaceable 580b57cec5SDimitry Andricvoid operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14 590b57cec5SDimitry Andricvoid operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17 600b57cec5SDimitry Andricvoid operator delete(void* ptr, std::size_t size, 610b57cec5SDimitry Andric std::align_val_t alignment) noexcept; // replaceable, C++17 620b57cec5SDimitry Andricvoid operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable 630b57cec5SDimitry Andricvoid operator delete(void* ptr, std:align_val_t alignment, 640b57cec5SDimitry Andric const std::nothrow_t&) noexcept; // replaceable, C++17 650b57cec5SDimitry Andric 66e8d8bef9SDimitry Andricvoid* operator new[](std::size_t size); // replaceable, nodiscard in C++20 670b57cec5SDimitry Andricvoid* operator new[](std::size_t size, 68e8d8bef9SDimitry Andric std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++20 69e8d8bef9SDimitry Andricvoid* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20 700b57cec5SDimitry Andricvoid* operator new[](std::size_t size, std::align_val_t alignment, 71e8d8bef9SDimitry Andric const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20 720b57cec5SDimitry Andricvoid operator delete[](void* ptr) noexcept; // replaceable 730b57cec5SDimitry Andricvoid operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14 740b57cec5SDimitry Andricvoid operator delete[](void* ptr, 750b57cec5SDimitry Andric std::align_val_t alignment) noexcept; // replaceable, C++17 760b57cec5SDimitry Andricvoid operator delete[](void* ptr, std::size_t size, 770b57cec5SDimitry Andric std::align_val_t alignment) noexcept; // replaceable, C++17 780b57cec5SDimitry Andricvoid operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable 790b57cec5SDimitry Andricvoid operator delete[](void* ptr, std::align_val_t alignment, 800b57cec5SDimitry Andric const std::nothrow_t&) noexcept; // replaceable, C++17 810b57cec5SDimitry Andric 82e8d8bef9SDimitry Andricvoid* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++20 83e8d8bef9SDimitry Andricvoid* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++20 840b57cec5SDimitry Andricvoid operator delete (void* ptr, void*) noexcept; 850b57cec5SDimitry Andricvoid operator delete[](void* ptr, void*) noexcept; 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric*/ 880b57cec5SDimitry Andric 8981ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler 90e8d8bef9SDimitry Andric#include <__availability> 91fe6060f1SDimitry Andric#include <__config> 9206c3fb27SDimitry Andric#include <__exception/exception.h> 93bdd1243dSDimitry Andric#include <__type_traits/is_function.h> 94bdd1243dSDimitry Andric#include <__type_traits/is_same.h> 95bdd1243dSDimitry Andric#include <__type_traits/remove_cv.h> 96e8d8bef9SDimitry Andric#include <cstddef> 970b57cec5SDimitry Andric#include <version> 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric#if defined(_LIBCPP_ABI_VCRUNTIME) 1000b57cec5SDimitry Andric# include <new.h> 1010b57cec5SDimitry Andric#endif 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 1040b57cec5SDimitry Andric# pragma GCC system_header 1050b57cec5SDimitry Andric#endif 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L 1080b57cec5SDimitry Andric# define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION 1090b57cec5SDimitry Andric#endif 1100b57cec5SDimitry Andric 111*cb14a3feSDimitry Andric#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION) 1120b57cec5SDimitry Andric# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 1130b57cec5SDimitry Andric#endif 1140b57cec5SDimitry Andric 115*cb14a3feSDimitry Andric#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION) 1160b57cec5SDimitry Andric# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION 1170b57cec5SDimitry Andric#endif 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andricnamespace std // purposefully not using versioning namespace 1200b57cec5SDimitry Andric{ 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric#if !defined(_LIBCPP_ABI_VCRUNTIME) 123*cb14a3feSDimitry Andricstruct _LIBCPP_EXPORTED_FROM_ABI nothrow_t { 124*cb14a3feSDimitry Andric explicit nothrow_t() = default; 125*cb14a3feSDimitry Andric}; 12606c3fb27SDimitry Andricextern _LIBCPP_EXPORTED_FROM_ABI const nothrow_t nothrow; 1270b57cec5SDimitry Andric 128*cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI bad_alloc : public exception { 1290b57cec5SDimitry Andricpublic: 1300b57cec5SDimitry Andric bad_alloc() _NOEXCEPT; 1315f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI bad_alloc(const bad_alloc&) _NOEXCEPT = default; 1325f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI bad_alloc& operator=(const bad_alloc&) _NOEXCEPT = default; 133bdd1243dSDimitry Andric ~bad_alloc() _NOEXCEPT override; 134bdd1243dSDimitry Andric const char* what() const _NOEXCEPT override; 1350b57cec5SDimitry Andric}; 1360b57cec5SDimitry Andric 137*cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length : public bad_alloc { 1380b57cec5SDimitry Andricpublic: 1390b57cec5SDimitry Andric bad_array_new_length() _NOEXCEPT; 1405f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI bad_array_new_length(const bad_array_new_length&) _NOEXCEPT = default; 1415f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI bad_array_new_length& operator=(const bad_array_new_length&) _NOEXCEPT = default; 142bdd1243dSDimitry Andric ~bad_array_new_length() _NOEXCEPT override; 143bdd1243dSDimitry Andric const char* what() const _NOEXCEPT override; 1440b57cec5SDimitry Andric}; 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andrictypedef void (*new_handler)(); 14706c3fb27SDimitry Andric_LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT; 14806c3fb27SDimitry Andric_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT; 1490b57cec5SDimitry Andric 150bdd1243dSDimitry Andric#elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME 151bdd1243dSDimitry Andric 152bdd1243dSDimitry Andric// When _HAS_EXCEPTIONS == 0, these complete definitions are needed, 153bdd1243dSDimitry Andric// since they would normally be provided in vcruntime_exception.h 154bdd1243dSDimitry Andricclass bad_alloc : public exception { 155bdd1243dSDimitry Andricpublic: 156bdd1243dSDimitry Andric bad_alloc() noexcept : exception("bad allocation") {} 157bdd1243dSDimitry Andric 158bdd1243dSDimitry Andricprivate: 159bdd1243dSDimitry Andric friend class bad_array_new_length; 160bdd1243dSDimitry Andric 161bdd1243dSDimitry Andric bad_alloc(char const* const __message) noexcept : exception(__message) {} 162bdd1243dSDimitry Andric}; 163bdd1243dSDimitry Andric 164bdd1243dSDimitry Andricclass bad_array_new_length : public bad_alloc { 165bdd1243dSDimitry Andricpublic: 166bdd1243dSDimitry Andric bad_array_new_length() noexcept : bad_alloc("bad array new length") {} 167bdd1243dSDimitry Andric}; 168bdd1243dSDimitry Andric#endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 1690b57cec5SDimitry Andric 17006c3fb27SDimitry Andric_LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_bad_alloc(); // not in C++ spec 1710b57cec5SDimitry Andric 172*cb14a3feSDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_array_new_length() { 17306c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 174349cc55cSDimitry Andric throw bad_array_new_length(); 175349cc55cSDimitry Andric#else 17606c3fb27SDimitry Andric _LIBCPP_VERBOSE_ABORT("bad_array_new_length was thrown in -fno-exceptions mode"); 177349cc55cSDimitry Andric#endif 178349cc55cSDimitry Andric} 179349cc55cSDimitry Andric 180*cb14a3feSDimitry Andric#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && !defined(_LIBCPP_ABI_VCRUNTIME) 1810b57cec5SDimitry Andric# ifndef _LIBCPP_CXX03_LANG 1825f757f3fSDimitry Andricenum class align_val_t : size_t {}; 1830b57cec5SDimitry Andric# else 1840b57cec5SDimitry Andricenum align_val_t { __zero = 0, __max = (size_t)-1 }; 1850b57cec5SDimitry Andric# endif 1860b57cec5SDimitry Andric#endif 1870b57cec5SDimitry Andric 18806c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20 1890b57cec5SDimitry Andric// Enable the declaration even if the compiler doesn't support the language 1900b57cec5SDimitry Andric// feature. 1910b57cec5SDimitry Andricstruct destroying_delete_t { 1920b57cec5SDimitry Andric explicit destroying_delete_t() = default; 1930b57cec5SDimitry Andric}; 194349cc55cSDimitry Andricinline constexpr destroying_delete_t destroying_delete{}; 19506c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 20 1960b57cec5SDimitry Andric 1970eae32dcSDimitry Andric} // namespace std 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric#if defined(_LIBCPP_CXX03_LANG) 2000b57cec5SDimitry Andric# define _THROW_BAD_ALLOC throw(std::bad_alloc) 2010b57cec5SDimitry Andric#else 2020b57cec5SDimitry Andric# define _THROW_BAD_ALLOC 2030b57cec5SDimitry Andric#endif 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric#if !defined(_LIBCPP_ABI_VCRUNTIME) 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC; 208*cb14a3feSDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* 209*cb14a3feSDimitry Andricoperator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; 2100b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT; 2110b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT; 2120b57cec5SDimitry Andric# ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 2135f757f3fSDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT; 2140b57cec5SDimitry Andric# endif 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC; 217*cb14a3feSDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* 218*cb14a3feSDimitry Andricoperator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; 2190b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT; 2200b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT; 2210b57cec5SDimitry Andric# ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 2225f757f3fSDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT; 2230b57cec5SDimitry Andric# endif 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric# ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION 226*cb14a3feSDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* 227*cb14a3feSDimitry Andricoperator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; 228*cb14a3feSDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* 229*cb14a3feSDimitry Andricoperator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; 2300b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT; 2310b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT; 2320b57cec5SDimitry Andric# ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 2335f757f3fSDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT; 2340b57cec5SDimitry Andric# endif 2350b57cec5SDimitry Andric 236*cb14a3feSDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* 237*cb14a3feSDimitry Andricoperator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; 238*cb14a3feSDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* 239*cb14a3feSDimitry Andricoperator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; 2400b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT; 2410b57cec5SDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT; 2420b57cec5SDimitry Andric# ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION 2435f757f3fSDimitry Andric_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT; 2440b57cec5SDimitry Andric# endif 2450b57cec5SDimitry Andric# endif 2460b57cec5SDimitry Andric 247*cb14a3feSDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI void* operator new(std::size_t, void* __p) _NOEXCEPT { 248*cb14a3feSDimitry Andric return __p; 249*cb14a3feSDimitry Andric} 250*cb14a3feSDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI void* operator new[](std::size_t, void* __p) _NOEXCEPT { 251*cb14a3feSDimitry Andric return __p; 252*cb14a3feSDimitry Andric} 2535f757f3fSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void operator delete(void*, void*) _NOEXCEPT {} 2545f757f3fSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void operator delete[](void*, void*) _NOEXCEPT {} 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric#endif // !_LIBCPP_ABI_VCRUNTIME 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 2590b57cec5SDimitry Andric 2605f757f3fSDimitry Andric_LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI bool __is_overaligned_for_new(size_t __align) _NOEXCEPT { 2610b57cec5SDimitry Andric#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ 2620b57cec5SDimitry Andric return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__; 2630b57cec5SDimitry Andric#else 2645f757f3fSDimitry Andric return __align > _LIBCPP_ALIGNOF(max_align_t); 2650b57cec5SDimitry Andric#endif 2660b57cec5SDimitry Andric} 2670b57cec5SDimitry Andric 268e8d8bef9SDimitry Andrictemplate <class... _Args> 269*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI void* __libcpp_operator_new(_Args... __args) { 270e8d8bef9SDimitry Andric#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete) 271e8d8bef9SDimitry Andric return __builtin_operator_new(__args...); 2720b57cec5SDimitry Andric#else 273e8d8bef9SDimitry Andric return ::operator new(__args...); 2740b57cec5SDimitry Andric#endif 2750b57cec5SDimitry Andric} 2760b57cec5SDimitry Andric 277e8d8bef9SDimitry Andrictemplate <class... _Args> 278*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) { 279e8d8bef9SDimitry Andric#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete) 280e8d8bef9SDimitry Andric __builtin_operator_delete(__args...); 281e8d8bef9SDimitry Andric#else 282e8d8bef9SDimitry Andric ::operator delete(__args...); 283e8d8bef9SDimitry Andric#endif 284e8d8bef9SDimitry Andric} 285e8d8bef9SDimitry Andric 286*cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void* __libcpp_allocate(size_t __size, size_t __align) { 287e8d8bef9SDimitry Andric#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION 288e8d8bef9SDimitry Andric if (__is_overaligned_for_new(__align)) { 289e8d8bef9SDimitry Andric const align_val_t __align_val = static_cast<align_val_t>(__align); 290e8d8bef9SDimitry Andric return __libcpp_operator_new(__size, __align_val); 291e8d8bef9SDimitry Andric } 292e8d8bef9SDimitry Andric#endif 293e8d8bef9SDimitry Andric 294e8d8bef9SDimitry Andric (void)__align; 295e8d8bef9SDimitry Andric return __libcpp_operator_new(__size); 296e8d8bef9SDimitry Andric} 297e8d8bef9SDimitry Andric 298e8d8bef9SDimitry Andrictemplate <class... _Args> 299*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI void __do_deallocate_handle_size(void* __ptr, size_t __size, _Args... __args) { 300e8d8bef9SDimitry Andric#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION 301e8d8bef9SDimitry Andric (void)__size; 302bdd1243dSDimitry Andric return std::__libcpp_operator_delete(__ptr, __args...); 303e8d8bef9SDimitry Andric#else 304bdd1243dSDimitry Andric return std::__libcpp_operator_delete(__ptr, __size, __args...); 305e8d8bef9SDimitry Andric#endif 306e8d8bef9SDimitry Andric} 307e8d8bef9SDimitry Andric 308*cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) { 3090b57cec5SDimitry Andric#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) 310e8d8bef9SDimitry Andric (void)__align; 3110b57cec5SDimitry Andric return __do_deallocate_handle_size(__ptr, __size); 3120b57cec5SDimitry Andric#else 3130b57cec5SDimitry Andric if (__is_overaligned_for_new(__align)) { 3140b57cec5SDimitry Andric const align_val_t __align_val = static_cast<align_val_t>(__align); 3150b57cec5SDimitry Andric return __do_deallocate_handle_size(__ptr, __size, __align_val); 3160b57cec5SDimitry Andric } else { 3170b57cec5SDimitry Andric return __do_deallocate_handle_size(__ptr, __size); 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric#endif 3200b57cec5SDimitry Andric} 3210b57cec5SDimitry Andric 3225f757f3fSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate_unsized(void* __ptr, size_t __align) { 3230b57cec5SDimitry Andric#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) 324e8d8bef9SDimitry Andric (void)__align; 325e8d8bef9SDimitry Andric return __libcpp_operator_delete(__ptr); 3260b57cec5SDimitry Andric#else 3270b57cec5SDimitry Andric if (__is_overaligned_for_new(__align)) { 3280b57cec5SDimitry Andric const align_val_t __align_val = static_cast<align_val_t>(__align); 329e8d8bef9SDimitry Andric return __libcpp_operator_delete(__ptr, __align_val); 3300b57cec5SDimitry Andric } else { 331e8d8bef9SDimitry Andric return __libcpp_operator_delete(__ptr); 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric#endif 3340b57cec5SDimitry Andric} 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andrictemplate <class _Tp> 337*cb14a3feSDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT { 3380b57cec5SDimitry Andric static_assert(!(is_function<_Tp>::value), "can't launder functions"); 339bdd1243dSDimitry Andric static_assert(!(is_same<void, __remove_cv_t<_Tp> >::value), "can't launder cv-void"); 3400b57cec5SDimitry Andric return __builtin_launder(__p); 3410b57cec5SDimitry Andric} 3420b57cec5SDimitry Andric 34306c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17 3440b57cec5SDimitry Andrictemplate <class _Tp> 345*cb14a3feSDimitry Andric_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp* launder(_Tp* __p) noexcept { 3465f757f3fSDimitry Andric return std::__launder(__p); 3470b57cec5SDimitry Andric} 3480b57cec5SDimitry Andric#endif 3490b57cec5SDimitry Andric 35006c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17 35181ad6265SDimitry Andric 35281ad6265SDimitry Andric# if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE) 35381ad6265SDimitry Andric 35481ad6265SDimitry Andricinline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE; 35581ad6265SDimitry Andricinline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE; 35681ad6265SDimitry Andric 35781ad6265SDimitry Andric# endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE) 35881ad6265SDimitry Andric 35906c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 17 36081ad6265SDimitry Andric 3610b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 3620b57cec5SDimitry Andric 363bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 3645f757f3fSDimitry Andric# include <cstdlib> 36506c3fb27SDimitry Andric# include <exception> 366bdd1243dSDimitry Andric# include <type_traits> 367bdd1243dSDimitry Andric#endif 368bdd1243dSDimitry Andric 3690b57cec5SDimitry Andric#endif // _LIBCPP_NEW 370