Repository navigation
Expand file tree
/
Copy pathMmapWin32.cc
More file actions
63 lines (51 loc) · 1.62 KB
/
Copy pathMmapWin32.cc
File metadata and controls
63 lines (51 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright © 2023 GothicKit Contributors.
// SPDX-License-Identifier: MIT
#include "zenkit/Mmap.hh"
#include <windows.h>
namespace zenkit {
struct Platform {
HANDLE hFile;
HANDLE hFileMapping;
};
Mmap::Mmap(std::filesystem::path const& path) {
HANDLE hFile;
hFile = CreateFileW(path.c_str(),
GENERIC_READ,
FILE_SHARE_READ,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr);
if (hFile == INVALID_HANDLE_VALUE) {
throw std::runtime_error {"Failed to open " + path.string()};
}
_m_size = GetFileSize(hFile, nullptr);
HANDLE hFileMapping = CreateFileMappingA(hFile, nullptr, PAGE_READONLY, 0, _m_size, nullptr);
if (hFileMapping == nullptr) {
CloseHandle(hFile);
throw std::runtime_error {"Failed to memory-map " + path.string()};
}
_m_data = static_cast<std::byte const*>(MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0));
_m_platform_handle = new Platform {hFile, hFileMapping};
}
Mmap::Mmap(Mmap&& other) noexcept {
_m_data = other._m_data;
_m_size = other._m_size;
_m_platform_handle = other._m_platform_handle;
other._m_data = nullptr;
other._m_size = 0;
other._m_platform_handle = nullptr;
}
Mmap::~Mmap() noexcept {
if (_m_platform_handle != nullptr) {
UnmapViewOfFile(_m_data);
auto* platform = reinterpret_cast<Platform*>(_m_platform_handle);
CloseHandle(platform->hFileMapping);
CloseHandle(platform->hFile);
delete platform;
_m_data = nullptr;
_m_size = 0;
_m_platform_handle = nullptr;
}
}
} // namespace zenkit