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