blob: 006e3951b6d4e158ddc501389fccce76b154bddc [file] [log] [blame]
James Kuszmaulcf324122023-01-14 14:07:17 -08001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#include "wpi/MappedFileRegion.h"
6
7#include <sys/types.h>
8
9#ifdef _WIN32
10#ifndef WIN32_LEAN_AND_MEAN
11#define WIN32_LEAN_AND_MEAN
12#endif
13
14#include <windows.h> // NOLINT(build/include_order)
15
16#include <memoryapi.h>
17#include <sysinfoapi.h>
18
19#else // _WIN32
20
21#include <sys/mman.h>
22#include <unistd.h>
23
24#endif // _WIN32
25
26#ifdef _MSC_VER
27#include <io.h>
28#endif
29
30#ifdef _WIN32
31#include "wpi/WindowsError.h"
32#endif
33
34using namespace wpi;
35
36MappedFileRegion::MappedFileRegion(fs::file_t f, uint64_t length,
37 uint64_t offset, MapMode mapMode,
38 std::error_code& ec)
39 : m_size(length) {
40#ifdef _WIN32
41 if (f == INVALID_HANDLE_VALUE) {
42 ec = std::make_error_code(std::errc::bad_file_descriptor);
43 return;
44 }
45
46 HANDLE fileMappingHandle = ::CreateFileMappingW(
47 f, 0, mapMode == kReadOnly ? PAGE_READONLY : PAGE_READWRITE, length >> 32,
48 length & 0xffffffff, 0);
49 if (fileMappingHandle == nullptr) {
50 ec = wpi::mapWindowsError(GetLastError());
51 return;
52 }
53
54 DWORD dwDesiredAccess = 0;
55 switch (mapMode) {
56 case kReadOnly:
57 dwDesiredAccess = FILE_MAP_READ;
58 break;
59 case kReadWrite:
60 dwDesiredAccess = FILE_MAP_WRITE;
61 break;
62 case kPriv:
63 dwDesiredAccess = FILE_MAP_WRITE | FILE_MAP_COPY;
64 break;
65 }
66 m_mapping = ::MapViewOfFile(fileMappingHandle, dwDesiredAccess, offset >> 32,
67 offset & 0xffffffff, length);
68 if (m_mapping == nullptr) {
69 ec = wpi::mapWindowsError(GetLastError());
70 ::CloseHandle(fileMappingHandle);
71 return;
72 }
73
74 // Close the file mapping handle, as it's kept alive by the file mapping. But
75 // neither the file mapping nor the file mapping handle keep the file handle
76 // alive, so we need to keep a reference to the file in case all other handles
77 // are closed and the file is deleted, which may cause invalid data to be read
78 // from the file.
79 ::CloseHandle(fileMappingHandle);
80 if (!::DuplicateHandle(::GetCurrentProcess(), f, ::GetCurrentProcess(),
81 &m_fileHandle, 0, 0, DUPLICATE_SAME_ACCESS)) {
82 ec = wpi::mapWindowsError(GetLastError());
83 ::UnmapViewOfFile(m_mapping);
84 m_mapping = nullptr;
85 return;
86 }
87#else
88 m_mapping =
89 ::mmap(nullptr, length,
90 mapMode == kReadOnly ? PROT_READ : (PROT_READ | PROT_WRITE),
91 mapMode == kPriv ? MAP_PRIVATE : MAP_SHARED, f, offset);
92 if (m_mapping == MAP_FAILED) {
93 ec = std::error_code(errno, std::generic_category());
94 m_mapping = nullptr;
95 }
96#endif
97}
98
99void MappedFileRegion::Flush() {
100#ifdef _WIN32
101 ::FlushViewOfFile(m_mapping, 0);
102 ::FlushFileBuffers(m_fileHandle);
103#else
104 ::msync(m_mapping, m_size, MS_ASYNC);
105#endif
106}
107
108void MappedFileRegion::Unmap() {
109 if (!m_mapping) {
110 return;
111 }
112#ifdef _WIN32
113 ::UnmapViewOfFile(m_mapping);
114 ::CloseHandle(m_fileHandle);
115#else
116 ::munmap(m_mapping, m_size);
117#endif
118 m_mapping = nullptr;
119}
120
121size_t MappedFileRegion::GetAlignment() {
122#ifdef _WIN32
123 SYSTEM_INFO SysInfo;
124 ::GetSystemInfo(&SysInfo);
125 return SysInfo.dwAllocationGranularity;
126#else
127 static long pageSize = ::getpagesize(); // NOLINT
128 if (pageSize < 0) {
129 pageSize = 4096;
130 }
131 return pageSize;
132#endif
133}