37 if (_data !=
nullptr) {
40 if (_mapping_handle) {
41 destroy_mapping(_mapping_handle);
49 _size = _file->size() - _offset;
52 if (_offset + _size > _file->size()) {
53 throw io_error(
"Requested mapping is beyond file size.");
56 if (_file->size() == 0) {
61 _mapping_handle = make_mapping(file_handle(),
access_mode(), _offset + _size);
63 _data = make_view(_mapping_handle,
access_mode(), _offset, _size);
65 destroy_mapping(_mapping_handle);
83 hi_assert_not_null(_file);
84 return _file->access_mode();
87 [[nodiscard]] void_span void_span()
const noexcept
89 hi_assert_not_null(_file);
91 return {_data, _size};
94 [[nodiscard]] const_void_span const_void_span()
const noexcept
96 return {_data, _size};
99 [[nodiscard]]
bool unmapped()
const noexcept
101 if (_file !=
nullptr) {
102 if (_file->closed()) {
119 destroy_mapping(_mapping_handle);
120 _mapping_handle =
nullptr;
125 void flush(hi::void_span span)
const noexcept
127 if (not FlushViewOfFile(span.data(), span.size())) {
133 mutable HANDLE _mapping_handle =
nullptr;
137 void *_data =
nullptr;
139 [[nodiscard]] HANDLE file_handle()
noexcept
141 hi_assert_not_null(_file);
142 return _file->file_handle();
145 static void destroy_mapping(HANDLE mapping)
147 if (not CloseHandle(mapping)) {
154 hi_assert(size != 0);
158 protect = PAGE_READWRITE;
159 }
else if (to_bool(
access_mode & hi::access_mode::read)) {
160 protect = PAGE_READONLY;
162 throw io_error(
"Illegal access mode when mapping file.");
165 DWORD size_high = size >> 32;
166 DWORD size_low = size & 0xffffffff;
168 if (
auto r = CreateFileMappingW(
file, NULL, protect, size_high, size_low,
nullptr)) {
175 static void destroy_view(
void *data)
177 if (not UnmapViewOfFile(data)) {
184 hi_assert(size != 0);
186 DWORD desired_access;
188 desired_access = FILE_MAP_WRITE;
190 desired_access = FILE_MAP_READ;
192 throw io_error(std::format(
"Illegal access mode when viewing file."));
195 DWORD offset_high = offset >> 32;
196 DWORD offset_low = offset & 0xffffffff;
198 if (
auto r = MapViewOfFile(mapping, desired_access, offset_high, offset_low, size)) {
hi_export std::string get_last_error_message()
Get the OS error message from the last error received on this thread.
Definition exception_win32_impl.hpp:30