allocation_guard.h 3.6 KB

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