-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeliner_util.cpp
77 lines (70 loc) · 2.21 KB
/
timeliner_util.cpp
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
#include "timeliner_diagnostics.h"
#include "timeliner_util.h"
#include <cassert>
#include <fcntl.h>
#include <unistd.h>
#ifndef _MSC_VER
#include <sys/mman.h>
#include <sys/stat.h>
#endif
#ifdef _MSC_VER
Mmap::Mmap(const std::string& szFilename, const bool fOptional) : _pch(NULL) {
// Disable unicode: config properties, general, project defaults, character set, "not set".
// h = CreateFile( std::wstring(szFilename.begin(), szFilename.end()).c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
h = CreateFileA( (LPCSTR)szFilename.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
if (!fOptional)
warn("mmap: problem reading file " + szFilename);
return;
}
h2 = CreateFileMapping(h, NULL, PAGE_READONLY, 0, 0, NULL);
if (h2 == NULL) {
warn("mmap: problem #2 reading file " + szFilename);
return;
}
_pch = (char*)MapViewOfFile(h2, FILE_MAP_READ, 0, 0, 0);
if (_pch == NULL) {
warn("mmap: problem #3 reading file " + szFilename); // To see why, call GetLastError.
return;
}
_cch.QuadPart = 0;
if (GetFileSizeEx(h, &_cch) == 0)
warn("mmap: problem measuring file " + szFilename);
}
Mmap::~Mmap() {
if (h != INVALID_HANDLE_VALUE) {
UnmapViewOfFile(_pch);
if (h2 != NULL)
CloseHandle(h2);
CloseHandle(h);
}
}
#else
Mmap::Mmap(const std::string& szFilename, bool fOptional) : _pch(NULL), _cch(0), _fd(-1) {
_fd = open(szFilename.c_str(), O_RDONLY);
if (_fd < 0) {
if (!fOptional)
warn("mmap: problem reading file " + szFilename);
return;
}
struct stat s;
const int err = fstat(_fd, &s); // EOVERFLOW possible if compiled on 32-bit
if (err != 0) {
warn("mmap: fstat failed");
return;
}
assert(sizeof(_cch) >= 8 && sizeof(s.st_size) >= 8); // Otherwise files over 2GB will fail.
_cch = s.st_size; // possibly 0, for an empty file
_pch = (char*)mmap(NULL, _cch, PROT_READ, MAP_SHARED, _fd, 0);
if (_pch == MAP_FAILED) {
_pch = NULL;
warn("mmap failed");
}
}
Mmap::~Mmap() {
if (_pch && munmap(_pch, _cch) == -1)
warn("munmap failed");
if (_fd >= 0)
close(_fd);
}
#endif