allocation_guard.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef _LIBCPP___MEMORY_ALLOCATION_GUARD_H
  10. #define _LIBCPP___MEMORY_ALLOCATION_GUARD_H
  11. #include <__config>
  12. #include <__memory/allocator_traits.h>
  13. #include <__utility/move.h>
  14. #include <cstddef>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. // Helper class to allocate memory using an Allocator in an exception safe
  20. // manner.
  21. //
  22. // The intended usage of this class is as follows:
  23. //
  24. // 0
  25. // 1 __allocation_guard<SomeAllocator> guard(alloc, 10);
  26. // 2 do_some_initialization_that_may_throw(guard.__get());
  27. // 3 save_allocated_pointer_in_a_noexcept_operation(guard.__release_ptr());
  28. // 4
  29. //
  30. // If line (2) throws an exception during initialization of the memory, the
  31. // guard's destructor will be called, and the memory will be released using
  32. // Allocator deallocation. Otherwise, we release the memory from the guard on
  33. // line (3) in an operation that can't throw -- after that, the guard is not
  34. // responsible for the memory anymore.
  35. //
  36. // This is similar to a unique_ptr, except it's easier to use with a
  37. // custom allocator.
  38. template<class _Alloc>
  39. struct __allocation_guard {
  40. using _Pointer = typename allocator_traits<_Alloc>::pointer;
  41. using _Size = typename allocator_traits<_Alloc>::size_type;
  42. template<class _AllocT> // we perform the allocator conversion inside the constructor
  43. _LIBCPP_HIDE_FROM_ABI
  44. explicit __allocation_guard(_AllocT __alloc, _Size __n)
  45. : __alloc_(_VSTD::move(__alloc))
  46. , __n_(__n)
  47. , __ptr_(allocator_traits<_Alloc>::allocate(__alloc_, __n_)) // initialization order is important
  48. { }
  49. _LIBCPP_HIDE_FROM_ABI
  50. ~__allocation_guard() _NOEXCEPT {
  51. if (__ptr_ != nullptr) {
  52. allocator_traits<_Alloc>::deallocate(__alloc_, __ptr_, __n_);
  53. }
  54. }
  55. _LIBCPP_HIDE_FROM_ABI
  56. _Pointer __release_ptr() _NOEXCEPT { // not called __release() because it's a keyword in objective-c++
  57. _Pointer __tmp = __ptr_;
  58. __ptr_ = nullptr;
  59. return __tmp;
  60. }
  61. _LIBCPP_HIDE_FROM_ABI
  62. _Pointer __get() const _NOEXCEPT {
  63. return __ptr_;
  64. }
  65. private:
  66. _Alloc __alloc_;
  67. _Size __n_;
  68. _Pointer __ptr_;
  69. };
  70. _LIBCPP_END_NAMESPACE_STD
  71. #endif // _LIBCPP___MEMORY_ALLOCATION_GUARD_H