Path: blob/main/contrib/llvm-project/libcxx/include/__memory/allocation_guard.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___MEMORY_ALLOCATION_GUARD_H10#define _LIBCPP___MEMORY_ALLOCATION_GUARD_H1112#include <__config>13#include <__memory/addressof.h>14#include <__memory/allocator_traits.h>15#include <__utility/move.h>16#include <cstddef>1718#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif2122_LIBCPP_PUSH_MACROS23#include <__undef_macros>2425_LIBCPP_BEGIN_NAMESPACE_STD2627// Helper class to allocate memory using an Allocator in an exception safe28// manner.29//30// The intended usage of this class is as follows:31//32// 033// 1 __allocation_guard<SomeAllocator> guard(alloc, 10);34// 2 do_some_initialization_that_may_throw(guard.__get());35// 3 save_allocated_pointer_in_a_noexcept_operation(guard.__release_ptr());36// 437//38// If line (2) throws an exception during initialization of the memory, the39// guard's destructor will be called, and the memory will be released using40// Allocator deallocation. Otherwise, we release the memory from the guard on41// line (3) in an operation that can't throw -- after that, the guard is not42// responsible for the memory anymore.43//44// This is similar to a unique_ptr, except it's easier to use with a45// custom allocator.46template <class _Alloc>47struct __allocation_guard {48using _Pointer = typename allocator_traits<_Alloc>::pointer;49using _Size = typename allocator_traits<_Alloc>::size_type;5051template <class _AllocT> // we perform the allocator conversion inside the constructor52_LIBCPP_HIDE_FROM_ABI explicit __allocation_guard(_AllocT __alloc, _Size __n)53: __alloc_(std::move(__alloc)),54__n_(__n),55__ptr_(allocator_traits<_Alloc>::allocate(__alloc_, __n_)) // initialization order is important56{}5758_LIBCPP_HIDE_FROM_ABI ~__allocation_guard() _NOEXCEPT { __destroy(); }5960_LIBCPP_HIDE_FROM_ABI __allocation_guard(const __allocation_guard&) = delete;61_LIBCPP_HIDE_FROM_ABI __allocation_guard(__allocation_guard&& __other) _NOEXCEPT62: __alloc_(std::move(__other.__alloc_)),63__n_(__other.__n_),64__ptr_(__other.__ptr_) {65__other.__ptr_ = nullptr;66}6768_LIBCPP_HIDE_FROM_ABI __allocation_guard& operator=(const __allocation_guard& __other) = delete;69_LIBCPP_HIDE_FROM_ABI __allocation_guard& operator=(__allocation_guard&& __other) _NOEXCEPT {70if (std::addressof(__other) != this) {71__destroy();7273__alloc_ = std::move(__other.__alloc_);74__n_ = __other.__n_;75__ptr_ = __other.__ptr_;76__other.__ptr_ = nullptr;77}7879return *this;80}8182_LIBCPP_HIDE_FROM_ABI _Pointer83__release_ptr() _NOEXCEPT { // not called __release() because it's a keyword in objective-c++84_Pointer __tmp = __ptr_;85__ptr_ = nullptr;86return __tmp;87}8889_LIBCPP_HIDE_FROM_ABI _Pointer __get() const _NOEXCEPT { return __ptr_; }9091private:92_LIBCPP_HIDE_FROM_ABI void __destroy() _NOEXCEPT {93if (__ptr_ != nullptr) {94allocator_traits<_Alloc>::deallocate(__alloc_, __ptr_, __n_);95}96}9798_Alloc __alloc_;99_Size __n_;100_Pointer __ptr_;101};102103_LIBCPP_END_NAMESPACE_STD104105_LIBCPP_POP_MACROS106107#endif // _LIBCPP___MEMORY_ALLOCATION_GUARD_H108109110