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> |
Rafael Espindola | d883087 | 2013-06-12 14:11:15 +0000 | [diff] [blame] | 36 | // Simplistic definitinos of these macros for use in getOpenFile. |
Dan Gohman | 3e17294 | 2013-02-19 22:38:58 +0000 | [diff] [blame] | 37 | #ifndef S_ISREG |
| 38 | #define S_ISREG(x) (1) |
| 39 | #endif |
| 40 | #ifndef S_ISBLK |
| 41 | #define S_ISBLK(x) (0) |
Daniel Dunbar | e2d25c2 | 2012-11-06 17:08:09 +0000 | [diff] [blame] | 42 | #endif |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 43 | #endif |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | // MemoryBuffer implementation itself. |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 50 | MemoryBuffer::~MemoryBuffer() { } |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 51 | |
| 52 | /// init - Initialize this MemoryBuffer as a reference to externally allocated |
| 53 | /// memory, memory that we know is already null terminated. |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 54 | void MemoryBuffer::init(const char *BufStart, const char *BufEnd, |
| 55 | bool RequiresNullTerminator) { |
Rafael Espindola | 7c9cc46 | 2011-03-18 02:55:51 +0000 | [diff] [blame] | 56 | assert((!RequiresNullTerminator || BufEnd[0] == 0) && |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 57 | "Buffer is not null terminated!"); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 58 | BufferStart = BufStart; |
| 59 | BufferEnd = BufEnd; |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | // MemoryBufferMem implementation. |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 66 | /// CopyStringRef - Copies contents of a StringRef into a block of memory and |
| 67 | /// null-terminates it. |
| 68 | static void CopyStringRef(char *Memory, StringRef Data) { |
| 69 | memcpy(Memory, Data.data(), Data.size()); |
| 70 | Memory[Data.size()] = 0; // Null terminate string. |
| 71 | } |
| 72 | |
Benjamin Kramer | a73cc5e | 2013-03-30 15:23:08 +0000 | [diff] [blame] | 73 | namespace { |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 74 | struct NamedBufferAlloc { |
| 75 | StringRef Name; |
| 76 | NamedBufferAlloc(StringRef Name) : Name(Name) {} |
| 77 | }; |
Benjamin Kramer | a73cc5e | 2013-03-30 15:23:08 +0000 | [diff] [blame] | 78 | } |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 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 | |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 175 | //===----------------------------------------------------------------------===// |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 176 | // MemoryBuffer::getFile implementation. |
| 177 | //===----------------------------------------------------------------------===// |
| 178 | |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 179 | namespace { |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 180 | /// \brief Memorry maps a file descriptor using sys::fs::mapped_file_region. |
| 181 | /// |
| 182 | /// This handles converting the offset into a legal offset on the platform. |
| 183 | class MemoryBufferMMapFile : public MemoryBuffer { |
| 184 | sys::fs::mapped_file_region MFR; |
| 185 | |
| 186 | static uint64_t getLegalMapOffset(uint64_t Offset) { |
| 187 | return Offset & ~(sys::fs::mapped_file_region::alignment() - 1); |
| 188 | } |
| 189 | |
| 190 | static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) { |
| 191 | return Len + (Offset - getLegalMapOffset(Offset)); |
| 192 | } |
| 193 | |
| 194 | const char *getStart(uint64_t Len, uint64_t Offset) { |
| 195 | return MFR.const_data() + (Offset - getLegalMapOffset(Offset)); |
| 196 | } |
| 197 | |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 198 | public: |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 199 | MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len, |
| 200 | uint64_t Offset, error_code EC) |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 201 | : MFR(FD, false, sys::fs::mapped_file_region::readonly, |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 202 | getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) { |
| 203 | if (!EC) { |
| 204 | const char *Start = getStart(Len, Offset); |
| 205 | init(Start, Start + Len, RequiresNullTerminator); |
| 206 | } |
| 207 | } |
Benjamin Kramer | ce2a922 | 2010-06-25 11:50:40 +0000 | [diff] [blame] | 208 | |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 209 | virtual const char *getBufferIdentifier() const LLVM_OVERRIDE { |
| 210 | // The name is stored after the class itself. |
| 211 | return reinterpret_cast<const char *>(this + 1); |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 212 | } |
Craig Topper | 3186c01 | 2012-09-23 02:12:10 +0000 | [diff] [blame] | 213 | |
| 214 | virtual BufferKind getBufferKind() const LLVM_OVERRIDE { |
Ted Kremenek | e203bbb | 2011-04-28 20:34:18 +0000 | [diff] [blame] | 215 | return MemoryBuffer_MMap; |
| 216 | } |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 217 | }; |
| 218 | } |
| 219 | |
Daniel Dunbar | 43a172d | 2012-11-05 21:55:40 +0000 | [diff] [blame] | 220 | static error_code getMemoryBufferForStream(int FD, |
| 221 | StringRef BufferName, |
| 222 | OwningPtr<MemoryBuffer> &result) { |
| 223 | const ssize_t ChunkSize = 4096*4; |
| 224 | SmallString<ChunkSize> Buffer; |
| 225 | ssize_t ReadBytes; |
| 226 | // Read into Buffer until we hit EOF. |
| 227 | do { |
| 228 | Buffer.reserve(Buffer.size() + ChunkSize); |
| 229 | ReadBytes = read(FD, Buffer.end(), ChunkSize); |
| 230 | if (ReadBytes == -1) { |
| 231 | if (errno == EINTR) continue; |
| 232 | return error_code(errno, posix_category()); |
| 233 | } |
| 234 | Buffer.set_size(Buffer.size() + ReadBytes); |
| 235 | } while (ReadBytes != 0); |
| 236 | |
| 237 | result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName)); |
| 238 | return error_code::success(); |
| 239 | } |
| 240 | |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 241 | error_code MemoryBuffer::getFile(StringRef Filename, |
| 242 | OwningPtr<MemoryBuffer> &result, |
Rafael Espindola | 2475adc | 2011-03-22 19:20:47 +0000 | [diff] [blame] | 243 | int64_t FileSize, |
| 244 | bool RequiresNullTerminator) { |
Chris Lattner | 6bf4e6d | 2010-11-23 22:20:27 +0000 | [diff] [blame] | 245 | // Ensure the path is null terminated. |
Dan Gohman | b377e28 | 2010-06-24 16:25:50 +0000 | [diff] [blame] | 246 | SmallString<256> PathBuf(Filename.begin(), Filename.end()); |
Rafael Espindola | 2475adc | 2011-03-22 19:20:47 +0000 | [diff] [blame] | 247 | return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize, |
| 248 | RequiresNullTerminator); |
Dan Gohman | b377e28 | 2010-06-24 16:25:50 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Rafael Espindola | 3d2ac2e | 2013-07-23 20:25:01 +0000 | [diff] [blame] | 251 | static error_code getOpenFileImpl(int FD, const char *Filename, |
| 252 | OwningPtr<MemoryBuffer> &Result, |
| 253 | uint64_t FileSize, uint64_t MapSize, |
| 254 | int64_t Offset, bool RequiresNullTerminator); |
| 255 | |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 256 | error_code MemoryBuffer::getFile(const char *Filename, |
| 257 | OwningPtr<MemoryBuffer> &result, |
Rafael Espindola | 2475adc | 2011-03-22 19:20:47 +0000 | [diff] [blame] | 258 | int64_t FileSize, |
| 259 | bool RequiresNullTerminator) { |
Rafael Espindola | 6d35481 | 2013-07-16 19:44:17 +0000 | [diff] [blame] | 260 | int FD; |
| 261 | error_code EC = sys::fs::openFileForRead(Filename, FD); |
| 262 | if (EC) |
| 263 | return EC; |
Chris Lattner | 6078926 | 2011-05-22 00:50:53 +0000 | [diff] [blame] | 264 | |
Rafael Espindola | 3d2ac2e | 2013-07-23 20:25:01 +0000 | [diff] [blame] | 265 | error_code ret = getOpenFileImpl(FD, Filename, result, FileSize, FileSize, 0, |
| 266 | RequiresNullTerminator); |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 267 | close(FD); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 268 | return ret; |
Chris Lattner | 6bf4e6d | 2010-11-23 22:20:27 +0000 | [diff] [blame] | 269 | } |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 270 | |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 271 | static bool shouldUseMmap(int FD, |
| 272 | size_t FileSize, |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 273 | size_t MapSize, |
| 274 | off_t Offset, |
| 275 | bool RequiresNullTerminator, |
| 276 | int PageSize) { |
| 277 | // We don't use mmap for small files because this can severely fragment our |
| 278 | // address space. |
| 279 | if (MapSize < 4096*4) |
| 280 | return false; |
| 281 | |
| 282 | if (!RequiresNullTerminator) |
| 283 | return true; |
| 284 | |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 285 | |
| 286 | // If we don't know the file size, use fstat to find out. fstat on an open |
| 287 | // file descriptor is cheaper than stat on a random path. |
| 288 | // FIXME: this chunk of code is duplicated, but it avoids a fstat when |
| 289 | // RequiresNullTerminator = false and MapSize != -1. |
| 290 | if (FileSize == size_t(-1)) { |
Rafael Espindola | 4d10587 | 2013-07-18 03:04:20 +0000 | [diff] [blame] | 291 | sys::fs::file_status Status; |
| 292 | error_code EC = sys::fs::status(FD, Status); |
| 293 | if (EC) |
| 294 | return EC; |
| 295 | FileSize = Status.getSize(); |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 298 | // If we need a null terminator and the end of the map is inside the file, |
| 299 | // we cannot use mmap. |
| 300 | size_t End = Offset + MapSize; |
| 301 | assert(End <= FileSize); |
| 302 | if (End != FileSize) |
| 303 | return false; |
| 304 | |
| 305 | // Don't try to map files that are exactly a multiple of the system page size |
| 306 | // if we need a null terminator. |
| 307 | if ((FileSize & (PageSize -1)) == 0) |
| 308 | return false; |
| 309 | |
| 310 | return true; |
| 311 | } |
| 312 | |
Rafael Espindola | 3d2ac2e | 2013-07-23 20:25:01 +0000 | [diff] [blame] | 313 | static error_code getOpenFileImpl(int FD, const char *Filename, |
| 314 | OwningPtr<MemoryBuffer> &result, |
| 315 | uint64_t FileSize, uint64_t MapSize, |
| 316 | int64_t Offset, bool RequiresNullTerminator) { |
Chandler Carruth | acd64be | 2012-12-31 23:31:56 +0000 | [diff] [blame] | 317 | static int PageSize = sys::process::get_self()->page_size(); |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 318 | |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 319 | // Default is to map the full file. |
Ivan Krasin | 639222d | 2011-09-15 23:13:00 +0000 | [diff] [blame] | 320 | if (MapSize == uint64_t(-1)) { |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 321 | // If we don't know the file size, use fstat to find out. fstat on an open |
| 322 | // file descriptor is cheaper than stat on a random path. |
Ivan Krasin | 639222d | 2011-09-15 23:13:00 +0000 | [diff] [blame] | 323 | if (FileSize == uint64_t(-1)) { |
Rafael Espindola | 4d10587 | 2013-07-18 03:04:20 +0000 | [diff] [blame] | 324 | sys::fs::file_status Status; |
| 325 | error_code EC = sys::fs::status(FD, Status); |
| 326 | if (EC) |
| 327 | return EC; |
Daniel Dunbar | 43a172d | 2012-11-05 21:55:40 +0000 | [diff] [blame] | 328 | |
Dan Gohman | 782609e | 2013-02-19 19:36:55 +0000 | [diff] [blame] | 329 | // If this not a file or a block device (e.g. it's a named pipe |
| 330 | // or character device), we can't trust the size. Create the memory |
| 331 | // buffer by copying off the stream. |
Rafael Espindola | 4d10587 | 2013-07-18 03:04:20 +0000 | [diff] [blame] | 332 | sys::fs::file_type Type = Status.type(); |
| 333 | if (Type != sys::fs::file_type::regular_file && |
| 334 | Type != sys::fs::file_type::block_file) |
Daniel Dunbar | 43a172d | 2012-11-05 21:55:40 +0000 | [diff] [blame] | 335 | return getMemoryBufferForStream(FD, Filename, result); |
Daniel Dunbar | 43a172d | 2012-11-05 21:55:40 +0000 | [diff] [blame] | 336 | |
Rafael Espindola | 4d10587 | 2013-07-18 03:04:20 +0000 | [diff] [blame] | 337 | FileSize = Status.getSize(); |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 338 | } |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 339 | MapSize = FileSize; |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 340 | } |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 341 | |
Rafael Espindola | cbe6a1a | 2011-03-10 20:54:07 +0000 | [diff] [blame] | 342 | if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator, |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 343 | PageSize)) { |
Michael J. Spencer | 2343a74 | 2013-03-12 19:28:19 +0000 | [diff] [blame] | 344 | error_code EC; |
| 345 | result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile( |
| 346 | RequiresNullTerminator, FD, MapSize, Offset, EC)); |
| 347 | if (!EC) |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 348 | return error_code::success(); |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 349 | } |
Evan Cheng | 333db7a | 2009-02-13 07:54:34 +0000 | [diff] [blame] | 350 | |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 351 | MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename); |
Evan Cheng | 333db7a | 2009-02-13 07:54:34 +0000 | [diff] [blame] | 352 | if (!Buf) { |
Michael J. Spencer | 7b6fef8 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 353 | // Failed to create a buffer. The only way it can fail is if |
| 354 | // new(std::nothrow) returns 0. |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 355 | return make_error_code(errc::not_enough_memory); |
Evan Cheng | 333db7a | 2009-02-13 07:54:34 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | OwningPtr<MemoryBuffer> SB(Buf); |
Chris Lattner | a542518 | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 359 | char *BufPtr = const_cast<char*>(SB->getBufferStart()); |
Benjamin Kramer | 10b0f3b | 2010-04-01 14:35:22 +0000 | [diff] [blame] | 360 | |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 361 | size_t BytesLeft = MapSize; |
Benjamin Kramer | e1effb0 | 2011-11-22 12:31:53 +0000 | [diff] [blame] | 362 | #ifndef HAVE_PREAD |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 363 | if (lseek(FD, Offset, SEEK_SET) == -1) |
| 364 | return error_code(errno, posix_category()); |
Benjamin Kramer | e1effb0 | 2011-11-22 12:31:53 +0000 | [diff] [blame] | 365 | #endif |
Rafael Espindola | 258a605 | 2011-03-10 18:33:29 +0000 | [diff] [blame] | 366 | |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 367 | while (BytesLeft) { |
Benjamin Kramer | e1effb0 | 2011-11-22 12:31:53 +0000 | [diff] [blame] | 368 | #ifdef HAVE_PREAD |
| 369 | ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset); |
| 370 | #else |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 371 | ssize_t NumRead = ::read(FD, BufPtr, BytesLeft); |
Benjamin Kramer | e1effb0 | 2011-11-22 12:31:53 +0000 | [diff] [blame] | 372 | #endif |
Benjamin Kramer | 10b0f3b | 2010-04-01 14:35:22 +0000 | [diff] [blame] | 373 | if (NumRead == -1) { |
| 374 | if (errno == EINTR) |
| 375 | continue; |
| 376 | // Error while reading. |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 377 | return error_code(errno, posix_category()); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 378 | } |
Argyrios Kyrtzidis | 3dc531e | 2012-03-13 20:18:42 +0000 | [diff] [blame] | 379 | if (NumRead == 0) { |
| 380 | assert(0 && "We got inaccurate FileSize value or fstat reported an " |
| 381 | "invalid file size."); |
Argyrios Kyrtzidis | ef909265 | 2012-04-05 04:23:56 +0000 | [diff] [blame] | 382 | *BufPtr = '\0'; // null-terminate at the actual size. |
Argyrios Kyrtzidis | 3dc531e | 2012-03-13 20:18:42 +0000 | [diff] [blame] | 383 | break; |
| 384 | } |
Benjamin Kramer | 10b0f3b | 2010-04-01 14:35:22 +0000 | [diff] [blame] | 385 | BytesLeft -= NumRead; |
| 386 | BufPtr += NumRead; |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 387 | } |
Benjamin Kramer | 10b0f3b | 2010-04-01 14:35:22 +0000 | [diff] [blame] | 388 | |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 389 | result.swap(SB); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 390 | return error_code::success(); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Rafael Espindola | 3d2ac2e | 2013-07-23 20:25:01 +0000 | [diff] [blame] | 393 | error_code MemoryBuffer::getOpenFile(int FD, const char *Filename, |
| 394 | OwningPtr<MemoryBuffer> &Result, |
| 395 | uint64_t FileSize, |
| 396 | bool RequiresNullTerminator) { |
| 397 | return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0, |
| 398 | RequiresNullTerminator); |
| 399 | } |
| 400 | |
| 401 | error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename, |
| 402 | OwningPtr<MemoryBuffer> &Result, |
| 403 | uint64_t MapSize, int64_t Offset) { |
| 404 | return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false); |
| 405 | } |
| 406 | |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 407 | //===----------------------------------------------------------------------===// |
| 408 | // MemoryBuffer::getSTDIN implementation. |
| 409 | //===----------------------------------------------------------------------===// |
| 410 | |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 411 | error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &result) { |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 412 | // Read in all of the data from stdin, we cannot mmap stdin. |
Daniel Dunbar | 124fc5e | 2009-11-10 00:43:58 +0000 | [diff] [blame] | 413 | // |
| 414 | // FIXME: That isn't necessarily true, we should try to mmap stdin and |
| 415 | // fallback if it fails. |
Rafael Espindola | cb2eca0 | 2013-06-12 20:58:35 +0000 | [diff] [blame] | 416 | sys::ChangeStdinToBinary(); |
Benjamin Kramer | 58e6c2e | 2010-06-25 16:07:18 +0000 | [diff] [blame] | 417 | |
Daniel Dunbar | 43a172d | 2012-11-05 21:55:40 +0000 | [diff] [blame] | 418 | return getMemoryBufferForStream(0, "<stdin>", result); |
Chris Lattner | ee2d1f1 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 419 | } |