Back to home page

EIC code displayed by LXR

 
 

    


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

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___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 // Helper class to allocate memory using an Allocator in an exception safe
0029 // manner.
0030 //
0031 // The intended usage of this class is as follows:
0032 //
0033 // 0
0034 // 1     __allocation_guard<SomeAllocator> guard(alloc, 10);
0035 // 2     do_some_initialization_that_may_throw(guard.__get());
0036 // 3     save_allocated_pointer_in_a_noexcept_operation(guard.__release_ptr());
0037 // 4
0038 //
0039 // If line (2) throws an exception during initialization of the memory, the
0040 // guard's destructor will be called, and the memory will be released using
0041 // Allocator deallocation. Otherwise, we release the memory from the guard on
0042 // line (3) in an operation that can't throw -- after that, the guard is not
0043 // responsible for the memory anymore.
0044 //
0045 // This is similar to a unique_ptr, except it's easier to use with a
0046 // custom allocator.
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> // we perform the allocator conversion inside the constructor
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_)) // initialization order is important
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 { // not called __release() because it's a keyword in objective-c++
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 // _LIBCPP___CXX03___MEMORY_ALLOCATION_GUARD_H