-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmmap_t.hpp
More file actions
37 lines (31 loc) · 727 Bytes
/
mmap_t.hpp
File metadata and controls
37 lines (31 loc) · 727 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
#ifndef MMAP_T_HPP
#define MMAP_T_HPP
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string>
struct mmap_t{
mmap_t(const std::string path, bool write_mode=false, int flags=MAP_FILE|MAP_PRIVATE){
int OPEN_MODE=O_RDONLY;
int PROT = PROT_READ;
if(write_mode) {
OPEN_MODE=O_RDWR;
PROT |= PROT_WRITE;
}
int f = open(path.c_str(), OPEN_MODE);
struct stat statbuf;
fstat(f, &statbuf);
ptr = mmap(0, statbuf.st_size, PROT, flags, f, 0);
size=statbuf.st_size;
close(f);
}
~mmap_t() {
munmap(ptr, size);
}
operator bool () const
{ return reinterpret_cast<int>(ptr)!=-1; }
size_t size;
void *ptr;
};
#endif