Represents a read-only file mapped to an address space in memory. If no memory-mapping API is available, falls back to allocating a buffer with a copy of the file's data.
| 8256 | // If no memory-mapping API is available, falls back to allocating a buffer |
| 8257 | // with a copy of the file's data. |
| 8258 | struct MemoryMappedFile { |
| 8259 | unsigned char *data; // To the start of the file's data. |
| 8260 | size_t size; // The size of the file in bytes. |
| 8261 | #ifdef TINYEXR_USE_WIN32_MMAP |
| 8262 | HANDLE windows_file; |
| 8263 | HANDLE windows_file_mapping; |
| 8264 | #elif defined(TINYEXR_USE_POSIX_MMAP) |
| 8265 | int posix_descriptor; |
| 8266 | #endif |
| 8267 | |
| 8268 | // MemoryMappedFile's constructor tries to map memory to a file. |
| 8269 | // If this succeeds, valid() will return true and all fields |
| 8270 | // are usable; otherwise, valid() will return false. |
| 8271 | MemoryMappedFile(const char *filename) { |
| 8272 | data = NULL; |
| 8273 | size = 0; |
| 8274 | #ifdef TINYEXR_USE_WIN32_MMAP |
| 8275 | windows_file_mapping = NULL; |
| 8276 | windows_file = |
| 8277 | CreateFileW(tinyexr::UTF8ToWchar(filename).c_str(), // lpFileName |
| 8278 | GENERIC_READ, // dwDesiredAccess |
| 8279 | FILE_SHARE_READ, // dwShareMode |
| 8280 | NULL, // lpSecurityAttributes |
| 8281 | OPEN_EXISTING, // dwCreationDisposition |
| 8282 | FILE_ATTRIBUTE_READONLY, // dwFlagsAndAttributes |
| 8283 | NULL); // hTemplateFile |
| 8284 | if (windows_file == INVALID_HANDLE_VALUE) { |
| 8285 | return; |
| 8286 | } |
| 8287 | |
| 8288 | windows_file_mapping = CreateFileMapping(windows_file, // hFile |
| 8289 | NULL, // lpFileMappingAttributes |
| 8290 | PAGE_READONLY, // flProtect |
| 8291 | 0, // dwMaximumSizeHigh |
| 8292 | 0, // dwMaximumSizeLow |
| 8293 | NULL); // lpName |
| 8294 | if (windows_file_mapping == NULL) { |
| 8295 | return; |
| 8296 | } |
| 8297 | |
| 8298 | data = reinterpret_cast<unsigned char *>( |
| 8299 | MapViewOfFile(windows_file_mapping, // hFileMappingObject |
| 8300 | FILE_MAP_READ, // dwDesiredAccess |
| 8301 | 0, // dwFileOffsetHigh |
| 8302 | 0, // dwFileOffsetLow |
| 8303 | 0)); // dwNumberOfBytesToMap |
| 8304 | if (!data) { |
| 8305 | return; |
| 8306 | } |
| 8307 | |
| 8308 | LARGE_INTEGER windows_file_size = {}; |
| 8309 | if (!GetFileSizeEx(windows_file, &windows_file_size) || |
| 8310 | static_cast<ULONGLONG>(windows_file_size.QuadPart) > |
| 8311 | std::numeric_limits<size_t>::max()) { |
| 8312 | UnmapViewOfFile(data); |
| 8313 | data = NULL; |
| 8314 | return; |
| 8315 | } |
nothing calls this directly
no outgoing calls
no test coverage detected