2004-10-04 11:08:32 +00:00
|
|
|
//===- Win32/MappedFile.cpp - Win32 MappedFile Implementation ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2004-10-14 03:05:59 +00:00
|
|
|
// This file was developed by Jeff Cohen and is distributed under the
|
2004-10-04 11:08:32 +00:00
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-10-14 03:05:59 +00:00
|
|
|
// This file provides the Win32 implementation of the MappedFile concept.
|
2004-10-04 11:08:32 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-10-14 03:05:59 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only Win32 code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-10-04 11:08:32 +00:00
|
|
|
#include "Win32.h"
|
2004-10-14 03:05:59 +00:00
|
|
|
#include "llvm/System/Process.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
|
|
|
struct sys::MappedFileInfo {
|
|
|
|
HANDLE hFile;
|
|
|
|
HANDLE hMapping;
|
|
|
|
size_t size;
|
|
|
|
};
|
2004-10-04 11:08:32 +00:00
|
|
|
|
2006-08-25 21:37:17 +00:00
|
|
|
bool MappedFile::initialize(std::string* ErrMsg) {
|
2004-10-14 03:05:59 +00:00
|
|
|
assert(!info_);
|
|
|
|
info_ = new MappedFileInfo;
|
|
|
|
info_->hFile = INVALID_HANDLE_VALUE;
|
|
|
|
info_->hMapping = NULL;
|
|
|
|
|
|
|
|
DWORD mode = options_ & WRITE_ACCESS ? GENERIC_WRITE : GENERIC_READ;
|
|
|
|
DWORD disposition = options_ & WRITE_ACCESS ? OPEN_ALWAYS : OPEN_EXISTING;
|
|
|
|
DWORD share = options_ & WRITE_ACCESS ? FILE_SHARE_WRITE : FILE_SHARE_READ;
|
|
|
|
share = options_ & SHARED_MAPPING ? share : 0;
|
|
|
|
info_->hFile = CreateFile(path_.c_str(), mode, share, NULL, disposition,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (info_->hFile == INVALID_HANDLE_VALUE) {
|
|
|
|
delete info_;
|
|
|
|
info_ = NULL;
|
2006-08-25 21:37:17 +00:00
|
|
|
return MakeErrMsg(ErrMsg,
|
|
|
|
std::string("Can't open file: ") + path_.toString());
|
2004-10-14 03:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LARGE_INTEGER size;
|
|
|
|
if (!GetFileSizeEx(info_->hFile, &size) ||
|
|
|
|
(info_->size = size_t(size.QuadPart), info_->size != size.QuadPart)) {
|
|
|
|
CloseHandle(info_->hFile);
|
|
|
|
delete info_;
|
|
|
|
info_ = NULL;
|
2006-08-25 21:37:17 +00:00
|
|
|
return MakeErrMsg(ErrMsg,
|
|
|
|
std::string("Can't get size of file: ") + path_.toString());
|
2004-10-14 03:05:59 +00:00
|
|
|
}
|
2006-09-01 20:35:17 +00:00
|
|
|
|
|
|
|
return false;
|
2004-10-04 11:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappedFile::terminate() {
|
2004-10-14 03:05:59 +00:00
|
|
|
unmap();
|
|
|
|
if (info_->hFile != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(info_->hFile);
|
|
|
|
delete info_;
|
|
|
|
info_ = NULL;
|
2004-10-04 11:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappedFile::unmap() {
|
2004-10-14 03:05:59 +00:00
|
|
|
assert(info_ && "MappedFile not initialized");
|
|
|
|
if (isMapped()) {
|
|
|
|
UnmapViewOfFile(base_);
|
|
|
|
base_ = NULL;
|
|
|
|
}
|
|
|
|
if (info_->hMapping != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(info_->hMapping);
|
|
|
|
info_->hMapping = NULL;
|
|
|
|
}
|
2004-10-04 11:08:32 +00:00
|
|
|
}
|
|
|
|
|
2006-08-25 21:37:17 +00:00
|
|
|
void* MappedFile::map(std::string* ErrMsg) {
|
2004-10-14 03:05:59 +00:00
|
|
|
if (!isMapped()) {
|
|
|
|
DWORD prot = PAGE_READONLY;
|
|
|
|
if (options_ & EXEC_ACCESS)
|
|
|
|
prot = SEC_IMAGE;
|
|
|
|
else if (options_ & WRITE_ACCESS)
|
|
|
|
prot = PAGE_READWRITE;
|
|
|
|
info_->hMapping = CreateFileMapping(info_->hFile, NULL, prot, 0, 0, NULL);
|
2006-08-25 21:37:17 +00:00
|
|
|
if (info_->hMapping == NULL) {
|
|
|
|
MakeErrMsg(ErrMsg, std::string("Can't map file: ") + path_.toString());
|
|
|
|
return 0;
|
|
|
|
}
|
2004-10-14 03:05:59 +00:00
|
|
|
|
|
|
|
prot = (options_ & WRITE_ACCESS) ? FILE_MAP_WRITE : FILE_MAP_READ;
|
|
|
|
base_ = MapViewOfFileEx(info_->hMapping, prot, 0, 0, 0, NULL);
|
|
|
|
if (base_ == NULL) {
|
|
|
|
CloseHandle(info_->hMapping);
|
|
|
|
info_->hMapping = NULL;
|
2006-08-25 21:37:17 +00:00
|
|
|
MakeErrMsg(ErrMsg, std::string("Can't map file: ") + path_.toString());
|
|
|
|
return 0;
|
2004-10-14 03:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return base_;
|
2004-10-04 11:08:32 +00:00
|
|
|
}
|
|
|
|
|
2004-12-13 06:26:35 +00:00
|
|
|
size_t MappedFile::size() const {
|
2004-10-14 03:05:59 +00:00
|
|
|
assert(info_ && "MappedFile not initialized");
|
|
|
|
return info_->size;
|
2004-10-04 11:08:32 +00:00
|
|
|
}
|
|
|
|
|
2006-08-25 21:37:17 +00:00
|
|
|
bool MappedFile::size(size_t new_size, std::string* ErrMsg) {
|
2004-10-14 03:05:59 +00:00
|
|
|
assert(info_ && "MappedFile not initialized");
|
|
|
|
|
|
|
|
// Take the mapping out of memory.
|
|
|
|
unmap();
|
|
|
|
|
|
|
|
// Adjust the new_size to a page boundary.
|
|
|
|
size_t pagesizem1 = Process::GetPageSize() - 1;
|
|
|
|
new_size = (new_size + pagesizem1) & ~pagesizem1;
|
|
|
|
|
|
|
|
// If the file needs to be extended, do so.
|
|
|
|
if (new_size > info_->size) {
|
|
|
|
LARGE_INTEGER eof;
|
|
|
|
eof.QuadPart = new_size;
|
|
|
|
if (!SetFilePointerEx(info_->hFile, eof, NULL, FILE_BEGIN))
|
2006-08-25 21:37:17 +00:00
|
|
|
return MakeErrMsg(ErrMsg,
|
|
|
|
std::string("Can't set end of file: ") + path_.toString());
|
2004-10-14 03:05:59 +00:00
|
|
|
if (!SetEndOfFile(info_->hFile))
|
2006-08-25 21:37:17 +00:00
|
|
|
return MakeErrMsg(ErrMsg,
|
|
|
|
std::string("Can't set end of file: ") + path_.toString());
|
2004-10-14 03:05:59 +00:00
|
|
|
info_->size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remap the file.
|
2006-08-25 21:37:17 +00:00
|
|
|
return map(ErrMsg);
|
2004-10-14 03:05:59 +00:00
|
|
|
}
|
|
|
|
|
2004-10-04 11:08:32 +00:00
|
|
|
}
|
|
|
|
|