new-inl.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. template <size_t Size, size_t Alignment>
  152. void* AllocateConstSizeAligned()
  153. {
  154. #ifdef _win_
  155. return ::aligned_malloc(Size, Alignment);
  156. #else
  157. if (Alignment <= alignof(std::max_align_t)) {
  158. return ::malloc(Size);
  159. } else {
  160. return ::aligned_malloc(Size, Alignment);
  161. }
  162. #endif
  163. }
  164. } // namespace NDetail
  165. ////////////////////////////////////////////////////////////////////////////////
  166. template <class T, class... As, class>
  167. Y_FORCE_INLINE TIntrusivePtr<T> New(
  168. As&&... args)
  169. {
  170. void* ptr = NYT::NDetail::AllocateConstSizeAligned<
  171. NYT::NDetail::TConstructHelper<T>::Size,
  172. NYT::NDetail::TConstructHelper<T>::Alignment>();
  173. return NYT::NDetail::SafeConstruct<T>(ptr, std::forward<As>(args)...);
  174. }
  175. template <class T, class... As, class>
  176. Y_FORCE_INLINE TIntrusivePtr<T> New(
  177. typename T::TAllocator* allocator,
  178. As&&... args)
  179. {
  180. auto* ptr = allocator->Allocate(NYT::NDetail::TConstructHelper<T>::Size);
  181. if (!ptr) {
  182. return nullptr;
  183. }
  184. return NYT::NDetail::SafeConstruct<T>(ptr, std::forward<As>(args)...);
  185. }
  186. ////////////////////////////////////////////////////////////////////////////////
  187. template <class T, class... As, class>
  188. Y_FORCE_INLINE TIntrusivePtr<T> NewWithExtraSpace(
  189. size_t extraSpaceSize,
  190. As&&... args)
  191. {
  192. auto totalSize = NYT::NDetail::TConstructHelper<T>::Size + extraSpaceSize;
  193. void* ptr = nullptr;
  194. #ifdef _win_
  195. ptr = ::aligned_malloc(totalSize, NYT::NDetail::TConstructHelper<T>::Alignment);
  196. #else
  197. if (NYT::NDetail::TConstructHelper<T>::Alignment <= alignof(std::max_align_t)) {
  198. ptr = ::malloc(totalSize);
  199. } else {
  200. ptr = ::aligned_malloc(totalSize, NYT::NDetail::TConstructHelper<T>::Alignment);
  201. }
  202. #endif
  203. return NYT::NDetail::SafeConstruct<T>(ptr, std::forward<As>(args)...);
  204. }
  205. template <class T, class... As, class>
  206. Y_FORCE_INLINE TIntrusivePtr<T> NewWithExtraSpace(
  207. typename T::TAllocator* allocator,
  208. size_t extraSpaceSize,
  209. As&&... args)
  210. {
  211. auto totalSize = NYT::NDetail::TConstructHelper<T>::Size + extraSpaceSize;
  212. auto* ptr = allocator->Allocate(totalSize);
  213. if (!ptr) {
  214. return nullptr;
  215. }
  216. return NYT::NDetail::SafeConstruct<T>(ptr, std::forward<As>(args)...);
  217. }
  218. ////////////////////////////////////////////////////////////////////////////////
  219. template <class T, class TDeleter, class... As>
  220. Y_FORCE_INLINE TIntrusivePtr<T> NewWithDeleter(TDeleter deleter, As&&... args)
  221. {
  222. using TWrapper = TRefCountedWrapperWithDeleter<T, TDeleter>;
  223. void* ptr = NYT::NDetail::AllocateConstSizeAligned<sizeof(TWrapper), alignof(TWrapper)>();
  224. auto* instance = NYT::NDetail::NewEpilogue<TWrapper>(
  225. ptr,
  226. std::move(deleter),
  227. std::forward<As>(args)...);
  228. return TIntrusivePtr<T>(instance, /*addReference*/ false);
  229. }
  230. ////////////////////////////////////////////////////////////////////////////////
  231. template <class T, class TTag, int Counter, class... As>
  232. Y_FORCE_INLINE TIntrusivePtr<T> NewWithLocation(
  233. const TSourceLocation& location,
  234. As&&... args)
  235. {
  236. using TWrapper = TRefCountedWrapperWithCookie<T>;
  237. void* ptr = NYT::NDetail::AllocateConstSizeAligned<sizeof(TWrapper), alignof(TWrapper)>();
  238. auto* instance = NYT::NDetail::NewEpilogue<TWrapper>(ptr, std::forward<As>(args)...);
  239. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  240. instance->InitializeTracking(GetRefCountedTypeCookieWithLocation<T, TTag, Counter>(location));
  241. #else
  242. Y_UNUSED(location);
  243. #endif
  244. return TIntrusivePtr<T>(instance, /*addReference*/ false);
  245. }
  246. ////////////////////////////////////////////////////////////////////////////////
  247. template <class T>
  248. const void* TWithExtraSpace<T>::GetExtraSpacePtr() const
  249. {
  250. return static_cast<const T*>(this) + 1;
  251. }
  252. template <class T>
  253. void* TWithExtraSpace<T>::GetExtraSpacePtr()
  254. {
  255. return static_cast<T*>(this) + 1;
  256. }
  257. template <class T>
  258. size_t TWithExtraSpace<T>::GetUsableSpaceSize() const
  259. {
  260. #ifdef _win_
  261. return 0;
  262. #else
  263. return malloc_usable_size(const_cast<T*>(static_cast<const T*>(this))) - sizeof(T);
  264. #endif
  265. }
  266. ////////////////////////////////////////////////////////////////////////////////
  267. } // namespace NYT