1 //===- Win32/MappedFile.cpp - Win32 MappedFile Implementation ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Jeff Cohen and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 implementation of the MappedFile concept.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only Win32 code.
16 //===----------------------------------------------------------------------===//
19 #include "llvm/System/Process.h"
24 struct sys::MappedFileInfo {
30 void MappedFile::initialize() {
32 info_ = new MappedFileInfo;
33 info_->hFile = INVALID_HANDLE_VALUE;
34 info_->hMapping = NULL;
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) {
45 ThrowError(std::string("Can't open file: ") + path_.toString());
49 if (!GetFileSizeEx(info_->hFile, &size) ||
50 (info_->size = size_t(size.QuadPart), info_->size != size.QuadPart)) {
51 CloseHandle(info_->hFile);
54 ThrowError(std::string("Can't get size of file: ") + path_.toString());
58 void MappedFile::terminate() {
60 if (info_->hFile != INVALID_HANDLE_VALUE)
61 CloseHandle(info_->hFile);
66 void MappedFile::unmap() {
67 assert(info_ && "MappedFile not initialized");
69 UnmapViewOfFile(base_);
72 if (info_->hMapping != INVALID_HANDLE_VALUE) {
73 CloseHandle(info_->hMapping);
74 info_->hMapping = NULL;
78 void* MappedFile::map() {
80 DWORD prot = PAGE_READONLY;
81 if (options_ & EXEC_ACCESS)
83 else if (options_ & WRITE_ACCESS)
84 prot = PAGE_READWRITE;
85 info_->hMapping = CreateFileMapping(info_->hFile, NULL, prot, 0, 0, NULL);
86 if (info_->hMapping == NULL)
87 ThrowError(std::string("Can't map file: ") + path_.toString());
89 prot = (options_ & WRITE_ACCESS) ? FILE_MAP_WRITE : FILE_MAP_READ;
90 base_ = MapViewOfFileEx(info_->hMapping, prot, 0, 0, 0, NULL);
92 CloseHandle(info_->hMapping);
93 info_->hMapping = NULL;
94 ThrowError(std::string("Can't map file: ") + path_.toString());
100 size_t MappedFile::size() {
101 assert(info_ && "MappedFile not initialized");
105 void MappedFile::size(size_t new_size) {
106 assert(info_ && "MappedFile not initialized");
108 // Take the mapping out of memory.
111 // Adjust the new_size to a page boundary.
112 size_t pagesizem1 = Process::GetPageSize() - 1;
113 new_size = (new_size + pagesizem1) & ~pagesizem1;
115 // If the file needs to be extended, do so.
116 if (new_size > info_->size) {
118 eof.QuadPart = new_size;
119 if (!SetFilePointerEx(info_->hFile, eof, NULL, FILE_BEGIN))
120 ThrowError(std::string("Can't set end of file: ") + path_.toString());
121 if (!SetEndOfFile(info_->hFile))
122 ThrowError(std::string("Can't set end of file: ") + path_.toString());
123 info_->size = new_size;
132 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab