-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmman.h
More file actions
187 lines (132 loc) · 4.46 KB
/
mman.h
File metadata and controls
187 lines (132 loc) · 4.46 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef MMAN_H
#define MMAN_H
#include <cstdint>
#include <cstddef>
#ifndef _WIN32
#include <sys/types.h>
#include <sys/mman.h>
#endif
// ====================== POSIX-compatible constants ======================
#ifndef PROT_NONE
#define PROT_NONE 0
#define PROT_READ 1
#define PROT_WRITE 2
#define PROT_EXEC 4
#endif
#ifndef MAP_FILE
#define MAP_FILE 0
#define MAP_SHARED 1
#define MAP_PRIVATE 2
#define MAP_FIXED 0x10
#define MAP_ANONYMOUS 0x20
#define MAP_ANON MAP_ANONYMOUS
#endif
#ifndef MS_ASYNC
#define MS_ASYNC 1
#define MS_SYNC 2
#define MS_INVALIDATE 4
#endif
#ifndef MAP_FAILED
#define MAP_FAILED ((void*)(~(size_t)0))
#endif
// ====================== Types ======================
#ifdef _WIN32
using mman_offset_t = int64_t;
#else
using mman_offset_t = off_t;
#endif
// ====================== C Interface ======================
#ifdef _WIN32
#ifdef __cplusplus
extern "C" {
#endif
void *mmap(void *addr, size_t len, int prot, int flags, int fd, mman_offset_t offset);
int munmap(void *addr, size_t len);
int mprotect(void *addr, size_t len, int prot);
int msync(void *addr, size_t len, int flags);
int mlock(const void *addr, size_t len);
int munlock(const void *addr, size_t len);
#ifdef __cplusplus
}
#endif
#endif
// ====================== C++ Interface ======================
#ifdef __cplusplus
#include <system_error>
namespace mman {
// ====================== Enums ======================
enum class Prot : int {
None = PROT_NONE,
Read = PROT_READ,
Write = PROT_WRITE,
Exec = PROT_EXEC
};
constexpr Prot operator|(Prot a, Prot b) noexcept {
return static_cast<Prot>(static_cast<int>(a) | static_cast<int>(b));
}
constexpr int operator&(Prot a, Prot b) noexcept {
return static_cast<int>(a) & static_cast<int>(b);
}
enum class MapFlags : int {
File = MAP_FILE,
Shared = MAP_SHARED,
Private = MAP_PRIVATE,
Fixed = MAP_FIXED,
Anonymous = MAP_ANONYMOUS
};
constexpr MapFlags operator|(MapFlags a, MapFlags b) noexcept {
return static_cast<MapFlags>(static_cast<int>(a) | static_cast<int>(b));
}
constexpr int operator&(MapFlags a, MapFlags b) noexcept {
return static_cast<int>(a) & static_cast<int>(b);
}
enum class SyncFlags : int {
Async = MS_ASYNC,
Sync = MS_SYNC,
Invalidate = MS_INVALIDATE
};
// ====================== Result Types ======================
struct MapResult {
void *ptr = MAP_FAILED;
std::error_code error{};
[[nodiscard]] bool ok() const noexcept { return ptr != MAP_FAILED; }
[[nodiscard]] explicit operator bool() const noexcept { return ok(); }
};
struct OpResult {
bool success = false;
std::error_code error{};
[[nodiscard]] explicit operator bool() const noexcept { return success; }
};
// ====================== Functions ======================
MapResult map(void *addr, size_t len, Prot prot, MapFlags flags, int fd = -1, mman_offset_t offset = 0) noexcept;
OpResult unmap(void *addr, size_t len) noexcept;
OpResult protect(void *addr, size_t len, Prot prot) noexcept;
OpResult sync(void *addr, size_t len, SyncFlags flags = SyncFlags::Sync) noexcept;
OpResult lock(const void *addr, size_t len) noexcept;
OpResult unlock(const void *addr, size_t len) noexcept;
// ====================== RAII Wrapper ======================
class MappedMemory {
public:
MappedMemory() = default;
MappedMemory(void *addr, size_t len, Prot prot, MapFlags flags,
int fd = -1, mman_offset_t offset = 0);
~MappedMemory();
// Move only
MappedMemory(MappedMemory &&other) noexcept;
MappedMemory &operator=(MappedMemory &&other) noexcept;
MappedMemory(const MappedMemory &) = delete;
MappedMemory &operator=(const MappedMemory &) = delete;
void reset() noexcept;
void *release() noexcept;
[[nodiscard]] void *get() const noexcept { return ptr_; }
[[nodiscard]] size_t size() const noexcept { return size_; }
[[nodiscard]] explicit operator bool() const noexcept { return ptr_ != nullptr; }
template<typename T>
[[nodiscard]] T *as() const noexcept { return static_cast<T *>(ptr_); }
private:
void *ptr_ = nullptr;
size_t size_ = 0;
};
} // namespace mman
#endif // __cplusplus
#endif // MMAN_H