ref-inl.h 15 KB

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