Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 1 | //===- Win32/MappedFile.cpp - Win32 MappedFile Implementation ---*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 10 | // This file provides the Win32 implementation of the MappedFile concept. |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only Win32 code. |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 18 | #include "Win32.h" |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 19 | #include "llvm/System/Process.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | using namespace sys; |
| 23 | |
| 24 | struct sys::MappedFileInfo { |
| 25 | HANDLE hFile; |
| 26 | HANDLE hMapping; |
| 27 | size_t size; |
| 28 | }; |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 29 | |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 30 | bool MappedFile::initialize(std::string* ErrMsg) { |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 31 | 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 Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 45 | return MakeErrMsg(ErrMsg, |
| 46 | std::string("Can't open file: ") + path_.toString()); |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 47 | } |
| 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 Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 55 | return MakeErrMsg(ErrMsg, |
| 56 | std::string("Can't get size of file: ") + path_.toString()); |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 57 | } |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 58 | |
| 59 | return false; |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void MappedFile::terminate() { |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 63 | unmap(); |
| 64 | if (info_->hFile != INVALID_HANDLE_VALUE) |
| 65 | CloseHandle(info_->hFile); |
| 66 | delete info_; |
| 67 | info_ = NULL; |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void MappedFile::unmap() { |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 71 | assert(info_ && "MappedFile not initialized"); |
| 72 | if (isMapped()) { |
| 73 | UnmapViewOfFile(base_); |
| 74 | base_ = NULL; |
| 75 | } |
| 76 | if (info_->hMapping != INVALID_HANDLE_VALUE) { |
| 77 | CloseHandle(info_->hMapping); |
| 78 | info_->hMapping = NULL; |
| 79 | } |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 82 | void* MappedFile::map(std::string* ErrMsg) { |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 83 | if (!isMapped()) { |
| 84 | DWORD prot = PAGE_READONLY; |
| 85 | if (options_ & EXEC_ACCESS) |
| 86 | prot = SEC_IMAGE; |
| 87 | else if (options_ & WRITE_ACCESS) |
| 88 | prot = PAGE_READWRITE; |
| 89 | info_->hMapping = CreateFileMapping(info_->hFile, NULL, prot, 0, 0, NULL); |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 90 | if (info_->hMapping == NULL) { |
| 91 | MakeErrMsg(ErrMsg, std::string("Can't map file: ") + path_.toString()); |
| 92 | return 0; |
| 93 | } |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 94 | |
| 95 | prot = (options_ & WRITE_ACCESS) ? FILE_MAP_WRITE : FILE_MAP_READ; |
| 96 | base_ = MapViewOfFileEx(info_->hMapping, prot, 0, 0, 0, NULL); |
| 97 | if (base_ == NULL) { |
| 98 | CloseHandle(info_->hMapping); |
| 99 | info_->hMapping = NULL; |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 100 | MakeErrMsg(ErrMsg, std::string("Can't map file: ") + path_.toString()); |
| 101 | return 0; |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | return base_; |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Jeff Cohen | 3265369 | 2004-12-13 06:26:35 +0000 | [diff] [blame] | 107 | size_t MappedFile::size() const { |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 108 | assert(info_ && "MappedFile not initialized"); |
| 109 | return info_->size; |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 112 | bool MappedFile::size(size_t new_size, std::string* ErrMsg) { |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 113 | assert(info_ && "MappedFile not initialized"); |
| 114 | |
| 115 | // Take the mapping out of memory. |
| 116 | unmap(); |
| 117 | |
| 118 | // Adjust the new_size to a page boundary. |
| 119 | size_t pagesizem1 = Process::GetPageSize() - 1; |
| 120 | new_size = (new_size + pagesizem1) & ~pagesizem1; |
| 121 | |
| 122 | // If the file needs to be extended, do so. |
| 123 | if (new_size > info_->size) { |
| 124 | LARGE_INTEGER eof; |
| 125 | eof.QuadPart = new_size; |
| 126 | if (!SetFilePointerEx(info_->hFile, eof, NULL, FILE_BEGIN)) |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 127 | return MakeErrMsg(ErrMsg, |
| 128 | std::string("Can't set end of file: ") + path_.toString()); |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 129 | if (!SetEndOfFile(info_->hFile)) |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 130 | return MakeErrMsg(ErrMsg, |
| 131 | std::string("Can't set end of file: ") + path_.toString()); |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 132 | info_->size = new_size; |
| 133 | } |
| 134 | |
| 135 | // Remap the file. |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 136 | return map(ErrMsg); |
Reid Spencer | 8545fc1 | 2004-10-14 03:05:59 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 139 | } |
| 140 | |