mapped_file.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <util/generic/flags.h>
  3. #include <util/generic/ptr.h>
  4. #include <util/generic/string.h>
  5. #include <util/generic/utility.h>
  6. #include <util/generic/yexception.h>
  7. #include <util/system/align.h>
  8. #include <util/system/file.h>
  9. #include <util/system/filemap.h>
  10. #include <util/system/yassert.h>
  11. #include <cstdio>
  12. #include <new>
  13. /// Deprecated (by pg@), use TFileMap or TMemoryMap instead
  14. class TMappedFile {
  15. private:
  16. TFileMap* Map_;
  17. private:
  18. TMappedFile(TFileMap* map, const char* dbgName);
  19. public:
  20. TMappedFile() {
  21. Map_ = nullptr;
  22. }
  23. ~TMappedFile() {
  24. term();
  25. }
  26. explicit TMappedFile(const TString& name) {
  27. Map_ = nullptr;
  28. init(name, TFileMap::oRdOnly);
  29. }
  30. TMappedFile(const TFile& file, TFileMap::EOpenMode om = TFileMap::oRdOnly, const char* dbgName = "unknown");
  31. void init(const TString& name);
  32. void init(const TString& name, TFileMap::EOpenMode om);
  33. void init(const TString& name, size_t length, TFileMap::EOpenMode om);
  34. void init(const TFile&, TFileMap::EOpenMode om = TFileMap::oRdOnly, const char* dbgName = "unknown");
  35. void flush();
  36. void term() {
  37. if (Map_) {
  38. Map_->Unmap();
  39. delete Map_;
  40. Map_ = nullptr;
  41. }
  42. }
  43. size_t getSize() const {
  44. return (Map_ ? Map_->MappedSize() : 0);
  45. }
  46. void* getData(size_t pos = 0) const {
  47. Y_ASSERT(!Map_ || (pos <= getSize()));
  48. return (Map_ ? (void*)((unsigned char*)Map_->Ptr() + pos) : nullptr);
  49. }
  50. void precharge(size_t pos = 0, size_t size = (size_t)-1) const;
  51. void swap(TMappedFile& file) noexcept {
  52. DoSwap(Map_, file.Map_);
  53. }
  54. };