327 lines · plain
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03_NEW11#define _LIBCPP___CXX03_NEW12 13/*14 new synopsis15 16namespace std17{18 19class bad_alloc20 : public exception21{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++1430{31public:32 bad_array_new_length() noexcept;33};34 35enum class align_val_t : size_t {}; // C++1736 37struct destroying_delete_t { // C++2038 explicit destroying_delete_t() = default;39};40inline constexpr destroying_delete_t destroying_delete{}; // C++2041 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 barrier49template <class T> [[nodiscard]] constexpr T* launder(T* p) noexcept; // C++17, nodiscard since C++2050} // std51 52void* operator new(std::size_t size); // replaceable, nodiscard in C++2053void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++2054void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2055void* operator new(std::size_t size, std::align_val_t alignment,56 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2057void operator delete(void* ptr) noexcept; // replaceable58void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++1459void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++1760void operator delete(void* ptr, std::size_t size,61 std::align_val_t alignment) noexcept; // replaceable, C++1762void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable63void operator delete(void* ptr, std:align_val_t alignment,64 const std::nothrow_t&) noexcept; // replaceable, C++1765 66void* operator new[](std::size_t size); // replaceable, nodiscard in C++2067void* operator new[](std::size_t size,68 std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++2069void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2070void* operator new[](std::size_t size, std::align_val_t alignment,71 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2072void operator delete[](void* ptr) noexcept; // replaceable73void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++1474void operator delete[](void* ptr,75 std::align_val_t alignment) noexcept; // replaceable, C++1776void operator delete[](void* ptr, std::size_t size,77 std::align_val_t alignment) noexcept; // replaceable, C++1778void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable79void operator delete[](void* ptr, std::align_val_t alignment,80 const std::nothrow_t&) noexcept; // replaceable, C++1781 82void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++2083void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++2084void operator delete (void* ptr, void*) noexcept;85void operator delete[](void* ptr, void*) noexcept;86 87*/88 89#include <__cxx03/__config>90#include <__cxx03/__exception/exception.h>91#include <__cxx03/__type_traits/is_function.h>92#include <__cxx03/__type_traits/is_same.h>93#include <__cxx03/__type_traits/remove_cv.h>94#include <__cxx03/__verbose_abort>95#include <__cxx03/cstddef>96#include <__cxx03/version>97 98#if defined(_LIBCPP_ABI_VCRUNTIME)99# include <__cxx03/new.h>100#endif101 102#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)103# pragma GCC system_header104#endif105 106#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L107# define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION108#endif109 110#if !defined(_LIBCPP_BUILDING_LIBRARY) && defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)111# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION112#endif113 114#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)115# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION116#endif117 118namespace std // purposefully not using versioning namespace119{120 121#if !defined(_LIBCPP_ABI_VCRUNTIME)122struct _LIBCPP_EXPORTED_FROM_ABI nothrow_t {123 explicit nothrow_t() = default;124};125extern _LIBCPP_EXPORTED_FROM_ABI const nothrow_t nothrow;126 127class _LIBCPP_EXPORTED_FROM_ABI bad_alloc : public exception {128public:129 bad_alloc() _NOEXCEPT;130 _LIBCPP_HIDE_FROM_ABI bad_alloc(const bad_alloc&) _NOEXCEPT = default;131 _LIBCPP_HIDE_FROM_ABI bad_alloc& operator=(const bad_alloc&) _NOEXCEPT = default;132 ~bad_alloc() _NOEXCEPT override;133 const char* what() const _NOEXCEPT override;134};135 136class _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length : public bad_alloc {137public:138 bad_array_new_length() _NOEXCEPT;139 _LIBCPP_HIDE_FROM_ABI bad_array_new_length(const bad_array_new_length&) _NOEXCEPT = default;140 _LIBCPP_HIDE_FROM_ABI bad_array_new_length& operator=(const bad_array_new_length&) _NOEXCEPT = default;141 ~bad_array_new_length() _NOEXCEPT override;142 const char* what() const _NOEXCEPT override;143};144 145typedef void (*new_handler)();146_LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;147_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;148 149#elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME150 151// When _HAS_EXCEPTIONS == 0, these complete definitions are needed,152// since they would normally be provided in vcruntime_exception.h153class bad_alloc : public exception {154public:155 bad_alloc() noexcept : exception("bad allocation") {}156 157private:158 friend class bad_array_new_length;159 160 bad_alloc(char const* const __message) noexcept : exception(__message) {}161};162 163class bad_array_new_length : public bad_alloc {164public:165 bad_array_new_length() noexcept : bad_alloc("bad array new length") {}166};167#endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0168 169_LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_bad_alloc(); // not in C++ spec170 171_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_array_new_length() {172#ifndef _LIBCPP_HAS_NO_EXCEPTIONS173 throw bad_array_new_length();174#else175 _LIBCPP_VERBOSE_ABORT("bad_array_new_length was thrown in -fno-exceptions mode");176#endif177}178 179#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && !defined(_LIBCPP_ABI_VCRUNTIME)180enum align_val_t { __zero = 0, __max = (size_t)-1 };181#endif182} // namespace std183 184#define _THROW_BAD_ALLOC throw(std::bad_alloc)185 186#if !defined(_LIBCPP_ABI_VCRUNTIME)187 188_LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;189_LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT190 _LIBCPP_NOALIAS;191_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;192_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;193# ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION194_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;195# endif196 197_LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;198_LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT199 _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_DEALLOCATION203_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;204# endif205 206# ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION207_LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;208_LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void*209operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;210_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;211_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;212# ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION213_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;214# endif215 216_LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void*217operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;218_LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void*219operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;220_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;221_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;222# ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION223_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;224# endif225# endif226 227_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI void* operator new(std::size_t, void* __p) _NOEXCEPT { return __p; }228_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI void* operator new[](std::size_t, void* __p) _NOEXCEPT { return __p; }229inline _LIBCPP_HIDE_FROM_ABI void operator delete(void*, void*) _NOEXCEPT {}230inline _LIBCPP_HIDE_FROM_ABI void operator delete[](void*, void*) _NOEXCEPT {}231 232#endif // !_LIBCPP_ABI_VCRUNTIME233 234_LIBCPP_BEGIN_NAMESPACE_STD235 236inline _LIBCPP_HIDE_FROM_ABI bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {237#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__238 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;239#else240 return __align > _LIBCPP_ALIGNOF(max_align_t);241#endif242}243 244template <class... _Args>245_LIBCPP_HIDE_FROM_ABI void* __libcpp_operator_new(_Args... __args) {246#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)247 return __builtin_operator_new(__args...);248#else249 return ::operator new(__args...);250#endif251}252 253template <class... _Args>254_LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) {255#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)256 __builtin_operator_delete(__args...);257#else258 ::operator delete(__args...);259#endif260}261 262inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_allocate(size_t __size, size_t __align) {263#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION264 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#endif269 270 (void)__align;271 return __libcpp_operator_new(__size);272}273 274template <class... _Args>275_LIBCPP_HIDE_FROM_ABI void __do_deallocate_handle_size(void* __ptr, size_t __size, _Args... __args) {276#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION277 (void)__size;278 return std::__libcpp_operator_delete(__ptr, __args...);279#else280 return std::__libcpp_operator_delete(__ptr, __size, __args...);281#endif282}283 284inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {285#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)286 (void)__align;287 return __do_deallocate_handle_size(__ptr, __size);288#else289 if (__is_overaligned_for_new(__align)) {290 const align_val_t __align_val = static_cast<align_val_t>(__align);291 return __do_deallocate_handle_size(__ptr, __size, __align_val);292 } else {293 return __do_deallocate_handle_size(__ptr, __size);294 }295#endif296}297 298inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {299#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)300 (void)__align;301 return __libcpp_operator_delete(__ptr);302#else303 if (__is_overaligned_for_new(__align)) {304 const align_val_t __align_val = static_cast<align_val_t>(__align);305 return __libcpp_operator_delete(__ptr, __align_val);306 } else {307 return __libcpp_operator_delete(__ptr);308 }309#endif310}311 312template <class _Tp>313_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _Tp* __launder(_Tp* __p) _NOEXCEPT {314 static_assert(!(is_function<_Tp>::value), "can't launder functions");315 static_assert(!(is_same<void, __remove_cv_t<_Tp> >::value), "can't launder cv-void");316 return __builtin_launder(__p);317}318 319_LIBCPP_END_NAMESPACE_STD320 321#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)322# include <__cxx03/cstdlib>323# include <__cxx03/type_traits>324#endif325 326#endif // _LIBCPP___CXX03_NEW327