-
Notifications
You must be signed in to change notification settings - Fork 8
/
mapped_file.cc
206 lines (179 loc) · 4.74 KB
/
mapped_file.cc
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// Copyright RIME Developers
// Distributed under the BSD License
//
// register components
//
// 2011-06-30 GONG Chen <[email protected]>
//
#include <fstream>
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
//#include <boost/filesystem.hpp>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include "mapped_file.h"
#ifdef BOOST_RESIZE_FILE
#define RESIZE_FILE boost::filesystem::resize_file
#else
#ifdef _WIN32
#include <windows.h>
#define RESIZE_FILE(P,SZ) (resize_file_api(P, SZ) != 0)
static BOOL resize_file_api(const char* p, boost::uintmax_t size) {
HANDLE handle = CreateFileA(p, GENERIC_WRITE, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
LARGE_INTEGER sz;
sz.QuadPart = size;
return handle != INVALID_HANDLE_VALUE
&& ::SetFilePointerEx(handle, sz, 0, FILE_BEGIN)
&& ::SetEndOfFile(handle)
&& ::CloseHandle(handle);
}
#else
#include <unistd.h>
#define RESIZE_FILE(P,SZ) (::truncate(P, SZ) == 0)
#endif // _WIN32
#endif // BOOST_RESIZE_FILE
namespace rime {
class MappedFileImpl {
public:
enum OpenMode {
kOpenReadOnly,
kOpenReadWrite,
};
MappedFileImpl(const string& file_name, OpenMode mode) {
boost::interprocess::mode_t file_mapping_mode =
(mode == kOpenReadOnly) ? boost::interprocess::read_only
: boost::interprocess::read_write;
file_.reset(new boost::interprocess::file_mapping(file_name.c_str(), file_mapping_mode));
region_.reset(new boost::interprocess::mapped_region(*file_, file_mapping_mode));
}
~MappedFileImpl() {
region_.reset();
file_.reset();
}
bool Flush() {
return region_->flush();
}
void* get_address() const {
return region_->get_address();
}
size_t get_size() const {
return region_->get_size();
}
private:
the<boost::interprocess::file_mapping> file_;
the<boost::interprocess::mapped_region> region_;
};
MappedFile::MappedFile(const string& file_name)
: file_name_(file_name) {
}
MappedFile::~MappedFile() {
if (file_) {
file_.reset();
}
}
bool MappedFile::Create(size_t capacity) {
if (Exists()) {
LOG(INFO) << "overwriting file '" << file_name_ << "'.";
Resize(capacity);
}
else {
LOG(INFO) << "creating file '" << file_name_ << "'.";
std::filebuf fbuf;
fbuf.open(file_name_.c_str(),
std::ios_base::in | std::ios_base::out |
std::ios_base::trunc | std::ios_base::binary);
if (capacity > 0) {
fbuf.pubseekoff(capacity - 1, std::ios_base::beg);
fbuf.sputc(0);
}
fbuf.close();
}
LOG(INFO) << "opening file for read/write access.";
file_.reset(new MappedFileImpl(file_name_, MappedFileImpl::kOpenReadWrite));
size_ = 0;
return bool(file_);
}
bool MappedFile::OpenReadOnly() {
if (!Exists()) {
LOG(ERROR) << "attempt to open non-existent file '" << file_name_ << "'.";
return false;
}
file_.reset(new MappedFileImpl(file_name_, MappedFileImpl::kOpenReadOnly));
size_ = file_->get_size();
return bool(file_);
}
bool MappedFile::OpenReadWrite() {
if (!Exists()) {
LOG(ERROR) << "attempt to open non-existent file '" << file_name_ << "'.";
return false;
}
file_.reset(new MappedFileImpl(file_name_, MappedFileImpl::kOpenReadWrite));
size_ = 0;
return bool(file_);
}
void MappedFile::Close() {
if (file_) {
file_.reset();
size_ = 0;
}
}
bool MappedFile::Exists() const {
return boost::filesystem::exists(file_name_);
}
bool MappedFile::IsOpen() const {
return bool(file_);
}
bool MappedFile::Flush() {
if (!file_)
return false;
return file_->Flush();
}
bool MappedFile::ShrinkToFit() {
LOG(INFO) << "shrinking file to fit data size. capacity: " << capacity();
return Resize(size_);
}
bool MappedFile::Remove() {
if (IsOpen())
Close();
return boost::interprocess::file_mapping::remove(file_name_.c_str());
}
bool MappedFile::Resize(size_t capacity) {
LOG(INFO) << "resize file to: " << capacity;
if (IsOpen())
Close();
try {
RESIZE_FILE(file_name_.c_str(), capacity);
}
catch (...) {
return false;
}
return true;
}
String* MappedFile::CreateString(const string& str) {
String* ret = Allocate<String>();
if (ret && !str.empty()) {
CopyString(str, ret);
}
return ret;
}
bool MappedFile::CopyString(const string& src, String* dest) {
if (!dest)
return false;
size_t size = src.length() + 1;
char* ptr = Allocate<char>(size);
if (!ptr)
return false;
std::strncpy(ptr, src.c_str(), size);
dest->data = ptr;
return true;
}
size_t MappedFile::capacity() const {
return file_->get_size();
}
char* MappedFile::address() const {
return reinterpret_cast<char*>(file_->get_address());
}
} // namespace rime