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"
22 //===----------------------------------------------------------------------===//
23 // MemoryBuffer implementation itself.
24 //===----------------------------------------------------------------------===//
26 MemoryBuffer::~MemoryBuffer() {
28 delete [] BufferStart;
31 /// initCopyOf - Initialize this source buffer with a copy of the specified
32 /// memory range. We make the copy so that we can null terminate it
34 void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) {
35 size_t Size = BufEnd-BufStart;
36 BufferStart = new char[Size+1];
37 BufferEnd = BufferStart+Size;
38 memcpy(const_cast<char*>(BufferStart), BufStart, Size);
39 *const_cast<char*>(BufferEnd) = 0; // Null terminate buffer.
40 MustDeleteBuffer = false;
43 /// init - Initialize this MemoryBuffer as a reference to externally allocated
44 /// memory, memory that we know is already null terminated.
45 void MemoryBuffer::init(const char *BufStart, const char *BufEnd) {
46 assert(BufEnd[0] == 0 && "Buffer is not null terminated!");
47 BufferStart = BufStart;
49 MustDeleteBuffer = false;
52 //===----------------------------------------------------------------------===//
53 // MemoryBufferMem implementation.
54 //===----------------------------------------------------------------------===//
57 class MemoryBufferMem : public MemoryBuffer {
60 MemoryBufferMem(const char *Start, const char *End, const char *FID)
65 virtual const char *getBufferIdentifier() const {
66 return FileID.c_str();
71 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
72 /// that EndPtr[0] must be a null byte and be accessible!
73 MemoryBuffer *MemoryBuffer::getMemBuffer(const char *StartPtr,
75 const char *BufferName) {
76 return new MemoryBufferMem(StartPtr, EndPtr, BufferName);
79 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
80 /// that is completely initialized to zeros. Note that the caller should
81 /// initialize the memory allocated by this method. The memory is owned by
82 /// the MemoryBuffer object.
83 MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(unsigned Size,
84 const char *BufferName) {
85 char *Buf = new char[Size+1];
87 MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName);
88 // The memory for this buffer is owned by the MemoryBuffer.
89 SB->MustDeleteBuffer = true;
93 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
94 /// is completely initialized to zeros. Note that the caller should
95 /// initialize the memory allocated by this method. The memory is owned by
96 /// the MemoryBuffer object.
97 MemoryBuffer *MemoryBuffer::getNewMemBuffer(unsigned Size,
98 const char *BufferName) {
99 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
100 memset(const_cast<char*>(SB->getBufferStart()), 0, Size+1);
105 //===----------------------------------------------------------------------===//
106 // MemoryBufferMMapFile implementation.
107 //===----------------------------------------------------------------------===//
110 class MemoryBufferMMapFile : public MemoryBuffer {
111 sys::MappedFile File;
113 MemoryBufferMMapFile(const sys::Path &Filename);
115 virtual const char *getBufferIdentifier() const {
116 return File.path().c_str();
119 ~MemoryBufferMMapFile();
123 MemoryBufferMMapFile::MemoryBufferMMapFile(const sys::Path &Filename) {
124 // FIXME: This does an extra stat syscall to figure out the size, but we
125 // already know the size!
126 bool Failure = File.open(Filename);
127 Failure = Failure; // Silence warning in no-asserts mode.
128 assert(!Failure && "Can't open file??");
132 size_t Size = File.size();
134 static unsigned PageSize = sys::Process::GetPageSize();
135 assert(((PageSize & (PageSize-1)) == 0) && PageSize &&
136 "Page size is not a power of 2!");
138 // If this file is not an exact multiple of the system page size (common
139 // case), then the OS has zero terminated the buffer for us.
140 if ((Size & (PageSize-1))) {
141 init(File.charBase(), File.charBase()+Size);
143 // Otherwise, we allocate a new memory buffer and copy the data over
144 initCopyOf(File.charBase(), File.charBase()+Size);
146 // No need to keep the file mapped any longer.
151 MemoryBufferMMapFile::~MemoryBufferMMapFile() {
155 //===----------------------------------------------------------------------===//
156 // MemoryBuffer::getFile implementation.
157 //===----------------------------------------------------------------------===//
159 MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
161 sys::PathWithStatus P(FilenameStart, FnSize);
163 return new MemoryBufferMMapFile(P);
165 // FIXME: We need an efficient and portable method to open a file and then use
166 // 'read' to copy the bits out. The unix implementation is below. This is
167 // an important optimization for clients that want to open large numbers of
168 // small files (using mmap on everything can easily exhaust address space!).
170 // If the user didn't specify a filesize, do a stat to find it.
171 if (FileSize == -1) {
172 const sys::FileStatus *FS = P.getFileStatus();
173 if (FS == 0) return 0; // Error stat'ing file.
175 FileSize = FS->fileSize;
178 // If the file is larger than some threshold, use mmap, otherwise use 'read'.
179 if (FileSize >= 4096*4)
180 return new MemoryBufferMMapFile(P);
182 MemoryBuffer *SB = getNewUninitMemBuffer(FileSize, FilenameStart);
183 char *BufPtr = const_cast<char*>(SB->getBufferStart());
185 int FD = ::open(FilenameStart, O_RDONLY);
191 unsigned BytesLeft = FileSize;
193 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
195 BytesLeft -= NumRead;
197 } else if (errno == EINTR) {
213 //===----------------------------------------------------------------------===//
214 // MemoryBuffer::getSTDIN implementation.
215 //===----------------------------------------------------------------------===//
218 class STDINBufferFile : public MemoryBuffer {
220 virtual const char *getBufferIdentifier() const {
226 MemoryBuffer *MemoryBuffer::getSTDIN() {
229 std::vector<char> FileData;
231 // Read in all of the data from stdin, we cannot mmap stdin.
232 while (size_t ReadBytes = fread(Buffer, 1, 4096*4, stdin))
233 FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes);
235 size_t Size = FileData.size();
236 MemoryBuffer *B = new STDINBufferFile();
237 B->initCopyOf(&FileData[0], &FileData[Size]);