-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmap_file.hh
99 lines (84 loc) · 2.57 KB
/
mmap_file.hh
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
#pragma once
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <assert.h>
#include <algorithm>
namespace nikki {
// A wrapper class that opens a file using mmap,
// and automatically cleans itself up (RAII).
class MmapFile {
public:
// Disallow copy.
MmapFile(const MmapFile&) = delete;
// TODO: support writing as well.
MmapFile(const char* file_name) : file_name_(file_name) {
fd_ = ::open(file_name, O_RDONLY, 0);
assert(fd_ != -1);
file_size_ = get_file_size(file_name);
mmap_start_ = static_cast<char*>(::mmap(NULL,
file_size_,
PROT_READ,
MAP_PRIVATE,
fd_,
0));
assert(mmap_start_ != MAP_FAILED);
// set cursor and boundary.
cur_ = mmap_start_;
end_ = cur_ + file_size_;
}
// Cleanup when goes out of scope
~MmapFile() {
int rc = ::munmap(mmap_start_, file_size_);
assert(rc == 0);
rc = ::close(fd_);
assert(rc == 0);
}
// Move constructor
MmapFile(MmapFile&& other) {
std::swap(file_name_, other.file_name_);
std::swap(mmap_start_, other.mmap_start_);
std::swap(fd_, other.fd_);
std::swap(file_size_, other.file_size_);
std::swap(cur_, other.cur_);
std::swap(end_, other.end_);
}
// copy bytes to destination and advance cursor.
// return bytes copied.
size_t read(char* dest, size_t n) {
n = std::min(n, static_cast<size_t>(end_ - cur_));
::memcpy(dest, cur_, n);
cur_ += n;
return n;
}
// seek to a position within the file.
// return false if seek out of bounds.
bool seek(size_t offset_from_begin) {
if (mmap_start_ + offset_from_begin >= end_) {
return false;
}
cur_ = mmap_start_ + offset_from_begin;
return true;
}
// return the size of the file.
size_t size() const {
return file_size_;
}
private:
static size_t get_file_size(const char* filename) {
struct stat st;
::stat(filename, &st);
return st.st_size;
}
const char* file_name_;
char* mmap_start_ = nullptr;
int fd_ = -1;
size_t file_size_ = 0;
// current cursor.
char* cur_ = nullptr;
// end of file.
char* end_ = nullptr;
};
} // namespace nikki