Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 1 | //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | 333ffd4 | 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 | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/OwningPtr.h" |
| 16 | #include "llvm/ADT/SmallString.h" |
| 17 | #include "llvm/System/Path.h" |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 18 | #include "llvm/System/Process.h" |
Jeff Cohen | 0fea8eb | 2007-05-07 15:21:46 +0000 | [diff] [blame] | 19 | #include "llvm/System/Program.h" |
Jeff Cohen | 9bc4060 | 2007-04-29 14:21:44 +0000 | [diff] [blame] | 20 | #include <cassert> |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 21 | #include <cstdio> |
| 22 | #include <cstring> |
| 23 | #include <cerrno> |
Chris Lattner | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 27 | #include <unistd.h> |
| 28 | #include <sys/uio.h> |
| 29 | #include <sys/fcntl.h> |
| 30 | #else |
| 31 | #include <io.h> |
Bill Wendling | a442006 | 2008-04-01 22:09:20 +0000 | [diff] [blame] | 32 | #include <fcntl.h> |
Chris Lattner | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 33 | #endif |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // MemoryBuffer implementation itself. |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
| 40 | MemoryBuffer::~MemoryBuffer() { |
| 41 | if (MustDeleteBuffer) |
| 42 | delete [] BufferStart; |
| 43 | } |
| 44 | |
| 45 | /// initCopyOf - Initialize this source buffer with a copy of the specified |
| 46 | /// memory range. We make the copy so that we can null terminate it |
| 47 | /// successfully. |
| 48 | void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) { |
| 49 | size_t Size = BufEnd-BufStart; |
| 50 | BufferStart = new char[Size+1]; |
| 51 | BufferEnd = BufferStart+Size; |
| 52 | memcpy(const_cast<char*>(BufferStart), BufStart, Size); |
| 53 | *const_cast<char*>(BufferEnd) = 0; // Null terminate buffer. |
Chris Lattner | a24b04e | 2007-05-11 00:43:26 +0000 | [diff] [blame] | 54 | MustDeleteBuffer = true; |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /// init - Initialize this MemoryBuffer as a reference to externally allocated |
| 58 | /// memory, memory that we know is already null terminated. |
| 59 | void MemoryBuffer::init(const char *BufStart, const char *BufEnd) { |
| 60 | assert(BufEnd[0] == 0 && "Buffer is not null terminated!"); |
| 61 | BufferStart = BufStart; |
| 62 | BufferEnd = BufEnd; |
| 63 | MustDeleteBuffer = false; |
| 64 | } |
| 65 | |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | // MemoryBufferMem implementation. |
| 68 | //===----------------------------------------------------------------------===// |
| 69 | |
| 70 | namespace { |
| 71 | class MemoryBufferMem : public MemoryBuffer { |
| 72 | std::string FileID; |
| 73 | public: |
Chris Lattner | 3daae27 | 2007-10-09 21:46:38 +0000 | [diff] [blame] | 74 | MemoryBufferMem(const char *Start, const char *End, const char *FID, |
| 75 | bool Copy = false) |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 76 | : FileID(FID) { |
Chris Lattner | 3daae27 | 2007-10-09 21:46:38 +0000 | [diff] [blame] | 77 | if (!Copy) |
| 78 | init(Start, End); |
| 79 | else |
| 80 | initCopyOf(Start, End); |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | virtual const char *getBufferIdentifier() const { |
| 84 | return FileID.c_str(); |
| 85 | } |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note |
| 90 | /// that EndPtr[0] must be a null byte and be accessible! |
| 91 | MemoryBuffer *MemoryBuffer::getMemBuffer(const char *StartPtr, |
| 92 | const char *EndPtr, |
| 93 | const char *BufferName) { |
| 94 | return new MemoryBufferMem(StartPtr, EndPtr, BufferName); |
| 95 | } |
| 96 | |
Chris Lattner | 3daae27 | 2007-10-09 21:46:38 +0000 | [diff] [blame] | 97 | /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer, |
| 98 | /// copying the contents and taking ownership of it. This has no requirements |
| 99 | /// on EndPtr[0]. |
| 100 | MemoryBuffer *MemoryBuffer::getMemBufferCopy(const char *StartPtr, |
| 101 | const char *EndPtr, |
| 102 | const char *BufferName) { |
| 103 | return new MemoryBufferMem(StartPtr, EndPtr, BufferName, true); |
| 104 | } |
| 105 | |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 106 | /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size |
| 107 | /// that is completely initialized to zeros. Note that the caller should |
| 108 | /// initialize the memory allocated by this method. The memory is owned by |
| 109 | /// the MemoryBuffer object. |
| 110 | MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(unsigned Size, |
| 111 | const char *BufferName) { |
| 112 | char *Buf = new char[Size+1]; |
| 113 | Buf[Size] = 0; |
| 114 | MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName); |
| 115 | // The memory for this buffer is owned by the MemoryBuffer. |
| 116 | SB->MustDeleteBuffer = true; |
| 117 | return SB; |
| 118 | } |
| 119 | |
| 120 | /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that |
| 121 | /// is completely initialized to zeros. Note that the caller should |
| 122 | /// initialize the memory allocated by this method. The memory is owned by |
| 123 | /// the MemoryBuffer object. |
| 124 | MemoryBuffer *MemoryBuffer::getNewMemBuffer(unsigned Size, |
| 125 | const char *BufferName) { |
| 126 | MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName); |
| 127 | memset(const_cast<char*>(SB->getBufferStart()), 0, Size+1); |
| 128 | return SB; |
| 129 | } |
| 130 | |
| 131 | |
Chris Lattner | 2b1f106 | 2007-11-18 18:52:28 +0000 | [diff] [blame] | 132 | /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin |
| 133 | /// if the Filename is "-". If an error occurs, this returns null and fills |
| 134 | /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN) |
| 135 | /// returns an empty buffer. |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 136 | MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *Filename, |
Chris Lattner | 2b1f106 | 2007-11-18 18:52:28 +0000 | [diff] [blame] | 137 | std::string *ErrStr, |
| 138 | int64_t FileSize) { |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 139 | if (Filename[0] != '-' || Filename[1] != 0) |
| 140 | return getFile(Filename, ErrStr, FileSize); |
Chris Lattner | 2b1f106 | 2007-11-18 18:52:28 +0000 | [diff] [blame] | 141 | MemoryBuffer *M = getSTDIN(); |
| 142 | if (M) return M; |
| 143 | |
| 144 | // If stdin was empty, M is null. Cons up an empty memory buffer now. |
| 145 | const char *EmptyStr = ""; |
| 146 | return MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<stdin>"); |
| 147 | } |
| 148 | |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 149 | //===----------------------------------------------------------------------===// |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 150 | // MemoryBuffer::getFile implementation. |
| 151 | //===----------------------------------------------------------------------===// |
| 152 | |
Chris Lattner | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 153 | namespace { |
| 154 | /// MemoryBufferMMapFile - This represents a file that was mapped in with the |
| 155 | /// sys::Path::MapInFilePages method. When destroyed, it calls the |
| 156 | /// sys::Path::UnMapFilePages method. |
| 157 | class MemoryBufferMMapFile : public MemoryBuffer { |
| 158 | std::string Filename; |
| 159 | public: |
| 160 | MemoryBufferMMapFile(const char *filename, const char *Pages, uint64_t Size) |
| 161 | : Filename(filename) { |
| 162 | init(Pages, Pages+Size); |
| 163 | } |
| 164 | |
| 165 | virtual const char *getBufferIdentifier() const { |
| 166 | return Filename.c_str(); |
| 167 | } |
| 168 | |
| 169 | ~MemoryBufferMMapFile() { |
| 170 | sys::Path::UnMapFilePages(getBufferStart(), getBufferSize()); |
| 171 | } |
| 172 | }; |
| 173 | } |
| 174 | |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 175 | MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr, |
| 176 | int64_t FileSize) { |
Chris Lattner | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 177 | int OpenFlags = 0; |
| 178 | #ifdef O_BINARY |
Bill Wendling | a442006 | 2008-04-01 22:09:20 +0000 | [diff] [blame] | 179 | OpenFlags |= O_BINARY; // Open input file in binary mode on win32. |
Chris Lattner | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 180 | #endif |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 181 | int FD = ::open(Filename, O_RDONLY|OpenFlags); |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 182 | if (FD == -1) { |
Chris Lattner | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 183 | if (ErrStr) *ErrStr = "could not open file"; |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
Chris Lattner | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 187 | // If we don't know the file size, use fstat to find out. fstat on an open |
| 188 | // file descriptor is cheaper than stat on a random path. |
| 189 | if (FileSize == -1) { |
| 190 | struct stat FileInfo; |
| 191 | // TODO: This should use fstat64 when available. |
| 192 | if (fstat(FD, &FileInfo) == -1) { |
| 193 | if (ErrStr) *ErrStr = "could not get file length"; |
| 194 | ::close(FD); |
| 195 | return 0; |
| 196 | } |
| 197 | FileSize = FileInfo.st_size; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | // If the file is large, try to use mmap to read it in. We don't use mmap |
| 202 | // for small files, because this can severely fragment our address space. Also |
| 203 | // don't try to map files that are exactly a multiple of the system page size, |
| 204 | // as the file would not have the required null terminator. |
| 205 | if (FileSize >= 4096*4 && |
| 206 | (FileSize & (sys::Process::GetPageSize()-1)) != 0) { |
| 207 | if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) { |
| 208 | // Close the file descriptor, now that the whole file is in memory. |
| 209 | ::close(FD); |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 210 | return new MemoryBufferMMapFile(Filename, Pages, FileSize); |
Chris Lattner | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | OwningPtr<MemoryBuffer> SB; |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 215 | SB.reset(MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename)); |
Chris Lattner | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 216 | char *BufPtr = const_cast<char*>(SB->getBufferStart()); |
| 217 | |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 218 | unsigned BytesLeft = FileSize; |
| 219 | while (BytesLeft) { |
| 220 | ssize_t NumRead = ::read(FD, BufPtr, BytesLeft); |
| 221 | if (NumRead != -1) { |
| 222 | BytesLeft -= NumRead; |
| 223 | BufPtr += NumRead; |
| 224 | } else if (errno == EINTR) { |
| 225 | // try again |
| 226 | } else { |
| 227 | // error reading. |
| 228 | close(FD); |
Chris Lattner | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 229 | if (ErrStr) *ErrStr = "error reading file data"; |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | } |
| 233 | close(FD); |
| 234 | |
Chris Lattner | 11da4cf | 2008-04-01 06:05:21 +0000 | [diff] [blame] | 235 | return SB.take(); |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 238 | //===----------------------------------------------------------------------===// |
| 239 | // MemoryBuffer::getSTDIN implementation. |
| 240 | //===----------------------------------------------------------------------===// |
| 241 | |
| 242 | namespace { |
| 243 | class STDINBufferFile : public MemoryBuffer { |
| 244 | public: |
| 245 | virtual const char *getBufferIdentifier() const { |
| 246 | return "<stdin>"; |
| 247 | } |
| 248 | }; |
| 249 | } |
| 250 | |
| 251 | MemoryBuffer *MemoryBuffer::getSTDIN() { |
| 252 | char Buffer[4096*4]; |
| 253 | |
| 254 | std::vector<char> FileData; |
| 255 | |
| 256 | // Read in all of the data from stdin, we cannot mmap stdin. |
Jeff Cohen | 0fea8eb | 2007-05-07 15:21:46 +0000 | [diff] [blame] | 257 | sys::Program::ChangeStdinToBinary(); |
Reid Spencer | 2372ccc | 2007-08-08 20:01:58 +0000 | [diff] [blame] | 258 | while (size_t ReadBytes = fread(Buffer, sizeof(char), 4096*4, stdin)) |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 259 | FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes); |
Reid Spencer | 2372ccc | 2007-08-08 20:01:58 +0000 | [diff] [blame] | 260 | |
Nick Lewycky | ea33294 | 2007-07-01 03:06:30 +0000 | [diff] [blame] | 261 | FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end(). |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 262 | size_t Size = FileData.size(); |
Reid Spencer | 2372ccc | 2007-08-08 20:01:58 +0000 | [diff] [blame] | 263 | if (Size <= 1) |
| 264 | return 0; |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 265 | MemoryBuffer *B = new STDINBufferFile(); |
Nick Lewycky | ea33294 | 2007-07-01 03:06:30 +0000 | [diff] [blame] | 266 | B->initCopyOf(&FileData[0], &FileData[Size-1]); |
Chris Lattner | 333ffd4 | 2007-04-29 06:58:52 +0000 | [diff] [blame] | 267 | return B; |
| 268 | } |