2004-10-04 13:08:32 +02:00
|
|
|
//===- Unix/MappedFile.cpp - Unix MappedFile Implementation -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Reid Spencer and is distributed under the
|
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides the generic Unix implementation of the MappedFile concept.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only generic UNIX code that
|
|
|
|
//=== is guaranteed to work on *all* UNIX variants.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Unix.h"
|
2004-12-27 07:16:52 +01:00
|
|
|
#include "llvm/System/Process.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL_H
|
2004-10-05 02:51:26 +02:00
|
|
|
#include <fcntl.h>
|
2004-12-27 07:16:52 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
2004-10-04 13:08:32 +02:00
|
|
|
#include <sys/mman.h>
|
2004-12-27 07:16:52 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2004-10-04 13:08:32 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
|
|
|
struct sys::MappedFileInfo {
|
2006-07-18 09:01:08 +02:00
|
|
|
int FD;
|
|
|
|
off_t Size;
|
2004-10-04 13:08:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void MappedFile::initialize() {
|
2006-07-18 08:52:52 +02:00
|
|
|
int mode = 0;
|
2006-07-18 09:03:14 +02:00
|
|
|
if (options_ & READ_ACCESS)
|
|
|
|
if (options_ & WRITE_ACCESS)
|
2006-07-18 08:52:52 +02:00
|
|
|
mode = O_RDWR;
|
|
|
|
else
|
|
|
|
mode = O_RDONLY;
|
2006-07-18 09:03:14 +02:00
|
|
|
else if (options_ & WRITE_ACCESS)
|
2006-07-18 08:52:52 +02:00
|
|
|
mode = O_WRONLY;
|
|
|
|
|
2006-07-18 08:57:51 +02:00
|
|
|
int FD = ::open(path_.c_str(), mode);
|
|
|
|
if (FD < 0)
|
2006-07-18 08:52:52 +02:00
|
|
|
ThrowErrno(std::string("Can't open file: ") + path_.toString());
|
2006-07-18 08:57:51 +02:00
|
|
|
|
2006-07-18 08:52:52 +02:00
|
|
|
struct stat sbuf;
|
2006-07-18 08:57:51 +02:00
|
|
|
if(::fstat(FD, &sbuf) < 0) {
|
|
|
|
::close(FD);
|
2006-07-18 08:52:52 +02:00
|
|
|
ThrowErrno(std::string("Can't stat file: ") + path_.toString());
|
2004-10-04 13:08:32 +02:00
|
|
|
}
|
2006-07-18 08:57:51 +02:00
|
|
|
info_ = new MappedFileInfo;
|
2006-07-18 09:01:08 +02:00
|
|
|
info_->FD = FD;
|
|
|
|
info_->Size = sbuf.st_size;
|
2004-10-04 13:08:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappedFile::terminate() {
|
|
|
|
assert(info_ && "MappedFile not initialized");
|
2006-07-18 09:03:14 +02:00
|
|
|
::close(info_->FD);
|
2004-10-04 13:08:32 +02:00
|
|
|
delete info_;
|
|
|
|
info_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappedFile::unmap() {
|
|
|
|
assert(info_ && "MappedFile not initialized");
|
|
|
|
if (isMapped()) {
|
|
|
|
if (options_ & WRITE_ACCESS)
|
2006-07-18 09:01:08 +02:00
|
|
|
::msync(base_, info_->Size, MS_SYNC);
|
|
|
|
::munmap(base_, info_->Size);
|
2004-10-04 13:08:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* MappedFile::map() {
|
2004-11-14 23:07:50 +01:00
|
|
|
assert(info_ && "MappedFile not initialized");
|
2004-10-04 13:08:32 +02:00
|
|
|
if (!isMapped()) {
|
|
|
|
int prot = PROT_NONE;
|
2004-10-05 02:46:21 +02:00
|
|
|
int flags = 0;
|
|
|
|
#ifdef MAP_FILE
|
|
|
|
flags |= MAP_FILE;
|
|
|
|
#endif
|
2004-10-04 13:08:32 +02:00
|
|
|
if (options_ == 0) {
|
|
|
|
prot = PROT_READ;
|
|
|
|
flags = MAP_PRIVATE;
|
|
|
|
} else {
|
|
|
|
if (options_ & READ_ACCESS)
|
|
|
|
prot |= PROT_READ;
|
|
|
|
if (options_ & WRITE_ACCESS)
|
|
|
|
prot |= PROT_WRITE;
|
|
|
|
if (options_ & EXEC_ACCESS)
|
|
|
|
prot |= PROT_EXEC;
|
|
|
|
if (options_ & SHARED_MAPPING)
|
|
|
|
flags |= MAP_SHARED;
|
|
|
|
else
|
|
|
|
flags |= MAP_PRIVATE;
|
|
|
|
}
|
2006-07-18 09:01:08 +02:00
|
|
|
size_t map_size = ((info_->Size / Process::GetPageSize())+1) *
|
2004-10-04 13:08:32 +02:00
|
|
|
Process::GetPageSize();
|
|
|
|
|
2006-07-18 09:01:08 +02:00
|
|
|
base_ = ::mmap(0, map_size, prot, flags, info_->FD, 0);
|
2004-10-04 13:08:32 +02:00
|
|
|
if (base_ == MAP_FAILED)
|
2004-12-11 01:14:15 +01:00
|
|
|
ThrowErrno(std::string("Can't map file:") + path_.toString());
|
2004-10-04 13:08:32 +02:00
|
|
|
}
|
|
|
|
return base_;
|
|
|
|
}
|
|
|
|
|
2004-12-13 03:58:51 +01:00
|
|
|
size_t MappedFile::size() const {
|
2004-10-04 13:08:32 +02:00
|
|
|
assert(info_ && "MappedFile not initialized");
|
2006-07-18 09:01:08 +02:00
|
|
|
return info_->Size;
|
2004-10-04 13:08:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappedFile::size(size_t new_size) {
|
|
|
|
assert(info_ && "MappedFile not initialized");
|
|
|
|
|
|
|
|
// Take the mapping out of memory
|
|
|
|
this->unmap();
|
|
|
|
|
|
|
|
// Adjust the current size to a page boundary
|
2006-07-18 09:01:08 +02:00
|
|
|
size_t cur_size = ((info_->Size / Process::GetPageSize())+1) *
|
2004-10-04 13:08:32 +02:00
|
|
|
Process::GetPageSize();
|
|
|
|
|
|
|
|
// Adjust the new_size to a page boundary
|
|
|
|
new_size = ((new_size / Process::GetPageSize())+1) *
|
|
|
|
Process::GetPageSize();
|
|
|
|
|
|
|
|
// If the file needs to be extended
|
|
|
|
if (new_size > cur_size) {
|
|
|
|
// Ensure we can allocate at least the idodes necessary to handle the
|
|
|
|
// file size requested.
|
2006-07-18 09:01:08 +02:00
|
|
|
::lseek(info_->FD, new_size, SEEK_SET);
|
|
|
|
::write(info_->FD, "\0", 1);
|
2004-10-04 13:08:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to current end of file.
|
|
|
|
this->map();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|