-
Notifications
You must be signed in to change notification settings - Fork 15.7k
[libc++] Remove get_temporary_buffer/return_temporary_buffer
#100914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
f7ff07e
25a9782
038af58
60212b2
2841f72
d0f6edf
5268aa9
3529007
2a71c0e
6d42fd6
cfc0ef5
3cc9bb7
9e2567a
ffce43a
b83103b
c812180
f6e94f7
5c50d6a
2890c1d
b8de11d
eda4edf
1f3fde7
5e4b157
2bd52cb
51b18a2
0961a50
dc503fa
74f4c4a
1406cd5
de9a474
fa30053
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // -*- 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef _LIBCPP___MEMORY_SCOPED_TEMPORARY_BUFFER_H | ||
| #define _LIBCPP___MEMORY_SCOPED_TEMPORARY_BUFFER_H | ||
|
|
||
| #include <__config> | ||
| #include <__memory/allocator.h> | ||
| #include <__type_traits/is_constant_evaluated.h> | ||
| #include <cstddef> | ||
| #include <new> | ||
|
|
||
| #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
| # pragma GCC system_header | ||
| #endif | ||
|
|
||
| _LIBCPP_BEGIN_NAMESPACE_STD | ||
|
|
||
| template <class _Tp> | ||
| struct __temporary_allocation_result { | ||
| _Tp* __ptr; | ||
| ptrdiff_t __count; | ||
| }; | ||
frederick-vs-ja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| template <class _Tp> | ||
| class __scoped_temporary_buffer { | ||
| public: | ||
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __scoped_temporary_buffer() _NOEXCEPT | ||
| : __ptr_(NULL), | ||
frederick-vs-ja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| __count_(0) {} | ||
|
|
||
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __scoped_temporary_buffer(ptrdiff_t __count) _NOEXCEPT | ||
| : __ptr_(NULL), | ||
frederick-vs-ja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| __count_(0) { | ||
| __try_allocate(__count); | ||
| } | ||
|
|
||
| #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_TEMPORARY_BUFFER) | ||
frederick-vs-ja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // pre: __buf_ptr points to the beginning of a previously allocated scoped temporary buffer or is null | ||
| // notes: __count_ is ignored in non-constant evaluation | ||
| _LIBCPP_HIDE_FROM_ABI explicit __scoped_temporary_buffer(_Tp* __buf_ptr) _NOEXCEPT : __ptr_(__buf_ptr), __count_(0) {} | ||
| #endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_TEMPORARY_BUFFER) | ||
|
|
||
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__scoped_temporary_buffer() _NOEXCEPT { | ||
| if (__libcpp_is_constant_evaluated()) { | ||
| allocator<_Tp>().deallocate(__ptr_, __count_); | ||
| } | ||
|
|
||
| std::__libcpp_deallocate_unsized((void*)__ptr_, _LIBCPP_ALIGNOF(_Tp)); | ||
|
||
| } | ||
|
|
||
| __scoped_temporary_buffer(const __scoped_temporary_buffer&) = delete; | ||
| __scoped_temporary_buffer& operator=(const __scoped_temporary_buffer&) = delete; | ||
|
|
||
| // pre: __ptr_ == nullptr && __count_ == 0 | ||
frederick-vs-ja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __try_allocate(ptrdiff_t __count) _NOEXCEPT { | ||
| if (__libcpp_is_constant_evaluated()) { | ||
| __ptr_ = allocator<_Tp>().allocate(__count); | ||
| __count_ = __count; | ||
| return; | ||
| } | ||
|
|
||
| const ptrdiff_t __max_count = | ||
| (~ptrdiff_t(0) ^ ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1))) / sizeof(_Tp); | ||
| if (__count > __max_count) | ||
| __count = __max_count; | ||
| while (__count > 0) { | ||
| #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) | ||
| if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp))) { | ||
| align_val_t __al = align_val_t(_LIBCPP_ALIGNOF(_Tp)); | ||
| __ptr_ = static_cast<_Tp*>(::operator new(__count * sizeof(_Tp), __al, nothrow)); | ||
| } else { | ||
| __ptr_ = static_cast<_Tp*>(::operator new(__count * sizeof(_Tp), nothrow)); | ||
| } | ||
| #else | ||
| if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp))) { | ||
| // Since aligned operator new is unavailable, constructs an empty buffer rather than one with invalid alignment. | ||
| return; | ||
| } | ||
|
|
||
| __ptr_ = static_cast<_Tp*>(::operator new(__count * sizeof(_Tp), nothrow)); | ||
| #endif | ||
|
||
|
|
||
| if (__ptr_) { | ||
| __count_ = __count; | ||
| break; | ||
| } | ||
| __count_ /= 2; | ||
| } | ||
| } | ||
|
|
||
| _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __temporary_allocation_result<_Tp> | ||
| __get() const _NOEXCEPT { | ||
| __temporary_allocation_result<_Tp> __result = {__ptr_, __count_}; | ||
| return __result; | ||
| } | ||
|
|
||
| #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_TEMPORARY_BUFFER) | ||
frederick-vs-ja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI __temporary_allocation_result<_Tp> __release_to_raw() _NOEXCEPT { | ||
ldionne marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| __temporary_allocation_result<_Tp> __result = {__ptr_, __count_}; | ||
|
|
||
| __ptr_ = NULL; | ||
| __count_ = 0; | ||
|
|
||
| return __result; | ||
| } | ||
| #endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_TEMPORARY_BUFFER) | ||
|
|
||
| private: | ||
| _Tp* __ptr_; | ||
| ptrdiff_t __count_; | ||
| }; | ||
|
|
||
| _LIBCPP_END_NAMESPACE_STD | ||
|
|
||
| #endif // _LIBCPP___MEMORY_SCOPED_TEMPORARY_BUFFER_H | ||
Uh oh!
There was an error while loading. Please reload this page.