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
|
//===- Win32/MappedFile.cpp - Win32 MappedFile Implementation ---*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file provides the Win32 implementation of the MappedFile concept.
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
//=== WARNING: Implementation here must contain only Win32 code.
//===----------------------------------------------------------------------===//
#include "Win32.h"
#include "llvm/System/Process.h"
namespace llvm {
using namespace sys;
struct sys::MappedFileInfo {
HANDLE hFile;
HANDLE hMapping;
size_t size;
};
bool MappedFile::initialize(std::string* ErrMsg) {
assert(!MapInfo);
MapInfo = new MappedFileInfo;
MapInfo->hFile = INVALID_HANDLE_VALUE;
MapInfo->hMapping = NULL;
MapInfo->hFile = CreateFile(Path.c_str(), GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (MapInfo->hFile == INVALID_HANDLE_VALUE) {
delete MapInfo;
MapInfo = NULL;
return MakeErrMsg(ErrMsg,
std::string("Can't open file: ") + Path.toString());
}
LARGE_INTEGER size;
if (!GetFileSizeEx(MapInfo->hFile, &size) ||
(MapInfo->size = size_t(size.QuadPart), MapInfo->size != size.QuadPart)) {
CloseHandle(MapInfo->hFile);
delete MapInfo;
MapInfo = NULL;
return MakeErrMsg(ErrMsg,
std::string("Can't get size of file: ") + Path.toString());
}
return false;
}
void MappedFile::terminate() {
unmap();
if (MapInfo->hFile != INVALID_HANDLE_VALUE)
CloseHandle(MapInfo->hFile);
delete MapInfo;
MapInfo = NULL;
}
void MappedFile::unmap() {
assert(MapInfo && "MappedFile not initialized");
if (isMapped()) {
UnmapViewOfFile(BasePtr);
BasePtr = NULL;
}
if (MapInfo->hMapping != INVALID_HANDLE_VALUE) {
CloseHandle(MapInfo->hMapping);
MapInfo->hMapping = NULL;
}
}
const void* MappedFile::map(std::string* ErrMsg) {
if (!isMapped()) {
MapInfo->hMapping = CreateFileMapping(MapInfo->hFile, NULL, PAGE_READONLY,
0, 0, NULL);
if (MapInfo->hMapping == NULL) {
MakeErrMsg(ErrMsg, std::string("Can't map file: ") + Path.toString());
return 0;
}
BasePtr = MapViewOfFileEx(MapInfo->hMapping, FILE_MAP_READ, 0, 0, 0, NULL);
if (BasePtr == NULL) {
CloseHandle(MapInfo->hMapping);
MapInfo->hMapping = NULL;
MakeErrMsg(ErrMsg, std::string("Can't map file: ") + Path.toString());
return 0;
}
}
return BasePtr;
}
size_t MappedFile::size() const {
assert(MapInfo && "MappedFile not initialized");
return MapInfo->size;
}
}
|