1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MemoryBuffer interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "llvm/ADT/OwningPtr.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/System/Path.h"
18 #include "llvm/System/Process.h"
19 #include "llvm/System/Program.h"
24 #include <sys/types.h>
26 #if !defined(_MSC_VER) && !defined(__MINGW32__)
35 //===----------------------------------------------------------------------===//
36 // MemoryBuffer implementation itself.
37 //===----------------------------------------------------------------------===//
39 MemoryBuffer::~MemoryBuffer() {
41 delete [] BufferStart;
44 /// initCopyOf - Initialize this source buffer with a copy of the specified
45 /// memory range. We make the copy so that we can null terminate it
47 void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) {
48 size_t Size = BufEnd-BufStart;
49 BufferStart = new char[Size+1];
50 BufferEnd = BufferStart+Size;
51 memcpy(const_cast<char*>(BufferStart), BufStart, Size);
52 *const_cast<char*>(BufferEnd) = 0; // Null terminate buffer.
53 MustDeleteBuffer = true;
56 /// init - Initialize this MemoryBuffer as a reference to externally allocated
57 /// memory, memory that we know is already null terminated.
58 void MemoryBuffer::init(const char *BufStart, const char *BufEnd) {
59 assert(BufEnd[0] == 0 && "Buffer is not null terminated!");
60 BufferStart = BufStart;
62 MustDeleteBuffer = false;
65 //===----------------------------------------------------------------------===//
66 // MemoryBufferMem implementation.
67 //===----------------------------------------------------------------------===//
70 class MemoryBufferMem : public MemoryBuffer {
73 MemoryBufferMem(const char *Start, const char *End, const char *FID,
79 initCopyOf(Start, End);
82 virtual const char *getBufferIdentifier() const {
83 return FileID.c_str();
88 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
89 /// that EndPtr[0] must be a null byte and be accessible!
90 MemoryBuffer *MemoryBuffer::getMemBuffer(const char *StartPtr,
92 const char *BufferName) {
93 return new MemoryBufferMem(StartPtr, EndPtr, BufferName);
96 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
97 /// copying the contents and taking ownership of it. This has no requirements
99 MemoryBuffer *MemoryBuffer::getMemBufferCopy(const char *StartPtr,
101 const char *BufferName) {
102 return new MemoryBufferMem(StartPtr, EndPtr, BufferName, true);
105 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
106 /// that is completely initialized to zeros. Note that the caller should
107 /// initialize the memory allocated by this method. The memory is owned by
108 /// the MemoryBuffer object.
109 MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
110 const char *BufferName) {
111 char *Buf = new char[Size+1];
113 MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName);
114 // The memory for this buffer is owned by the MemoryBuffer.
115 SB->MustDeleteBuffer = true;
119 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
120 /// is completely initialized to zeros. Note that the caller should
121 /// initialize the memory allocated by this method. The memory is owned by
122 /// the MemoryBuffer object.
123 MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size,
124 const char *BufferName) {
125 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
126 memset(const_cast<char*>(SB->getBufferStart()), 0, Size+1);
131 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
132 /// if the Filename is "-". If an error occurs, this returns null and fills
133 /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
134 /// returns an empty buffer.
135 MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *Filename,
138 if (Filename[0] != '-' || Filename[1] != 0)
139 return getFile(Filename, ErrStr, FileSize);
140 MemoryBuffer *M = getSTDIN();
143 // If stdin was empty, M is null. Cons up an empty memory buffer now.
144 const char *EmptyStr = "";
145 return MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<stdin>");
148 //===----------------------------------------------------------------------===//
149 // MemoryBuffer::getFile implementation.
150 //===----------------------------------------------------------------------===//
153 /// MemoryBufferMMapFile - This represents a file that was mapped in with the
154 /// sys::Path::MapInFilePages method. When destroyed, it calls the
155 /// sys::Path::UnMapFilePages method.
156 class MemoryBufferMMapFile : public MemoryBuffer {
157 std::string Filename;
159 MemoryBufferMMapFile(const char *filename, const char *Pages, uint64_t Size)
160 : Filename(filename) {
161 init(Pages, Pages+Size);
164 virtual const char *getBufferIdentifier() const {
165 return Filename.c_str();
168 ~MemoryBufferMMapFile() {
169 sys::Path::UnMapFilePages(getBufferStart(), getBufferSize());
174 MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr,
178 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
180 int FD = ::open(Filename, O_RDONLY|OpenFlags);
182 if (ErrStr) *ErrStr = "could not open file";
186 // If we don't know the file size, use fstat to find out. fstat on an open
187 // file descriptor is cheaper than stat on a random path.
188 if (FileSize == -1) {
189 struct stat FileInfo;
190 // TODO: This should use fstat64 when available.
191 if (fstat(FD, &FileInfo) == -1) {
192 if (ErrStr) *ErrStr = "could not get file length";
196 FileSize = FileInfo.st_size;
200 // If the file is large, try to use mmap to read it in. We don't use mmap
201 // for small files, because this can severely fragment our address space. Also
202 // don't try to map files that are exactly a multiple of the system page size,
203 // as the file would not have the required null terminator.
204 if (FileSize >= 4096*4 &&
205 (FileSize & (sys::Process::GetPageSize()-1)) != 0) {
206 if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
207 // Close the file descriptor, now that the whole file is in memory.
209 return new MemoryBufferMMapFile(Filename, Pages, FileSize);
213 OwningPtr<MemoryBuffer> SB;
214 SB.reset(MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename));
215 char *BufPtr = const_cast<char*>(SB->getBufferStart());
217 size_t BytesLeft = FileSize;
219 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
221 BytesLeft -= NumRead;
223 } else if (errno == EINTR) {
228 if (ErrStr) *ErrStr = "error reading file data";
237 //===----------------------------------------------------------------------===//
238 // MemoryBuffer::getSTDIN implementation.
239 //===----------------------------------------------------------------------===//
242 class STDINBufferFile : public MemoryBuffer {
244 virtual const char *getBufferIdentifier() const {
250 MemoryBuffer *MemoryBuffer::getSTDIN() {
253 std::vector<char> FileData;
255 // Read in all of the data from stdin, we cannot mmap stdin.
256 sys::Program::ChangeStdinToBinary();
257 while (size_t ReadBytes = fread(Buffer, sizeof(char), 4096*4, stdin))
258 FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes);
260 FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end().
261 size_t Size = FileData.size();
264 MemoryBuffer *B = new STDINBufferFile();
265 B->initCopyOf(&FileData[0], &FileData[Size-1]);