new-inl.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #ifndef NEW_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include new.h"
  3. // For the sake of sane code completion.
  4. #include "new.h"
  5. #endif
  6. #include "ref_tracked.h"
  7. #include <library/cpp/yt/misc/port.h>
  8. #include <library/cpp/yt/malloc//malloc.h>
  9. namespace NYT {
  10. ////////////////////////////////////////////////////////////////////////////////
  11. struct TRefCountedCookieHolder
  12. {
  13. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  14. TRefCountedTypeCookie Cookie = NullRefCountedTypeCookie;
  15. void InitializeTracking(TRefCountedTypeCookie cookie)
  16. {
  17. YT_ASSERT(Cookie == NullRefCountedTypeCookie);
  18. Cookie = cookie;
  19. TRefCountedTrackerFacade::AllocateInstance(Cookie);
  20. }
  21. ~TRefCountedCookieHolder()
  22. {
  23. if (Cookie != NullRefCountedTypeCookie) {
  24. TRefCountedTrackerFacade::FreeInstance(Cookie);
  25. }
  26. }
  27. #endif
  28. };
  29. template <class T>
  30. struct TRefCountedWrapper final
  31. : public T
  32. , public TRefTracked<T>
  33. {
  34. template <class... TArgs>
  35. explicit TRefCountedWrapper(TArgs&&... args)
  36. : T(std::forward<TArgs>(args)...)
  37. { }
  38. ~TRefCountedWrapper() = default;
  39. void DestroyRefCounted() override
  40. {
  41. T::DestroyRefCountedImpl(this);
  42. }
  43. };
  44. template <class T, class TDeleter>
  45. class TRefCountedWrapperWithDeleter final
  46. : public T
  47. , public TRefTracked<T>
  48. {
  49. public:
  50. template <class... TArgs>
  51. explicit TRefCountedWrapperWithDeleter(TDeleter deleter, TArgs&&... args)
  52. : T(std::forward<TArgs>(args)...)
  53. , Deleter_(std::move(deleter))
  54. { }
  55. ~TRefCountedWrapperWithDeleter() = default;
  56. void DestroyRefCounted() override
  57. {
  58. Deleter_(this);
  59. }
  60. private:
  61. const TDeleter Deleter_;
  62. };
  63. template <class T>
  64. struct TRefCountedWrapperWithCookie final
  65. : public T
  66. , public TRefCountedCookieHolder
  67. {
  68. template <class... TArgs>
  69. explicit TRefCountedWrapperWithCookie(TArgs&&... args)
  70. : T(std::forward<TArgs>(args)...)
  71. { }
  72. ~TRefCountedWrapperWithCookie() = default;
  73. void DestroyRefCounted() override
  74. {
  75. T::DestroyRefCountedImpl(this);
  76. }
  77. };
  78. namespace NDetail {
  79. template <class... Args>
  80. Y_FORCE_INLINE void CustomInitialize(Args... args)
  81. {
  82. Y_UNUSED(args...);
  83. }
  84. template <class T>
  85. Y_FORCE_INLINE auto CustomInitialize(T* ptr) -> decltype(&T::InitializeRefCounted, void())
  86. {
  87. ptr->InitializeRefCounted();
  88. }
  89. template <class T, class... As>
  90. Y_FORCE_INLINE T* NewEpilogue(void* ptr, As&& ... args)
  91. {
  92. try {
  93. auto* instance = static_cast<T*>(ptr);
  94. new (instance) T(std::forward<As>(args)...);
  95. CustomInitialize(instance);
  96. return instance;
  97. } catch (...) {
  98. // Do not forget to free the memory.
  99. TFreeMemory<T>::Do(ptr);
  100. throw;
  101. }
  102. }
  103. template <class T, bool = std::derived_from<T, TRefCountedBase>>
  104. struct TConstructHelper
  105. {
  106. static constexpr size_t RefCounterSpace = (sizeof(TRefCounter) + alignof(T) - 1) & ~(alignof(T) - 1);
  107. static constexpr size_t RefCounterOffset = RefCounterSpace - sizeof(TRefCounter);
  108. static constexpr size_t Size = RefCounterSpace + sizeof(T);
  109. static constexpr size_t Alignment = alignof(T);
  110. template <class... As>
  111. Y_FORCE_INLINE static T* Construct(void* ptr, As&&... args)
  112. {
  113. auto* refCounter = reinterpret_cast<TRefCounter*>(static_cast<char*>(ptr) + RefCounterOffset);
  114. new (refCounter) TRefCounter();
  115. auto* object = reinterpret_cast<T*>(refCounter + 1);
  116. if constexpr (std::is_constructible_v<T, As...>) {
  117. new(object) T(std::forward<As>(args)...);
  118. } else {
  119. new(object) T{std::forward<As>(args)...};
  120. }
  121. CustomInitialize(object);
  122. return object;
  123. }
  124. };
  125. template <class T>
  126. struct TConstructHelper<T, true>
  127. {
  128. static constexpr size_t Size = sizeof(TRefCountedWrapper<T>);
  129. static constexpr size_t Alignment = alignof(TRefCountedWrapper<T>);
  130. template <class... As>
  131. Y_FORCE_INLINE static TRefCountedWrapper<T>* Construct(void* ptr, As&&... args)
  132. {
  133. using TDerived = TRefCountedWrapper<T>;
  134. auto* object = new(static_cast<TDerived*>(ptr)) TDerived(std::forward<As>(args)...);
  135. CustomInitialize(object);
  136. return object;
  137. }
  138. };
  139. template <class T, class... As>
  140. Y_FORCE_INLINE TIntrusivePtr<T> SafeConstruct(void* ptr, As&&... args)
  141. {
  142. try {
  143. auto* instance = TConstructHelper<T>::Construct(ptr, std::forward<As>(args)...);
  144. return TIntrusivePtr<T>(instance, /*addReference*/ false);
  145. } catch (...) {
  146. // Do not forget to free the memory.
  147. TFreeMemory<T>::Do(ptr);
  148. throw;
  149. }
  150. }
  151. void AbortOnOom();
  152. template <size_t Size, size_t Alignment>
  153. Y_FORCE_INLINE void* AllocateConstSizeAlignedOrCrash()
  154. {
  155. void* ptr;
  156. #ifdef _win_
  157. ptr = ::aligned_malloc(Size, Alignment);
  158. #else
  159. if (Alignment <= alignof(std::max_align_t)) {
  160. ptr = ::malloc(Size);
  161. } else {
  162. ptr = ::aligned_malloc(Size, Alignment);
  163. }
  164. #endif
  165. if (Y_UNLIKELY(!ptr)) {
  166. AbortOnOom();
  167. }
  168. return ptr;
  169. }
  170. template <size_t Alignment>
  171. Y_FORCE_INLINE void* AllocateOrCrash(size_t size)
  172. {
  173. void* ptr;
  174. #ifdef _win_
  175. ptr = ::aligned_malloc(size, Alignment);
  176. #else
  177. if (Alignment <= alignof(std::max_align_t)) {
  178. ptr = ::malloc(size);
  179. } else {
  180. ptr = ::aligned_malloc(size, Alignment);
  181. }
  182. #endif
  183. if (Y_UNLIKELY(!ptr)) {
  184. AbortOnOom();
  185. }
  186. return ptr;
  187. }
  188. } // namespace NDetail
  189. ////////////////////////////////////////////////////////////////////////////////
  190. template <class T, class... As, class>
  191. Y_FORCE_INLINE TIntrusivePtr<T> New(
  192. As&&... args)
  193. {
  194. void* ptr = NYT::NDetail::AllocateConstSizeAlignedOrCrash<
  195. NYT::NDetail::TConstructHelper<T>::Size,
  196. NYT::NDetail::TConstructHelper<T>::Alignment>();
  197. return NYT::NDetail::SafeConstruct<T>(ptr, std::forward<As>(args)...);
  198. }
  199. template <class T, class... As, class>
  200. Y_FORCE_INLINE TIntrusivePtr<T> TryNew(
  201. typename T::TAllocator* allocator,
  202. As&&... args)
  203. {
  204. auto* ptr = allocator->Allocate(NYT::NDetail::TConstructHelper<T>::Size);
  205. if (Y_UNLIKELY(!ptr)) {
  206. return nullptr;
  207. }
  208. return NYT::NDetail::SafeConstruct<T>(ptr, std::forward<As>(args)...);
  209. }
  210. template <class T, class... As, class>
  211. Y_FORCE_INLINE TIntrusivePtr<T> New(
  212. typename T::TAllocator* allocator,
  213. As&&... args)
  214. {
  215. auto obj = TryNew<T>(allocator, std::forward<As>(args)...);
  216. if (Y_UNLIKELY(!obj)) {
  217. NYT::NDetail::AbortOnOom();
  218. }
  219. return obj;
  220. }
  221. ////////////////////////////////////////////////////////////////////////////////
  222. template <class T, class... As, class>
  223. Y_FORCE_INLINE TIntrusivePtr<T> NewWithExtraSpace(
  224. size_t extraSpaceSize,
  225. As&&... args)
  226. {
  227. auto totalSize = NYT::NDetail::TConstructHelper<T>::Size + extraSpaceSize;
  228. void* ptr = NYT::NDetail::AllocateOrCrash<NYT::NDetail::TConstructHelper<T>::Alignment>(totalSize);
  229. return NYT::NDetail::SafeConstruct<T>(ptr, std::forward<As>(args)...);
  230. }
  231. template <class T, class... As, class>
  232. Y_FORCE_INLINE TIntrusivePtr<T> TryNewWithExtraSpace(
  233. typename T::TAllocator* allocator,
  234. size_t extraSpaceSize,
  235. As&&... args)
  236. {
  237. auto totalSize = NYT::NDetail::TConstructHelper<T>::Size + extraSpaceSize;
  238. auto* ptr = allocator->Allocate(totalSize);
  239. if (Y_UNLIKELY(!ptr)) {
  240. return nullptr;
  241. }
  242. return NYT::NDetail::SafeConstruct<T>(ptr, std::forward<As>(args)...);
  243. }
  244. template <class T, class... As, class>
  245. Y_FORCE_INLINE TIntrusivePtr<T> NewWithExtraSpace(
  246. typename T::TAllocator* allocator,
  247. size_t extraSpaceSize,
  248. As&&... args)
  249. {
  250. auto obj = TryNewWithExtraSpace<T>(allocator, extraSpaceSize, std::forward<As>(args)...);
  251. if (Y_UNLIKELY(!obj)) {
  252. NYT::NDetail::AbortOnOom();
  253. }
  254. return obj;
  255. }
  256. ////////////////////////////////////////////////////////////////////////////////
  257. template <class T, class TDeleter, class... As>
  258. Y_FORCE_INLINE TIntrusivePtr<T> NewWithDeleter(TDeleter deleter, As&&... args)
  259. {
  260. using TWrapper = TRefCountedWrapperWithDeleter<T, TDeleter>;
  261. void* ptr = NYT::NDetail::AllocateConstSizeAlignedOrCrash<sizeof(TWrapper), alignof(TWrapper)>();
  262. auto* instance = NYT::NDetail::NewEpilogue<TWrapper>(
  263. ptr,
  264. std::move(deleter),
  265. std::forward<As>(args)...);
  266. return TIntrusivePtr<T>(instance, /*addReference*/ false);
  267. }
  268. ////////////////////////////////////////////////////////////////////////////////
  269. template <class T, class TTag, int Counter, class... As>
  270. Y_FORCE_INLINE TIntrusivePtr<T> NewWithLocation(
  271. const TSourceLocation& location,
  272. As&&... args)
  273. {
  274. using TWrapper = TRefCountedWrapperWithCookie<T>;
  275. void* ptr = NYT::NDetail::AllocateConstSizeAlignedOrCrash<sizeof(TWrapper), alignof(TWrapper)>();
  276. auto* instance = NYT::NDetail::NewEpilogue<TWrapper>(ptr, std::forward<As>(args)...);
  277. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  278. instance->InitializeTracking(GetRefCountedTypeCookieWithLocation<T, TTag, Counter>(location));
  279. #else
  280. Y_UNUSED(location);
  281. #endif
  282. return TIntrusivePtr<T>(instance, /*addReference*/ false);
  283. }
  284. ////////////////////////////////////////////////////////////////////////////////
  285. template <class T>
  286. const void* TWithExtraSpace<T>::GetExtraSpacePtr() const
  287. {
  288. return static_cast<const T*>(this) + 1;
  289. }
  290. template <class T>
  291. void* TWithExtraSpace<T>::GetExtraSpacePtr()
  292. {
  293. return static_cast<T*>(this) + 1;
  294. }
  295. template <class T>
  296. size_t TWithExtraSpace<T>::GetUsableSpaceSize() const
  297. {
  298. #ifdef _win_
  299. return 0;
  300. #else
  301. return malloc_usable_size(const_cast<T*>(static_cast<const T*>(this))) - sizeof(T);
  302. #endif
  303. }
  304. ////////////////////////////////////////////////////////////////////////////////
  305. } // namespace NYT