Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 1 | //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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. |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the MemoryBuffer interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/OwningPtr.h" |
| 16 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | e1effb0 | 2011-11-22 12:31:53 +0000 | [diff] [blame] | 17 | #include "llvm/Config/config.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Errno.h" |
Kaelyn Uhrain | 23fb5c3 | 2012-06-20 20:21:33 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Path.h" |
| 22 | #include "llvm/Support/Process.h" |
| 23 | #include "llvm/Support/Program.h" |
Michael J. Spencer | 7b6fef8 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/system_error.h" |
Jeff Cohen | 50b2d2c6 | 2007-04-29 14:21:44 +0000 | [diff] [blame] | 25 | #include <cassert> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include <cerrno> |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 27 | #include <cstdio> |
| 28 | #include <cstring> |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 29 | #include <new> |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 30 | #include <sys/stat.h> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 32 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 33 | #include <unistd.h> |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 34 | #else |
| 35 | #include <io.h> |
Dan Gohman | 3e17294 | 2013-02-19 22:38:58 +0000 | [diff] [blame] | 36 | // Simplistic definitinos of these macros to allow files to be read with |
| 37 | // MapInFilePages. |
| 38 | #ifndef S_ISREG |
| 39 | #define S_ISREG(x) (1) |
| 40 | #endif |
| 41 | #ifndef S_ISBLK |
| 42 | #define S_ISBLK(x) (0) |
Daniel Dunbar | e2d25c2 | 2012-11-06 17:08:09 +0000 | [diff] [blame] | 43 | #endif |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 44 | #endif |
Gabor Greif | 659e5c4 | 2008-04-30 08:53:22 +0000 | [diff] [blame] | 45 | #include <fcntl.h> |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 46 | using namespace llvm; |
| 47 | |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | // MemoryBuffer implementation itself. |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 52 | MemoryBuffer::~MemoryBuffer() { } |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 53 | |
| 54 | /// init - Initialize this MemoryBuffer as a reference to externally allocated |
| 55 | /// memory, memory that we know is already null terminated. |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 56 | void MemoryBuffer::init(const char *BufStart, const char *BufEnd, |
| 57 | bool RequiresNullTerminator) { |
Rafael Espindola | 7c9cc46 | 2011-03-18 02:55:51 +0000 | [diff] [blame] | 58 | assert((!RequiresNullTerminator || BufEnd[0] == 0) && |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 59 | "Buffer is not null terminated!"); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 60 | BufferStart = BufStart; |
| 61 | BufferEnd = BufEnd; |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | // MemoryBufferMem implementation. |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 68 | /// CopyStringRef - Copies contents of a StringRef into a block of memory and |
| 69 | /// null-terminates it. |
| 70 | static void CopyStringRef(char *Memory, StringRef Data) { |
| 71 | memcpy(Memory, Data.data(), Data.size()); |
| 72 | Memory[Data.size()] = 0; // Null terminate string. |
| 73 | } |
| 74 | |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 75 | struct NamedBufferAlloc { |
| 76 | StringRef Name; |
| 77 | NamedBufferAlloc(StringRef Name) : Name(Name) {} |
| 78 | }; |
| 79 | |
| 80 | void *operator new(size_t N, const NamedBufferAlloc &Alloc) { |
| 81 | char *Mem = static_cast<char *>(operator new(N + Alloc.Name.size() + 1)); |
| 82 | CopyStringRef(Mem + N, Alloc.Name); |
| 83 | return Mem; |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 86 | namespace { |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 87 | /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory. |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 88 | class MemoryBufferMem : public MemoryBuffer { |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 89 | public: |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 90 | MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) { |
| 91 | init(InputData.begin(), InputData.end(), RequiresNullTerminator); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 92 | } |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 93 | |
Craig Topper | 3186c01 | 2012-09-23 02:12:10 +0000 | [diff] [blame] | 94 | virtual const char *getBufferIdentifier() const LLVM_OVERRIDE { |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 95 | // The name is stored after the class itself. |
| 96 | return reinterpret_cast<const char*>(this + 1); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 97 | } |
Craig Topper | 3186c01 | 2012-09-23 02:12:10 +0000 | [diff] [blame] | 98 | |
| 99 | virtual BufferKind getBufferKind() const LLVM_OVERRIDE { |
Ted Kremenek | e203bbb | 2011-04-28 20:34:18 +0000 | [diff] [blame] | 100 | return MemoryBuffer_Malloc; |
| 101 | } |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 102 | }; |
| 103 | } |
| 104 | |
| 105 | /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note |
Chris Lattner | 6078926 | 2011-05-22 00:50:53 +0000 | [diff] [blame] | 106 | /// that InputData must be a null terminated if RequiresNullTerminator is true! |
Chris Lattner | 0e45d24 | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 107 | MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData, |
Rafael Espindola | ab959a2 | 2011-03-17 22:18:42 +0000 | [diff] [blame] | 108 | StringRef BufferName, |
| 109 | bool RequiresNullTerminator) { |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 110 | return new (NamedBufferAlloc(BufferName)) |
| 111 | MemoryBufferMem(InputData, RequiresNullTerminator); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | f5ea386 | 2007-10-09 21:46:38 +0000 | [diff] [blame] | 114 | /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer, |
| 115 | /// copying the contents and taking ownership of it. This has no requirements |
| 116 | /// on EndPtr[0]. |
Chris Lattner | 0e45d24 | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 117 | MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData, |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 118 | StringRef BufferName) { |
| 119 | MemoryBuffer *Buf = getNewUninitMemBuffer(InputData.size(), BufferName); |
| 120 | if (!Buf) return 0; |
| 121 | memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(), |
| 122 | InputData.size()); |
| 123 | return Buf; |
Chris Lattner | f5ea386 | 2007-10-09 21:46:38 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 126 | /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 127 | /// that is not initialized. Note that the caller should initialize the |
| 128 | /// memory allocated by this method. The memory is owned by the MemoryBuffer |
| 129 | /// object. |
Evan Cheng | 86cb318 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 130 | MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size, |
Daniel Dunbar | 124fc5e | 2009-11-10 00:43:58 +0000 | [diff] [blame] | 131 | StringRef BufferName) { |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 132 | // Allocate space for the MemoryBuffer, the data and the name. It is important |
| 133 | // that MemoryBuffer and data are aligned so PointerIntPair works with them. |
| 134 | size_t AlignedStringLen = |
| 135 | RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1, |
| 136 | sizeof(void*)); // TODO: Is sizeof(void*) enough? |
| 137 | size_t RealLen = AlignedStringLen + Size + 1; |
| 138 | char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow)); |
| 139 | if (!Mem) return 0; |
| 140 | |
| 141 | // The name is stored after the class itself. |
| 142 | CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName); |
| 143 | |
| 144 | // The buffer begins after the name and must be aligned. |
| 145 | char *Buf = Mem + AlignedStringLen; |
| 146 | Buf[Size] = 0; // Null terminate buffer. |
| 147 | |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 148 | return new (Mem) MemoryBufferMem(StringRef(Buf, Size), true); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that |
| 152 | /// is completely initialized to zeros. Note that the caller should |
| 153 | /// initialize the memory allocated by this method. The memory is owned by |
| 154 | /// the MemoryBuffer object. |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 155 | MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) { |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 156 | MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName); |
Evan Cheng | 333db7a | 2009-02-13 07:54:34 +0000 | [diff] [blame] | 157 | if (!SB) return 0; |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 158 | memset(const_cast<char*>(SB->getBufferStart()), 0, Size); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 159 | return SB; |
| 160 | } |
| 161 | |
| 162 | |
Chris Lattner | 4415847 | 2007-11-18 18:52:28 +0000 | [diff] [blame] | 163 | /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin |
| 164 | /// if the Filename is "-". If an error occurs, this returns null and fills |
| 165 | /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN) |
| 166 | /// returns an empty buffer. |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 167 | error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename, |
| 168 | OwningPtr<MemoryBuffer> &result, |
| 169 | int64_t FileSize) { |
Daniel Dunbar | 124fc5e | 2009-11-10 00:43:58 +0000 | [diff] [blame] | 170 | if (Filename == "-") |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 171 | return getSTDIN(result); |
| 172 | return getFile(Filename, result, FileSize); |
Chris Lattner | 4415847 | 2007-11-18 18:52:28 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 175 | error_code MemoryBuffer::getFileOrSTDIN(const char *Filename, |
| 176 | OwningPtr<MemoryBuffer> &result, |
| 177 | int64_t FileSize) { |
Dan Gohman | b377e28 | 2010-06-24 16:25:50 +0000 | [diff] [blame] | 178 | if (strcmp(Filename, "-") == 0) |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 179 | return getSTDIN(result); |
| 180 | return getFile(Filename, result, FileSize); |
Dan Gohman | b377e28 | 2010-06-24 16:25:50 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 183 | //===----------------------------------------------------------------------===// |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 184 | // MemoryBuffer::getFile implementation. |
| 185 | //===----------------------------------------------------------------------===// |
| 186 | |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 187 | namespace { |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 188 | /// \brief Memorry maps a file descriptor using sys::fs::mapped_file_region. |
| 189 | /// |
| 190 | /// This handles converting the offset into a legal offset on the platform. |
| 191 | class MemoryBufferMMapFile : public MemoryBuffer { |
| 192 | sys::fs::mapped_file_region MFR; |
| 193 | |
| 194 | static uint64_t getLegalMapOffset(uint64_t Offset) { |
| 195 | return Offset & ~(sys::fs::mapped_file_region::alignment() - 1); |
| 196 | } |
| 197 | |
| 198 | static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) { |
| 199 | return Len + (Offset - getLegalMapOffset(Offset)); |
| 200 | } |
| 201 | |
| 202 | const char *getStart(uint64_t Len, uint64_t Offset) { |
| 203 | return MFR.const_data() + (Offset - getLegalMapOffset(Offset)); |
| 204 | } |
| 205 | |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 206 | public: |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 207 | MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len, |
| 208 | uint64_t Offset, error_code EC) |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 209 | : MFR(FD, false, sys::fs::mapped_file_region::readonly, |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 210 | getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) { |
| 211 | if (!EC) { |
| 212 | const char *Start = getStart(Len, Offset); |
| 213 | init(Start, Start + Len, RequiresNullTerminator); |
| 214 | } |
| 215 | } |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 216 | |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 217 | virtual const char *getBufferIdentifier() const LLVM_OVERRIDE { |
| 218 | // The name is stored after the class itself. |
| 219 | return reinterpret_cast<const char *>(this + 1); |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 220 | } |
Craig Topper | 3186c01 | 2012-09-23 02:12:10 +0000 | [diff] [blame] | 221 | |
| 222 | virtual BufferKind getBufferKind() const LLVM_OVERRIDE { |
Ted Kremenek | e203bbb | 2011-04-28 20:34:18 +0000 | [diff] [blame] | 223 | return MemoryBuffer_MMap; |
| 224 | } |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 225 | }; |
| 226 | } |
| 227 | |
Daniel Dunbar | 43a172d | 2012-11-05 21:55:40 +0000 | [diff] [blame] | 228 | static error_code getMemoryBufferForStream(int FD, |
| 229 | StringRef BufferName, |
| 230 | OwningPtr<MemoryBuffer> &result) { |
| 231 | const ssize_t ChunkSize = 4096*4; |
| 232 | SmallString<ChunkSize> Buffer; |
| 233 | ssize_t ReadBytes; |
| 234 | // Read into Buffer until we hit EOF. |
| 235 | do { |
| 236 | Buffer.reserve(Buffer.size() + ChunkSize); |
| 237 | ReadBytes = read(FD, Buffer.end(), ChunkSize); |
| 238 | if (ReadBytes == -1) { |
| 239 | if (errno == EINTR) continue; |
| 240 | return error_code(errno, posix_category()); |
| 241 | } |
| 242 | Buffer.set_size(Buffer.size() + ReadBytes); |
| 243 | } while (ReadBytes != 0); |
| 244 | |
| 245 | result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName)); |
| 246 | return error_code::success(); |
| 247 | } |
| 248 | |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 249 | error_code MemoryBuffer::getFile(StringRef Filename, |
| 250 | OwningPtr<MemoryBuffer> &result, |
Rafael Espindola | 2475adc | 2011-03-22 19:20:47 +0000 | [diff] [blame] | 251 | int64_t FileSize, |
| 252 | bool RequiresNullTerminator) { |
Chris Lattner | 6bf4e6d | 2010-11-23 22:20:27 +0000 | [diff] [blame] | 253 | // Ensure the path is null terminated. |
Dan Gohman | b377e28 | 2010-06-24 16:25:50 +0000 | [diff] [blame] | 254 | SmallString<256> PathBuf(Filename.begin(), Filename.end()); |
Rafael Espindola | 2475adc | 2011-03-22 19:20:47 +0000 | [diff] [blame] | 255 | return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize, |
| 256 | RequiresNullTerminator); |
Dan Gohman | b377e28 | 2010-06-24 16:25:50 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 259 | error_code MemoryBuffer::getFile(const char *Filename, |
| 260 | OwningPtr<MemoryBuffer> &result, |
Rafael Espindola | 2475adc | 2011-03-22 19:20:47 +0000 | [diff] [blame] | 261 | int64_t FileSize, |
| 262 | bool RequiresNullTerminator) { |
Argyrios Kyrtzidis | db4443f | 2013-03-01 22:48:51 +0000 | [diff] [blame] | 263 | // FIXME: Review if this check is unnecessary on windows as well. |
| 264 | #ifdef LLVM_ON_WIN32 |
Kaelyn Uhrain | 23fb5c3 | 2012-06-20 20:21:33 +0000 | [diff] [blame] | 265 | // First check that the "file" is not a directory |
| 266 | bool is_dir = false; |
| 267 | error_code err = sys::fs::is_directory(Filename, is_dir); |
| 268 | if (err) |
| 269 | return err; |
Kaelyn Uhrain | 4cb2e2d | 2012-06-20 20:38:36 +0000 | [diff] [blame] | 270 | if (is_dir) |
Kaelyn Uhrain | 23fb5c3 | 2012-06-20 20:21:33 +0000 | [diff] [blame] | 271 | return make_error_code(errc::is_a_directory); |
Argyrios Kyrtzidis | db4443f | 2013-03-01 22:48:51 +0000 | [diff] [blame] | 272 | #endif |
Kaelyn Uhrain | 23fb5c3 | 2012-06-20 20:21:33 +0000 | [diff] [blame] | 273 | |
Dan Gohman | b377e28 | 2010-06-24 16:25:50 +0000 | [diff] [blame] | 274 | int OpenFlags = O_RDONLY; |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 275 | #ifdef O_BINARY |
Bill Wendling | fc9f25d | 2008-04-01 22:09:20 +0000 | [diff] [blame] | 276 | OpenFlags |= O_BINARY; // Open input file in binary mode on win32. |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 277 | #endif |
Dan Gohman | b377e28 | 2010-06-24 16:25:50 +0000 | [diff] [blame] | 278 | int FD = ::open(Filename, OpenFlags); |
Chris Lattner | 6078926 | 2011-05-22 00:50:53 +0000 | [diff] [blame] | 279 | if (FD == -1) |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 280 | return error_code(errno, posix_category()); |
Chris Lattner | 6078926 | 2011-05-22 00:50:53 +0000 | [diff] [blame] | 281 | |
Rafael Espindola | 2475adc | 2011-03-22 19:20:47 +0000 | [diff] [blame] | 282 | error_code ret = getOpenFile(FD, Filename, result, FileSize, FileSize, |
| 283 | 0, RequiresNullTerminator); |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 284 | close(FD); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 285 | return ret; |
Chris Lattner | 6bf4e6d | 2010-11-23 22:20:27 +0000 | [diff] [blame] | 286 | } |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 287 | |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 288 | static bool shouldUseMmap(int FD, |
| 289 | size_t FileSize, |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 290 | size_t MapSize, |
| 291 | off_t Offset, |
| 292 | bool RequiresNullTerminator, |
| 293 | int PageSize) { |
| 294 | // We don't use mmap for small files because this can severely fragment our |
| 295 | // address space. |
| 296 | if (MapSize < 4096*4) |
| 297 | return false; |
| 298 | |
| 299 | if (!RequiresNullTerminator) |
| 300 | return true; |
| 301 | |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 302 | |
| 303 | // If we don't know the file size, use fstat to find out. fstat on an open |
| 304 | // file descriptor is cheaper than stat on a random path. |
| 305 | // FIXME: this chunk of code is duplicated, but it avoids a fstat when |
| 306 | // RequiresNullTerminator = false and MapSize != -1. |
| 307 | if (FileSize == size_t(-1)) { |
| 308 | struct stat FileInfo; |
| 309 | // TODO: This should use fstat64 when available. |
| 310 | if (fstat(FD, &FileInfo) == -1) { |
| 311 | return error_code(errno, posix_category()); |
| 312 | } |
| 313 | FileSize = FileInfo.st_size; |
| 314 | } |
| 315 | |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 316 | // If we need a null terminator and the end of the map is inside the file, |
| 317 | // we cannot use mmap. |
| 318 | size_t End = Offset + MapSize; |
| 319 | assert(End <= FileSize); |
| 320 | if (End != FileSize) |
| 321 | return false; |
| 322 | |
| 323 | // Don't try to map files that are exactly a multiple of the system page size |
| 324 | // if we need a null terminator. |
| 325 | if ((FileSize & (PageSize -1)) == 0) |
| 326 | return false; |
| 327 | |
| 328 | return true; |
| 329 | } |
| 330 | |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 331 | error_code MemoryBuffer::getOpenFile(int FD, const char *Filename, |
| 332 | OwningPtr<MemoryBuffer> &result, |
Ivan Krasin | 639222d | 2011-09-15 23:13:00 +0000 | [diff] [blame] | 333 | uint64_t FileSize, uint64_t MapSize, |
| 334 | int64_t Offset, |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 335 | bool RequiresNullTerminator) { |
Chandler Carruth | acd64be | 2012-12-31 23:31:56 +0000 | [diff] [blame] | 336 | static int PageSize = sys::process::get_self()->page_size(); |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 337 | |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 338 | // Default is to map the full file. |
Ivan Krasin | 639222d | 2011-09-15 23:13:00 +0000 | [diff] [blame] | 339 | if (MapSize == uint64_t(-1)) { |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 340 | // If we don't know the file size, use fstat to find out. fstat on an open |
| 341 | // file descriptor is cheaper than stat on a random path. |
Ivan Krasin | 639222d | 2011-09-15 23:13:00 +0000 | [diff] [blame] | 342 | if (FileSize == uint64_t(-1)) { |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 343 | struct stat FileInfo; |
| 344 | // TODO: This should use fstat64 when available. |
| 345 | if (fstat(FD, &FileInfo) == -1) { |
| 346 | return error_code(errno, posix_category()); |
| 347 | } |
Daniel Dunbar | 43a172d | 2012-11-05 21:55:40 +0000 | [diff] [blame] | 348 | |
Dan Gohman | 782609e | 2013-02-19 19:36:55 +0000 | [diff] [blame] | 349 | // If this not a file or a block device (e.g. it's a named pipe |
| 350 | // or character device), we can't trust the size. Create the memory |
| 351 | // buffer by copying off the stream. |
| 352 | if (!S_ISREG(FileInfo.st_mode) && !S_ISBLK(FileInfo.st_mode)) { |
Daniel Dunbar | 43a172d | 2012-11-05 21:55:40 +0000 | [diff] [blame] | 353 | return getMemoryBufferForStream(FD, Filename, result); |
| 354 | } |
| 355 | |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 356 | FileSize = FileInfo.st_size; |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 357 | } |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 358 | MapSize = FileSize; |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 359 | } |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 360 | |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 361 | if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator, |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 362 | PageSize)) { |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 363 | error_code EC; |
| 364 | result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile( |
| 365 | RequiresNullTerminator, FD, MapSize, Offset, EC)); |
| 366 | if (!EC) |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 367 | return error_code::success(); |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 368 | } |
Evan Cheng | 333db7a | 2009-02-13 07:54:34 +0000 | [diff] [blame] | 369 | |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 370 | MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename); |
Evan Cheng | 333db7a | 2009-02-13 07:54:34 +0000 | [diff] [blame] | 371 | if (!Buf) { |
Michael J. Spencer | 7b6fef8 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 372 | // Failed to create a buffer. The only way it can fail is if |
| 373 | // new(std::nothrow) returns 0. |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 374 | return make_error_code(errc::not_enough_memory); |
Evan Cheng | 333db7a | 2009-02-13 07:54:34 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | OwningPtr<MemoryBuffer> SB(Buf); |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 378 | char *BufPtr = const_cast<char*>(SB->getBufferStart()); |
Benjamin Kramer | 10b0f3b | 2010-04-01 14:35:22 +0000 | [diff] [blame] | 379 | |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 380 | size_t BytesLeft = MapSize; |
Benjamin Kramer | e1effb0 | 2011-11-22 12:31:53 +0000 | [diff] [blame] | 381 | #ifndef HAVE_PREAD |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 382 | if (lseek(FD, Offset, SEEK_SET) == -1) |
| 383 | return error_code(errno, posix_category()); |
Benjamin Kramer | e1effb0 | 2011-11-22 12:31:53 +0000 | [diff] [blame] | 384 | #endif |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 385 | |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 386 | while (BytesLeft) { |
Benjamin Kramer | e1effb0 | 2011-11-22 12:31:53 +0000 | [diff] [blame] | 387 | #ifdef HAVE_PREAD |
| 388 | ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset); |
| 389 | #else |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 390 | ssize_t NumRead = ::read(FD, BufPtr, BytesLeft); |
Benjamin Kramer | e1effb0 | 2011-11-22 12:31:53 +0000 | [diff] [blame] | 391 | #endif |
Benjamin Kramer | 10b0f3b | 2010-04-01 14:35:22 +0000 | [diff] [blame] | 392 | if (NumRead == -1) { |
| 393 | if (errno == EINTR) |
| 394 | continue; |
| 395 | // Error while reading. |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 396 | return error_code(errno, posix_category()); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 397 | } |
Argyrios Kyrtzidis | 3dc531e | 2012-03-13 20:18:42 +0000 | [diff] [blame] | 398 | if (NumRead == 0) { |
| 399 | assert(0 && "We got inaccurate FileSize value or fstat reported an " |
| 400 | "invalid file size."); |
Argyrios Kyrtzidis | ef909265 | 2012-04-05 04:23:56 +0000 | [diff] [blame] | 401 | *BufPtr = '\0'; // null-terminate at the actual size. |
Argyrios Kyrtzidis | 3dc531e | 2012-03-13 20:18:42 +0000 | [diff] [blame] | 402 | break; |
| 403 | } |
Benjamin Kramer | 10b0f3b | 2010-04-01 14:35:22 +0000 | [diff] [blame] | 404 | BytesLeft -= NumRead; |
| 405 | BufPtr += NumRead; |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 406 | } |
Benjamin Kramer | 10b0f3b | 2010-04-01 14:35:22 +0000 | [diff] [blame] | 407 | |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 408 | result.swap(SB); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 409 | return error_code::success(); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 412 | //===----------------------------------------------------------------------===// |
| 413 | // MemoryBuffer::getSTDIN implementation. |
| 414 | //===----------------------------------------------------------------------===// |
| 415 | |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 416 | error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &result) { |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 417 | // Read in all of the data from stdin, we cannot mmap stdin. |
Daniel Dunbar | 124fc5e | 2009-11-10 00:43:58 +0000 | [diff] [blame] | 418 | // |
| 419 | // FIXME: That isn't necessarily true, we should try to mmap stdin and |
| 420 | // fallback if it fails. |
Jeff Cohen | b6e144c | 2007-05-07 15:21:46 +0000 | [diff] [blame] | 421 | sys::Program::ChangeStdinToBinary(); |
Benjamin Kramer | 58e6c2e | 2010-06-25 16:07:18 +0000 | [diff] [blame] | 422 | |
Daniel Dunbar | 43a172d | 2012-11-05 21:55:40 +0000 | [diff] [blame] | 423 | return getMemoryBufferForStream(0, "<stdin>", result); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 424 | } |