new-inl.h 9.7 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. #include <library/cpp/yt/system/exit.h>
  10. namespace NYT {
  11. ////////////////////////////////////////////////////////////////////////////////
  12. struct TRefCountedCookieHolder
  13. {
  14. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  15. TRefCountedTypeCookie Cookie = NullRefCountedTypeCookie;
  16. void InitializeTracking(TRefCountedTypeCookie cookie)
  17. {
  18. YT_ASSERT(Cookie == NullRefCountedTypeCookie);
  19. Cookie = cookie;
  20. TRefCountedTrackerFacade::AllocateInstance(Cookie);
  21. }
  22. ~TRefCountedCookieHolder()
  23. {
  24. if (Cookie != NullRefCountedTypeCookie) {
  25. TRefCountedTrackerFacade::FreeInstance(Cookie);
  26. }
  27. }
  28. #endif
  29. };
  30. template <class T>
  31. struct TRefCountedWrapper final
  32. : public T
  33. , public TRefTracked<T>
  34. {
  35. template <class... TArgs>
  36. explicit TRefCountedWrapper(TArgs&&... args)
  37. : T(std::forward<TArgs>(args)...)
  38. { }
  39. ~TRefCountedWrapper() = default;
  40. void DestroyRefCounted() override
  41. {
  42. T::DestroyRefCountedImpl(this);
  43. }
  44. };
  45. template <class T, class TDeleter>
  46. class TRefCountedWrapperWithDeleter final
  47. : public T
  48. , public TRefTracked<T>
  49. {
  50. public:
  51. template <class... TArgs>
  52. explicit TRefCountedWrapperWithDeleter(TDeleter deleter, TArgs&&... args)
  53. : T(std::forward<TArgs>(args)...)
  54. , Deleter_(std::move(deleter))
  55. { }
  56. ~TRefCountedWrapperWithDeleter() = default;
  57. void DestroyRefCounted() override
  58. {
  59. Deleter_(this);
  60. }
  61. private:
  62. const TDeleter Deleter_;
  63. };
  64. template <class T>
  65. struct TRefCountedWrapperWithCookie final
  66. : public T
  67. , public TRefCountedCookieHolder
  68. {
  69. template <class... TArgs>
  70. explicit TRefCountedWrapperWithCookie(TArgs&&... args)
  71. : T(std::forward<TArgs>(args)...)
  72. { }
  73. ~TRefCountedWrapperWithCookie() = default;
  74. void DestroyRefCounted() override
  75. {
  76. T::DestroyRefCountedImpl(this);
  77. }
  78. };
  79. namespace NDetail {
  80. template <class... Args>
  81. Y_FORCE_INLINE void CustomInitialize(Args... args)
  82. {
  83. Y_UNUSED(args...);
  84. }
  85. template <class T>
  86. Y_FORCE_INLINE auto CustomInitialize(T* ptr) -> decltype(&T::InitializeRefCounted, void())
  87. {
  88. ptr->InitializeRefCounted();
  89. }
  90. template <class T, class... As>
  91. Y_FORCE_INLINE T* NewEpilogue(void* ptr, As&& ... args)
  92. {
  93. try {
  94. auto* instance = static_cast<T*>(ptr);
  95. new (instance) T(std::forward<As>(args)...);
  96. CustomInitialize(instance);
  97. return instance;
  98. } catch (...) {
  99. // Do not forget to free the memory.
  100. TFreeMemory<T>::Do(ptr);
  101. throw;
  102. }
  103. }
  104. template <class T, bool = std::derived_from<T, TRefCountedBase>>
  105. struct TConstructHelper
  106. {
  107. static constexpr size_t RefCounterSpace = (sizeof(TRefCounter) + alignof(T) - 1) & ~(alignof(T) - 1);
  108. static constexpr size_t RefCounterOffset = RefCounterSpace - sizeof(TRefCounter);
  109. static constexpr size_t Size = RefCounterSpace + sizeof(T);
  110. static constexpr size_t Alignment = alignof(T);
  111. template <class... As>
  112. Y_FORCE_INLINE static T* Construct(void* ptr, As&&... args)
  113. {
  114. auto* refCounter = reinterpret_cast<TRefCounter*>(static_cast<char*>(ptr) + RefCounterOffset);
  115. new (refCounter) TRefCounter();
  116. auto* object = reinterpret_cast<T*>(refCounter + 1);
  117. if constexpr (std::is_constructible_v<T, As...>) {
  118. new(object) T(std::forward<As>(args)...);
  119. } else {
  120. new(object) T{std::forward<As>(args)...};
  121. }
  122. CustomInitialize(object);
  123. return object;
  124. }
  125. };
  126. template <class T>
  127. struct TConstructHelper<T, true>
  128. {
  129. static constexpr size_t Size = sizeof(TRefCountedWrapper<T>);
  130. static constexpr size_t Alignment = alignof(TRefCountedWrapper<T>);
  131. template <class... As>
  132. Y_FORCE_INLINE static TRefCountedWrapper<T>* Construct(void* ptr, As&&... args)
  133. {
  134. using TDerived = TRefCountedWrapper<T>;
  135. auto* object = new(static_cast<TDerived*>(ptr)) TDerived(std::forward<As>(args)...);
  136. CustomInitialize(object);
  137. return object;
  138. }
  139. };
  140. template <class T, class... As>
  141. Y_FORCE_INLINE TIntrusivePtr<T> SafeConstruct(void* ptr, As&&... args)
  142. {
  143. try {
  144. auto* instance = TConstructHelper<T>::Construct(ptr, std::forward<As>(args)...);
  145. return TIntrusivePtr<T>(instance, /*addReference*/ false);
  146. } catch (...) {
  147. // Do not forget to free the memory.
  148. TFreeMemory<T>::Do(ptr);
  149. throw;
  150. }
  151. }
  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. AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory));
  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. AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory));
  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. AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory));
  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. AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory));
  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