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 | // |
| 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. |
| 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 | |
| 42 | void MappedFile::initialize() { |
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); |
| 53 | if (FD < 0) |
Chris Lattner | c22c7d3 | 2006-07-18 06:52:52 +0000 | [diff] [blame] | 54 | ThrowErrno(std::string("Can't open file: ") + path_.toString()); |
Chris Lattner | cca68fa | 2006-07-18 06:57:51 +0000 | [diff] [blame] | 55 | |
Chris Lattner | c22c7d3 | 2006-07-18 06:52:52 +0000 | [diff] [blame] | 56 | struct stat sbuf; |
Chris Lattner | cca68fa | 2006-07-18 06:57:51 +0000 | [diff] [blame] | 57 | if(::fstat(FD, &sbuf) < 0) { |
| 58 | ::close(FD); |
Chris Lattner | c22c7d3 | 2006-07-18 06:52:52 +0000 | [diff] [blame] | 59 | ThrowErrno(std::string("Can't stat file: ") + path_.toString()); |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 60 | } |
Chris Lattner | cca68fa | 2006-07-18 06:57:51 +0000 | [diff] [blame] | 61 | info_ = new MappedFileInfo; |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 62 | info_->FD = FD; |
| 63 | info_->Size = sbuf.st_size; |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void MappedFile::terminate() { |
| 67 | assert(info_ && "MappedFile not initialized"); |
Chris Lattner | 84bad2f | 2006-07-18 07:03:14 +0000 | [diff] [blame] | 68 | ::close(info_->FD); |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 69 | delete info_; |
| 70 | info_ = 0; |
| 71 | } |
| 72 | |
| 73 | void MappedFile::unmap() { |
| 74 | assert(info_ && "MappedFile not initialized"); |
| 75 | if (isMapped()) { |
| 76 | if (options_ & WRITE_ACCESS) |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 77 | ::msync(base_, info_->Size, MS_SYNC); |
| 78 | ::munmap(base_, info_->Size); |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | void* MappedFile::map() { |
Reid Spencer | 083507c | 2004-11-14 22:07:50 +0000 | [diff] [blame] | 83 | assert(info_ && "MappedFile not initialized"); |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 84 | if (!isMapped()) { |
| 85 | int prot = PROT_NONE; |
Chris Lattner | 341e1da | 2004-10-05 00:46:21 +0000 | [diff] [blame] | 86 | int flags = 0; |
| 87 | #ifdef MAP_FILE |
| 88 | flags |= MAP_FILE; |
| 89 | #endif |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 90 | if (options_ == 0) { |
| 91 | prot = PROT_READ; |
| 92 | flags = MAP_PRIVATE; |
| 93 | } else { |
| 94 | if (options_ & READ_ACCESS) |
| 95 | prot |= PROT_READ; |
| 96 | if (options_ & WRITE_ACCESS) |
| 97 | prot |= PROT_WRITE; |
| 98 | if (options_ & EXEC_ACCESS) |
| 99 | prot |= PROT_EXEC; |
| 100 | if (options_ & SHARED_MAPPING) |
| 101 | flags |= MAP_SHARED; |
| 102 | else |
| 103 | flags |= MAP_PRIVATE; |
| 104 | } |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 105 | size_t map_size = ((info_->Size / Process::GetPageSize())+1) * |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 106 | Process::GetPageSize(); |
| 107 | |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 108 | base_ = ::mmap(0, map_size, prot, flags, info_->FD, 0); |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 109 | if (base_ == MAP_FAILED) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 110 | ThrowErrno(std::string("Can't map file:") + path_.toString()); |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 111 | } |
| 112 | return base_; |
| 113 | } |
| 114 | |
Reid Spencer | 56c3ed8 | 2004-12-13 02:58:51 +0000 | [diff] [blame] | 115 | size_t MappedFile::size() const { |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 116 | assert(info_ && "MappedFile not initialized"); |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 117 | return info_->Size; |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void MappedFile::size(size_t new_size) { |
| 121 | assert(info_ && "MappedFile not initialized"); |
| 122 | |
| 123 | // Take the mapping out of memory |
| 124 | this->unmap(); |
| 125 | |
| 126 | // Adjust the current size to a page boundary |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 127 | size_t cur_size = ((info_->Size / Process::GetPageSize())+1) * |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 128 | Process::GetPageSize(); |
| 129 | |
| 130 | // Adjust the new_size to a page boundary |
| 131 | new_size = ((new_size / Process::GetPageSize())+1) * |
| 132 | Process::GetPageSize(); |
| 133 | |
| 134 | // If the file needs to be extended |
| 135 | if (new_size > cur_size) { |
| 136 | // Ensure we can allocate at least the idodes necessary to handle the |
| 137 | // file size requested. |
Chris Lattner | 0af7093 | 2006-07-18 07:01:08 +0000 | [diff] [blame] | 138 | ::lseek(info_->FD, new_size, SEEK_SET); |
| 139 | ::write(info_->FD, "\0", 1); |
Reid Spencer | 9737492 | 2004-10-04 11:08:32 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Seek to current end of file. |
| 143 | this->map(); |
| 144 | } |
| 145 | |
| 146 | } |
| 147 | |