-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilemapping_unix.cpp
More file actions
52 lines (45 loc) · 894 Bytes
/
filemapping_unix.cpp
File metadata and controls
52 lines (45 loc) · 894 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
#include "filemapping.h"
#include <fcntl.h>
#include <new>
#include <stdexcept>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
namespace symreader
{
shared_ptr<const mapped_region> map_file(const char *path)
{
struct mapping : mapped_region
{
mapping(const char *path)
: mapped_region(0, 0)
{
if ((file = ::open(path, O_RDONLY)) >= 0)
{
struct stat st;
if (::fstat(file, &st) >= 0)
{
second = static_cast<size_t>(st.st_size);
first = ::mmap(0, second, PROT_READ, MAP_PRIVATE, file, 0);
if (first != MAP_FAILED)
return;
}
close(file);
throw bad_alloc();
}
else
{
throw invalid_argument("");
}
}
~mapping()
{
::munmap((void *)first, second);
::close(file);
}
int file;
};
return shared_ptr<mapping>(new mapping(path));
}
}