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
13 #include <__cxx03/__config>
14 #include <__cxx03/__utility/pair.h>
15 #include <__cxx03/cstddef>
16 #include <__cxx03/new>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
22 _LIBCPP_BEGIN_NAMESPACE_STD
25 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _LIBCPP_DEPRECATED_IN_CXX17 pair
<_Tp
*, ptrdiff_t>
26 get_temporary_buffer(ptrdiff_t __n
) _NOEXCEPT
{
27 pair
<_Tp
*, ptrdiff_t> __r(0, 0);
29 (~ptrdiff_t(0) ^ ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__
- 1))) / sizeof(_Tp
);
33 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
34 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp
))) {
35 align_val_t __al
= align_val_t(_LIBCPP_ALIGNOF(_Tp
));
36 __r
.first
= static_cast<_Tp
*>(::operator new(__n
* sizeof(_Tp
), __al
, nothrow
));
38 __r
.first
= static_cast<_Tp
*>(::operator new(__n
* sizeof(_Tp
), nothrow
));
41 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp
))) {
42 // Since aligned operator new is unavailable, return an empty
43 // buffer rather than one with invalid alignment.
47 __r
.first
= static_cast<_Tp
*>(::operator new(__n
* sizeof(_Tp
), nothrow
));
60 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17
void return_temporary_buffer(_Tp
* __p
) _NOEXCEPT
{
61 std::__libcpp_deallocate_unsized((void*)__p
, _LIBCPP_ALIGNOF(_Tp
));
64 struct __return_temporary_buffer
{
65 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
67 _LIBCPP_HIDE_FROM_ABI
void operator()(_Tp
* __p
) const {
68 std::return_temporary_buffer(__p
);
70 _LIBCPP_SUPPRESS_DEPRECATED_POP
73 _LIBCPP_END_NAMESPACE_STD
75 #endif // _LIBCPP___MEMORY_TEMPORARY_BUFFER_H