blob: ced79f52baf71d0efc2ccacf02941c8d5e6f2680 (
plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#pragma once
#include <string>
#include <cstdint>
#include <cstdio>
#include <optional>
// Call this when initializing the plugin so that the process file descriptor limit is raised.
uint64_t AdjustFileDescriptorLimit();
enum MapStatus : int
{
Success,
// TODO: Split this error out into more contextual errors.
Error,
};
struct MappedFile
{
uint8_t* _mmap = nullptr;
size_t len = 0;
#ifdef _MSC_VER
HANDLE hFile = INVALID_HANDLE_VALUE;
#else
FILE* fd = nullptr;
#endif
MappedFile() = default;
~MappedFile();
MappedFile(const MappedFile&) = delete;
MappedFile& operator=(const MappedFile&) = delete;
MappedFile(MappedFile&& other) noexcept : _mmap(other._mmap), len(other.len)
{
#ifdef _MSC_VER
hFile = other.hFile;
// Don't close the hFile in the move.
other.hFile = nullptr;
#else
fd = other.fd;
// Don't close the fd in the move.
other.fd = nullptr;
#endif
other._mmap = nullptr;
}
// I hate C++
MappedFile& operator=(MappedFile&& other) noexcept
{
if (this != &other)
{
Unmap();
#ifdef _MSC_VER
if (hFile != nullptr)
{
CloseHandle(hFile);
}
hFile = other.hFile;
// Don't close the hFile in the move.
other.hFile = nullptr;
#else
if (fd != nullptr)
{
fclose(fd);
}
fd = other.fd;
// Don't close the fd in the move.
other.fd = nullptr;
#endif
len = other.len;
_mmap = other._mmap;
other._mmap = nullptr;
}
return *this;
}
static std::optional<MappedFile> OpenFile(const std::string& path);
MapStatus Map();
MapStatus Unmap();
};
|