Chris Lattner | b342b7c | 2008-04-01 03:10:22 +0000 | [diff] [blame^] | 1 | //===- Unix/MappedFile.inc - Unix MappedFile Implementation -----*- C++ -*-===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 14 | #include "Unix.h" |
| 15 | #include "llvm/System/Process.h" |
| 16 | |
| 17 | #ifdef HAVE_FCNTL_H |
| 18 | #include <fcntl.h> |
| 19 | #endif |
| 20 | |
| 21 | #ifdef HAVE_SYS_MMAN_H |
| 22 | #include <sys/mman.h> |
| 23 | #endif |
| 24 | |
| 25 | #ifdef HAVE_SYS_STAT_H |
| 26 | #include <sys/stat.h> |
| 27 | #endif |
| 28 | |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | using namespace sys; |
| 31 | |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 32 | namespace llvm { |
| 33 | namespace sys { |
| 34 | struct MappedFileInfo { |
| 35 | int FD; |
| 36 | off_t Size; |
| 37 | }; |
| 38 | } |
| 39 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | |
| 41 | bool MappedFile::initialize(std::string* ErrMsg) { |
Chris Lattner | b342b7c | 2008-04-01 03:10:22 +0000 | [diff] [blame^] | 42 | int FD = ::open(Path.c_str(), O_RDONLY); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 43 | if (FD < 0) { |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 44 | MakeErrMsg(ErrMsg, "can't open file '" + Path.toString() + "'"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 45 | return true; |
| 46 | } |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 47 | const FileStatus *Status = Path.getFileStatus(false, ErrMsg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | if (!Status) { |
| 49 | ::close(FD); |
| 50 | return true; |
| 51 | } |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 52 | MapInfo = new MappedFileInfo(); |
| 53 | MapInfo->FD = FD; |
| 54 | MapInfo->Size = Status->getSize(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
| 58 | void MappedFile::terminate() { |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 59 | assert(MapInfo && "MappedFile not initialized"); |
| 60 | ::close(MapInfo->FD); |
| 61 | delete MapInfo; |
| 62 | MapInfo = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void MappedFile::unmap() { |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 66 | assert(MapInfo && "MappedFile not initialized"); |
| 67 | if (!isMapped()) return; |
| 68 | |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 69 | ::munmap(BasePtr, MapInfo->Size); |
| 70 | BasePtr = 0; // Mark this as non-mapped. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void* MappedFile::map(std::string* ErrMsg) { |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 74 | assert(MapInfo && "MappedFile not initialized"); |
| 75 | if (isMapped()) return BasePtr; |
| 76 | |
Chris Lattner | b342b7c | 2008-04-01 03:10:22 +0000 | [diff] [blame^] | 77 | int prot = PROT_READ; |
| 78 | int flags = MAP_PRIVATE; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 79 | #ifdef MAP_FILE |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 80 | flags |= MAP_FILE; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | #endif |
Chris Lattner | b342b7c | 2008-04-01 03:10:22 +0000 | [diff] [blame^] | 82 | size_t PageSize = Process::GetPageSize(); |
| 83 | size_t map_size = ((MapInfo->Size / PageSize)+1) * PageSize; |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 84 | |
| 85 | BasePtr = ::mmap(0, map_size, prot, flags, MapInfo->FD, 0); |
| 86 | if (BasePtr == MAP_FAILED) { |
| 87 | MakeErrMsg(ErrMsg, "Can't map file:" + Path.toString()); |
| 88 | return 0; |
| 89 | } |
| 90 | return BasePtr; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | size_t MappedFile::size() const { |
Chris Lattner | b8a0afe | 2008-04-01 00:53:25 +0000 | [diff] [blame] | 94 | assert(MapInfo && "MappedFile not initialized"); |
| 95 | return MapInfo->Size; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 96 | } |
| 97 | |