1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___MEMORY_ALLOCATION_GUARD_H
11 #define _LIBCPP___MEMORY_ALLOCATION_GUARD_H
12 
13 #include <__config>
14 #include <__memory/addressof.h>
15 #include <__memory/allocator_traits.h>
16 #include <__utility/move.h>
17 #include <cstddef>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 _LIBCPP_PUSH_MACROS
24 #include <__undef_macros>
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 
28 // Helper class to allocate memory using an Allocator in an exception safe
29 // manner.
30 //
31 // The intended usage of this class is as follows:
32 //
33 // 0
34 // 1     __allocation_guard<SomeAllocator> guard(alloc, 10);
35 // 2     do_some_initialization_that_may_throw(guard.__get());
36 // 3     save_allocated_pointer_in_a_noexcept_operation(guard.__release_ptr());
37 // 4
38 //
39 // If line (2) throws an exception during initialization of the memory, the
40 // guard's destructor will be called, and the memory will be released using
41 // Allocator deallocation. Otherwise, we release the memory from the guard on
42 // line (3) in an operation that can't throw -- after that, the guard is not
43 // responsible for the memory anymore.
44 //
45 // This is similar to a unique_ptr, except it's easier to use with a
46 // custom allocator.
47 template<class _Alloc>
48 struct __allocation_guard {
49     using _Pointer = typename allocator_traits<_Alloc>::pointer;
50     using _Size = typename allocator_traits<_Alloc>::size_type;
51 
52     template<class _AllocT> // we perform the allocator conversion inside the constructor
53     _LIBCPP_HIDE_FROM_ABI
54     explicit __allocation_guard(_AllocT __alloc, _Size __n)
55         : __alloc_(_VSTD::move(__alloc))
56         , __n_(__n)
57         , __ptr_(allocator_traits<_Alloc>::allocate(__alloc_, __n_)) // initialization order is important
58     { }
59 
60     _LIBCPP_HIDE_FROM_ABI
61     ~__allocation_guard() _NOEXCEPT {
62         __destroy();
63     }
64 
65     _LIBCPP_HIDE_FROM_ABI __allocation_guard(const __allocation_guard&) = delete;
66     _LIBCPP_HIDE_FROM_ABI __allocation_guard(__allocation_guard&& __other) _NOEXCEPT
67         : __alloc_(std::move(__other.__alloc_))
68         , __n_(__other.__n_)
69         , __ptr_(__other.__ptr_) {
70       __other.__ptr_ = nullptr;
71     }
72 
73     _LIBCPP_HIDE_FROM_ABI __allocation_guard& operator=(const __allocation_guard& __other) = delete;
74     _LIBCPP_HIDE_FROM_ABI __allocation_guard& operator=(__allocation_guard&& __other) _NOEXCEPT {
75         if (std::addressof(__other) != this) {
76             __destroy();
77 
78             __alloc_ = std::move(__other.__alloc_);
79             __n_ = __other.__n_;
80             __ptr_ = __other.__ptr_;
81             __other.__ptr_ = nullptr;
82         }
83 
84         return *this;
85     }
86 
87     _LIBCPP_HIDE_FROM_ABI
88     _Pointer __release_ptr() _NOEXCEPT { // not called __release() because it's a keyword in objective-c++
89         _Pointer __tmp = __ptr_;
90         __ptr_ = nullptr;
91         return __tmp;
92     }
93 
94     _LIBCPP_HIDE_FROM_ABI
95     _Pointer __get() const _NOEXCEPT {
96         return __ptr_;
97     }
98 
99 private:
100     _LIBCPP_HIDE_FROM_ABI
101     void __destroy() _NOEXCEPT {
102         if (__ptr_ != nullptr) {
103             allocator_traits<_Alloc>::deallocate(__alloc_, __ptr_, __n_);
104         }
105     }
106 
107     _Alloc __alloc_;
108     _Size __n_;
109     _Pointer __ptr_;
110 };
111 
112 _LIBCPP_END_NAMESPACE_STD
113 
114 _LIBCPP_POP_MACROS
115 
116 #endif // _LIBCPP___MEMORY_ALLOCATION_GUARD_H
117