filemap.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. }
  197. return Dummy();
  198. }
  199. void SetDummy(const T& n_Dummy) {
  200. Dummy_.Destroy();
  201. Dummy_.Reset(new (DummyData()) T(n_Dummy));
  202. }
  203. inline char* DummyData() const noexcept {
  204. return AlignUp((char*)DummyData_);
  205. }
  206. inline const T& Dummy() const {
  207. if (!Dummy_) {
  208. Dummy_.Reset(new (DummyData()) T());
  209. }
  210. return *Dummy_;
  211. }
  212. /// for STL compatibility only, Empty() usage is recommended
  213. Y_PURE_FUNCTION bool empty() const noexcept {
  214. return Empty();
  215. }
  216. Y_PURE_FUNCTION bool Empty() const noexcept {
  217. return 0 == Size_;
  218. }
  219. /// for STL compatibility only, Begin() usage is recommended
  220. const T* begin() const noexcept {
  221. return Begin();
  222. }
  223. const T* Begin() const noexcept {
  224. return Ptr_;
  225. }
  226. /// for STL compatibility only, End() usage is recommended
  227. const T* end() const noexcept {
  228. return End_;
  229. }
  230. const T* End() const noexcept {
  231. return End_;
  232. }
  233. private:
  234. void DoInit(const TString& fileName) {
  235. DataHolder_->Map(0, DataHolder_->Length());
  236. if (DataHolder_->Length() % sizeof(T)) {
  237. Term();
  238. ythrow yexception() << "Incorrect size of file " << fileName.Quote();
  239. }
  240. Ptr_ = (const T*)DataHolder_->Ptr();
  241. Size_ = DataHolder_->Length() / sizeof(T);
  242. End_ = Ptr_ + Size_;
  243. }
  244. };
  245. class TMappedAllocation: TMoveOnly {
  246. public:
  247. TMappedAllocation(size_t size = 0, bool shared = false, void* addr = nullptr);
  248. ~TMappedAllocation() {
  249. Dealloc();
  250. }
  251. TMappedAllocation(TMappedAllocation&& other) noexcept {
  252. this->swap(other);
  253. }
  254. TMappedAllocation& operator=(TMappedAllocation&& other) noexcept {
  255. this->swap(other);
  256. return *this;
  257. }
  258. void* Alloc(size_t size, void* addr = nullptr);
  259. void Dealloc();
  260. void* Ptr() const {
  261. return Ptr_;
  262. }
  263. char* Data(ui32 pos = 0) const {
  264. return (char*)(Ptr_ ? ((char*)Ptr_ + pos) : nullptr);
  265. }
  266. char* Begin() const noexcept {
  267. return (char*)Ptr();
  268. }
  269. char* End() const noexcept {
  270. return Begin() + MappedSize();
  271. }
  272. size_t MappedSize() const {
  273. return Size_;
  274. }
  275. void swap(TMappedAllocation& with) noexcept;
  276. private:
  277. void* Ptr_ = nullptr;
  278. size_t Size_ = 0;
  279. bool Shared_ = false;
  280. #ifdef _win_
  281. void* Mapping_ = nullptr;
  282. #endif
  283. };
  284. template <class T>
  285. class TMappedArray: private TMappedAllocation {
  286. public:
  287. TMappedArray(size_t siz = 0)
  288. : TMappedAllocation(0)
  289. {
  290. if (siz) {
  291. Create(siz);
  292. }
  293. }
  294. ~TMappedArray() {
  295. Destroy();
  296. }
  297. T* Create(size_t siz) {
  298. Y_ASSERT(MappedSize() == 0 && Ptr() == nullptr);
  299. T* arr = (T*)Alloc((sizeof(T) * siz));
  300. if (!arr) {
  301. return nullptr;
  302. }
  303. Y_ASSERT(MappedSize() == sizeof(T) * siz);
  304. for (size_t n = 0; n < siz; n++) {
  305. new (&arr[n]) T();
  306. }
  307. return arr;
  308. }
  309. void Destroy() {
  310. T* arr = (T*)Ptr();
  311. if (arr) {
  312. for (size_t n = 0; n < size(); n++) {
  313. arr[n].~T();
  314. }
  315. Dealloc();
  316. }
  317. }
  318. T& operator[](size_t pos) {
  319. Y_ASSERT(pos < size());
  320. return ((T*)Ptr())[pos];
  321. }
  322. const T& operator[](size_t pos) const {
  323. Y_ASSERT(pos < size());
  324. return ((T*)Ptr())[pos];
  325. }
  326. T* begin() {
  327. return (T*)Ptr();
  328. }
  329. T* end() {
  330. return (T*)((char*)Ptr() + MappedSize());
  331. }
  332. size_t size() const {
  333. return MappedSize() / sizeof(T);
  334. }
  335. void swap(TMappedArray<T>& with) {
  336. TMappedAllocation::swap(with);
  337. }
  338. };