forked from taku910/crfpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmap.h
213 lines (174 loc) · 4.66 KB
/
mmap.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
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
207
208
209
210
211
212
213
//
// CRF++ -- Yet Another CRF toolkit
//
// $Id: mmap.h 1588 2007-02-12 09:03:39Z taku $;
//
// Copyright(C) 2005-2007 Taku Kudo <[email protected]>
//
#ifndef CRFPP_MMAP_H_
#define CRFPP_MMAP_H_
#include <errno.h>
#include <string>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
extern "C" {
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
#else
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#endif
}
#include "common.h"
#ifndef O_BINARY
#define O_BINARY 0
#endif
namespace CRFPP {
template <class T> class Mmap {
private:
T *text;
size_t length;
std::string fileName;
whatlog what_;
#if defined(_WIN32) && !defined(__CYGWIN__)
HANDLE hFile;
HANDLE hMap;
#else
int fd;
int flag;
#endif
public:
T& operator[](size_t n) { return *(text + n); }
const T& operator[](size_t n) const { return *(text + n); }
T* begin() { return text; }
const T* begin() const { return text; }
T* end() { return text + size(); }
const T* end() const { return text + size(); }
size_t size() { return length/sizeof(T); }
const char *what() { return what_.str(); }
const char *file_name() { return fileName.c_str(); }
size_t file_size() { return length; }
bool empty() { return(length == 0); }
// This code is imported from sufary, develoved by
// TATUO Yamashita <[email protected]> Thanks!
#if defined(_WIN32) && !defined(__CYGWIN__)
bool open(const char *filename, const char *mode = "r") {
this->close();
unsigned long mode1, mode2, mode3;
fileName = std::string(filename);
if (std::strcmp(mode, "r") == 0) {
mode1 = GENERIC_READ;
mode2 = PAGE_READONLY;
mode3 = FILE_MAP_READ;
} else if (std::strcmp(mode, "r+") == 0) {
mode1 = GENERIC_READ | GENERIC_WRITE;
mode2 = PAGE_READWRITE;
mode3 = FILE_MAP_ALL_ACCESS;
} else {
CHECK_FALSE(false) << "unknown open mode:" << filename;
}
hFile = ::CreateFileW(WPATH(filename), mode1, FILE_SHARE_READ, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
CHECK_FALSE(hFile != INVALID_HANDLE_VALUE)
<< "CreateFile() failed: " << filename;
length = ::GetFileSize(hFile, 0);
hMap = ::CreateFileMapping(hFile, 0, mode2, 0, 0, 0);
CHECK_FALSE(hMap) << "CreateFileMapping() failed: " << filename;
text = reinterpret_cast<T *>(::MapViewOfFile(hMap, mode3, 0, 0, 0));
CHECK_FALSE(text) << "MapViewOfFile() failed: " << filename;
return true;
}
void close() {
if (text) { ::UnmapViewOfFile(text); }
if (hFile != INVALID_HANDLE_VALUE) {
::CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
if (hMap) {
::CloseHandle(hMap);
hMap = 0;
}
text = 0;
}
Mmap(): text(0), hFile(INVALID_HANDLE_VALUE), hMap(0) {}
#else
bool open(const char *filename, const char *mode = "r") {
this->close();
struct stat st;
fileName = std::string(filename);
if (std::strcmp(mode, "r") == 0)
flag = O_RDONLY;
else if (std::strcmp(mode, "r+") == 0)
flag = O_RDWR;
else
CHECK_FALSE(false) << "unknown open mode: " << filename;
CHECK_FALSE((fd = ::open(filename, flag | O_BINARY)) >= 0)
<< "open failed: " << filename;
CHECK_FALSE(fstat(fd, &st) >= 0)
<< "failed to get file size: " << filename;
length = st.st_size;
#ifdef HAVE_MMAP
int prot = PROT_READ;
if (flag == O_RDWR) prot |= PROT_WRITE;
char *p;
CHECK_FALSE((p = reinterpret_cast<char *>
(mmap(0, length, prot, MAP_SHARED, fd, 0)))
!= MAP_FAILED)
<< "mmap() failed: " << filename;
text = reinterpret_cast<T *>(p);
#else
text = new T[length];
CHECK_FALSE(read(fd, text, length) >= 0)
<< "read() failed: " << filename;
#endif
::close(fd);
fd = -1;
return true;
}
void close() {
if (fd >= 0) {
::close(fd);
fd = -1;
}
if (text) {
#ifdef HAVE_MMAP
munmap(reinterpret_cast<char *>(text), length);
text = 0;
#else
if (flag == O_RDWR) {
int fd2;
if ((fd2 = ::open(fileName.c_str(), O_RDWR)) >= 0) {
write(fd2, text, length);
::close(fd2);
}
}
delete [] text;
#endif
}
text = 0;
}
Mmap(): text(0), fd(-1) {}
#endif
virtual ~Mmap() { this->close(); }
};
}
#endif