File indexing completed on 2026-05-03 08:13:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___MEMORY_ALLOCATION_GUARD_H
0011 #define _LIBCPP___MEMORY_ALLOCATION_GUARD_H
0012
0013 #include <__config>
0014 #include <__memory/addressof.h>
0015 #include <__memory/allocator_traits.h>
0016 #include <__utility/move.h>
0017
0018 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0019 # pragma GCC system_header
0020 #endif
0021
0022 _LIBCPP_PUSH_MACROS
0023 #include <__undef_macros>
0024
0025 _LIBCPP_BEGIN_NAMESPACE_STD
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 template <class _Alloc>
0047 struct __allocation_guard {
0048 using _Pointer _LIBCPP_NODEBUG = typename allocator_traits<_Alloc>::pointer;
0049 using _Size _LIBCPP_NODEBUG = typename allocator_traits<_Alloc>::size_type;
0050
0051 template <class _AllocT>
0052 _LIBCPP_HIDE_FROM_ABI explicit __allocation_guard(_AllocT __alloc, _Size __n)
0053 : __alloc_(std::move(__alloc)),
0054 __n_(__n),
0055 __ptr_(allocator_traits<_Alloc>::allocate(__alloc_, __n_))
0056 {}
0057
0058 _LIBCPP_HIDE_FROM_ABI ~__allocation_guard() _NOEXCEPT { __destroy(); }
0059
0060 _LIBCPP_HIDE_FROM_ABI __allocation_guard(const __allocation_guard&) = delete;
0061 _LIBCPP_HIDE_FROM_ABI __allocation_guard(__allocation_guard&& __other) _NOEXCEPT
0062 : __alloc_(std::move(__other.__alloc_)),
0063 __n_(__other.__n_),
0064 __ptr_(__other.__ptr_) {
0065 __other.__ptr_ = nullptr;
0066 }
0067
0068 _LIBCPP_HIDE_FROM_ABI __allocation_guard& operator=(const __allocation_guard& __other) = delete;
0069 _LIBCPP_HIDE_FROM_ABI __allocation_guard& operator=(__allocation_guard&& __other) _NOEXCEPT {
0070 if (std::addressof(__other) != this) {
0071 __destroy();
0072
0073 __alloc_ = std::move(__other.__alloc_);
0074 __n_ = __other.__n_;
0075 __ptr_ = __other.__ptr_;
0076 __other.__ptr_ = nullptr;
0077 }
0078
0079 return *this;
0080 }
0081
0082 _LIBCPP_HIDE_FROM_ABI _Pointer
0083 __release_ptr() _NOEXCEPT {
0084 _Pointer __tmp = __ptr_;
0085 __ptr_ = nullptr;
0086 return __tmp;
0087 }
0088
0089 _LIBCPP_HIDE_FROM_ABI _Pointer __get() const _NOEXCEPT { return __ptr_; }
0090
0091 private:
0092 _LIBCPP_HIDE_FROM_ABI void __destroy() _NOEXCEPT {
0093 if (__ptr_ != nullptr) {
0094 allocator_traits<_Alloc>::deallocate(__alloc_, __ptr_, __n_);
0095 }
0096 }
0097
0098 _Alloc __alloc_;
0099 _Size __n_;
0100 _Pointer __ptr_;
0101 };
0102
0103 _LIBCPP_END_NAMESPACE_STD
0104
0105 _LIBCPP_POP_MACROS
0106
0107 #endif