Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 1 | //===- Unix/MappedFile.cpp - Unix 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 | // |
| 10 | // This file provides the generic Unix implementation of the MappedFile concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 16 | //=== is guaranteed to work on *all* UNIX variants. |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 19 | #include "Unix.h" |
Reid Spencer | cdf54d0 | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 20 | #include "llvm/System/Process.h" |
| 21 | |
| 22 | #ifdef HAVE_FCNTL_H |
Tanya Lattner | c5a0bbf | 2004-10-05 00:51:26 +0000 | [diff] [blame] | 23 | #include <fcntl.h> |
Reid Spencer | cdf54d0 | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | #ifdef HAVE_SYS_MMAN_H |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 27 | #include <sys/mman.h> |
Reid Spencer | cdf54d0 | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
| 30 | #ifdef HAVE_SYS_STAT_H |
| 31 | #include <sys/stat.h> |
| 32 | #endif |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 33 | |
| 34 | namespace llvm { |
| 35 | using namespace sys; |
| 36 | |
| 37 | struct sys::MappedFileInfo { |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 38 | int FD; |
| 39 | off_t Size; |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Reid Spencer | 6d045fc | 2006-08-22 16:04:22 +0000 | [diff] [blame] | 42 | bool MappedFile::initialize(std::string* ErrMsg) { |
Chris Lattner | c22c7d3 | 2006-07-18 06:52:52 +0000 | [diff] [blame] | 43 | int mode = 0; |
Chris Lattner | 84bad2f | 2006-07-18 07:03:14 +0000 | [diff] [blame] | 44 | if (options_ & READ_ACCESS) |
| 45 | if (options_ & WRITE_ACCESS) |
Chris Lattner | c22c7d3 | 2006-07-18 06:52:52 +0000 | [diff] [blame] | 46 | mode = O_RDWR; |
| 47 | else |
| 48 | mode = O_RDONLY; |
Chris Lattner | 84bad2f | 2006-07-18 07:03:14 +0000 | [diff] [blame] | 49 | else if (options_ & WRITE_ACCESS) |
Chris Lattner | c22c7d3 | 2006-07-18 06:52:52 +0000 | [diff] [blame] | 50 | mode = O_WRONLY; |
Chris Lattner | c22c7d3 | 2006-07-18 06:52:52 +0000 | [diff] [blame] | 51 | |
Chris Lattner | cca68fa | 2006-07-18 06:57:51 +0000 | [diff] [blame] | 52 | int FD = ::open(path_.c_str(), mode); |
Reid Spencer | 6d045fc | 2006-08-22 16:04:22 +0000 | [diff] [blame] | 53 | if (FD < 0) { |
| 54 | MakeErrMsg(ErrMsg, "can't open file '" + path_.toString() + "'"); |
| 55 | return true; |
| 56 | } |
Reid Spencer | 1ea733d | 2007-03-29 19:11:22 +0000 | [diff] [blame] | 57 | const FileStatus *Status = path_.getFileStatus(false, ErrMsg); |
| 58 | if (!Status) { |
Chris Lattner | cca68fa | 2006-07-18 06:57:51 +0000 | [diff] [blame] | 59 | ::close(FD); |
Reid Spencer | 6d045fc | 2006-08-22 16:04:22 +0000 | [diff] [blame] | 60 | return true; |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 61 | } |
Chris Lattner | cca68fa | 2006-07-18 06:57:51 +0000 | [diff] [blame] | 62 | info_ = new MappedFileInfo; |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 63 | info_->FD = FD; |
Reid Spencer | 1ea733d | 2007-03-29 19:11:22 +0000 | [diff] [blame] | 64 | info_->Size = Status->getSize(); |
Reid Spencer | 6d045fc | 2006-08-22 16:04:22 +0000 | [diff] [blame] | 65 | return false; |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void MappedFile::terminate() { |
| 69 | assert(info_ && "MappedFile not initialized"); |
Chris Lattner | 84bad2f | 2006-07-18 07:03:14 +0000 | [diff] [blame] | 70 | ::close(info_->FD); |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 71 | delete info_; |
| 72 | info_ = 0; |
| 73 | } |
| 74 | |
| 75 | void MappedFile::unmap() { |
| 76 | assert(info_ && "MappedFile not initialized"); |
| 77 | if (isMapped()) { |
| 78 | if (options_ & WRITE_ACCESS) |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 79 | ::msync(base_, info_->Size, MS_SYNC); |
| 80 | ::munmap(base_, info_->Size); |
Chris Lattner | 3925326 | 2007-05-11 00:00:27 +0000 | [diff] [blame] | 81 | base_ = 0; // Mark this as non-mapped. |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Reid Spencer | 6d045fc | 2006-08-22 16:04:22 +0000 | [diff] [blame] | 85 | void* MappedFile::map(std::string* ErrMsg) { |
Reid Spencer | 083507c | 2004-11-14 22:07:50 +0000 | [diff] [blame] | 86 | assert(info_ && "MappedFile not initialized"); |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 87 | if (!isMapped()) { |
| 88 | int prot = PROT_NONE; |
Chris Lattner | 341e1da | 2004-10-05 00:46:21 +0000 | [diff] [blame] | 89 | int flags = 0; |
| 90 | #ifdef MAP_FILE |
| 91 | flags |= MAP_FILE; |
| 92 | #endif |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 93 | if (options_ == 0) { |
| 94 | prot = PROT_READ; |
| 95 | flags = MAP_PRIVATE; |
| 96 | } else { |
| 97 | if (options_ & READ_ACCESS) |
| 98 | prot |= PROT_READ; |
| 99 | if (options_ & WRITE_ACCESS) |
| 100 | prot |= PROT_WRITE; |
| 101 | if (options_ & EXEC_ACCESS) |
| 102 | prot |= PROT_EXEC; |
| 103 | if (options_ & SHARED_MAPPING) |
| 104 | flags |= MAP_SHARED; |
| 105 | else |
| 106 | flags |= MAP_PRIVATE; |
| 107 | } |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 108 | size_t map_size = ((info_->Size / Process::GetPageSize())+1) * |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 109 | Process::GetPageSize(); |
| 110 | |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 111 | base_ = ::mmap(0, map_size, prot, flags, info_->FD, 0); |
Reid Spencer | 6d045fc | 2006-08-22 16:04:22 +0000 | [diff] [blame] | 112 | if (base_ == MAP_FAILED) { |
| 113 | MakeErrMsg(ErrMsg, "Can't map file:" + path_.toString()); |
| 114 | return 0; |
| 115 | } |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 116 | } |
| 117 | return base_; |
| 118 | } |
| 119 | |
Reid Spencer | 56c3ed8 | 2004-12-13 02:58:51 +0000 | [diff] [blame] | 120 | size_t MappedFile::size() const { |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 121 | assert(info_ && "MappedFile not initialized"); |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 122 | return info_->Size; |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 125 | bool MappedFile::size(size_t new_size, std::string* ErrMsg) { |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 126 | assert(info_ && "MappedFile not initialized"); |
| 127 | |
| 128 | // Take the mapping out of memory |
| 129 | this->unmap(); |
| 130 | |
| 131 | // Adjust the current size to a page boundary |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 132 | size_t cur_size = ((info_->Size / Process::GetPageSize())+1) * |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 133 | Process::GetPageSize(); |
| 134 | |
| 135 | // Adjust the new_size to a page boundary |
| 136 | new_size = ((new_size / Process::GetPageSize())+1) * |
| 137 | Process::GetPageSize(); |
| 138 | |
| 139 | // If the file needs to be extended |
| 140 | if (new_size > cur_size) { |
| 141 | // Ensure we can allocate at least the idodes necessary to handle the |
| 142 | // file size requested. |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 143 | if ((off_t)-1 == ::lseek(info_->FD, new_size, SEEK_SET)) |
| 144 | return MakeErrMsg(ErrMsg, "Can't lseek: "); |
| 145 | if (-1 == ::write(info_->FD, "\0", 1)) |
| 146 | return MakeErrMsg(ErrMsg, "Can't write: "); |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Reid Spencer | 6d045fc | 2006-08-22 16:04:22 +0000 | [diff] [blame] | 149 | // Put the mapping back into memory. |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 150 | return this->map(ErrMsg); |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | } |
| 154 | |