-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreservation.h
58 lines (53 loc) · 1.56 KB
/
reservation.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
#pragma once
#include <sys/mman.h>
#include <fcntl.h>
#include <system_error>
class reservation{
void* start;
size_t reserved_length;
static int open_dev_zero() {
int fd_zero = open("/dev/zero", O_RDONLY);
if (fd_zero == -1) {
throw std::system_error(errno, std::generic_category(), "open(/dev/zero) failed");
}
return fd_zero;
}
static int get_zero_fd(){
static int fd_zero = open_dev_zero();
return fd_zero;
}
public:
reservation():reserved_length(0){}
reservation(size_t length):reserved_length(length){
if(length){
start = mmap(NULL, length, PROT_READ, MAP_SHARED, get_zero_fd(), 0);
if (start == MAP_FAILED) {
throw std::system_error(errno, std::generic_category(), "mmap failed");
}
}
}
reservation &operator=(reservation &&res){
if (reserved_length) {
if(munmap(start, reserved_length)!=0){
throw std::system_error(errno, std::generic_category(), "munmap failed");
}
}
start = res.start;
reserved_length = res.reserved_length;
res.reserved_length = 0;
return *this;
}
void* getStart(){
return start;
}
size_t getReservedLength(){
return reserved_length;
}
~reservation() noexcept(false){
if (reserved_length) {
if(munmap(start, reserved_length)!=0){
throw std::system_error(errno, std::generic_category(), "munmap failed");
}
}
}
};