1 //===- Unix/MappedFile.cpp - Unix MappedFile Implementation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the generic Unix implementation of the MappedFile concept.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //=== is guaranteed to work on *all* UNIX variants.
17 //===----------------------------------------------------------------------===//
20 #include "llvm/System/Process.h"
26 #ifdef HAVE_SYS_MMAN_H
30 #ifdef HAVE_SYS_STAT_H
37 struct sys::MappedFileInfo {
42 void MappedFile::initialize() {
44 info_ = new MappedFileInfo;
46 if (options_&READ_ACCESS)
47 if (options_&WRITE_ACCESS)
51 else if (options_&WRITE_ACCESS)
53 info_->fd_ = ::open(path_.c_str(),mode);
58 ThrowErrno(std::string("Can't open file: ") + path_.toString());
61 if(::fstat(info_->fd_, &info_->sbuf_) < 0) {
65 ThrowErrno(std::string("Can't stat file: ") + path_.toString());
68 throw std::string("Can't open file: ") + path_.toString();
72 void MappedFile::terminate() {
73 assert(info_ && "MappedFile not initialized");
80 void MappedFile::unmap() {
81 assert(info_ && "MappedFile not initialized");
83 if (options_ & WRITE_ACCESS)
84 ::msync(base_, info_->sbuf_.st_size, MS_SYNC);
85 ::munmap(base_, info_->sbuf_.st_size);
89 void* MappedFile::map() {
90 assert(info_ && "MappedFile not initialized");
101 if (options_ & READ_ACCESS)
103 if (options_ & WRITE_ACCESS)
105 if (options_ & EXEC_ACCESS)
107 if (options_ & SHARED_MAPPING)
110 flags |= MAP_PRIVATE;
112 size_t map_size = ((info_->sbuf_.st_size / Process::GetPageSize())+1) *
113 Process::GetPageSize();
115 base_ = ::mmap(0, map_size, prot, flags, info_->fd_, 0);
116 if (base_ == MAP_FAILED)
117 ThrowErrno(std::string("Can't map file:") + path_.toString());
122 size_t MappedFile::size() const {
123 assert(info_ && "MappedFile not initialized");
124 return info_->sbuf_.st_size;
127 void MappedFile::size(size_t new_size) {
128 assert(info_ && "MappedFile not initialized");
130 // Take the mapping out of memory
133 // Adjust the current size to a page boundary
134 size_t cur_size = ((info_->sbuf_.st_size / Process::GetPageSize())+1) *
135 Process::GetPageSize();
137 // Adjust the new_size to a page boundary
138 new_size = ((new_size / Process::GetPageSize())+1) *
139 Process::GetPageSize();
141 // If the file needs to be extended
142 if (new_size > cur_size) {
143 // Ensure we can allocate at least the idodes necessary to handle the
144 // file size requested.
145 ::lseek(info_->fd_, new_size, SEEK_SET);
146 ::write(info_->fd_, "\0", 1);
149 // Seek to current end of file.