Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:55

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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 // Helper class to allocate memory using an Allocator in an exception safe
0028 // manner.
0029 //
0030 // The intended usage of this class is as follows:
0031 //
0032 // 0
0033 // 1     __allocation_guard<SomeAllocator> guard(alloc, 10);
0034 // 2     do_some_initialization_that_may_throw(guard.__get());
0035 // 3     save_allocated_pointer_in_a_noexcept_operation(guard.__release_ptr());
0036 // 4
0037 //
0038 // If line (2) throws an exception during initialization of the memory, the
0039 // guard's destructor will be called, and the memory will be released using
0040 // Allocator deallocation. Otherwise, we release the memory from the guard on
0041 // line (3) in an operation that can't throw -- after that, the guard is not
0042 // responsible for the memory anymore.
0043 //
0044 // This is similar to a unique_ptr, except it's easier to use with a
0045 // custom allocator.
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> // we perform the allocator conversion inside the constructor
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_)) // initialization order is important
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 { // not called __release() because it's a keyword in objective-c++
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 // _LIBCPP___MEMORY_ALLOCATION_GUARD_H