diff options
author | klyachin <klyachin@yandex-team.ru> | 2022-02-10 16:50:49 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:49 +0300 |
commit | e6be5cdb79b60e82133df0867c6ca8b7597e1329 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 | |
parent | f821209587857f439489e1cdf0a9102225809935 (diff) | |
download | ydb-e6be5cdb79b60e82133df0867c6ca8b7597e1329.tar.gz |
Restoring authorship annotation for <klyachin@yandex-team.ru>. Commit 2 of 2.
-rw-r--r-- | library/cpp/yt/memory/intrusive_ptr.h | 16 | ||||
-rw-r--r-- | library/cpp/yt/memory/new-inl.h | 106 | ||||
-rw-r--r-- | library/cpp/yt/memory/new.h | 38 | ||||
-rw-r--r-- | library/cpp/yt/memory/ref.cpp | 6 | ||||
-rw-r--r-- | library/cpp/yt/memory/ref_counted-inl.h | 54 | ||||
-rw-r--r-- | library/cpp/yt/memory/ref_counted.h | 16 | ||||
-rw-r--r-- | library/cpp/yt/memory/weak_ptr.h | 4 |
7 files changed, 120 insertions, 120 deletions
diff --git a/library/cpp/yt/memory/intrusive_ptr.h b/library/cpp/yt/memory/intrusive_ptr.h index 84f11ae6f6..3dead7db1d 100644 --- a/library/cpp/yt/memory/intrusive_ptr.h +++ b/library/cpp/yt/memory/intrusive_ptr.h @@ -148,14 +148,14 @@ public: return T_; } - //! Returns the pointer and releases the ownership. - T* Release() noexcept - { - auto* p = T_; - T_ = nullptr; - return p; - } - + //! Returns the pointer and releases the ownership. + T* Release() noexcept + { + auto* p = T_; + T_ = nullptr; + return p; + } + T& operator*() const noexcept { YT_ASSERT(T_); diff --git a/library/cpp/yt/memory/new-inl.h b/library/cpp/yt/memory/new-inl.h index fe07440e30..0a84818516 100644 --- a/library/cpp/yt/memory/new-inl.h +++ b/library/cpp/yt/memory/new-inl.h @@ -1,15 +1,15 @@ -#ifndef NEW_INL_H_ -#error "Direct inclusion of this file is not allowed, include new.h" +#ifndef NEW_INL_H_ +#error "Direct inclusion of this file is not allowed, include new.h" // For the sake of sane code completion. #include "new.h" -#endif - +#endif + #include <library/cpp/ytalloc/api/ytalloc.h> -namespace NYT { - -//////////////////////////////////////////////////////////////////////////////// - +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + struct TRefCountedCookieHolder { #ifdef YT_ENABLE_REF_COUNTED_TRACKING @@ -31,7 +31,7 @@ struct TRefCountedCookieHolder #endif }; -template <class T> +template <class T> struct TRefCountedWrapper final : public T , public TRefTracked<T> @@ -81,7 +81,7 @@ struct TRefCountedWrapperWithCookie final explicit TRefCountedWrapperWithCookie(TArgs&&... args) : T(std::forward<TArgs>(args)...) { } - + ~TRefCountedWrapperWithCookie() = default; void DestroyRefCounted() override @@ -89,9 +89,9 @@ struct TRefCountedWrapperWithCookie final T::DestroyRefCountedImpl(this); } }; - + namespace NDetail { - + Y_FORCE_INLINE void* AllignedMalloc(size_t size, size_t allignment) { #ifdef _win_ @@ -170,9 +170,9 @@ struct TConstructHelper<T, true> } }; -template <class T, class... As> +template <class T, class... As> Y_FORCE_INLINE TIntrusivePtr<T> SafeConstruct(void* ptr, As&&... args) -{ +{ try { auto* instance = TConstructHelper<T>::Construct(ptr, std::forward<As>(args)...); return TIntrusivePtr<T>(instance, false); @@ -181,8 +181,8 @@ Y_FORCE_INLINE TIntrusivePtr<T> SafeConstruct(void* ptr, As&&... args) TFreeMemory<T>::Do(ptr); throw; } -} - +} + template <size_t Size, size_t Alignment> void* AllocateConstSizeAligned() { @@ -193,14 +193,14 @@ void* AllocateConstSizeAligned() } } -} // namespace NDetail - +} // namespace NDetail + //////////////////////////////////////////////////////////////////////////////// template <class T, class... As, class> Y_FORCE_INLINE TIntrusivePtr<T> New( As&&... args) -{ +{ void* ptr = NDetail::AllocateConstSizeAligned< NDetail::TConstructHelper<T>::Size, NDetail::TConstructHelper<T>::Alignment>(); @@ -218,15 +218,15 @@ Y_FORCE_INLINE TIntrusivePtr<T> New( return nullptr; } return NDetail::SafeConstruct<T>(ptr, std::forward<As>(args)...); -} - +} + //////////////////////////////////////////////////////////////////////////////// template <class T, class... As, class> Y_FORCE_INLINE TIntrusivePtr<T> NewWithExtraSpace( - size_t extraSpaceSize, - As&&... args) -{ + size_t extraSpaceSize, + As&&... args) +{ auto totalSize = NYT::NDetail::TConstructHelper<T>::Size + extraSpaceSize; void* ptr = nullptr; @@ -268,43 +268,43 @@ Y_FORCE_INLINE TIntrusivePtr<T> NewWithDelete(const TDeleter& deleter, As&&... a std::forward<As>(args)...); return TIntrusivePtr<T>(instance, false); -} - +} + //////////////////////////////////////////////////////////////////////////////// -template <class T, class TTag, int Counter, class... As> -Y_FORCE_INLINE TIntrusivePtr<T> NewWithLocation( - const TSourceLocation& location, - As&&... args) -{ +template <class T, class TTag, int Counter, class... As> +Y_FORCE_INLINE TIntrusivePtr<T> NewWithLocation( + const TSourceLocation& location, + As&&... args) +{ using TWrapper = TRefCountedWrapperWithCookie<T>; void* ptr = NDetail::AllocateConstSizeAligned<sizeof(TWrapper), alignof(TWrapper)>(); auto* instance = NDetail::NewEpilogue<TWrapper>(ptr, std::forward<As>(args)...); -#ifdef YT_ENABLE_REF_COUNTED_TRACKING +#ifdef YT_ENABLE_REF_COUNTED_TRACKING instance->InitializeTracking(GetRefCountedTypeCookieWithLocation<T, TTag, Counter>(location)); -#else +#else Y_UNUSED(location); -#endif +#endif return TIntrusivePtr<T>(instance, false); -} - -//////////////////////////////////////////////////////////////////////////////// - -template <class T> -const void* TWithExtraSpace<T>::GetExtraSpacePtr() const -{ - return static_cast<const T*>(this) + 1; -} - -template <class T> -void* TWithExtraSpace<T>::GetExtraSpacePtr() -{ - return static_cast<T*>(this) + 1; -} - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT +} + +//////////////////////////////////////////////////////////////////////////////// + +template <class T> +const void* TWithExtraSpace<T>::GetExtraSpacePtr() const +{ + return static_cast<const T*>(this) + 1; +} + +template <class T> +void* TWithExtraSpace<T>::GetExtraSpacePtr() +{ + return static_cast<T*>(this) + 1; +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yt/memory/new.h b/library/cpp/yt/memory/new.h index fafdcfcd16..2db45e0465 100644 --- a/library/cpp/yt/memory/new.h +++ b/library/cpp/yt/memory/new.h @@ -79,9 +79,9 @@ struct THasAllocator<T, std::void_t<typename T::TAllocator>> //////////////////////////////////////////////////////////////////////////////// -//! Allocates a new instance of |T|. +//! Allocates a new instance of |T|. template <class T, class... As, class = typename THasAllocator<T>::TFalse> -TIntrusivePtr<T> New(As&&... args); +TIntrusivePtr<T> New(As&&... args); template <class T, class... As, class = typename THasAllocator<T>::TTrue> TIntrusivePtr<T> New(typename T::TAllocator* allocator, As&&... args); @@ -97,31 +97,31 @@ TIntrusivePtr<T> NewWithExtraSpace(typename T::TAllocator* allocator, size_t ext template <class T, class TDeleter, class... As> TIntrusivePtr<T> NewWithDelete(const TDeleter& deleter, As&&... args); -//! Allocates a new instance of |T|. -//! The allocation is additionally marked with #location. -template <class T, class TTag, int Counter, class... As> +//! Allocates a new instance of |T|. +//! The allocation is additionally marked with #location. +template <class T, class TTag, int Counter, class... As> TIntrusivePtr<T> NewWithLocation(const TSourceLocation& location, As&&... args); -//! Enables calling #New and co for types with private ctors. -#define DECLARE_NEW_FRIEND() \ +//! Enables calling #New and co for types with private ctors. +#define DECLARE_NEW_FRIEND() \ template <class DECLARE_NEW_FRIEND_T> \ friend struct NYT::TRefCountedWrapper; -//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// -//! CRTP mixin enabling access to instance's extra space. -template <class T> -class TWithExtraSpace +//! CRTP mixin enabling access to instance's extra space. +template <class T> +class TWithExtraSpace { -protected: - const void* GetExtraSpacePtr() const; - void* GetExtraSpacePtr(); -}; +protected: + const void* GetExtraSpacePtr() const; + void* GetExtraSpacePtr(); +}; //////////////////////////////////////////////////////////////////////////////// } // namespace NYT - -#define NEW_INL_H_ -#include "new-inl.h" -#undef NEW_INL_H_ + +#define NEW_INL_H_ +#include "new-inl.h" +#undef NEW_INL_H_ diff --git a/library/cpp/yt/memory/ref.cpp b/library/cpp/yt/memory/ref.cpp index 787eadfe88..e8ff42e976 100644 --- a/library/cpp/yt/memory/ref.cpp +++ b/library/cpp/yt/memory/ref.cpp @@ -261,14 +261,14 @@ TSharedMutableRef TSharedMutableRef::MakeCopy(TRef ref, TRefCountedTypeCookie ta } if (ref.Empty()) { return TSharedMutableRef::MakeEmpty(); - } + } auto result = Allocate(ref.Size(), false, tagCookie); ::memcpy(result.Begin(), ref.Begin(), ref.Size()); return result; } -//////////////////////////////////////////////////////////////////////////////// - +//////////////////////////////////////////////////////////////////////////////// + TString ToString(TRef ref) { return TString(ref.Begin(), ref.End()); diff --git a/library/cpp/yt/memory/ref_counted-inl.h b/library/cpp/yt/memory/ref_counted-inl.h index ebc30ef6df..e6d64fec18 100644 --- a/library/cpp/yt/memory/ref_counted-inl.h +++ b/library/cpp/yt/memory/ref_counted-inl.h @@ -13,10 +13,10 @@ constexpr uintptr_t PtrMask = (1ULL << PtrBits) - 1; template <class T> Y_FORCE_INLINE char* PackPointer(T* ptr, uint16_t data) -{ +{ return reinterpret_cast<char*>((static_cast<uintptr_t>(data) << PtrBits) | reinterpret_cast<uintptr_t>(ptr)); -} - +} + template <class T> struct TPackedPointer { @@ -75,10 +75,10 @@ Y_FORCE_INLINE void TRefCounter::Ref() const noexcept StrongCount_.fetch_add(1, std::memory_order_relaxed); YT_ASSERT(WeakCount_.load(std::memory_order_relaxed) > 0); -} - +} + Y_FORCE_INLINE bool TRefCounter::TryRef() const noexcept -{ +{ auto value = StrongCount_.load(std::memory_order_relaxed); YT_ASSERT(WeakCount_.load(std::memory_order_relaxed) > 0); @@ -102,18 +102,18 @@ Y_FORCE_INLINE bool TRefCounter::Unref() const return false; } } - + Y_FORCE_INLINE int TRefCounter::GetWeakRefCount() const noexcept { return WeakCount_.load(std::memory_order_acquire); } - + Y_FORCE_INLINE void TRefCounter::WeakRef() const noexcept { auto oldWeakCount = WeakCount_.fetch_add(1, std::memory_order_relaxed); YT_ASSERT(oldWeakCount > 0); -} - +} + Y_FORCE_INLINE bool TRefCounter::WeakUnref() const { auto oldWeakCount = WeakCount_.fetch_sub(1, std::memory_order_release); @@ -130,11 +130,11 @@ Y_FORCE_INLINE bool TRefCounter::WeakUnref() const template <class T, bool = std::is_base_of_v<TRefCountedBase, T>> struct TRefCountedHelper -{ +{ static_assert( std::is_final_v<T>, "Ref-counted objects must be derived from TRefCountedBase or to be final"); - + static constexpr size_t RefCounterSpace = (sizeof(TRefCounter) + alignof(T) - 1) & ~(alignof(T) - 1); static constexpr size_t RefCounterOffset = RefCounterSpace - sizeof(TRefCounter); @@ -142,7 +142,7 @@ struct TRefCountedHelper { return reinterpret_cast<const TRefCounter*>(obj) - 1; } - + Y_FORCE_INLINE static void Destroy(const T* obj) { auto* refCounter = GetRefCounter(obj); @@ -199,14 +199,14 @@ template <class T> Y_FORCE_INLINE const TRefCounter* GetRefCounter(const T* obj) { return TRefCountedHelper<T>::GetRefCounter(obj); -} - +} + template <class T> Y_FORCE_INLINE void DestroyRefCounted(const T* obj) -{ +{ TRefCountedHelper<T>::Destroy(obj); -} - +} + template <class T> Y_FORCE_INLINE void DeallocateRefCounted(const T* obj) { @@ -232,10 +232,10 @@ Y_FORCE_INLINE void Unref(T* obj) //////////////////////////////////////////////////////////////////////////////// Y_FORCE_INLINE void TRefCounted::Unref() const -{ +{ ::NYT::Unref(this); -} - +} + Y_FORCE_INLINE void TRefCounted::WeakUnref() const { if (TRefCounter::WeakUnref()) { @@ -244,9 +244,9 @@ Y_FORCE_INLINE void TRefCounted::WeakUnref() const } -template <class T> +template <class T> void TRefCounted::DestroyRefCountedImpl(T* ptr) -{ +{ // No standard way to statically calculate the base offset even if T is final. // static_cast<TFinalDerived*>(virtualBasePtr) does not work. @@ -271,8 +271,8 @@ void TRefCounted::DestroyRefCountedImpl(T* ptr) if (refCounter->WeakUnref()) { TMemoryReleaser<T>::Do(ptr, offset); } -} - -//////////////////////////////////////////////////////////////////////////////// - +} + +//////////////////////////////////////////////////////////////////////////////// + } // namespace NYT diff --git a/library/cpp/yt/memory/ref_counted.h b/library/cpp/yt/memory/ref_counted.h index fa35a9c0f1..b683615b83 100644 --- a/library/cpp/yt/memory/ref_counted.h +++ b/library/cpp/yt/memory/ref_counted.h @@ -13,22 +13,22 @@ namespace NYT { //////////////////////////////////////////////////////////////////////////////// //! A technical base class for ref-counted objects and promise states. -class TRefCountedBase -{ -public: - TRefCountedBase() = default; +class TRefCountedBase +{ +public: + TRefCountedBase() = default; // Make destructor protected virtual ~TRefCountedBase() noexcept = default; virtual void DestroyRefCounted() = 0; -private: +private: TRefCountedBase(const TRefCountedBase&) = delete; TRefCountedBase(TRefCountedBase&&) = delete; - TRefCountedBase& operator=(const TRefCountedBase&) = delete; - TRefCountedBase& operator=(TRefCountedBase&&) = delete; + TRefCountedBase& operator=(const TRefCountedBase&) = delete; + TRefCountedBase& operator=(TRefCountedBase&&) = delete; }; //////////////////////////////////////////////////////////////////////////////// @@ -62,7 +62,7 @@ public: * Note that you should never ever use this method in production code. * This method is mainly for debugging purposes. */ - int GetRefCount() const noexcept; + int GetRefCount() const noexcept; //! Increments the strong reference counter. void Ref() const noexcept; diff --git a/library/cpp/yt/memory/weak_ptr.h b/library/cpp/yt/memory/weak_ptr.h index 683686b955..25a242bb8a 100644 --- a/library/cpp/yt/memory/weak_ptr.h +++ b/library/cpp/yt/memory/weak_ptr.h @@ -180,8 +180,8 @@ public: TIntrusivePtr<T> Lock() const noexcept { return T_ && RefCounter()->TryRef() - ? TIntrusivePtr<T>(T_, false) - : TIntrusivePtr<T>(); + ? TIntrusivePtr<T>(T_, false) + : TIntrusivePtr<T>(); } bool IsExpired() const noexcept |