2 //===----------------------------------------------------------------------===//
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-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___MEMORY_TEMPORARY_BUFFER_H
11 #define _LIBCPP___MEMORY_TEMPORARY_BUFFER_H
14 #include <__type_traits/alignment_of.h>
15 #include <__utility/pair.h>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
23 _LIBCPP_BEGIN_NAMESPACE_STD
26 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _LIBCPP_DEPRECATED_IN_CXX17
28 get_temporary_buffer(ptrdiff_t __n
) _NOEXCEPT
30 pair
<_Tp
*, ptrdiff_t> __r(0, 0);
31 const ptrdiff_t __m
= (~ptrdiff_t(0) ^
32 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__
- 1)))
38 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
39 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp
)))
41 align_val_t __al
= align_val_t(_LIBCPP_ALIGNOF(_Tp
));
42 __r
.first
= static_cast<_Tp
*>(::operator new(
43 __n
* sizeof(_Tp
), __al
, nothrow
));
45 __r
.first
= static_cast<_Tp
*>(::operator new(
46 __n
* sizeof(_Tp
), nothrow
));
49 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp
)))
51 // Since aligned operator new is unavailable, return an empty
52 // buffer rather than one with invalid alignment.
56 __r
.first
= static_cast<_Tp
*>(::operator new(__n
* sizeof(_Tp
), nothrow
));
70 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
71 void return_temporary_buffer(_Tp
* __p
) _NOEXCEPT
73 _VSTD::__libcpp_deallocate_unsized((void*)__p
, _LIBCPP_ALIGNOF(_Tp
));
76 struct __return_temporary_buffer
78 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
80 _LIBCPP_INLINE_VISIBILITY
void operator()(_Tp
* __p
) const {_VSTD::return_temporary_buffer(__p
);}
81 _LIBCPP_SUPPRESS_DEPRECATED_POP
84 _LIBCPP_END_NAMESPACE_STD
86 #endif // _LIBCPP___MEMORY_TEMPORARY_BUFFER_H