|
| 1 | +#include "memory_mapped_file.h" |
| 2 | + |
| 3 | +#if !defined(_WIN32) |
| 4 | +# include <cstring> |
| 5 | +# include <cstdio> |
| 6 | +# include <fcntl.h> |
| 7 | +# include <unistd.h> |
| 8 | +#endif |
| 9 | + |
| 10 | +MemoryMappedFile::MemoryMappedFile() |
| 11 | +{ |
| 12 | + // Default constructor. |
| 13 | +} |
| 14 | + |
| 15 | +MemoryMappedFile::MemoryMappedFile(const std::filesystem::path &path) |
| 16 | +{ |
| 17 | + open(path); |
| 18 | +} |
| 19 | + |
| 20 | +MemoryMappedFile::~MemoryMappedFile() |
| 21 | +{ |
| 22 | + close(); |
| 23 | +} |
| 24 | + |
| 25 | +MemoryMappedFile::MemoryMappedFile(MemoryMappedFile &&other) |
| 26 | +{ |
| 27 | +#if defined(_WIN32) |
| 28 | + fileHandle = other.fileHandle; |
| 29 | + fileMappingHandle = other.fileMappingHandle; |
| 30 | + fileView = other.fileView; |
| 31 | + fileSize = other.fileSize; |
| 32 | + |
| 33 | + other.fileHandle = nullptr; |
| 34 | + other.fileMappingHandle = nullptr; |
| 35 | + other.fileView = nullptr; |
| 36 | + other.fileSize.QuadPart = 0; |
| 37 | +#else |
| 38 | + fileHandle = other.fileHandle; |
| 39 | + fileView = other.fileView; |
| 40 | + fileSize = other.fileSize; |
| 41 | + |
| 42 | + other.fileHandle = -1; |
| 43 | + other.fileView = MAP_FAILED; |
| 44 | + other.fileSize = 0; |
| 45 | +#endif |
| 46 | +} |
| 47 | + |
| 48 | +bool MemoryMappedFile::open(const std::filesystem::path &path) |
| 49 | +{ |
| 50 | +#if defined(_WIN32) |
| 51 | + fileHandle = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 52 | + if (fileHandle == INVALID_HANDLE_VALUE) |
| 53 | + { |
| 54 | + fprintf(stderr, "CreateFileW failed with error %lu.\n", GetLastError()); |
| 55 | + fileHandle = nullptr; |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + if (!GetFileSizeEx(fileHandle, &fileSize)) |
| 60 | + { |
| 61 | + fprintf(stderr, "GetFileSizeEx failed with error %lu.\n", GetLastError()); |
| 62 | + CloseHandle(fileHandle); |
| 63 | + fileHandle = nullptr; |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + fileMappingHandle = CreateFileMappingW(fileHandle, nullptr, PAGE_READONLY, 0, 0, nullptr); |
| 68 | + if (fileMappingHandle == nullptr) |
| 69 | + { |
| 70 | + fprintf(stderr, "CreateFileMappingW failed with error %lu.\n", GetLastError()); |
| 71 | + CloseHandle(fileHandle); |
| 72 | + fileHandle = nullptr; |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + fileView = MapViewOfFile(fileMappingHandle, FILE_MAP_READ, 0, 0, 0); |
| 77 | + if (fileView == nullptr) |
| 78 | + { |
| 79 | + fprintf(stderr, "MapViewOfFile failed with error %lu.\n", GetLastError()); |
| 80 | + CloseHandle(fileMappingHandle); |
| 81 | + CloseHandle(fileHandle); |
| 82 | + fileMappingHandle = nullptr; |
| 83 | + fileHandle = nullptr; |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + return true; |
| 88 | +#else |
| 89 | + fileHandle = ::open(path.c_str(), O_RDONLY); |
| 90 | + if (fileHandle == -1) |
| 91 | + { |
| 92 | + fprintf(stderr, "open for %s failed with error %s.\n", path.c_str(), strerror(errno)); |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + fileSize = lseek(fileHandle, 0, SEEK_END); |
| 97 | + if (fileSize == (off_t)(-1)) |
| 98 | + { |
| 99 | + fprintf(stderr, "lseek failed with error %s.\n", strerror(errno)); |
| 100 | + ::close(fileHandle); |
| 101 | + fileHandle = -1; |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + fileView = mmap(nullptr, fileSize, PROT_READ, MAP_PRIVATE, fileHandle, 0); |
| 106 | + if (fileView == MAP_FAILED) |
| 107 | + { |
| 108 | + fprintf(stderr, "mmap failed with error %s.\n", strerror(errno)); |
| 109 | + ::close(fileHandle); |
| 110 | + fileHandle = -1; |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + return true; |
| 115 | +#endif |
| 116 | +} |
| 117 | + |
| 118 | +void MemoryMappedFile::close() |
| 119 | +{ |
| 120 | +#if defined(_WIN32) |
| 121 | + if (fileView != nullptr) |
| 122 | + { |
| 123 | + UnmapViewOfFile(fileView); |
| 124 | + } |
| 125 | + |
| 126 | + if (fileMappingHandle != nullptr) |
| 127 | + { |
| 128 | + CloseHandle(fileMappingHandle); |
| 129 | + } |
| 130 | + |
| 131 | + if (fileHandle != nullptr) |
| 132 | + { |
| 133 | + CloseHandle(fileHandle); |
| 134 | + } |
| 135 | +#else |
| 136 | + if (fileView != MAP_FAILED) |
| 137 | + { |
| 138 | + munmap(fileView, fileSize); |
| 139 | + } |
| 140 | + |
| 141 | + if (fileHandle != -1) |
| 142 | + { |
| 143 | + ::close(fileHandle); |
| 144 | + } |
| 145 | +#endif |
| 146 | +} |
| 147 | + |
| 148 | +bool MemoryMappedFile::isOpen() const |
| 149 | +{ |
| 150 | +#if defined(_WIN32) |
| 151 | + return (fileView != nullptr); |
| 152 | +#else |
| 153 | + return (fileView != MAP_FAILED); |
| 154 | +#endif |
| 155 | +} |
| 156 | + |
| 157 | +uint8_t *MemoryMappedFile::data() const |
| 158 | +{ |
| 159 | + return reinterpret_cast<uint8_t *>(fileView); |
| 160 | +} |
| 161 | + |
| 162 | +size_t MemoryMappedFile::size() const |
| 163 | +{ |
| 164 | +#if defined(_WIN32) |
| 165 | + return fileSize.QuadPart; |
| 166 | +#else |
| 167 | + return static_cast<size_t>(fileSize); |
| 168 | +#endif |
| 169 | +} |
0 commit comments