diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/memory/blob.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/memory/blob.cpp')
-rw-r--r-- | util/memory/blob.cpp | 430 |
1 files changed, 215 insertions, 215 deletions
diff --git a/util/memory/blob.cpp b/util/memory/blob.cpp index 91da5cadca..08e3964dab 100644 --- a/util/memory/blob.cpp +++ b/util/memory/blob.cpp @@ -1,225 +1,225 @@ -#include "blob.h" -#include "addstorage.h" - -#include <util/system/yassert.h> -#include <util/system/filemap.h> +#include "blob.h" +#include "addstorage.h" + +#include <util/system/yassert.h> +#include <util/system/filemap.h> #include <util/system/mlock.h> #include <util/stream/buffer.h> #include <util/generic/ptr.h> #include <util/generic/string.h> -#include <util/generic/buffer.h> -#include <util/generic/ylimits.h> -#include <util/generic/singleton.h> +#include <util/generic/buffer.h> +#include <util/generic/ylimits.h> +#include <util/generic/singleton.h> #include <util/generic/yexception.h> - -template <class TCounter> -class TDynamicBlobBase: public TBlob::TBase, - public TRefCounted<TDynamicBlobBase<TCounter>, TCounter>, - public TAdditionalStorage<TDynamicBlobBase<TCounter>> { + +template <class TCounter> +class TDynamicBlobBase: public TBlob::TBase, + public TRefCounted<TDynamicBlobBase<TCounter>, TCounter>, + public TAdditionalStorage<TDynamicBlobBase<TCounter>> { using TRefBase = TRefCounted<TDynamicBlobBase, TCounter>; - -public: + +public: inline TDynamicBlobBase() = default; - + ~TDynamicBlobBase() override = default; - + void Ref() noexcept override { - TRefBase::Ref(); - } - + TRefBase::Ref(); + } + void UnRef() noexcept override { - TRefBase::UnRef(); - } - + TRefBase::UnRef(); + } + inline void* Data() const noexcept { - return this->AdditionalData(); - } - + return this->AdditionalData(); + } + inline size_t Length() const noexcept { - return this->AdditionalDataLength(); - } -}; - -template <class TCounter> -class TBufferBlobBase: public TBlob::TBase, public TRefCounted<TBufferBlobBase<TCounter>, TCounter> { + return this->AdditionalDataLength(); + } +}; + +template <class TCounter> +class TBufferBlobBase: public TBlob::TBase, public TRefCounted<TBufferBlobBase<TCounter>, TCounter> { using TRefBase = TRefCounted<TBufferBlobBase, TCounter>; - -public: - inline TBufferBlobBase(TBuffer& buf) { - Buf_.Swap(buf); - } - + +public: + inline TBufferBlobBase(TBuffer& buf) { + Buf_.Swap(buf); + } + ~TBufferBlobBase() override = default; - + void Ref() noexcept override { - TRefBase::Ref(); - } - + TRefBase::Ref(); + } + void UnRef() noexcept override { - TRefBase::UnRef(); - } - + TRefBase::UnRef(); + } + inline const TBuffer& Buffer() const noexcept { - return Buf_; - } - -private: - TBuffer Buf_; -}; - -template <class TCounter> -class TStringBlobBase: public TBlob::TBase, public TRefCounted<TStringBlobBase<TCounter>, TCounter> { + return Buf_; + } + +private: + TBuffer Buf_; +}; + +template <class TCounter> +class TStringBlobBase: public TBlob::TBase, public TRefCounted<TStringBlobBase<TCounter>, TCounter> { using TRefBase = TRefCounted<TStringBlobBase, TCounter>; - -public: + +public: inline TStringBlobBase(const TString& s) - : S_(s) - { - } - + : S_(s) + { + } + TStringBlobBase(TString&& s) noexcept : S_(std::move(s)) { } ~TStringBlobBase() override = default; - + void Ref() noexcept override { - TRefBase::Ref(); - } - + TRefBase::Ref(); + } + void UnRef() noexcept override { - TRefBase::UnRef(); - } - + TRefBase::UnRef(); + } + inline const TString& String() const noexcept { - return S_; - } - -private: + return S_; + } + +private: const TString S_; -}; - -template <class TCounter> -class TMappedBlobBase: public TBlob::TBase, public TRefCounted<TMappedBlobBase<TCounter>, TCounter> { +}; + +template <class TCounter> +class TMappedBlobBase: public TBlob::TBase, public TRefCounted<TMappedBlobBase<TCounter>, TCounter> { using TRefBase = TRefCounted<TMappedBlobBase<TCounter>, TCounter>; - -public: + +public: inline TMappedBlobBase(const TMemoryMap& map, ui64 offset, size_t len, EMappingMode mode) - : Map_(map) + : Map_(map) , Mode_(mode) - { + { Y_ENSURE(Map_.IsOpen(), TStringBuf("memory map not open")); - - Map_.Map(offset, len); - - if (len && !Map_.Ptr()) { // Ptr is 0 for blob of size 0 - ythrow yexception() << "can not map(" << offset << ", " << len << ")"; - } + + Map_.Map(offset, len); + + if (len && !Map_.Ptr()) { // Ptr is 0 for blob of size 0 + ythrow yexception() << "can not map(" << offset << ", " << len << ")"; + } if (Mode_ == EMappingMode::Locked) { LockMemory(Data(), Length()); - } - } - + } + } + ~TMappedBlobBase() override { if (Mode_ == EMappingMode::Locked && Length()) { UnlockMemory(Data(), Length()); - } + } } - + void Ref() noexcept override { - TRefBase::Ref(); - } - + TRefBase::Ref(); + } + void UnRef() noexcept override { - TRefBase::UnRef(); - } - + TRefBase::UnRef(); + } + inline const void* Data() const noexcept { - return Map_.Ptr(); - } - + return Map_.Ptr(); + } + inline size_t Length() const noexcept { - return Map_.MappedSize(); - } - -private: - TFileMap Map_; + return Map_.MappedSize(); + } + +private: + TFileMap Map_; EMappingMode Mode_; -}; - -TBlob TBlob::SubBlob(size_t len) const { - /* - * may be slightly optimized - */ - - return SubBlob(0, len); -} - -TBlob TBlob::SubBlob(size_t begin, size_t end) const { - if (begin > Length() || end > Length() || begin > end) { +}; + +TBlob TBlob::SubBlob(size_t len) const { + /* + * may be slightly optimized + */ + + return SubBlob(0, len); +} + +TBlob TBlob::SubBlob(size_t begin, size_t end) const { + if (begin > Length() || end > Length() || begin > end) { ythrow yexception() << "incorrect subblob (" << begin << ", " << end << ", outer length = " << Length() << ")"; - } - - return TBlob(Begin() + begin, end - begin, S_.Base); -} - -TBlob TBlob::DeepCopy() const { + } + + return TBlob(Begin() + begin, end - begin, S_.Base); +} + +TBlob TBlob::DeepCopy() const { return TBlob::Copy(Data(), Length()); -} - -template <class TCounter> -static inline TBlob CopyConstruct(const void* data, size_t len) { +} + +template <class TCounter> +static inline TBlob CopyConstruct(const void* data, size_t len) { using Base = TDynamicBlobBase<TCounter>; - THolder<Base> base(new (len) Base); - + THolder<Base> base(new (len) Base); + Y_ASSERT(base->Length() == len); - - memcpy(base->Data(), data, len); - - TBlob ret(base->Data(), len, base.Get()); + + memcpy(base->Data(), data, len); + + TBlob ret(base->Data(), len, base.Get()); Y_UNUSED(base.Release()); - - return ret; -} - + + return ret; +} + TBlob TBlob::CopySingleThreaded(const void* data, size_t length) { - return CopyConstruct<TSimpleCounter>(data, length); -} - + return CopyConstruct<TSimpleCounter>(data, length); +} + TBlob TBlob::Copy(const void* data, size_t length) { - return CopyConstruct<TAtomicCounter>(data, length); -} - -TBlob TBlob::NoCopy(const void* data, size_t length) { + return CopyConstruct<TAtomicCounter>(data, length); +} + +TBlob TBlob::NoCopy(const void* data, size_t length) { return TBlob(data, length, nullptr); -} - -template <class TCounter> +} + +template <class TCounter> static inline TBlob ConstructFromMap(const TMemoryMap& map, ui64 offset, size_t length, EMappingMode mode) { using TBase = TMappedBlobBase<TCounter>; THolder<TBase> base(new TBase(map, offset, length, mode)); - TBlob ret(base->Data(), base->Length(), base.Get()); + TBlob ret(base->Data(), base->Length(), base.Get()); Y_UNUSED(base.Release()); - - return ret; -} - + + return ret; +} + template <class TCounter, class T> static inline TBlob ConstructAsMap(const T& t, EMappingMode mode) { TMemoryMap::EOpenMode openMode = (mode == EMappingMode::Precharged) ? (TMemoryMap::oRdOnly | TMemoryMap::oPrecharge) : TMemoryMap::oRdOnly; TMemoryMap map(t, openMode); - const ui64 toMap = map.Length(); - - if (toMap > Max<size_t>()) { - ythrow yexception() << "can not map whole file(length = " << toMap << ")"; - } - + const ui64 toMap = map.Length(); + + if (toMap > Max<size_t>()) { + ythrow yexception() << "can not map whole file(length = " << toMap << ")"; + } + return ConstructFromMap<TCounter>(map, 0, static_cast<size_t>(toMap), mode); -} - +} + TBlob TBlob::FromFileSingleThreaded(const TString& path, EMappingMode mode) { return ConstructAsMap<TSimpleCounter>(path, mode); } @@ -238,20 +238,20 @@ TBlob TBlob::FromFile(const TFile& file, EMappingMode mode) { TBlob TBlob::FromFileSingleThreaded(const TString& path) { return ConstructAsMap<TSimpleCounter>(path, EMappingMode::Standard); -} - +} + TBlob TBlob::FromFile(const TString& path) { return ConstructAsMap<TAtomicCounter>(path, EMappingMode::Standard); -} - +} + TBlob TBlob::FromFileSingleThreaded(const TFile& file) { return ConstructAsMap<TSimpleCounter>(file, EMappingMode::Standard); -} - +} + TBlob TBlob::FromFile(const TFile& file) { return ConstructAsMap<TAtomicCounter>(file, EMappingMode::Standard); -} - +} + TBlob TBlob::PrechargedFromFileSingleThreaded(const TString& path) { return ConstructAsMap<TSimpleCounter>(path, EMappingMode::Precharged); } @@ -294,36 +294,36 @@ TBlob TBlob::LockedFromMemoryMap(const TMemoryMap& map, ui64 offset, size_t leng TBlob TBlob::FromMemoryMapSingleThreaded(const TMemoryMap& map, ui64 offset, size_t length) { return ConstructFromMap<TSimpleCounter>(map, offset, length, EMappingMode::Standard); -} - +} + TBlob TBlob::FromMemoryMap(const TMemoryMap& map, ui64 offset, size_t length) { return ConstructFromMap<TAtomicCounter>(map, offset, length, EMappingMode::Standard); -} - -template <class TCounter> +} + +template <class TCounter> static inline TBlob ReadFromFile(const TFile& file, ui64 offset, size_t length) { using TBase = TDynamicBlobBase<TCounter>; - THolder<TBase> base(new (length) TBase); - + THolder<TBase> base(new (length) TBase); + Y_ASSERT(base->Length() == length); - - file.Pload(base->Data(), length, offset); - - TBlob ret(base->Data(), length, base.Get()); + + file.Pload(base->Data(), length, offset); + + TBlob ret(base->Data(), length, base.Get()); Y_UNUSED(base.Release()); - - return ret; -} - -template <class TCounter> + + return ret; +} + +template <class TCounter> static inline TBlob ConstructFromFileContent(const TFile& file, ui64 offset, ui64 length) { if (length > Max<size_t>()) { ythrow yexception() << "can not read whole file(length = " << length << ")"; } return ReadFromFile<TCounter>(file, offset, static_cast<size_t>(length)); -} - +} + TBlob TBlob::FromFileContentSingleThreaded(const TString& path) { TFile file(path, RdOnly); return ConstructFromFileContent<TSimpleCounter>(file, 0, file.GetLength()); @@ -343,14 +343,14 @@ TBlob TBlob::FromFileContent(const TFile& file) { } TBlob TBlob::FromFileContentSingleThreaded(const TFile& file, ui64 offset, size_t length) { - return ConstructFromFileContent<TSimpleCounter>(file, offset, length); -} - + return ConstructFromFileContent<TSimpleCounter>(file, offset, length); +} + TBlob TBlob::FromFileContent(const TFile& file, ui64 offset, size_t length) { - return ConstructFromFileContent<TAtomicCounter>(file, offset, length); -} - -template <class TCounter> + return ConstructFromFileContent<TAtomicCounter>(file, offset, length); +} + +template <class TCounter> static inline TBlob ConstructFromBuffer(TBuffer& in) { using TBase = TBufferBlobBase<TCounter>; THolder<TBase> base(new TBase(in)); @@ -364,23 +364,23 @@ static inline TBlob ConstructFromBuffer(TBuffer& in) { template <class TCounter> static inline TBlob ConstructFromStream(IInputStream& in) { TBuffer buf; - - { - TBufferOutput out(buf); - - TransferData(&in, &out); - } - + + { + TBufferOutput out(buf); + + TransferData(&in, &out); + } + return ConstructFromBuffer<TCounter>(buf); -} - +} + TBlob TBlob::FromStreamSingleThreaded(IInputStream& in) { return ConstructFromStream<TSimpleCounter>(in); -} - +} + TBlob TBlob::FromStream(IInputStream& in) { return ConstructFromStream<TAtomicCounter>(in); -} +} TBlob TBlob::FromBufferSingleThreaded(TBuffer& in) { return ConstructFromBuffer<TSimpleCounter>(in); @@ -389,29 +389,29 @@ TBlob TBlob::FromBufferSingleThreaded(TBuffer& in) { TBlob TBlob::FromBuffer(TBuffer& in) { return ConstructFromBuffer<TAtomicCounter>(in); } - + template <class TCounter, class S> TBlob ConstructFromString(S&& s) { using TBase = TStringBlobBase<TCounter>; auto base = MakeHolder<TBase>(std::forward<S>(s)); - + TBlob ret(base->String().data(), base->String().size(), base.Get()); Y_UNUSED(base.Release()); - - return ret; -} - + + return ret; +} + TBlob TBlob::FromStringSingleThreaded(const TString& s) { - return ConstructFromString<TSimpleCounter>(s); -} - + return ConstructFromString<TSimpleCounter>(s); +} + TBlob TBlob::FromStringSingleThreaded(TString&& s) { return ConstructFromString<TSimpleCounter>(std::move(s)); } TBlob TBlob::FromString(const TString& s) { - return ConstructFromString<TAtomicCounter>(s); -} + return ConstructFromString<TAtomicCounter>(s); +} TBlob TBlob::FromString(TString&& s) { return ConstructFromString<TAtomicCounter>(std::move(s)); |