MaybeOwned.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <c10/macros/Macros.h>
  4. #include <c10/util/Exception.h>
  5. #include <memory>
  6. #include <type_traits>
  7. #include <utility>
  8. namespace c10 {
  9. /// MaybeOwnedTraits<T> describes how to borrow from T. Here is how we
  10. /// can implement borrowing from an arbitrary type T using a raw
  11. /// pointer to const:
  12. template <typename T>
  13. struct MaybeOwnedTraitsGenericImpl {
  14. using owned_type = T;
  15. using borrow_type = const T*;
  16. static borrow_type createBorrow(const owned_type& from) {
  17. return &from;
  18. }
  19. static void assignBorrow(borrow_type& lhs, borrow_type rhs) {
  20. lhs = rhs;
  21. }
  22. static void destroyBorrow(borrow_type& /*toDestroy*/) {}
  23. static const owned_type& referenceFromBorrow(const borrow_type& borrow) {
  24. return *borrow;
  25. }
  26. static const owned_type* pointerFromBorrow(const borrow_type& borrow) {
  27. return borrow;
  28. }
  29. static bool debugBorrowIsValid(const borrow_type& borrow) {
  30. return borrow != nullptr;
  31. }
  32. };
  33. /// It is possible to eliminate the extra layer of indirection for
  34. /// borrows for some types that we control. For examples, see
  35. /// intrusive_ptr.h and TensorBody.h.
  36. template <typename T>
  37. struct MaybeOwnedTraits;
  38. // Explicitly enable MaybeOwned<shared_ptr<T>>, rather than allowing
  39. // MaybeOwned to be used for any type right away.
  40. template <typename T>
  41. struct MaybeOwnedTraits<std::shared_ptr<T>>
  42. : public MaybeOwnedTraitsGenericImpl<std::shared_ptr<T>> {};
  43. /// A smart pointer around either a borrowed or owned T. When
  44. /// constructed with borrowed(), the caller MUST ensure that the
  45. /// borrowed-from argument outlives this MaybeOwned<T>. Compare to
  46. /// Rust's std::borrow::Cow
  47. /// (https://doc.rust-lang.org/std/borrow/enum.Cow.html), but note
  48. /// that it is probably not suitable for general use because C++ has
  49. /// no borrow checking. Included here to support
  50. /// Tensor::expect_contiguous.
  51. template <typename T>
  52. class MaybeOwned final {
  53. using borrow_type = typename MaybeOwnedTraits<T>::borrow_type;
  54. using owned_type = typename MaybeOwnedTraits<T>::owned_type;
  55. bool isBorrowed_;
  56. union {
  57. borrow_type borrow_;
  58. owned_type own_;
  59. };
  60. /// Don't use this; use borrowed() instead.
  61. explicit MaybeOwned(const owned_type& t)
  62. : isBorrowed_(true), borrow_(MaybeOwnedTraits<T>::createBorrow(t)) {}
  63. /// Don't use this; use owned() instead.
  64. explicit MaybeOwned(T&& t) noexcept(std::is_nothrow_move_constructible_v<T>)
  65. : isBorrowed_(false), own_(std::move(t)) {}
  66. /// Don't use this; use owned() instead.
  67. template <class... Args>
  68. explicit MaybeOwned(std::in_place_t /*unused*/, Args&&... args)
  69. : isBorrowed_(false), own_(std::forward<Args>(args)...) {}
  70. public:
  71. explicit MaybeOwned() : isBorrowed_(true), borrow_() {}
  72. // Copying a borrow yields another borrow of the original, as with a
  73. // T*. Copying an owned T yields another owned T for safety: no
  74. // chains of borrowing by default! (Note you could get that behavior
  75. // with MaybeOwned<T>::borrowed(*rhs) if you wanted it.)
  76. MaybeOwned(const MaybeOwned& rhs) : isBorrowed_(rhs.isBorrowed_) {
  77. if (C10_LIKELY(rhs.isBorrowed_)) {
  78. MaybeOwnedTraits<T>::assignBorrow(borrow_, rhs.borrow_);
  79. } else {
  80. new (&own_) T(rhs.own_);
  81. }
  82. }
  83. MaybeOwned& operator=(const MaybeOwned& rhs) {
  84. if (this == &rhs) {
  85. return *this;
  86. }
  87. if (C10_UNLIKELY(!isBorrowed_)) {
  88. if (rhs.isBorrowed_) {
  89. own_.~T();
  90. MaybeOwnedTraits<T>::assignBorrow(borrow_, rhs.borrow_);
  91. isBorrowed_ = true;
  92. } else {
  93. own_ = rhs.own_;
  94. }
  95. } else {
  96. if (C10_LIKELY(rhs.isBorrowed_)) {
  97. MaybeOwnedTraits<T>::assignBorrow(borrow_, rhs.borrow_);
  98. } else {
  99. MaybeOwnedTraits<T>::destroyBorrow(borrow_);
  100. new (&own_) T(rhs.own_);
  101. isBorrowed_ = false;
  102. }
  103. }
  104. TORCH_INTERNAL_ASSERT_DEBUG_ONLY(isBorrowed_ == rhs.isBorrowed_);
  105. return *this;
  106. }
  107. MaybeOwned(MaybeOwned&& rhs) noexcept(
  108. // NOLINTNEXTLINE(*-noexcept-move-*)
  109. std::is_nothrow_move_constructible_v<T> &&
  110. std::is_nothrow_move_assignable_v<borrow_type>)
  111. : isBorrowed_(rhs.isBorrowed_) {
  112. if (C10_LIKELY(rhs.isBorrowed_)) {
  113. MaybeOwnedTraits<T>::assignBorrow(borrow_, rhs.borrow_);
  114. } else {
  115. new (&own_) T(std::move(rhs.own_));
  116. }
  117. }
  118. MaybeOwned& operator=(MaybeOwned&& rhs) noexcept(
  119. std::is_nothrow_move_assignable_v<T> &&
  120. std::is_nothrow_move_assignable_v<borrow_type> &&
  121. std::is_nothrow_move_constructible_v<T> &&
  122. // NOLINTNEXTLINE(*-noexcept-move-*)
  123. std::is_nothrow_destructible_v<T> &&
  124. std::is_nothrow_destructible_v<borrow_type>) {
  125. if (this == &rhs) {
  126. return *this;
  127. }
  128. if (C10_UNLIKELY(!isBorrowed_)) {
  129. if (rhs.isBorrowed_) {
  130. own_.~T();
  131. MaybeOwnedTraits<T>::assignBorrow(borrow_, rhs.borrow_);
  132. isBorrowed_ = true;
  133. } else {
  134. own_ = std::move(rhs.own_);
  135. }
  136. } else {
  137. if (C10_LIKELY(rhs.isBorrowed_)) {
  138. MaybeOwnedTraits<T>::assignBorrow(borrow_, rhs.borrow_);
  139. } else {
  140. MaybeOwnedTraits<T>::destroyBorrow(borrow_);
  141. new (&own_) T(std::move(rhs.own_));
  142. isBorrowed_ = false;
  143. }
  144. }
  145. return *this;
  146. }
  147. static MaybeOwned borrowed(const T& t) {
  148. return MaybeOwned(t);
  149. }
  150. static MaybeOwned owned(T&& t) noexcept(
  151. std::is_nothrow_move_constructible_v<T>) {
  152. return MaybeOwned(std::move(t));
  153. }
  154. template <class... Args>
  155. static MaybeOwned owned(std::in_place_t /*unused*/, Args&&... args) {
  156. return MaybeOwned(std::in_place, std::forward<Args>(args)...);
  157. }
  158. ~MaybeOwned() noexcept(
  159. // NOLINTNEXTLINE(*-noexcept-destructor)
  160. std::is_nothrow_destructible_v<T> &&
  161. std::is_nothrow_destructible_v<borrow_type>) {
  162. if (C10_UNLIKELY(!isBorrowed_)) {
  163. own_.~T();
  164. } else {
  165. MaybeOwnedTraits<T>::destroyBorrow(borrow_);
  166. }
  167. }
  168. // This is an implementation detail! You should know what you're doing
  169. // if you are testing this. If you just want to guarantee ownership move
  170. // this into a T
  171. bool unsafeIsBorrowed() const {
  172. return isBorrowed_;
  173. }
  174. const T& operator*() const& {
  175. if (isBorrowed_) {
  176. TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
  177. MaybeOwnedTraits<T>::debugBorrowIsValid(borrow_));
  178. }
  179. return C10_LIKELY(isBorrowed_)
  180. ? MaybeOwnedTraits<T>::referenceFromBorrow(borrow_)
  181. : own_;
  182. }
  183. const T* operator->() const {
  184. if (isBorrowed_) {
  185. TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
  186. MaybeOwnedTraits<T>::debugBorrowIsValid(borrow_));
  187. }
  188. return C10_LIKELY(isBorrowed_)
  189. ? MaybeOwnedTraits<T>::pointerFromBorrow(borrow_)
  190. : &own_;
  191. }
  192. // If borrowed, copy the underlying T. If owned, move from
  193. // it. borrowed/owned state remains the same, and either we
  194. // reference the same borrow as before or we are an owned moved-from
  195. // T.
  196. T operator*() && {
  197. if (isBorrowed_) {
  198. TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
  199. MaybeOwnedTraits<T>::debugBorrowIsValid(borrow_));
  200. return MaybeOwnedTraits<T>::referenceFromBorrow(borrow_);
  201. } else {
  202. return std::move(own_);
  203. }
  204. }
  205. };
  206. } // namespace c10
  207. #else
  208. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  209. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)