ref-inl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #ifndef REF_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include ref.h"
  3. // For the sake of sane code completion.
  4. #include "ref.h"
  5. #endif
  6. namespace NYT {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. namespace NDetail {
  9. extern const char EmptyRefData[];
  10. extern char MutableEmptyRefData[];
  11. } // namespace NDetail
  12. ////////////////////////////////////////////////////////////////////////////////
  13. Y_FORCE_INLINE TRef::TRef(const void* data, size_t size)
  14. : TRange<char>(static_cast<const char*>(data), size)
  15. { }
  16. Y_FORCE_INLINE TRef::TRef(const void* begin, const void* end)
  17. : TRange<char>(static_cast<const char*>(begin), static_cast<const char*>(end))
  18. { }
  19. Y_FORCE_INLINE TRef TRef::MakeEmpty()
  20. {
  21. return TRef(NDetail::EmptyRefData, NDetail::EmptyRefData);
  22. }
  23. Y_FORCE_INLINE TRef TRef::FromString(const TString& str)
  24. {
  25. return FromStringBuf(str);
  26. }
  27. Y_FORCE_INLINE TRef TRef::FromString(const std::string& str)
  28. {
  29. return TRef(str.data(), str.size());
  30. }
  31. Y_FORCE_INLINE TRef TRef::FromStringBuf(TStringBuf strBuf)
  32. {
  33. return TRef(strBuf.data(), strBuf.length());
  34. }
  35. template <class T>
  36. Y_FORCE_INLINE TRef TRef::FromPod(const T& data)
  37. {
  38. static_assert(TTypeTraits<T>::IsPod || (std::is_standard_layout_v<T> && std::is_trivial_v<T>), "T must be a pod-type.");
  39. return TRef(&data, sizeof (data));
  40. }
  41. Y_FORCE_INLINE TStringBuf TRef::ToStringBuf() const
  42. {
  43. return TStringBuf(Begin(), Size());
  44. }
  45. Y_FORCE_INLINE TRef TRef::Slice(size_t startOffset, size_t endOffset) const
  46. {
  47. YT_ASSERT(endOffset >= startOffset && endOffset <= Size());
  48. return TRef(Begin() + startOffset, endOffset - startOffset);
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////
  51. Y_FORCE_INLINE TMutableRef::TMutableRef(void* data, size_t size)
  52. : TMutableRange<char>(static_cast<char*>(data), size)
  53. { }
  54. Y_FORCE_INLINE TMutableRef::TMutableRef(void* begin, void* end)
  55. : TMutableRange<char>(static_cast<char*>(begin), static_cast<char*>(end))
  56. { }
  57. Y_FORCE_INLINE TMutableRef TMutableRef::MakeEmpty()
  58. {
  59. return TMutableRef(NDetail::MutableEmptyRefData, NDetail::MutableEmptyRefData);
  60. }
  61. Y_FORCE_INLINE TMutableRef::operator TRef() const
  62. {
  63. return TRef(Begin(), Size());
  64. }
  65. template <class T>
  66. Y_FORCE_INLINE TMutableRef TMutableRef::FromPod(T& data)
  67. {
  68. static_assert(TTypeTraits<T>::IsPod || (std::is_standard_layout_v<T> && std::is_trivial_v<T>), "T must be a pod-type.");
  69. return TMutableRef(&data, sizeof (data));
  70. }
  71. Y_FORCE_INLINE TMutableRef TMutableRef::FromString(TString& str)
  72. {
  73. // NB: begin() invokes CloneIfShared().
  74. return TMutableRef(str.begin(), str.length());
  75. }
  76. Y_FORCE_INLINE TMutableRef TMutableRef::Slice(size_t startOffset, size_t endOffset) const
  77. {
  78. YT_ASSERT(endOffset >= startOffset && endOffset <= Size());
  79. return TMutableRef(Begin() + startOffset, endOffset - startOffset);
  80. }
  81. ////////////////////////////////////////////////////////////////////////////////
  82. Y_FORCE_INLINE TSharedRef::TSharedRef(TRef ref, TSharedRangeHolderPtr holder)
  83. : TSharedRange<char>(ref, std::move(holder))
  84. { }
  85. Y_FORCE_INLINE TSharedRef::TSharedRef(const void* data, size_t length, TSharedRangeHolderPtr holder)
  86. : TSharedRange<char>(static_cast<const char*>(data), length, std::move(holder))
  87. { }
  88. Y_FORCE_INLINE TSharedRef::TSharedRef(const void* begin, const void* end, TSharedRangeHolderPtr holder)
  89. : TSharedRange<char>(static_cast<const char*>(begin), static_cast<const char*>(end), std::move(holder))
  90. { }
  91. Y_FORCE_INLINE TSharedRef TSharedRef::MakeEmpty()
  92. {
  93. return TSharedRef(TRef::MakeEmpty(), nullptr);
  94. }
  95. Y_FORCE_INLINE TSharedRef::operator TRef() const
  96. {
  97. return TRef(Begin(), Size());
  98. }
  99. template <class TTag>
  100. Y_FORCE_INLINE TSharedRef TSharedRef::FromString(TString str)
  101. {
  102. return FromString(std::move(str), GetRefCountedTypeCookie<TTag>());
  103. }
  104. Y_FORCE_INLINE TSharedRef TSharedRef::FromString(TString str)
  105. {
  106. return FromString<TDefaultSharedBlobTag>(std::move(str));
  107. }
  108. Y_FORCE_INLINE TStringBuf TSharedRef::ToStringBuf() const
  109. {
  110. return TStringBuf(Begin(), Size());
  111. }
  112. template <class TTag>
  113. Y_FORCE_INLINE TSharedRef TSharedRef::MakeCopy(TRef ref)
  114. {
  115. return MakeCopy(ref, GetRefCountedTypeCookie<TTag>());
  116. }
  117. Y_FORCE_INLINE TSharedRef TSharedRef::Slice(size_t startOffset, size_t endOffset) const
  118. {
  119. YT_ASSERT(endOffset >= startOffset && endOffset <= Size());
  120. return TSharedRef(Begin() + startOffset, endOffset - startOffset, Holder_);
  121. }
  122. Y_FORCE_INLINE TSharedRef TSharedRef::Slice(const void* begin, const void* end) const
  123. {
  124. YT_ASSERT(begin >= Begin());
  125. YT_ASSERT(end <= End());
  126. return TSharedRef(begin, end, Holder_);
  127. }
  128. ////////////////////////////////////////////////////////////////////////////////
  129. Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(const TMutableRef& ref, TSharedRangeHolderPtr holder)
  130. : TSharedMutableRange<char>(ref, std::move(holder))
  131. { }
  132. Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* data, size_t length, TSharedRangeHolderPtr holder)
  133. : TSharedMutableRange<char>(static_cast<char*>(data), length, std::move(holder))
  134. { }
  135. Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* begin, void* end, TSharedRangeHolderPtr holder)
  136. : TSharedMutableRange<char>(static_cast<char*>(begin), static_cast<char*>(end), std::move(holder))
  137. { }
  138. Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::MakeEmpty()
  139. {
  140. return TSharedMutableRef(TMutableRef::MakeEmpty(), nullptr);
  141. }
  142. Y_FORCE_INLINE TSharedMutableRef::operator TMutableRef() const
  143. {
  144. return TMutableRef(Begin(), Size());
  145. }
  146. Y_FORCE_INLINE TSharedMutableRef::operator TSharedRef() const
  147. {
  148. return TSharedRef(Begin(), Size(), Holder_);
  149. }
  150. Y_FORCE_INLINE TSharedMutableRef::operator TRef() const
  151. {
  152. return TRef(Begin(), Size());
  153. }
  154. Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Allocate(size_t size, TSharedMutableRefAllocateOptions options)
  155. {
  156. return Allocate<TDefaultSharedBlobTag>(size, options);
  157. }
  158. Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::AllocatePageAligned(size_t size, TSharedMutableRefAllocateOptions options)
  159. {
  160. return AllocatePageAligned<TDefaultSharedBlobTag>(size, options);
  161. }
  162. template <class TTag>
  163. Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::MakeCopy(TRef ref)
  164. {
  165. return MakeCopy(ref, GetRefCountedTypeCookie<TTag>());
  166. }
  167. Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Slice(size_t startOffset, size_t endOffset) const
  168. {
  169. YT_ASSERT(endOffset >= startOffset && endOffset <= Size());
  170. return TSharedMutableRef(Begin() + startOffset, endOffset - startOffset, Holder_);
  171. }
  172. Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Slice(void* begin, void* end) const
  173. {
  174. YT_ASSERT(begin >= Begin());
  175. YT_ASSERT(end <= End());
  176. return TSharedMutableRef(begin, end, Holder_);
  177. }
  178. template <class TTag>
  179. Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Allocate(size_t size, TSharedMutableRefAllocateOptions options)
  180. {
  181. return Allocate(size, options, GetRefCountedTypeCookie<TTag>());
  182. }
  183. template <class TTag>
  184. Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::AllocatePageAligned(size_t size, TSharedMutableRefAllocateOptions options)
  185. {
  186. return AllocatePageAligned(size, options, GetRefCountedTypeCookie<TTag>());
  187. }
  188. ////////////////////////////////////////////////////////////////////////////////
  189. Y_FORCE_INLINE size_t GetByteSize(TRef ref)
  190. {
  191. return ref ? ref.Size() : 0;
  192. }
  193. template <class T>
  194. size_t GetByteSize(TRange<T> parts)
  195. {
  196. size_t size = 0;
  197. for (const auto& part : parts) {
  198. size += part.Size();
  199. }
  200. return size;
  201. }
  202. template <class T>
  203. size_t GetByteSize(const std::vector<T>& parts)
  204. {
  205. return GetByteSize(MakeRange(parts));
  206. }
  207. ////////////////////////////////////////////////////////////////////////////////
  208. class TSharedRefArrayImpl
  209. : public TSharedRangeHolder
  210. , public TWithExtraSpace<TSharedRefArrayImpl>
  211. {
  212. public:
  213. TSharedRefArrayImpl(
  214. size_t extraSpaceSize,
  215. TRefCountedTypeCookie tagCookie,
  216. size_t size)
  217. : Size_(size)
  218. , ExtraSpaceSize_(extraSpaceSize)
  219. , TagCookie_(tagCookie)
  220. {
  221. for (size_t index = 0; index < Size_; ++index) {
  222. new (MutableBegin() + index) TSharedRef();
  223. }
  224. RegisterWithRefCountedTracker();
  225. }
  226. TSharedRefArrayImpl(
  227. size_t extraSpaceSize,
  228. TRefCountedTypeCookie tagCookie,
  229. const TSharedRef& part)
  230. : Size_(1)
  231. , ExtraSpaceSize_(extraSpaceSize)
  232. , TagCookie_(tagCookie)
  233. {
  234. new (MutableBegin()) TSharedRef(part);
  235. RegisterWithRefCountedTracker();
  236. }
  237. TSharedRefArrayImpl(
  238. size_t extraSpaceSize,
  239. TRefCountedTypeCookie tagCookie,
  240. TSharedRef&& part)
  241. : Size_(1)
  242. , ExtraSpaceSize_(extraSpaceSize)
  243. , TagCookie_(tagCookie)
  244. {
  245. new (MutableBegin()) TSharedRef(std::move(part));
  246. RegisterWithRefCountedTracker();
  247. }
  248. template <class TParts>
  249. TSharedRefArrayImpl(
  250. size_t extraSpaceSize,
  251. TRefCountedTypeCookie tagCookie,
  252. const TParts& parts,
  253. TSharedRefArray::TCopyParts)
  254. : Size_(parts.size())
  255. , ExtraSpaceSize_(extraSpaceSize)
  256. , TagCookie_(tagCookie)
  257. {
  258. for (size_t index = 0; index < Size_; ++index) {
  259. new (MutableBegin() + index) TSharedRef(parts[index]);
  260. }
  261. RegisterWithRefCountedTracker();
  262. }
  263. template <class TParts>
  264. TSharedRefArrayImpl(
  265. size_t extraSpaceSize,
  266. TRefCountedTypeCookie tagCookie,
  267. TParts&& parts,
  268. TSharedRefArray::TMoveParts)
  269. : Size_(parts.size())
  270. , ExtraSpaceSize_(extraSpaceSize)
  271. , TagCookie_(tagCookie)
  272. {
  273. for (size_t index = 0; index < Size_; ++index) {
  274. new (MutableBegin() + index) TSharedRef(std::move(parts[index]));
  275. }
  276. RegisterWithRefCountedTracker();
  277. }
  278. ~TSharedRefArrayImpl()
  279. {
  280. for (size_t index = 0; index < Size_; ++index) {
  281. auto& part = MutableBegin()[index];
  282. if (part.GetHolder() == this) {
  283. part.Holder_.Release();
  284. }
  285. part.TSharedRef::~TSharedRef();
  286. }
  287. UnregisterFromRefCountedTracker();
  288. }
  289. size_t Size() const
  290. {
  291. return Size_;
  292. }
  293. bool Empty() const
  294. {
  295. return Size_ == 0;
  296. }
  297. const TSharedRef& operator [] (size_t index) const
  298. {
  299. YT_ASSERT(index < Size());
  300. return Begin()[index];
  301. }
  302. const TSharedRef* Begin() const
  303. {
  304. return static_cast<const TSharedRef*>(GetExtraSpacePtr());
  305. }
  306. const TSharedRef* End() const
  307. {
  308. return Begin() + Size_;
  309. }
  310. // TSharedRangeHolder overrides.
  311. std::optional<size_t> GetTotalByteSize() const override
  312. {
  313. size_t result = 0;
  314. for (size_t index = 0; index < Size(); ++index) {
  315. const auto& part = (*this)[index];
  316. if (!part) {
  317. continue;
  318. }
  319. auto partSize = part.GetHolder()->GetTotalByteSize();
  320. if (!partSize) {
  321. return std::nullopt;
  322. }
  323. result += *partSize;
  324. }
  325. return result;
  326. }
  327. private:
  328. friend class TSharedRefArrayBuilder;
  329. const size_t Size_;
  330. const size_t ExtraSpaceSize_;
  331. const TRefCountedTypeCookie TagCookie_;
  332. void RegisterWithRefCountedTracker()
  333. {
  334. TRefCountedTrackerFacade::AllocateTagInstance(TagCookie_);
  335. TRefCountedTrackerFacade::AllocateSpace(TagCookie_, ExtraSpaceSize_);
  336. }
  337. void UnregisterFromRefCountedTracker()
  338. {
  339. TRefCountedTrackerFacade::FreeTagInstance(TagCookie_);
  340. TRefCountedTrackerFacade::FreeSpace(TagCookie_, ExtraSpaceSize_);
  341. }
  342. TSharedRef* MutableBegin()
  343. {
  344. return static_cast<TSharedRef*>(GetExtraSpacePtr());
  345. }
  346. TSharedRef* MutableEnd()
  347. {
  348. return MutableBegin() + Size_;
  349. }
  350. char* GetBeginAllocationPtr()
  351. {
  352. return static_cast<char*>(static_cast<void*>(MutableEnd()));
  353. }
  354. };
  355. DEFINE_REFCOUNTED_TYPE(TSharedRefArrayImpl)
  356. ////////////////////////////////////////////////////////////////////////////////
  357. struct TSharedRefArrayTag { };
  358. Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(TIntrusivePtr<TSharedRefArrayImpl> impl)
  359. : Impl_(std::move(impl))
  360. { }
  361. Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(const TSharedRefArray& other)
  362. : Impl_(other.Impl_)
  363. { }
  364. Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(TSharedRefArray&& other) noexcept
  365. : Impl_(std::move(other.Impl_))
  366. { }
  367. Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(const TSharedRef& part)
  368. : Impl_(NewImpl(1, 0, GetRefCountedTypeCookie<TSharedRefArrayTag>(), part))
  369. { }
  370. Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(TSharedRef&& part)
  371. : Impl_(NewImpl(1, 0, GetRefCountedTypeCookie<TSharedRefArrayTag>(), std::move(part)))
  372. { }
  373. template <class TParts>
  374. Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(const TParts& parts, TSharedRefArray::TCopyParts)
  375. : Impl_(NewImpl(parts.size(), 0, GetRefCountedTypeCookie<TSharedRefArrayTag>(), parts, TSharedRefArray::TCopyParts{}))
  376. { }
  377. template <class TParts>
  378. Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(TParts&& parts, TSharedRefArray::TMoveParts)
  379. : Impl_(NewImpl(parts.size(), 0, GetRefCountedTypeCookie<TSharedRefArrayTag>(), std::move(parts), TSharedRefArray::TMoveParts{}))
  380. { }
  381. Y_FORCE_INLINE TSharedRefArray& TSharedRefArray::operator=(const TSharedRefArray& other)
  382. {
  383. Impl_ = other.Impl_;
  384. return *this;
  385. }
  386. Y_FORCE_INLINE TSharedRefArray& TSharedRefArray::operator=(TSharedRefArray&& other)
  387. {
  388. Impl_ = std::move(other.Impl_);
  389. return *this;
  390. }
  391. Y_FORCE_INLINE void TSharedRefArray::Reset()
  392. {
  393. Impl_.Reset();
  394. }
  395. Y_FORCE_INLINE TSharedRefArray::operator bool() const
  396. {
  397. return Impl_.operator bool();
  398. }
  399. Y_FORCE_INLINE size_t TSharedRefArray::Size() const
  400. {
  401. return Impl_ ? Impl_->Size() : 0;
  402. }
  403. Y_FORCE_INLINE size_t TSharedRefArray::size() const
  404. {
  405. return Impl_ ? Impl_->Size() : 0;
  406. }
  407. Y_FORCE_INLINE bool TSharedRefArray::Empty() const
  408. {
  409. return Impl_ ? Impl_->Empty() : true;
  410. }
  411. Y_FORCE_INLINE const TSharedRef& TSharedRefArray::operator[](size_t index) const
  412. {
  413. YT_ASSERT(Impl_);
  414. return (*Impl_)[index];
  415. }
  416. Y_FORCE_INLINE const TSharedRef* TSharedRefArray::Begin() const
  417. {
  418. return Impl_ ? Impl_->Begin() : nullptr;
  419. }
  420. Y_FORCE_INLINE const TSharedRef* TSharedRefArray::End() const
  421. {
  422. return Impl_ ? Impl_->End() : nullptr;
  423. }
  424. template <class... As>
  425. TSharedRefArrayImplPtr TSharedRefArray::NewImpl(
  426. size_t size,
  427. size_t poolCapacity,
  428. TRefCountedTypeCookie tagCookie,
  429. As&&... args)
  430. {
  431. auto extraSpaceSize = sizeof (TSharedRef) * size + poolCapacity;
  432. return NewWithExtraSpace<TSharedRefArrayImpl>(
  433. extraSpaceSize,
  434. extraSpaceSize,
  435. tagCookie,
  436. std::forward<As>(args)...);
  437. }
  438. Y_FORCE_INLINE const TSharedRef* begin(const TSharedRefArray& array)
  439. {
  440. return array.Begin();
  441. }
  442. Y_FORCE_INLINE const TSharedRef* end(const TSharedRefArray& array)
  443. {
  444. return array.End();
  445. }
  446. ////////////////////////////////////////////////////////////////////////////////
  447. } // namespace NYT