filemap.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #pragma once
  2. #include "file.h"
  3. #include "align.h"
  4. #include "yassert.h"
  5. #include <util/generic/noncopyable.h>
  6. #include <util/generic/ptr.h>
  7. #include <util/generic/utility.h>
  8. #include <util/generic/yexception.h>
  9. #include <util/generic/flags.h>
  10. #include <util/generic/string.h>
  11. #include <new>
  12. #include <cstdio>
  13. namespace NPrivate {
  14. // NB: use TFileMap::Precharge() and TFileMappedArray::Prechage()
  15. void Precharge(const void* data, size_t dataSize, size_t offset, size_t size);
  16. } // namespace NPrivate
  17. struct TMemoryMapCommon {
  18. struct TMapResult {
  19. inline size_t MappedSize() const noexcept {
  20. return Size - Head;
  21. }
  22. inline void* MappedData() const noexcept {
  23. return Ptr ? (void*)((char*)Ptr + Head) : nullptr;
  24. }
  25. inline bool IsMapped() const noexcept {
  26. return Ptr != nullptr;
  27. }
  28. inline void Reset() noexcept {
  29. Ptr = nullptr;
  30. Size = 0;
  31. Head = 0;
  32. }
  33. void* Ptr;
  34. size_t Size;
  35. i32 Head;
  36. TMapResult(void) noexcept {
  37. Reset();
  38. }
  39. };
  40. enum EOpenModeFlag {
  41. oRdOnly = 1,
  42. oRdWr = 2,
  43. oCopyOnWr = 4,
  44. oAccessMask = 7,
  45. oNotGreedy = 8,
  46. oPrecharge = 16,
  47. oPopulate = 32, // Populate page table entries (see mmap's MAP_POPULATE)
  48. };
  49. Y_DECLARE_FLAGS(EOpenMode, EOpenModeFlag);
  50. /**
  51. * Name that will be printed in exceptions if not specified.
  52. * Overridden by name obtained from `TFile` if it's not empty.
  53. */
  54. static const TString& UnknownFileName();
  55. };
  56. Y_DECLARE_OPERATORS_FOR_FLAGS(TMemoryMapCommon::EOpenMode);
  57. class TMemoryMap: public TMemoryMapCommon {
  58. public:
  59. explicit TMemoryMap(const TString& name);
  60. explicit TMemoryMap(const TString& name, EOpenMode om);
  61. TMemoryMap(const TString& name, i64 length, EOpenMode om);
  62. TMemoryMap(FILE* f, TString dbgName = UnknownFileName());
  63. TMemoryMap(FILE* f, EOpenMode om, TString dbgName = UnknownFileName());
  64. TMemoryMap(const TFile& file, const TString& dbgName = UnknownFileName());
  65. TMemoryMap(const TFile& file, EOpenMode om, const TString& dbgName = UnknownFileName());
  66. ~TMemoryMap();
  67. TMapResult Map(i64 offset, size_t size);
  68. bool Unmap(TMapResult region);
  69. void ResizeAndReset(i64 size);
  70. TMapResult ResizeAndRemap(i64 offset, size_t size);
  71. i64 Length() const noexcept;
  72. bool IsOpen() const noexcept;
  73. bool IsWritable() const noexcept;
  74. EOpenMode GetMode() const noexcept;
  75. TFile GetFile() const noexcept;
  76. void SetSequential();
  77. void Evict(void* ptr, size_t len);
  78. void Evict();
  79. /*
  80. * deprecated
  81. */
  82. bool Unmap(void* ptr, size_t size);
  83. private:
  84. class TImpl;
  85. TSimpleIntrusivePtr<TImpl> Impl_;
  86. };
  87. class TFileMap: public TMemoryMapCommon {
  88. public:
  89. TFileMap(const TMemoryMap& map) noexcept;
  90. TFileMap(const TString& name);
  91. TFileMap(const TString& name, EOpenMode om);
  92. TFileMap(const TString& name, i64 length, EOpenMode om);
  93. TFileMap(FILE* f, EOpenMode om = oRdOnly, TString dbgName = UnknownFileName());
  94. TFileMap(const TFile& file, EOpenMode om = oRdOnly, const TString& dbgName = UnknownFileName());
  95. TFileMap(const TFileMap& fm) noexcept;
  96. ~TFileMap();
  97. TMapResult Map(i64 offset, size_t size);
  98. TMapResult ResizeAndRemap(i64 offset, size_t size);
  99. void Unmap();
  100. void Flush(void* ptr, size_t size) {
  101. Flush(ptr, size, true);
  102. }
  103. void Flush() {
  104. Flush(Ptr(), MappedSize());
  105. }
  106. void FlushAsync(void* ptr, size_t size) {
  107. Flush(ptr, size, false);
  108. }
  109. void FlushAsync() {
  110. FlushAsync(Ptr(), MappedSize());
  111. }
  112. inline i64 Length() const noexcept {
  113. return Map_.Length();
  114. }
  115. inline bool IsOpen() const noexcept {
  116. return Map_.IsOpen();
  117. }
  118. inline bool IsWritable() const noexcept {
  119. return Map_.IsWritable();
  120. }
  121. EOpenMode GetMode() const noexcept {
  122. return Map_.GetMode();
  123. }
  124. inline void* Ptr() const noexcept {
  125. return Region_.MappedData();
  126. }
  127. inline size_t MappedSize() const noexcept {
  128. return Region_.MappedSize();
  129. }
  130. TFile GetFile() const noexcept {
  131. return Map_.GetFile();
  132. }
  133. void Precharge(size_t pos = 0, size_t size = (size_t)-1) const;
  134. void SetSequential() {
  135. Map_.SetSequential();
  136. }
  137. void Evict() {
  138. Map_.Evict();
  139. }
  140. private:
  141. void Flush(void* ptr, size_t size, bool sync);
  142. TMemoryMap Map_;
  143. TMapResult Region_;
  144. };
  145. template <class T>
  146. class TFileMappedArray {
  147. private:
  148. const T* Ptr_;
  149. const T* End_;
  150. size_t Size_;
  151. char DummyData_[sizeof(T) + PLATFORM_DATA_ALIGN];
  152. mutable THolder<T, TDestructor> Dummy_;
  153. THolder<TFileMap> DataHolder_;
  154. public:
  155. TFileMappedArray()
  156. : Ptr_(nullptr)
  157. , End_(nullptr)
  158. , Size_(0)
  159. {
  160. }
  161. ~TFileMappedArray() {
  162. Ptr_ = nullptr;
  163. End_ = nullptr;
  164. }
  165. void Init(const char* name) {
  166. DataHolder_.Reset(new TFileMap(name));
  167. DoInit(name);
  168. }
  169. void Init(const TFileMap& fileMap) {
  170. DataHolder_.Reset(new TFileMap(fileMap));
  171. DoInit(fileMap.GetFile().GetName());
  172. }
  173. void Term() {
  174. DataHolder_.Destroy();
  175. Ptr_ = nullptr;
  176. Size_ = 0;
  177. End_ = nullptr;
  178. }
  179. void Precharge() {
  180. DataHolder_->Precharge();
  181. }
  182. const T& operator[](size_t pos) const {
  183. Y_ASSERT(pos < size());
  184. return Ptr_[pos];
  185. }
  186. /// for STL compatibility only, Size() usage is recommended
  187. size_t size() const {
  188. return Size_;
  189. }
  190. size_t Size() const {
  191. return Size_;
  192. }
  193. const T& GetAt(size_t pos) const {
  194. if (pos < Size_)
  195. return Ptr_[pos];
  196. return Dummy();
  197. }
  198. void SetDummy(const T& n_Dummy) {
  199. Dummy_.Destroy();
  200. Dummy_.Reset(new (DummyData()) T(n_Dummy));
  201. }
  202. inline char* DummyData() const noexcept {
  203. return AlignUp((char*)DummyData_);
  204. }
  205. inline const T& Dummy() const {
  206. if (!Dummy_) {
  207. Dummy_.Reset(new (DummyData()) T());
  208. }
  209. return *Dummy_;
  210. }
  211. /// for STL compatibility only, Empty() usage is recommended
  212. Y_PURE_FUNCTION bool empty() const noexcept {
  213. return Empty();
  214. }
  215. Y_PURE_FUNCTION bool Empty() const noexcept {
  216. return 0 == Size_;
  217. }
  218. /// for STL compatibility only, Begin() usage is recommended
  219. const T* begin() const noexcept {
  220. return Begin();
  221. }
  222. const T* Begin() const noexcept {
  223. return Ptr_;
  224. }
  225. /// for STL compatibility only, End() usage is recommended
  226. const T* end() const noexcept {
  227. return End_;
  228. }
  229. const T* End() const noexcept {
  230. return End_;
  231. }
  232. private:
  233. void DoInit(const TString& fileName) {
  234. DataHolder_->Map(0, DataHolder_->Length());
  235. if (DataHolder_->Length() % sizeof(T)) {
  236. Term();
  237. ythrow yexception() << "Incorrect size of file " << fileName.Quote();
  238. }
  239. Ptr_ = (const T*)DataHolder_->Ptr();
  240. Size_ = DataHolder_->Length() / sizeof(T);
  241. End_ = Ptr_ + Size_;
  242. }
  243. };
  244. class TMappedAllocation: TMoveOnly {
  245. public:
  246. TMappedAllocation(size_t size = 0, bool shared = false, void* addr = nullptr);
  247. ~TMappedAllocation() {
  248. Dealloc();
  249. }
  250. TMappedAllocation(TMappedAllocation&& other) noexcept {
  251. this->swap(other);
  252. }
  253. TMappedAllocation& operator=(TMappedAllocation&& other) noexcept {
  254. this->swap(other);
  255. return *this;
  256. }
  257. void* Alloc(size_t size, void* addr = nullptr);
  258. void Dealloc();
  259. void* Ptr() const {
  260. return Ptr_;
  261. }
  262. char* Data(ui32 pos = 0) const {
  263. return (char*)(Ptr_ ? ((char*)Ptr_ + pos) : nullptr);
  264. }
  265. char* Begin() const noexcept {
  266. return (char*)Ptr();
  267. }
  268. char* End() const noexcept {
  269. return Begin() + MappedSize();
  270. }
  271. size_t MappedSize() const {
  272. return Size_;
  273. }
  274. void swap(TMappedAllocation& with) noexcept;
  275. private:
  276. void* Ptr_ = nullptr;
  277. size_t Size_ = 0;
  278. bool Shared_ = false;
  279. #ifdef _win_
  280. void* Mapping_ = nullptr;
  281. #endif
  282. };
  283. template <class T>
  284. class TMappedArray: private TMappedAllocation {
  285. public:
  286. TMappedArray(size_t siz = 0)
  287. : TMappedAllocation(0)
  288. {
  289. if (siz)
  290. Create(siz);
  291. }
  292. ~TMappedArray() {
  293. Destroy();
  294. }
  295. T* Create(size_t siz) {
  296. Y_ASSERT(MappedSize() == 0 && Ptr() == nullptr);
  297. T* arr = (T*)Alloc((sizeof(T) * siz));
  298. if (!arr)
  299. return nullptr;
  300. Y_ASSERT(MappedSize() == sizeof(T) * siz);
  301. for (size_t n = 0; n < siz; n++)
  302. new (&arr[n]) T();
  303. return arr;
  304. }
  305. void Destroy() {
  306. T* arr = (T*)Ptr();
  307. if (arr) {
  308. for (size_t n = 0; n < size(); n++)
  309. arr[n].~T();
  310. Dealloc();
  311. }
  312. }
  313. T& operator[](size_t pos) {
  314. Y_ASSERT(pos < size());
  315. return ((T*)Ptr())[pos];
  316. }
  317. const T& operator[](size_t pos) const {
  318. Y_ASSERT(pos < size());
  319. return ((T*)Ptr())[pos];
  320. }
  321. T* begin() {
  322. return (T*)Ptr();
  323. }
  324. T* end() {
  325. return (T*)((char*)Ptr() + MappedSize());
  326. }
  327. size_t size() const {
  328. return MappedSize() / sizeof(T);
  329. }
  330. void swap(TMappedArray<T>& with) {
  331. TMappedAllocation::swap(with);
  332. }
  333. };