blob: d5bedfcfed85398b5cb8244eed48c44f150bb045 [file] [log] [blame]
Reid Spencer97374922004-10-04 11:08:32 +00001//===- Win32/MappedFile.cpp - Win32 MappedFile Implementation ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer8545fc12004-10-14 03:05:59 +00005// This file was developed by Jeff Cohen and is distributed under the
Reid Spencer97374922004-10-04 11:08:32 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer8545fc12004-10-14 03:05:59 +000010// This file provides the Win32 implementation of the MappedFile concept.
Reid Spencer97374922004-10-04 11:08:32 +000011//
12//===----------------------------------------------------------------------===//
13
Reid Spencer8545fc12004-10-14 03:05:59 +000014//===----------------------------------------------------------------------===//
15//=== WARNING: Implementation here must contain only Win32 code.
16//===----------------------------------------------------------------------===//
17
Reid Spencer97374922004-10-04 11:08:32 +000018#include "Win32.h"
Reid Spencer8545fc12004-10-14 03:05:59 +000019#include "llvm/System/Process.h"
20
21namespace llvm {
22using namespace sys;
23
24struct sys::MappedFileInfo {
25 HANDLE hFile;
26 HANDLE hMapping;
27 size_t size;
28};
Reid Spencer97374922004-10-04 11:08:32 +000029
Reid Spencer05545752006-08-25 21:37:17 +000030bool MappedFile::initialize(std::string* ErrMsg) {
Reid Spencer8545fc12004-10-14 03:05:59 +000031 assert(!info_);
32 info_ = new MappedFileInfo;
33 info_->hFile = INVALID_HANDLE_VALUE;
34 info_->hMapping = NULL;
35
36 DWORD mode = options_ & WRITE_ACCESS ? GENERIC_WRITE : GENERIC_READ;
37 DWORD disposition = options_ & WRITE_ACCESS ? OPEN_ALWAYS : OPEN_EXISTING;
38 DWORD share = options_ & WRITE_ACCESS ? FILE_SHARE_WRITE : FILE_SHARE_READ;
39 share = options_ & SHARED_MAPPING ? share : 0;
40 info_->hFile = CreateFile(path_.c_str(), mode, share, NULL, disposition,
41 FILE_ATTRIBUTE_NORMAL, NULL);
42 if (info_->hFile == INVALID_HANDLE_VALUE) {
43 delete info_;
44 info_ = NULL;
Reid Spencer05545752006-08-25 21:37:17 +000045 return MakeErrMsg(ErrMsg,
46 std::string("Can't open file: ") + path_.toString());
Reid Spencer8545fc12004-10-14 03:05:59 +000047 }
48
49 LARGE_INTEGER size;
50 if (!GetFileSizeEx(info_->hFile, &size) ||
51 (info_->size = size_t(size.QuadPart), info_->size != size.QuadPart)) {
52 CloseHandle(info_->hFile);
53 delete info_;
54 info_ = NULL;
Reid Spencer05545752006-08-25 21:37:17 +000055 return MakeErrMsg(ErrMsg,
56 std::string("Can't get size of file: ") + path_.toString());
Reid Spencer8545fc12004-10-14 03:05:59 +000057 }
Reid Spencer97374922004-10-04 11:08:32 +000058}
59
60void MappedFile::terminate() {
Reid Spencer8545fc12004-10-14 03:05:59 +000061 unmap();
62 if (info_->hFile != INVALID_HANDLE_VALUE)
63 CloseHandle(info_->hFile);
64 delete info_;
65 info_ = NULL;
Reid Spencer97374922004-10-04 11:08:32 +000066}
67
68void MappedFile::unmap() {
Reid Spencer8545fc12004-10-14 03:05:59 +000069 assert(info_ && "MappedFile not initialized");
70 if (isMapped()) {
71 UnmapViewOfFile(base_);
72 base_ = NULL;
73 }
74 if (info_->hMapping != INVALID_HANDLE_VALUE) {
75 CloseHandle(info_->hMapping);
76 info_->hMapping = NULL;
77 }
Reid Spencer97374922004-10-04 11:08:32 +000078}
79
Reid Spencer05545752006-08-25 21:37:17 +000080void* MappedFile::map(std::string* ErrMsg) {
Reid Spencer8545fc12004-10-14 03:05:59 +000081 if (!isMapped()) {
82 DWORD prot = PAGE_READONLY;
83 if (options_ & EXEC_ACCESS)
84 prot = SEC_IMAGE;
85 else if (options_ & WRITE_ACCESS)
86 prot = PAGE_READWRITE;
87 info_->hMapping = CreateFileMapping(info_->hFile, NULL, prot, 0, 0, NULL);
Reid Spencer05545752006-08-25 21:37:17 +000088 if (info_->hMapping == NULL) {
89 MakeErrMsg(ErrMsg, std::string("Can't map file: ") + path_.toString());
90 return 0;
91 }
Reid Spencer8545fc12004-10-14 03:05:59 +000092
93 prot = (options_ & WRITE_ACCESS) ? FILE_MAP_WRITE : FILE_MAP_READ;
94 base_ = MapViewOfFileEx(info_->hMapping, prot, 0, 0, 0, NULL);
95 if (base_ == NULL) {
96 CloseHandle(info_->hMapping);
97 info_->hMapping = NULL;
Reid Spencer05545752006-08-25 21:37:17 +000098 MakeErrMsg(ErrMsg, std::string("Can't map file: ") + path_.toString());
99 return 0;
Reid Spencer8545fc12004-10-14 03:05:59 +0000100 }
101 }
102 return base_;
Reid Spencer97374922004-10-04 11:08:32 +0000103}
104
Jeff Cohen32653692004-12-13 06:26:35 +0000105size_t MappedFile::size() const {
Reid Spencer8545fc12004-10-14 03:05:59 +0000106 assert(info_ && "MappedFile not initialized");
107 return info_->size;
Reid Spencer97374922004-10-04 11:08:32 +0000108}
109
Reid Spencer05545752006-08-25 21:37:17 +0000110bool MappedFile::size(size_t new_size, std::string* ErrMsg) {
Reid Spencer8545fc12004-10-14 03:05:59 +0000111 assert(info_ && "MappedFile not initialized");
112
113 // Take the mapping out of memory.
114 unmap();
115
116 // Adjust the new_size to a page boundary.
117 size_t pagesizem1 = Process::GetPageSize() - 1;
118 new_size = (new_size + pagesizem1) & ~pagesizem1;
119
120 // If the file needs to be extended, do so.
121 if (new_size > info_->size) {
122 LARGE_INTEGER eof;
123 eof.QuadPart = new_size;
124 if (!SetFilePointerEx(info_->hFile, eof, NULL, FILE_BEGIN))
Reid Spencer05545752006-08-25 21:37:17 +0000125 return MakeErrMsg(ErrMsg,
126 std::string("Can't set end of file: ") + path_.toString());
Reid Spencer8545fc12004-10-14 03:05:59 +0000127 if (!SetEndOfFile(info_->hFile))
Reid Spencer05545752006-08-25 21:37:17 +0000128 return MakeErrMsg(ErrMsg,
129 std::string("Can't set end of file: ") + path_.toString());
Reid Spencer8545fc12004-10-14 03:05:59 +0000130 info_->size = new_size;
131 }
132
133 // Remap the file.
Reid Spencer05545752006-08-25 21:37:17 +0000134 return map(ErrMsg);
Reid Spencer8545fc12004-10-14 03:05:59 +0000135}
136
Reid Spencer97374922004-10-04 11:08:32 +0000137}
138