// -*- C++ -*- //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef _LIBCPP___MEMORY_ALLOCATION_GUARD_H #define _LIBCPP___MEMORY_ALLOCATION_GUARD_H #include <__config> #include <__memory/addressof.h> #include <__memory/allocator_traits.h> #include <__utility/move.h> #include <cstddef> #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header #endif _LIBCPP_PUSH_MACROS #include <__undef_macros> _LIBCPP_BEGIN_NAMESPACE_STD // Helper class to allocate memory using an Allocator in an exception safe // manner. // // The intended usage of this class is as follows: // // 0 // 1 __allocation_guard<SomeAllocator> guard(alloc, 10); // 2 do_some_initialization_that_may_throw(guard.__get()); // 3 save_allocated_pointer_in_a_noexcept_operation(guard.__release_ptr()); // 4 // // If line (2) throws an exception during initialization of the memory, the // guard's destructor will be called, and the memory will be released using // Allocator deallocation. Otherwise, we release the memory from the guard on // line (3) in an operation that can't throw -- after that, the guard is not // responsible for the memory anymore. // // This is similar to a unique_ptr, except it's easier to use with a // custom allocator. template <class _Alloc> struct __allocation_guard { … }; _LIBCPP_END_NAMESPACE_STD _LIBCPP_POP_MACROS #endif // _LIBCPP___MEMORY_ALLOCATION_GUARD_H