Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- Unix/MappedFile.cpp - Unix MappedFile Implementation -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +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 | |
| 19 | #include "Unix.h" |
| 20 | #include "llvm/System/Process.h" |
| 21 | |
| 22 | #ifdef HAVE_FCNTL_H |
| 23 | #include <fcntl.h> |
| 24 | #endif |
| 25 | |
| 26 | #ifdef HAVE_SYS_MMAN_H |
| 27 | #include <sys/mman.h> |
| 28 | #endif |
| 29 | |
| 30 | #ifdef HAVE_SYS_STAT_H |
| 31 | #include <sys/stat.h> |
| 32 | #endif |
| 33 | |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 34 | using namespace llvm; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 35 | using namespace sys; |
| 36 | |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 37 | namespace llvm { |
| 38 | namespace sys { |
| 39 | struct MappedFileInfo { |
| 40 | int FD; |
| 41 | off_t Size; |
| 42 | }; |
| 43 | } |
| 44 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 45 | |
| 46 | bool MappedFile::initialize(std::string* ErrMsg) { |
| 47 | int mode = 0; |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 48 | if (Options & READ_ACCESS) |
| 49 | if (Options & WRITE_ACCESS) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 50 | mode = O_RDWR; |
| 51 | else |
| 52 | mode = O_RDONLY; |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 53 | else if (Options & WRITE_ACCESS) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | mode = O_WRONLY; |
| 55 | |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 56 | int FD = ::open(Path.c_str(), mode); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 57 | if (FD < 0) { |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 58 | MakeErrMsg(ErrMsg, "can't open file '" + Path.toString() + "'"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 59 | return true; |
| 60 | } |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 61 | const FileStatus *Status = Path.getFileStatus(false, ErrMsg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 62 | if (!Status) { |
| 63 | ::close(FD); |
| 64 | return true; |
| 65 | } |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 66 | MapInfo = new MappedFileInfo(); |
| 67 | MapInfo->FD = FD; |
| 68 | MapInfo->Size = Status->getSize(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | |
| 72 | void MappedFile::terminate() { |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 73 | assert(MapInfo && "MappedFile not initialized"); |
| 74 | ::close(MapInfo->FD); |
| 75 | delete MapInfo; |
| 76 | MapInfo = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void MappedFile::unmap() { |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 80 | assert(MapInfo && "MappedFile not initialized"); |
| 81 | if (!isMapped()) return; |
| 82 | |
| 83 | if (Options & WRITE_ACCESS) |
| 84 | ::msync(BasePtr, MapInfo->Size, MS_SYNC); |
| 85 | ::munmap(BasePtr, MapInfo->Size); |
| 86 | BasePtr = 0; // Mark this as non-mapped. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void* MappedFile::map(std::string* ErrMsg) { |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 90 | assert(MapInfo && "MappedFile not initialized"); |
| 91 | if (isMapped()) return BasePtr; |
| 92 | |
| 93 | int prot = PROT_NONE; |
| 94 | int flags = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 95 | #ifdef MAP_FILE |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 96 | flags |= MAP_FILE; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 97 | #endif |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 98 | if (Options == 0) { |
| 99 | prot = PROT_READ; |
| 100 | flags = MAP_PRIVATE; |
| 101 | } else { |
| 102 | if (Options & READ_ACCESS) |
| 103 | prot |= PROT_READ; |
| 104 | if (Options & WRITE_ACCESS) |
| 105 | prot |= PROT_WRITE; |
| 106 | if (Options & EXEC_ACCESS) |
| 107 | prot |= PROT_EXEC; |
| 108 | if (Options & SHARED_MAPPING) |
| 109 | flags |= MAP_SHARED; |
| 110 | else |
| 111 | flags |= MAP_PRIVATE; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 112 | } |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 113 | size_t map_size = ((MapInfo->Size / Process::GetPageSize())+1) * |
| 114 | Process::GetPageSize(); |
| 115 | |
| 116 | BasePtr = ::mmap(0, map_size, prot, flags, MapInfo->FD, 0); |
| 117 | if (BasePtr == MAP_FAILED) { |
| 118 | MakeErrMsg(ErrMsg, "Can't map file:" + Path.toString()); |
| 119 | return 0; |
| 120 | } |
| 121 | return BasePtr; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | size_t MappedFile::size() const { |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 125 | assert(MapInfo && "MappedFile not initialized"); |
| 126 | return MapInfo->Size; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 129 | bool MappedFile::resize(size_t new_size, std::string* ErrMsg) { |
| 130 | assert(MapInfo && "MappedFile not initialized"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 131 | |
| 132 | // Take the mapping out of memory |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 133 | unmap(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 134 | |
| 135 | // Adjust the current size to a page boundary |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 136 | size_t cur_size = ((MapInfo->Size / Process::GetPageSize())+1) * |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 137 | Process::GetPageSize(); |
| 138 | |
| 139 | // Adjust the new_size to a page boundary |
| 140 | new_size = ((new_size / Process::GetPageSize())+1) * |
| 141 | Process::GetPageSize(); |
| 142 | |
| 143 | // If the file needs to be extended |
| 144 | if (new_size > cur_size) { |
| 145 | // Ensure we can allocate at least the idodes necessary to handle the |
| 146 | // file size requested. |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 147 | if ((off_t)-1 == ::lseek(MapInfo->FD, new_size, SEEK_SET)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 148 | return MakeErrMsg(ErrMsg, "Can't lseek: "); |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 149 | if (-1 == ::write(MapInfo->FD, "\0", 1)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 150 | return MakeErrMsg(ErrMsg, "Can't write: "); |
| 151 | } |
| 152 | |
| 153 | // Put the mapping back into memory. |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 154 | return map(ErrMsg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 155 | } |