-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmap.h
174 lines (139 loc) · 4.47 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
// mmap(2) wrapper
// Part of lvvlib - https://github.com/lvv/lvvlib,
// Copyright (c) 2000-2013
// Leonid Volnitsky ([email protected])
//
// Usage docs at doc/index.txt, see also t-mmap.cc
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
using std::cerr;
namespace lvv {
struct no_file: std::exception {};
struct no_free_space: std::exception {};
struct open_error: std::exception {};
struct io_error: std::exception {};
// this function is not used directly
template<int SHARING=MAP_PRIVATE>
void * mmap_read_ptr (const char *path, size_t& n) {
int src_fd = open(path, O_RDWR);
if (src_fd < 0) {
cerr << "mmap_read error: couldn't open \"" << path << "\" file\n";
throw no_file();
}
struct stat sb;
// get file size
if (fstat(src_fd, &sb) < 0) {
cerr << "mmap_read error: couldn't stat \"" << path << "\" file\n";
close(src_fd);
throw io_error();
}
n = sb.st_size;
void *p = mmap(NULL, n, PROT_READ | PROT_WRITE , SHARING, src_fd, 0);
//void *p = mmap(NULL, sb.st_size , PROT_READ, SHARING, src_fd, 0);
if ( p == MAP_FAILED ) {
cerr << "mmap_read error: couldn't mmap \"" << path << "\" file\n";
close(src_fd);
throw io_error();
}
return p;
}
// read an object of MMAPTED_TYPE
template<typename MMAPED_TYPE, int SHARING=MAP_SHARED>
MMAPED_TYPE& mmap_read(const char* path) {
size_t unused;
return *(MMAPED_TYPE*) mmap_read_ptr<SHARING>(path, unused);
};
// read c-array of REC_T[] type
template<typename REC_T, int SHARING=MAP_SHARED>
REC_T* mmap_read(const char* path, size_t& n) {
size_t n_bytes;
auto res = (REC_T*) mmap_read_ptr<SHARING>(path, n_bytes);
n = n_bytes/sizeof(REC_T);
return res;
};
// write an oject of T type
template<typename T> void
mmap_write(const char* path, T &obj) {
size_t size=sizeof(T);
unlink(path);
int fd = open(path, O_CREAT | O_RDWR, S_IRWXU);
if (fd < 0) {
cerr << "mmap_write error: couldn't open \"" << path << "\" file\n";
//exit(2);
throw open_error();
}
if (ftruncate(fd, size) < 0) {
cerr << "mmap_write error: couldn't allocate space for \"" << path << "\" file\n";
close(fd);
//exit(4);
throw no_free_space();
}
void *p = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
if ( p == MAP_FAILED ) {
cerr << "mmap_write error: couldn't mmap \"" << path << "\" file\n";
close(fd);
//exit(6);
throw io_error();
}
/* MAP_FIXED
Don’t interpret addr as a hint: place the mapping at exactly that address. addr must be a multiple of the page size. If the
memory region specified by addr and len overlaps pages of any existing mapping(s), then the overlapped part of the existing map‐
ping(s) will be discarded. If the specified address cannot be used, mmap() will fail. Because requiring a fixed address for a
mapping is less portable, the use of this option is discouraged. */
if (memcpy(p, &obj, size) < 0) {
cerr << "mmap_write error: couldn't memcpy() for \"" << path << "\" file\n";
close(fd);
//exit(8);
throw std::exception();
}
if (munmap(p, size) < 0) {
cerr << "mmap_write error: couldn't munmap() for \"" << path << "\" file\n";
close(fd);
//exit(10);
throw std::exception();
}
}
// wirte c-array of REC_t[n] type
template<typename REC_T>
void mmap_write(const char* path, REC_T* rec, size_t n) {
size_t size = n * sizeof(REC_T);
unlink(path);
int fd = open(path, O_CREAT | O_RDWR, S_IRWXU);
if (fd < 0) {
cerr << "mmap_write error: couldn't open \"" << path << "\" file\n";
throw open_error();
}
if (ftruncate(fd, size) < 0) {
cerr << "mmap_write error: couldn't allocate space for \"" << path << "\" file\n";
close(fd);
throw no_free_space();
}
void *p = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
if ( p == MAP_FAILED ) {
cerr << "mmap_write error: couldn't mmap \"" << path << "\" file\n";
close(fd);
//exit(6);
throw io_error();
}
if (memcpy(p, rec, size) < 0) {
cerr << "mmap_write error: couldn't memcpy() for \"" << path << "\" file\n";
close(fd);
//exit(8);
throw std::exception();
}
if (munmap(p, size) < 0) {
cerr << "mmap_write error: couldn't munmap() for \"" << path << "\" file\n";
close(fd);
//exit(10);
throw std::exception();
}
}
} // namespace lvv