-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMappedFile.cpp
More file actions
55 lines (43 loc) · 891 Bytes
/
MappedFile.cpp
File metadata and controls
55 lines (43 loc) · 891 Bytes
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
#include "MappedFile.hpp"
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
using namespace std;
MappedFile::MappedFile(const string &s){
#if WILL_USE_BOOST
file.open(s);
start = file.data();
length = file.size();
#else
int fd = open( s.c_str(), O_RDONLY);
struct stat sb;
if (fstat (fd, &sb) == -1) {
perror ("fstat");
throw("fstat");
}
if (!S_ISREG (sb.st_mode)) {
throw("not a file");
}
length = sb.st_size;
start = (char*) mmap (0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (start == MAP_FAILED) {
perror ("mmap");
throw("map failed");
}
if (close (fd) == -1) {
perror ("close");
throw("close failed");
}
#endif
}
size_t MappedFile::size() const {
return length;
}
char * MappedFile::data() const {
return start;
}
#if ! WILL_USE_BOOST
MappedFile::~MappedFile(){
munmap(start, size() );
}
#endif