allocated_ptr.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Guarded Allocation -*- C++ -*-
  2. // Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/allocated_ptr.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{memory}
  23. */
  24. #ifndef _ALLOCATED_PTR_H
  25. #define _ALLOCATED_PTR_H 1
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0xwarning.h>
  28. #else
  29. # include <type_traits>
  30. # include <bits/ptr_traits.h>
  31. # include <bits/alloc_traits.h>
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /// @cond undocumented
  36. /// Non-standard RAII type for managing pointers obtained from allocators.
  37. template<typename _Alloc>
  38. struct __allocated_ptr
  39. {
  40. using pointer = typename allocator_traits<_Alloc>::pointer;
  41. using value_type = typename allocator_traits<_Alloc>::value_type;
  42. /// Take ownership of __ptr
  43. __allocated_ptr(_Alloc& __a, pointer __ptr) noexcept
  44. : _M_alloc(std::__addressof(__a)), _M_ptr(__ptr)
  45. { }
  46. /// Convert __ptr to allocator's pointer type and take ownership of it
  47. template<typename _Ptr,
  48. typename _Req = _Require<is_same<_Ptr, value_type*>>>
  49. __allocated_ptr(_Alloc& __a, _Ptr __ptr)
  50. : _M_alloc(std::__addressof(__a)),
  51. _M_ptr(pointer_traits<pointer>::pointer_to(*__ptr))
  52. { }
  53. /// Transfer ownership of the owned pointer
  54. __allocated_ptr(__allocated_ptr&& __gd) noexcept
  55. : _M_alloc(__gd._M_alloc), _M_ptr(__gd._M_ptr)
  56. { __gd._M_ptr = nullptr; }
  57. /// Deallocate the owned pointer
  58. ~__allocated_ptr()
  59. {
  60. if (_M_ptr != nullptr)
  61. std::allocator_traits<_Alloc>::deallocate(*_M_alloc, _M_ptr, 1);
  62. }
  63. /// Release ownership of the owned pointer
  64. __allocated_ptr&
  65. operator=(std::nullptr_t) noexcept
  66. {
  67. _M_ptr = nullptr;
  68. return *this;
  69. }
  70. /// Get the address that the owned pointer refers to.
  71. value_type* get() { return std::__to_address(_M_ptr); }
  72. private:
  73. _Alloc* _M_alloc;
  74. pointer _M_ptr;
  75. };
  76. /// Allocate space for a single object using __a
  77. template<typename _Alloc>
  78. __allocated_ptr<_Alloc>
  79. __allocate_guarded(_Alloc& __a)
  80. {
  81. return { __a, std::allocator_traits<_Alloc>::allocate(__a, 1) };
  82. }
  83. /// @endcond
  84. _GLIBCXX_END_NAMESPACE_VERSION
  85. } // namespace std
  86. #endif
  87. #endif