diff options
author | babenko <babenko@yandex-team.com> | 2022-08-07 17:29:43 +0300 |
---|---|---|
committer | babenko <babenko@yandex-team.com> | 2022-08-07 17:29:43 +0300 |
commit | e3b2f4c99d645adbb0956e98dd7d38bc01b7db29 (patch) | |
tree | 00fcb67e7026e42d4af185a268606e3f5ba95829 | |
parent | ccc7c68ba2a051861c8d517c87f293368e0b7c7b (diff) | |
download | ydb-e3b2f4c99d645adbb0956e98dd7d38bc01b7db29.tar.gz |
Introduce TSharedMutableRefAllocateOptions
-rw-r--r-- | library/cpp/yt/memory/blob.cpp | 2 | ||||
-rw-r--r-- | library/cpp/yt/memory/ref-inl.h | 16 | ||||
-rw-r--r-- | library/cpp/yt/memory/ref.cpp | 24 | ||||
-rw-r--r-- | library/cpp/yt/memory/ref.h | 18 | ||||
-rw-r--r-- | library/cpp/yt/yson_string/convert.cpp | 2 |
5 files changed, 34 insertions, 28 deletions
diff --git a/library/cpp/yt/memory/blob.cpp b/library/cpp/yt/memory/blob.cpp index bade6a972b..bffb3ad249 100644 --- a/library/cpp/yt/memory/blob.cpp +++ b/library/cpp/yt/memory/blob.cpp @@ -76,7 +76,7 @@ void TBlob::Reserve(size_t newCapacity) } } -void TBlob::Resize(size_t newSize, bool initializeStorage /*= true*/) +void TBlob::Resize(size_t newSize, bool initializeStorage) { if (newSize > Size_) { if (newSize > Capacity_) { diff --git a/library/cpp/yt/memory/ref-inl.h b/library/cpp/yt/memory/ref-inl.h index 79be8356c5..2d9a174e02 100644 --- a/library/cpp/yt/memory/ref-inl.h +++ b/library/cpp/yt/memory/ref-inl.h @@ -180,14 +180,14 @@ Y_FORCE_INLINE TSharedMutableRef::operator TRef() const return TRef(Begin(), Size()); } -Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Allocate(size_t size, bool initializeStorage) +Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Allocate(size_t size, TSharedMutableRefAllocateOptions options) { - return Allocate<TDefaultSharedBlobTag>(size, initializeStorage); + return Allocate<TDefaultSharedBlobTag>(size, options); } -Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::AllocatePageAligned(size_t size, bool initializeStorage) +Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::AllocatePageAligned(size_t size, TSharedMutableRefAllocateOptions options) { - return AllocatePageAligned<TDefaultSharedBlobTag>(size, initializeStorage); + return AllocatePageAligned<TDefaultSharedBlobTag>(size, options); } template <class TTag> @@ -210,15 +210,15 @@ Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Slice(void* begin, void* end } template <class TTag> -Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Allocate(size_t size, bool initializeStorage) +Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Allocate(size_t size, TSharedMutableRefAllocateOptions options) { - return Allocate(size, initializeStorage, GetRefCountedTypeCookie<TTag>()); + return Allocate(size, options, GetRefCountedTypeCookie<TTag>()); } template <class TTag> -Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::AllocatePageAligned(size_t size, bool initializeStorage) +Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::AllocatePageAligned(size_t size, TSharedMutableRefAllocateOptions options) { - return AllocatePageAligned(size, initializeStorage, GetRefCountedTypeCookie<TTag>()); + return AllocatePageAligned(size, options, GetRefCountedTypeCookie<TTag>()); } //////////////////////////////////////////////////////////////////////////////// diff --git a/library/cpp/yt/memory/ref.cpp b/library/cpp/yt/memory/ref.cpp index d9f2104090..1a6b1830f7 100644 --- a/library/cpp/yt/memory/ref.cpp +++ b/library/cpp/yt/memory/ref.cpp @@ -103,9 +103,9 @@ protected: const TRefCountedTypeCookie Cookie_; #endif - void Initialize(bool initializeStorage) + void Initialize(TSharedMutableRefAllocateOptions options) { - if (initializeStorage) { + if (options.InitializeStorage) { ::memset(static_cast<TDerived*>(this)->GetBegin(), 0, Size_); } #ifdef YT_ENABLE_REF_COUNTED_TRACKING @@ -122,10 +122,10 @@ class TDefaultAllocationHolder , public TWithExtraSpace<TDefaultAllocationHolder> { public: - TDefaultAllocationHolder(size_t size, bool initializeStorage, TRefCountedTypeCookie cookie) + TDefaultAllocationHolder(size_t size, TSharedMutableRefAllocateOptions options, TRefCountedTypeCookie cookie) : TAllocationHolderBase(size, cookie) { - Initialize(initializeStorage); + Initialize(options); } char* GetBegin() @@ -140,11 +140,11 @@ class TPageAlignedAllocationHolder : public TAllocationHolderBase<TPageAlignedAllocationHolder> { public: - TPageAlignedAllocationHolder(size_t size, bool initializeStorage, TRefCountedTypeCookie cookie) + TPageAlignedAllocationHolder(size_t size, TSharedMutableRefAllocateOptions options, TRefCountedTypeCookie cookie) : TAllocationHolderBase(size, cookie) , Begin_(static_cast<char*>(NYTAlloc::AllocatePageAligned(size))) { - Initialize(initializeStorage); + Initialize(options); } ~TPageAlignedAllocationHolder() @@ -210,7 +210,7 @@ TSharedRef TSharedRef::MakeCopy(TRef ref, TRefCountedTypeCookie tagCookie) if (ref.Empty()) { return TSharedRef::MakeEmpty(); } - auto result = TSharedMutableRef::Allocate(ref.Size(), false, tagCookie); + auto result = TSharedMutableRef::Allocate(ref.Size(), {.InitializeStorage = false}, tagCookie); ::memcpy(result.Begin(), ref.Begin(), ref.Size()); return result; } @@ -234,16 +234,16 @@ std::vector<TSharedRef> TSharedRef::Split(size_t partSize) const //////////////////////////////////////////////////////////////////////////////// -TSharedMutableRef TSharedMutableRef::Allocate(size_t size, bool initializeStorage, TRefCountedTypeCookie tagCookie) +TSharedMutableRef TSharedMutableRef::Allocate(size_t size, TSharedMutableRefAllocateOptions options, TRefCountedTypeCookie tagCookie) { - auto holder = NewWithExtraSpace<TDefaultAllocationHolder>(size, size, initializeStorage, tagCookie); + auto holder = NewWithExtraSpace<TDefaultAllocationHolder>(size, size, options, tagCookie); auto ref = holder->GetRef(); return TSharedMutableRef(ref, std::move(holder)); } -TSharedMutableRef TSharedMutableRef::AllocatePageAligned(size_t size, bool initializeStorage, TRefCountedTypeCookie tagCookie) +TSharedMutableRef TSharedMutableRef::AllocatePageAligned(size_t size, TSharedMutableRefAllocateOptions options, TRefCountedTypeCookie tagCookie) { - auto holder = New<TPageAlignedAllocationHolder>(size, initializeStorage, tagCookie); + auto holder = New<TPageAlignedAllocationHolder>(size, options, tagCookie); auto ref = holder->GetRef(); return TSharedMutableRef(ref, std::move(holder)); } @@ -263,7 +263,7 @@ TSharedMutableRef TSharedMutableRef::MakeCopy(TRef ref, TRefCountedTypeCookie ta if (ref.Empty()) { return TSharedMutableRef::MakeEmpty(); } - auto result = Allocate(ref.Size(), false, tagCookie); + auto result = Allocate(ref.Size(), {.InitializeStorage = false}, tagCookie); ::memcpy(result.Begin(), ref.Begin(), ref.Size()); return result; } diff --git a/library/cpp/yt/memory/ref.h b/library/cpp/yt/memory/ref.h index 73d19d9013..2003ba0c0d 100644 --- a/library/cpp/yt/memory/ref.h +++ b/library/cpp/yt/memory/ref.h @@ -166,6 +166,12 @@ private: //////////////////////////////////////////////////////////////////////////////// +//! Various options for allocating TSharedMutableRef. +struct TSharedMutableRefAllocateOptions +{ + bool InitializeStorage = true; +}; + //! A reference to a mutable range of memory with shared ownership. //! Use with caution :) class TSharedMutableRef @@ -200,31 +206,31 @@ public: //! Allocates a new shared block of memory. //! The memory is marked with a given tag. template <class TTag> - static TSharedMutableRef Allocate(size_t size, bool initializeStorage = true); + static TSharedMutableRef Allocate(size_t size, TSharedMutableRefAllocateOptions options = {}); //! Allocates a new shared block of memory. //! The memory is marked with TDefaultSharedBlobTag. - static TSharedMutableRef Allocate(size_t size, bool initializeStorage = true); + static TSharedMutableRef Allocate(size_t size, TSharedMutableRefAllocateOptions options = {}); //! Allocates a new shared block of memory. //! The memory is marked with a given tag. - static TSharedMutableRef Allocate(size_t size, bool initializeStorage, TRefCountedTypeCookie tagCookie); + static TSharedMutableRef Allocate(size_t size, TSharedMutableRefAllocateOptions options, TRefCountedTypeCookie tagCookie); //! Allocates a new page aligned shared block of memory. //! #size must be divisible by page size. //! The memory is marked with a given tag. template <class TTag> - static TSharedMutableRef AllocatePageAligned(size_t size, bool initializeStorage = true); + static TSharedMutableRef AllocatePageAligned(size_t size, TSharedMutableRefAllocateOptions options = {}); //! Allocates a new page aligned shared block of memory. //! #size must be divisible by page size. //! The memory is marked with TDefaultSharedBlobTag. - static TSharedMutableRef AllocatePageAligned(size_t size, bool initializeStorage = true); + static TSharedMutableRef AllocatePageAligned(size_t size, TSharedMutableRefAllocateOptions options = {}); //! Allocates a new page aligned shared block of memory. //! #size must be divisible by page size. //! The memory is marked with a given tag. - static TSharedMutableRef AllocatePageAligned(size_t size, bool initializeStorage, TRefCountedTypeCookie tagCookie); + static TSharedMutableRef AllocatePageAligned(size_t size, TSharedMutableRefAllocateOptions options, TRefCountedTypeCookie tagCookie); //! Creates a TSharedMutableRef for the whole blob taking ownership of its content. static TSharedMutableRef FromBlob(TBlob&& blob); diff --git a/library/cpp/yt/yson_string/convert.cpp b/library/cpp/yt/yson_string/convert.cpp index 27f5c30d01..51a646f0ac 100644 --- a/library/cpp/yt/yson_string/convert.cpp +++ b/library/cpp/yt/yson_string/convert.cpp @@ -75,7 +75,7 @@ TYsonString ConvertToYsonString<TStringBuf>(const TStringBuf& value) { auto buffer = TSharedMutableRef::Allocate<TConvertStringToYsonStringTag>( 1 + MaxVarInt64Size + value.length(), - /*initializeStorage*/ false); + {.InitializeStorage = false}); auto* ptr = buffer.Begin(); *ptr++ = NDetail::StringMarker; ptr += WriteVarInt64(ptr, static_cast<i64>(value.length())); |