Warning, /include/c++/v1/__cxx03/new is written in an unsupported language. File is not indexed.
0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009
0010 #ifndef _LIBCPP___CXX03_NEW
0011 #define _LIBCPP___CXX03_NEW
0012
0013 /*
0014 new synopsis
0015
0016 namespace std
0017 {
0018
0019 class bad_alloc
0020 : public exception
0021 {
0022 public:
0023 bad_alloc() noexcept;
0024 bad_alloc(const bad_alloc&) noexcept;
0025 bad_alloc& operator=(const bad_alloc&) noexcept;
0026 virtual const char* what() const noexcept;
0027 };
0028
0029 class bad_array_new_length : public bad_alloc // C++14
0030 {
0031 public:
0032 bad_array_new_length() noexcept;
0033 };
0034
0035 enum class align_val_t : size_t {}; // C++17
0036
0037 struct destroying_delete_t { // C++20
0038 explicit destroying_delete_t() = default;
0039 };
0040 inline constexpr destroying_delete_t destroying_delete{}; // C++20
0041
0042 struct nothrow_t { explicit nothrow_t() = default; };
0043 extern const nothrow_t nothrow;
0044 typedef void (*new_handler)();
0045 new_handler set_new_handler(new_handler new_p) noexcept;
0046 new_handler get_new_handler() noexcept;
0047
0048 // 21.6.4, pointer optimization barrier
0049 template <class T> [[nodiscard]] constexpr T* launder(T* p) noexcept; // C++17, nodiscard since C++20
0050 } // std
0051
0052 void* operator new(std::size_t size); // replaceable, nodiscard in C++20
0053 void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++20
0054 void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
0055 void* operator new(std::size_t size, std::align_val_t alignment,
0056 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
0057 void operator delete(void* ptr) noexcept; // replaceable
0058 void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
0059 void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17
0060 void operator delete(void* ptr, std::size_t size,
0061 std::align_val_t alignment) noexcept; // replaceable, C++17
0062 void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
0063 void operator delete(void* ptr, std:align_val_t alignment,
0064 const std::nothrow_t&) noexcept; // replaceable, C++17
0065
0066 void* operator new[](std::size_t size); // replaceable, nodiscard in C++20
0067 void* operator new[](std::size_t size,
0068 std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++20
0069 void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
0070 void* operator new[](std::size_t size, std::align_val_t alignment,
0071 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
0072 void operator delete[](void* ptr) noexcept; // replaceable
0073 void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
0074 void operator delete[](void* ptr,
0075 std::align_val_t alignment) noexcept; // replaceable, C++17
0076 void operator delete[](void* ptr, std::size_t size,
0077 std::align_val_t alignment) noexcept; // replaceable, C++17
0078 void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
0079 void operator delete[](void* ptr, std::align_val_t alignment,
0080 const std::nothrow_t&) noexcept; // replaceable, C++17
0081
0082 void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++20
0083 void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++20
0084 void operator delete (void* ptr, void*) noexcept;
0085 void operator delete[](void* ptr, void*) noexcept;
0086
0087 */
0088
0089 #include <__cxx03/__config>
0090 #include <__cxx03/__exception/exception.h>
0091 #include <__cxx03/__type_traits/is_function.h>
0092 #include <__cxx03/__type_traits/is_same.h>
0093 #include <__cxx03/__type_traits/remove_cv.h>
0094 #include <__cxx03/__verbose_abort>
0095 #include <__cxx03/cstddef>
0096 #include <__cxx03/version>
0097
0098 #if defined(_LIBCPP_ABI_VCRUNTIME)
0099 # include <__cxx03/new.h>
0100 #endif
0101
0102 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0103 # pragma GCC system_header
0104 #endif
0105
0106 #if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
0107 # define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
0108 #endif
0109
0110 #if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
0111 # define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
0112 #endif
0113
0114 #if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
0115 # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
0116 #endif
0117
0118 namespace std // purposefully not using versioning namespace
0119 {
0120
0121 #if !defined(_LIBCPP_ABI_VCRUNTIME)
0122 struct _LIBCPP_EXPORTED_FROM_ABI nothrow_t {
0123 explicit nothrow_t() = default;
0124 };
0125 extern _LIBCPP_EXPORTED_FROM_ABI const nothrow_t nothrow;
0126
0127 class _LIBCPP_EXPORTED_FROM_ABI bad_alloc : public exception {
0128 public:
0129 bad_alloc() _NOEXCEPT;
0130 _LIBCPP_HIDE_FROM_ABI bad_alloc(const bad_alloc&) _NOEXCEPT = default;
0131 _LIBCPP_HIDE_FROM_ABI bad_alloc& operator=(const bad_alloc&) _NOEXCEPT = default;
0132 ~bad_alloc() _NOEXCEPT override;
0133 const char* what() const _NOEXCEPT override;
0134 };
0135
0136 class _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length : public bad_alloc {
0137 public:
0138 bad_array_new_length() _NOEXCEPT;
0139 _LIBCPP_HIDE_FROM_ABI bad_array_new_length(const bad_array_new_length&) _NOEXCEPT = default;
0140 _LIBCPP_HIDE_FROM_ABI bad_array_new_length& operator=(const bad_array_new_length&) _NOEXCEPT = default;
0141 ~bad_array_new_length() _NOEXCEPT override;
0142 const char* what() const _NOEXCEPT override;
0143 };
0144
0145 typedef void (*new_handler)();
0146 _LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
0147 _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
0148
0149 #elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
0150
0151 // When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
0152 // since they would normally be provided in vcruntime_exception.h
0153 class bad_alloc : public exception {
0154 public:
0155 bad_alloc() noexcept : exception("bad allocation") {}
0156
0157 private:
0158 friend class bad_array_new_length;
0159
0160 bad_alloc(char const* const __message) noexcept : exception(__message) {}
0161 };
0162
0163 class bad_array_new_length : public bad_alloc {
0164 public:
0165 bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
0166 };
0167 #endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
0168
0169 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_bad_alloc(); // not in C++ spec
0170
0171 _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_array_new_length() {
0172 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0173 throw bad_array_new_length();
0174 #else
0175 _LIBCPP_VERBOSE_ABORT("bad_array_new_length was thrown in -fno-exceptions mode");
0176 #endif
0177 }
0178
0179 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && !defined(_LIBCPP_ABI_VCRUNTIME)
0180 # ifndef _LIBCPP_CXX03_LANG
0181 enum class align_val_t : size_t {};
0182 # else
0183 enum align_val_t { __zero = 0, __max = (size_t)-1 };
0184 # endif
0185 #endif
0186
0187 #if _LIBCPP_STD_VER >= 20
0188 // Enable the declaration even if the compiler doesn't support the language
0189 // feature.
0190 struct destroying_delete_t {
0191 explicit destroying_delete_t() = default;
0192 };
0193 inline constexpr destroying_delete_t destroying_delete{};
0194 #endif // _LIBCPP_STD_VER >= 20
0195
0196 } // namespace std
0197
0198 #if defined(_LIBCPP_CXX03_LANG)
0199 # define _THROW_BAD_ALLOC throw(std::bad_alloc)
0200 #else
0201 # define _THROW_BAD_ALLOC
0202 #endif
0203
0204 #if !defined(_LIBCPP_ABI_VCRUNTIME)
0205
0206 _LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
0207 _LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT
0208 _LIBCPP_NOALIAS;
0209 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
0210 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
0211 # ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
0212 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
0213 # endif
0214
0215 _LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
0216 _LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT
0217 _LIBCPP_NOALIAS;
0218 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
0219 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
0220 # ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
0221 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
0222 # endif
0223
0224 # ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
0225 _LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
0226 _LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void*
0227 operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
0228 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
0229 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
0230 # ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
0231 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
0232 # endif
0233
0234 _LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void*
0235 operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
0236 _LIBCPP_NODISCARD _LIBCPP_OVERRIDABLE_FUNC_VIS void*
0237 operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
0238 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
0239 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
0240 # ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
0241 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
0242 # endif
0243 # endif
0244
0245 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI void* operator new(std::size_t, void* __p) _NOEXCEPT { return __p; }
0246 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI void* operator new[](std::size_t, void* __p) _NOEXCEPT { return __p; }
0247 inline _LIBCPP_HIDE_FROM_ABI void operator delete(void*, void*) _NOEXCEPT {}
0248 inline _LIBCPP_HIDE_FROM_ABI void operator delete[](void*, void*) _NOEXCEPT {}
0249
0250 #endif // !_LIBCPP_ABI_VCRUNTIME
0251
0252 _LIBCPP_BEGIN_NAMESPACE_STD
0253
0254 _LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
0255 #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
0256 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
0257 #else
0258 return __align > _LIBCPP_ALIGNOF(max_align_t);
0259 #endif
0260 }
0261
0262 template <class... _Args>
0263 _LIBCPP_HIDE_FROM_ABI void* __libcpp_operator_new(_Args... __args) {
0264 #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
0265 return __builtin_operator_new(__args...);
0266 #else
0267 return ::operator new(__args...);
0268 #endif
0269 }
0270
0271 template <class... _Args>
0272 _LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) {
0273 #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
0274 __builtin_operator_delete(__args...);
0275 #else
0276 ::operator delete(__args...);
0277 #endif
0278 }
0279
0280 inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_allocate(size_t __size, size_t __align) {
0281 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
0282 if (__is_overaligned_for_new(__align)) {
0283 const align_val_t __align_val = static_cast<align_val_t>(__align);
0284 return __libcpp_operator_new(__size, __align_val);
0285 }
0286 #endif
0287
0288 (void)__align;
0289 return __libcpp_operator_new(__size);
0290 }
0291
0292 template <class... _Args>
0293 _LIBCPP_HIDE_FROM_ABI void __do_deallocate_handle_size(void* __ptr, size_t __size, _Args... __args) {
0294 #ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
0295 (void)__size;
0296 return std::__libcpp_operator_delete(__ptr, __args...);
0297 #else
0298 return std::__libcpp_operator_delete(__ptr, __size, __args...);
0299 #endif
0300 }
0301
0302 inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
0303 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
0304 (void)__align;
0305 return __do_deallocate_handle_size(__ptr, __size);
0306 #else
0307 if (__is_overaligned_for_new(__align)) {
0308 const align_val_t __align_val = static_cast<align_val_t>(__align);
0309 return __do_deallocate_handle_size(__ptr, __size, __align_val);
0310 } else {
0311 return __do_deallocate_handle_size(__ptr, __size);
0312 }
0313 #endif
0314 }
0315
0316 inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
0317 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
0318 (void)__align;
0319 return __libcpp_operator_delete(__ptr);
0320 #else
0321 if (__is_overaligned_for_new(__align)) {
0322 const align_val_t __align_val = static_cast<align_val_t>(__align);
0323 return __libcpp_operator_delete(__ptr, __align_val);
0324 } else {
0325 return __libcpp_operator_delete(__ptr);
0326 }
0327 #endif
0328 }
0329
0330 template <class _Tp>
0331 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT {
0332 static_assert(!(is_function<_Tp>::value), "can't launder functions");
0333 static_assert(!(is_same<void, __remove_cv_t<_Tp> >::value), "can't launder cv-void");
0334 return __builtin_launder(__p);
0335 }
0336
0337 #if _LIBCPP_STD_VER >= 17
0338 template <class _Tp>
0339 [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp* launder(_Tp* __p) noexcept {
0340 return std::__launder(__p);
0341 }
0342 #endif
0343
0344 #if _LIBCPP_STD_VER >= 17
0345
0346 # if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
0347
0348 inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
0349 inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
0350
0351 # endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
0352
0353 #endif // _LIBCPP_STD_VER >= 17
0354
0355 _LIBCPP_END_NAMESPACE_STD
0356
0357 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
0358 # include <__cxx03/cstdlib>
0359 # include <__cxx03/type_traits>
0360 #endif
0361
0362 #endif // _LIBCPP___CXX03_NEW