-
Notifications
You must be signed in to change notification settings - Fork 18
/
mmaplib.h
154 lines (122 loc) · 2.56 KB
/
mmaplib.h
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
//
// mmaplib.h
//
// Copyright (c) 2021 Yuji Hirose. All rights reserved.
// MIT License
//
#ifndef _CPPMMAPLIB_MMAPLIB_H_
#define _CPPMMAPLIB_MMAPLIB_H_
#if defined(_WIN32)
#include <windows.h>
#else
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <stdexcept>
namespace mmaplib {
class mmap {
public:
mmap();
mmap(const char *path);
~mmap();
bool open(const char *path);
void close();
bool is_open() const;
size_t size() const;
const char *data() const;
private:
#if defined(_WIN32)
HANDLE hFile_;
HANDLE hMapping_;
#else
int fd_;
#endif
size_t size_;
void *addr_;
};
#if defined(_WIN32)
#define MAP_FAILED NULL
#endif
inline mmap::mmap()
#if defined(_WIN32)
: hFile_(NULL), hMapping_(NULL)
#else
: fd_(-1)
#endif
,
size_(0), addr_(MAP_FAILED) {
}
inline mmap::mmap(const char *path)
#if defined(_WIN32)
: hFile_(NULL), hMapping_(NULL)
#else
: fd_(-1)
#endif
,
size_(0), addr_(MAP_FAILED) {
open(path);
}
inline mmap::~mmap() { close(); }
inline bool mmap::open(const char *path) {
close();
#if defined(_WIN32)
hFile_ = ::CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile_ == INVALID_HANDLE_VALUE) { return false; }
size_ = ::GetFileSize(hFile_, NULL);
hMapping_ = ::CreateFileMapping(hFile_, NULL, PAGE_READONLY, 0, 0, NULL);
if (hMapping_ == NULL) {
close();
return false;
}
addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0);
#else
fd_ = ::open(path, O_RDONLY);
if (fd_ == -1) { return false; }
struct stat sb;
if (fstat(fd_, &sb) == -1) {
close();
return false;
}
size_ = sb.st_size;
addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0);
#endif
if (addr_ == MAP_FAILED) {
close();
return false;
}
return true;
}
inline bool mmap::is_open() const { return addr_ != MAP_FAILED; }
inline size_t mmap::size() const { return size_; }
inline const char *mmap::data() const { return (const char *)addr_; }
inline void mmap::close() {
#if defined(_WIN32)
if (addr_) {
::UnmapViewOfFile(addr_);
addr_ = MAP_FAILED;
}
if (hMapping_) {
::CloseHandle(hMapping_);
hMapping_ = NULL;
}
if (hFile_ != INVALID_HANDLE_VALUE) {
::CloseHandle(hFile_);
hFile_ = INVALID_HANDLE_VALUE;
}
#else
if (addr_ != MAP_FAILED) {
munmap(addr_, size_);
addr_ = MAP_FAILED;
}
if (fd_ != -1) {
::close(fd_);
fd_ = -1;
}
#endif
size_ = 0;
}
} // namespace mmaplib
#endif