codekingpro/portable-devtools
114k
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCUDACXX_NEW11#define _LIBCUDACXX_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> constexpr T* launder(T* p) noexcept; // C++1750} // std51 52void* operator new(std::size_t size); // replaceable, nodiscard in C++2a53void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++2a54void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a55void* operator new(std::size_t size, std::align_val_t alignment,56 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a57void 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++2a67void* operator new[](std::size_t size,68 std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++2a69void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a70void* operator new[](std::size_t size, std::align_val_t alignment,71 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a72void 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++2a83void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++2a84void operator delete (void* ptr, void*) noexcept;85void operator delete[](void* ptr, void*) noexcept;86 87*/88 89#ifndef __cuda_std__90#include <__config>91#include <exception>92#ifdef _LIBCUDACXX_NO_EXCEPTIONS93#include <cstdlib>94#endif95#if defined(_LIBCUDACXX_ABI_VCRUNTIME)96#include <new.h>97#endif98#endif // __cuda_std__99 100#include "__assert" // all public C++ headers provide the assertion handler101#include "cstddef"102#include "type_traits"103#include "version"104 105#ifndef __cuda_std__106#include <__pragma_push>107#endif // __cuda_std__108 109#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)110# pragma GCC system_header111#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)112# pragma clang system_header113#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)114# pragma system_header115#endif // no system header116 117#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L118#define _LIBCUDACXX_HAS_NO_LANGUAGE_SIZED_DEALLOCATION119#endif120 121#if !defined(_LIBCUDACXX_BUILDING_LIBRARY) && _LIBCUDACXX_STD_VER < 14 && \122 defined(_LIBCUDACXX_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)123# define _LIBCUDACXX_HAS_NO_LIBRARY_SIZED_DEALLOCATION124#endif125 126#if defined(_LIBCUDACXX_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \127 defined(_LIBCUDACXX_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)128# define _LIBCUDACXX_HAS_NO_SIZED_DEALLOCATION129#endif130 131#if !__has_builtin(__builtin_operator_new) || \132 __has_builtin(__builtin_operator_new) < 201802L133#define _LIBCUDACXX_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE134#endif135 136namespace std // purposefully not using versioning namespace137{138 139#if !defined(_LIBCUDACXX_ABI_VCRUNTIME)140struct _LIBCUDACXX_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };141extern _LIBCUDACXX_FUNC_VIS const nothrow_t nothrow;142 143class _LIBCUDACXX_EXCEPTION_ABI bad_alloc144 : public exception145{146public:147 bad_alloc() noexcept;148 virtual ~bad_alloc() noexcept;149 virtual const char* what() const noexcept;150};151 152class _LIBCUDACXX_EXCEPTION_ABI bad_array_new_length153 : public bad_alloc154{155public:156 bad_array_new_length() noexcept;157 virtual ~bad_array_new_length() noexcept;158 virtual const char* what() const noexcept;159};160 161typedef void (*new_handler)();162_LIBCUDACXX_FUNC_VIS new_handler set_new_handler(new_handler) noexcept;163_LIBCUDACXX_FUNC_VIS new_handler get_new_handler() noexcept;164 165#endif // !_LIBCUDACXX_ABI_VCRUNTIME166 167_LIBCUDACXX_NORETURN _LIBCUDACXX_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec168 169#if !defined(_LIBCUDACXX_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \170 !defined(_LIBCUDACXX_ABI_VCRUNTIME)171enum class _LIBCUDACXX_ENUM_VIS align_val_t : size_t { };172#endif173 174#if _LIBCUDACXX_STD_VER > 17175// Enable the declaration even if the compiler doesn't support the language176// feature.177struct destroying_delete_t {178 explicit destroying_delete_t() = default;179};180_LIBCUDACXX_INLINE_VAR constexpr destroying_delete_t destroying_delete{};181#endif // _LIBCUDACXX_STD_VER > 17182 183} // std184 185#if !defined(_LIBCUDACXX_ABI_VCRUNTIME)186 187_LIBCUDACXX_NODISCARD_AFTER_CXX17 _LIBCUDACXX_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz);188_LIBCUDACXX_NODISCARD_AFTER_CXX17 _LIBCUDACXX_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) noexcept _LIBCUDACXX_NOALIAS;189_LIBCUDACXX_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) noexcept;190_LIBCUDACXX_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) noexcept;191#ifndef _LIBCUDACXX_HAS_NO_LIBRARY_SIZED_DEALLOCATION192_LIBCUDACXX_OVERRIDABLE_FUNC_VIS _LIBCUDACXX_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) noexcept;193#endif194 195_LIBCUDACXX_NODISCARD_AFTER_CXX17 _LIBCUDACXX_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz);196_LIBCUDACXX_NODISCARD_AFTER_CXX17 _LIBCUDACXX_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) noexcept _LIBCUDACXX_NOALIAS;197_LIBCUDACXX_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) noexcept;198_LIBCUDACXX_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) noexcept;199#ifndef _LIBCUDACXX_HAS_NO_LIBRARY_SIZED_DEALLOCATION200_LIBCUDACXX_OVERRIDABLE_FUNC_VIS _LIBCUDACXX_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) noexcept;201#endif202 203#ifndef _LIBCUDACXX_HAS_NO_LIBRARY_ALIGNED_ALLOCATION204_LIBCUDACXX_NODISCARD_AFTER_CXX17 _LIBCUDACXX_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t);205_LIBCUDACXX_NODISCARD_AFTER_CXX17 _LIBCUDACXX_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) noexcept _LIBCUDACXX_NOALIAS;206_LIBCUDACXX_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) noexcept;207_LIBCUDACXX_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) noexcept;208#ifndef _LIBCUDACXX_HAS_NO_LIBRARY_SIZED_DEALLOCATION209_LIBCUDACXX_OVERRIDABLE_FUNC_VIS _LIBCUDACXX_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) noexcept;210#endif211 212_LIBCUDACXX_NODISCARD_AFTER_CXX17 _LIBCUDACXX_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t);213_LIBCUDACXX_NODISCARD_AFTER_CXX17 _LIBCUDACXX_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) noexcept _LIBCUDACXX_NOALIAS;214_LIBCUDACXX_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) noexcept;215_LIBCUDACXX_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) noexcept;216#ifndef _LIBCUDACXX_HAS_NO_LIBRARY_SIZED_DEALLOCATION217_LIBCUDACXX_OVERRIDABLE_FUNC_VIS _LIBCUDACXX_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) noexcept;218#endif219#endif220 221_LIBCUDACXX_NODISCARD_AFTER_CXX17 inline _LIBCUDACXX_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) noexcept {return __p;}222_LIBCUDACXX_NODISCARD_AFTER_CXX17 inline _LIBCUDACXX_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) noexcept {return __p;}223inline _LIBCUDACXX_INLINE_VISIBILITY void operator delete (void*, void*) noexcept {}224inline _LIBCUDACXX_INLINE_VISIBILITY void operator delete[](void*, void*) noexcept {}225 226#endif // !_LIBCUDACXX_ABI_VCRUNTIME227 228_LIBCUDACXX_BEGIN_NAMESPACE_STD229 230constexpr inline _LIBCUDACXX_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) noexcept {231#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__232 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;233#else234 return __align > alignment_of<max_align_t>::value;235#endif236}237 238inline _LIBCUDACXX_INLINE_VISIBILITY void *__libcpp_allocate(size_t __size, size_t __align) {239#ifndef _LIBCUDACXX_HAS_NO_ALIGNED_ALLOCATION240 if (__is_overaligned_for_new(__align)) {241 const align_val_t __align_val = static_cast<align_val_t>(__align);242# ifdef _LIBCUDACXX_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE243 return ::operator new(__size, __align_val);244# else245 return __builtin_operator_new(__size, __align_val);246# endif247 }248#else249 ((void)__align);250#endif251#ifdef _LIBCUDACXX_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE252 return ::operator new(__size);253#else254 return __builtin_operator_new(__size);255#endif256}257 258struct _DeallocateCaller {259 static inline _LIBCUDACXX_INLINE_VISIBILITY260 void __do_deallocate_handle_size_align(void *__ptr, size_t __size, size_t __align) {261#if defined(_LIBCUDACXX_HAS_NO_ALIGNED_ALLOCATION)262 ((void)__align);263 return __do_deallocate_handle_size(__ptr, __size);264#else265 if (__is_overaligned_for_new(__align)) {266 const align_val_t __align_val = static_cast<align_val_t>(__align);267 return __do_deallocate_handle_size(__ptr, __size, __align_val);268 } else {269 return __do_deallocate_handle_size(__ptr, __size);270 }271#endif272 }273 274 static inline _LIBCUDACXX_INLINE_VISIBILITY275 void __do_deallocate_handle_align(void *__ptr, size_t __align) {276#if defined(_LIBCUDACXX_HAS_NO_ALIGNED_ALLOCATION)277 ((void)__align);278 return __do_call(__ptr);279#else280 if (__is_overaligned_for_new(__align)) {281 const align_val_t __align_val = static_cast<align_val_t>(__align);282 return __do_call(__ptr, __align_val);283 } else {284 return __do_call(__ptr);285 }286#endif287 }288 289 private:290 static inline void __do_deallocate_handle_size(void *__ptr, size_t __size) {291#ifdef _LIBCUDACXX_HAS_NO_SIZED_DEALLOCATION292 ((void)__size);293 return __do_call(__ptr);294#else295 return __do_call(__ptr, __size);296#endif297 }298 299#ifndef _LIBCUDACXX_HAS_NO_ALIGNED_ALLOCATION300 static inline void __do_deallocate_handle_size(void *__ptr, size_t __size, align_val_t __align) {301#ifdef _LIBCUDACXX_HAS_NO_SIZED_DEALLOCATION302 ((void)__size);303 return __do_call(__ptr, __align);304#else305 return __do_call(__ptr, __size, __align);306#endif307 }308#endif309 310private:311 template <class _A1, class _A2>312 static inline void __do_call(void *__ptr, _A1 __a1, _A2 __a2) {313#if defined(_LIBCUDACXX_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \314 defined(_LIBCUDACXX_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE)315 return ::operator delete(__ptr, __a1, __a2);316#else317 return __builtin_operator_delete(__ptr, __a1, __a2);318#endif319 }320 321 template <class _A1>322 static inline void __do_call(void *__ptr, _A1 __a1) {323#if defined(_LIBCUDACXX_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \324 defined(_LIBCUDACXX_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE)325 return ::operator delete(__ptr, __a1);326#else327 return __builtin_operator_delete(__ptr, __a1);328#endif329 }330 331 static inline void __do_call(void *__ptr) {332#ifdef _LIBCUDACXX_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE333 return ::operator delete(__ptr);334#else335 return __builtin_operator_delete(__ptr);336#endif337 }338};339 340inline _LIBCUDACXX_INLINE_VISIBILITY void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {341 _DeallocateCaller::__do_deallocate_handle_size_align(__ptr, __size, __align);342}343 344inline _LIBCUDACXX_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {345 _DeallocateCaller::__do_deallocate_handle_align(__ptr, __align);346}347 348template <class _Tp>349_LIBCUDACXX_NODISCARD_AFTER_CXX17 inline350constexpr _Tp* __launder(_Tp* __p) noexcept351{352 static_assert (!(is_function<_Tp>::value), "can't launder functions" );353 static_assert (!(is_same<void, __remove_cv_t<_Tp>>::value), "can't launder cv-void" );354#if defined(_LIBCUDACXX_LAUNDER)355 return _LIBCUDACXX_LAUNDER(__p);356#else357 return __p;358#endif // defined(_LIBCUDACXX_LAUNDER)359}360 361 362#if _LIBCUDACXX_STD_VER > 14363template <class _Tp>364_LIBCUDACXX_NODISCARD_AFTER_CXX17 inline _LIBCUDACXX_INLINE_VISIBILITY365constexpr _Tp* launder(_Tp* __p) noexcept366{367 return _CUDA_VSTD::__launder(__p);368}369#endif370 371_LIBCUDACXX_END_NAMESPACE_STD372 373#ifndef __cuda_std__374#include <__pragma_pop>375#endif //__cuda_std__376 377#endif // _LIBCUDACXX_NEW378 