diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/deprecated/mapped_file | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/deprecated/mapped_file')
-rw-r--r-- | library/cpp/deprecated/mapped_file/mapped_file.cpp | 64 | ||||
-rw-r--r-- | library/cpp/deprecated/mapped_file/mapped_file.h | 72 | ||||
-rw-r--r-- | library/cpp/deprecated/mapped_file/ut/mapped_file_ut.cpp | 18 | ||||
-rw-r--r-- | library/cpp/deprecated/mapped_file/ya.make | 9 |
4 files changed, 163 insertions, 0 deletions
diff --git a/library/cpp/deprecated/mapped_file/mapped_file.cpp b/library/cpp/deprecated/mapped_file/mapped_file.cpp new file mode 100644 index 0000000000..b0e4511299 --- /dev/null +++ b/library/cpp/deprecated/mapped_file/mapped_file.cpp @@ -0,0 +1,64 @@ +#include "mapped_file.h" + +#include <util/generic/yexception.h> +#include <util/system/defaults.h> +#include <util/system/hi_lo.h> +#include <util/system/filemap.h> + +TMappedFile::TMappedFile(TFileMap* map, const char* dbgName) { + Map_ = map; + i64 len = Map_->Length(); + if (Hi32(len) != 0 && sizeof(size_t) <= sizeof(ui32)) + ythrow yexception() << "File '" << dbgName << "' mapping error: " << len << " too large"; + + Map_->Map(0, static_cast<size_t>(len)); +} + +TMappedFile::TMappedFile(const TFile& file, TFileMap::EOpenMode om, const char* dbgName) + : Map_(nullptr) +{ + init(file, om, dbgName); +} + +void TMappedFile::precharge(size_t off, size_t size) const { + if (!Map_) + return; + + Map_->Precharge(off, size); +} + +void TMappedFile::init(const TString& name) { + THolder<TFileMap> map(new TFileMap(name)); + TMappedFile newFile(map.Get(), name.data()); + Y_UNUSED(map.Release()); + newFile.swap(*this); + newFile.term(); +} + +void TMappedFile::init(const TString& name, size_t length, TFileMap::EOpenMode om) { + THolder<TFileMap> map(new TFileMap(name, length, om)); + TMappedFile newFile(map.Get(), name.data()); + Y_UNUSED(map.Release()); + newFile.swap(*this); + newFile.term(); +} + +void TMappedFile::init(const TFile& file, TFileMap::EOpenMode om, const char* dbgName) { + THolder<TFileMap> map(new TFileMap(file, om)); + TMappedFile newFile(map.Get(), dbgName); + Y_UNUSED(map.Release()); + newFile.swap(*this); + newFile.term(); +} + +void TMappedFile::init(const TString& name, TFileMap::EOpenMode om) { + THolder<TFileMap> map(new TFileMap(name, om)); + TMappedFile newFile(map.Get(), name.data()); + Y_UNUSED(map.Release()); + newFile.swap(*this); + newFile.term(); +} + +void TMappedFile::flush() { + Map_->Flush(); +} diff --git a/library/cpp/deprecated/mapped_file/mapped_file.h b/library/cpp/deprecated/mapped_file/mapped_file.h new file mode 100644 index 0000000000..45859ed65a --- /dev/null +++ b/library/cpp/deprecated/mapped_file/mapped_file.h @@ -0,0 +1,72 @@ +#pragma once + +#include <util/generic/flags.h> +#include <util/generic/ptr.h> +#include <util/generic/string.h> +#include <util/generic/utility.h> +#include <util/generic/yexception.h> +#include <util/system/align.h> +#include <util/system/file.h> +#include <util/system/filemap.h> +#include <util/system/yassert.h> + +#include <cstdio> +#include <new> + +/// Deprecated (by pg@), use TFileMap or TMemoryMap instead +class TMappedFile { +private: + TFileMap* Map_; + +private: + TMappedFile(TFileMap* map, const char* dbgName); + +public: + TMappedFile() { + Map_ = nullptr; + } + + ~TMappedFile() { + term(); + } + + explicit TMappedFile(const TString& name) { + Map_ = nullptr; + init(name, TFileMap::oRdOnly); + } + + TMappedFile(const TFile& file, TFileMap::EOpenMode om = TFileMap::oRdOnly, const char* dbgName = "unknown"); + + void init(const TString& name); + + void init(const TString& name, TFileMap::EOpenMode om); + + void init(const TString& name, size_t length, TFileMap::EOpenMode om); + + void init(const TFile&, TFileMap::EOpenMode om = TFileMap::oRdOnly, const char* dbgName = "unknown"); + + void flush(); + + void term() { + if (Map_) { + Map_->Unmap(); + delete Map_; + Map_ = nullptr; + } + } + + size_t getSize() const { + return (Map_ ? Map_->MappedSize() : 0); + } + + void* getData(size_t pos = 0) const { + Y_ASSERT(!Map_ || (pos <= getSize())); + return (Map_ ? (void*)((unsigned char*)Map_->Ptr() + pos) : nullptr); + } + + void precharge(size_t pos = 0, size_t size = (size_t)-1) const; + + void swap(TMappedFile& file) noexcept { + DoSwap(Map_, file.Map_); + } +}; diff --git a/library/cpp/deprecated/mapped_file/ut/mapped_file_ut.cpp b/library/cpp/deprecated/mapped_file/ut/mapped_file_ut.cpp new file mode 100644 index 0000000000..afbd5b3358 --- /dev/null +++ b/library/cpp/deprecated/mapped_file/ut/mapped_file_ut.cpp @@ -0,0 +1,18 @@ +#include <library/cpp/deprecated/mapped_file/mapped_file.h> +#include <library/cpp/testing/unittest/registar.h> + +#include <util/system/fs.h> + +Y_UNIT_TEST_SUITE(TMappedFileTest) { + static const char* FileName_("./mappped_file"); + Y_UNIT_TEST(TestFileMapEmpty) { + TFile file(FileName_, CreateAlways | WrOnly); + file.Close(); + + TMappedFile map; + map.init(FileName_); + map.getData(0); + + NFs::Remove(FileName_); + } +}; diff --git a/library/cpp/deprecated/mapped_file/ya.make b/library/cpp/deprecated/mapped_file/ya.make new file mode 100644 index 0000000000..415c438382 --- /dev/null +++ b/library/cpp/deprecated/mapped_file/ya.make @@ -0,0 +1,9 @@ +LIBRARY() + +OWNER(g:util) + +SRCS( + mapped_file.cpp +) + +END() |