1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MemoryBuffer interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "llvm/System/MappedFile.h"
16 #include "llvm/System/Process.h"
23 //===----------------------------------------------------------------------===//
24 // MemoryBuffer implementation itself.
25 //===----------------------------------------------------------------------===//
27 MemoryBuffer::~MemoryBuffer() {
29 delete [] BufferStart;
32 /// initCopyOf - Initialize this source buffer with a copy of the specified
33 /// memory range. We make the copy so that we can null terminate it
35 void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) {
36 size_t Size = BufEnd-BufStart;
37 BufferStart = new char[Size+1];
38 BufferEnd = BufferStart+Size;
39 memcpy(const_cast<char*>(BufferStart), BufStart, Size);
40 *const_cast<char*>(BufferEnd) = 0; // Null terminate buffer.
41 MustDeleteBuffer = false;
44 /// init - Initialize this MemoryBuffer as a reference to externally allocated
45 /// memory, memory that we know is already null terminated.
46 void MemoryBuffer::init(const char *BufStart, const char *BufEnd) {
47 assert(BufEnd[0] == 0 && "Buffer is not null terminated!");
48 BufferStart = BufStart;
50 MustDeleteBuffer = false;
53 //===----------------------------------------------------------------------===//
54 // MemoryBufferMem implementation.
55 //===----------------------------------------------------------------------===//
58 class MemoryBufferMem : public MemoryBuffer {
61 MemoryBufferMem(const char *Start, const char *End, const char *FID)
66 virtual const char *getBufferIdentifier() const {
67 return FileID.c_str();
72 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
73 /// that EndPtr[0] must be a null byte and be accessible!
74 MemoryBuffer *MemoryBuffer::getMemBuffer(const char *StartPtr,
76 const char *BufferName) {
77 return new MemoryBufferMem(StartPtr, EndPtr, BufferName);
80 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
81 /// that is completely initialized to zeros. Note that the caller should
82 /// initialize the memory allocated by this method. The memory is owned by
83 /// the MemoryBuffer object.
84 MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(unsigned Size,
85 const char *BufferName) {
86 char *Buf = new char[Size+1];
88 MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName);
89 // The memory for this buffer is owned by the MemoryBuffer.
90 SB->MustDeleteBuffer = true;
94 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
95 /// is completely initialized to zeros. Note that the caller should
96 /// initialize the memory allocated by this method. The memory is owned by
97 /// the MemoryBuffer object.
98 MemoryBuffer *MemoryBuffer::getNewMemBuffer(unsigned Size,
99 const char *BufferName) {
100 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
101 memset(const_cast<char*>(SB->getBufferStart()), 0, Size+1);
106 //===----------------------------------------------------------------------===//
107 // MemoryBufferMMapFile implementation.
108 //===----------------------------------------------------------------------===//
111 class MemoryBufferMMapFile : public MemoryBuffer {
112 sys::MappedFile File;
114 MemoryBufferMMapFile() {}
116 bool open(const sys::Path &Filename, std::string *ErrStr);
118 virtual const char *getBufferIdentifier() const {
119 return File.path().c_str();
122 ~MemoryBufferMMapFile();
126 bool MemoryBufferMMapFile::open(const sys::Path &Filename,
127 std::string *ErrStr) {
128 // FIXME: This does an extra stat syscall to figure out the size, but we
129 // already know the size!
130 bool Failure = File.open(Filename, sys::MappedFile::READ_ACCESS, ErrStr);
131 if (Failure) return true;
133 if (!File.map(ErrStr))
136 size_t Size = File.size();
138 static unsigned PageSize = sys::Process::GetPageSize();
139 assert(((PageSize & (PageSize-1)) == 0) && PageSize &&
140 "Page size is not a power of 2!");
142 // If this file is not an exact multiple of the system page size (common
143 // case), then the OS has zero terminated the buffer for us.
144 if ((Size & (PageSize-1))) {
145 init(File.charBase(), File.charBase()+Size);
147 // Otherwise, we allocate a new memory buffer and copy the data over
148 initCopyOf(File.charBase(), File.charBase()+Size);
150 // No need to keep the file mapped any longer.
156 MemoryBufferMMapFile::~MemoryBufferMMapFile() {
161 //===----------------------------------------------------------------------===//
162 // MemoryBuffer::getFile implementation.
163 //===----------------------------------------------------------------------===//
165 MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
166 std::string *ErrStr, int64_t FileSize){
167 // FIXME: it would be nice if PathWithStatus didn't copy the filename into a
168 // temporary string. :(
169 sys::PathWithStatus P(FilenameStart, FnSize);
171 MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
172 if (!M->open(P, ErrStr))
177 // FIXME: We need an efficient and portable method to open a file and then use
178 // 'read' to copy the bits out. The unix implementation is below. This is
179 // an important optimization for clients that want to open large numbers of
180 // small files (using mmap on everything can easily exhaust address space!).
182 // If the user didn't specify a filesize, do a stat to find it.
183 if (FileSize == -1) {
184 const sys::FileStatus *FS = P.getFileStatus();
185 if (FS == 0) return 0; // Error stat'ing file.
187 FileSize = FS->fileSize;
190 // If the file is larger than some threshold, use mmap, otherwise use 'read'.
191 if (FileSize >= 4096*4) {
192 MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
193 if (!M->open(P, ErrStr))
199 MemoryBuffer *SB = getNewUninitMemBuffer(FileSize, FilenameStart);
200 char *BufPtr = const_cast<char*>(SB->getBufferStart());
202 int FD = ::open(FilenameStart, O_RDONLY);
208 unsigned BytesLeft = FileSize;
210 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
212 BytesLeft -= NumRead;
214 } else if (errno == EINTR) {
230 //===----------------------------------------------------------------------===//
231 // MemoryBuffer::getSTDIN implementation.
232 //===----------------------------------------------------------------------===//
235 class STDINBufferFile : public MemoryBuffer {
237 virtual const char *getBufferIdentifier() const {
243 MemoryBuffer *MemoryBuffer::getSTDIN() {
246 std::vector<char> FileData;
248 // Read in all of the data from stdin, we cannot mmap stdin.
249 while (size_t ReadBytes = fread(Buffer, 1, 4096*4, stdin))
250 FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes);
252 size_t Size = FileData.size();
253 MemoryBuffer *B = new STDINBufferFile();
254 B->initCopyOf(&FileData[0], &FileData[Size]);