// -*- C++ -*- //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// //============================================================================== // NOTICE: This file has been modified by Green Hills Software, Inc. Such // modifications are subject to copyright by Green Hills Software, Inc. See the // "Green Hills Software Modifications" section in the LICENSE.txt included with // this distribution for more information. //============================================================================== #ifndef _LIBCPP___MEMORY_TEMPORARY_BUFFER_H #define _LIBCPP___MEMORY_TEMPORARY_BUFFER_H #include <__config> #include <__type_traits/alignment_of.h> #include <__utility/pair.h> #include #include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header #endif #if defined(__ghs__) #pragma ghs start_cxx_lib_header #pragma ghs startdata #if defined(__ghs_max_pack_value) #pragma pack (push, __ghs_max_pack_value) #endif #endif _LIBCPP_BEGIN_NAMESPACE_STD template _LIBCPP_NODISCARD_EXT _GHS_LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _LIBCPP_DEPRECATED_IN_CXX17 pair<_Tp*, ptrdiff_t> #if defined(__ghs__) __get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT #else get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT #endif { pair<_Tp*, ptrdiff_t> __r(0, 0); const ptrdiff_t __m = (~ptrdiff_t(0) ^ ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1))) / sizeof(_Tp); if (__n > __m) __n = __m; while (__n > 0) { #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp))) { align_val_t __al = align_val_t(alignment_of<_Tp>::value); __r.first = static_cast<_Tp*>(::operator new( __n * sizeof(_Tp), __al, nothrow)); } else { __r.first = static_cast<_Tp*>(::operator new( __n * sizeof(_Tp), nothrow)); } #else if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp))) { // Since aligned operator new is unavailable, return an empty // buffer rather than one with invalid alignment. return __r; } __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow)); #endif if (__r.first) { __r.second = __n; break; } __n /= 2; } return __r; } template inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17 #if defined(__ghs__) void __return_temporary_buffer(_Tp* __p) #else void return_temporary_buffer(_Tp* __p) #endif #ifndef __ghs__ _NOEXCEPT #else _LIBCPP_NOEXCEPT_AFTER_CXX14 #endif { _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp)); } #if defined(__ghs__) && _LIBCPP_STD_VER <= 17 // {get, return}_temporary_buffer was removed in C++20 template _LIBCPP_NODISCARD_EXT _GHS_LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _LIBCPP_DEPRECATED_IN_CXX17 pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT { return std::__get_temporary_buffer<_Tp>(__n); } template inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17 void return_temporary_buffer(_Tp* __p) #ifndef __ghs__ _NOEXCEPT #else _LIBCPP_NOEXCEPT_AFTER_CXX14 #endif { return std::__return_temporary_buffer(__p); } #endif #if defined(__ghs__) struct __return_temporary_buffer_obj #else struct __return_temporary_buffer #endif { _LIBCPP_SUPPRESS_DEPRECATED_PUSH template #if defined(__ghs__) _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::__return_temporary_buffer(__p);} #else _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);} #endif _LIBCPP_SUPPRESS_DEPRECATED_POP }; _LIBCPP_END_NAMESPACE_STD #if defined(__ghs__) #if defined(__ghs_max_pack_value) #pragma pack(pop) #endif #pragma ghs enddata #pragma ghs end_cxx_lib_header #endif #endif // _LIBCPP___MEMORY_TEMPORARY_BUFFER_H