Add new MemoryBuffer::getMemBufferCopy method.
[oota-llvm.git] / lib / Support / MemoryBuffer.cpp
1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the MemoryBuffer interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "llvm/System/MappedFile.h"
16 #include "llvm/System/Process.h"
17 #include "llvm/System/Program.h"
18 #include <cassert>
19 #include <cstdio>
20 #include <cstring>
21 #include <cerrno>
22 using namespace llvm;
23
24 //===----------------------------------------------------------------------===//
25 // MemoryBuffer implementation itself.
26 //===----------------------------------------------------------------------===//
27
28 MemoryBuffer::~MemoryBuffer() {
29   if (MustDeleteBuffer)
30     delete [] BufferStart;
31 }
32
33 /// initCopyOf - Initialize this source buffer with a copy of the specified
34 /// memory range.  We make the copy so that we can null terminate it
35 /// successfully.
36 void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) {
37   size_t Size = BufEnd-BufStart;
38   BufferStart = new char[Size+1];
39   BufferEnd = BufferStart+Size;
40   memcpy(const_cast<char*>(BufferStart), BufStart, Size);
41   *const_cast<char*>(BufferEnd) = 0;   // Null terminate buffer.
42   MustDeleteBuffer = true;
43 }
44
45 /// init - Initialize this MemoryBuffer as a reference to externally allocated
46 /// memory, memory that we know is already null terminated.
47 void MemoryBuffer::init(const char *BufStart, const char *BufEnd) {
48   assert(BufEnd[0] == 0 && "Buffer is not null terminated!");
49   BufferStart = BufStart;
50   BufferEnd = BufEnd;
51   MustDeleteBuffer = false;
52 }
53
54 //===----------------------------------------------------------------------===//
55 // MemoryBufferMem implementation.
56 //===----------------------------------------------------------------------===//
57
58 namespace {
59 class MemoryBufferMem : public MemoryBuffer {
60   std::string FileID;
61 public:
62   MemoryBufferMem(const char *Start, const char *End, const char *FID,
63                   bool Copy = false)
64   : FileID(FID) {
65     if (!Copy)
66       init(Start, End);
67     else
68       initCopyOf(Start, End);
69   }
70   
71   virtual const char *getBufferIdentifier() const {
72     return FileID.c_str();
73   }
74 };
75 }
76
77 /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
78 /// that EndPtr[0] must be a null byte and be accessible!
79 MemoryBuffer *MemoryBuffer::getMemBuffer(const char *StartPtr, 
80                                          const char *EndPtr,
81                                          const char *BufferName) {
82   return new MemoryBufferMem(StartPtr, EndPtr, BufferName);
83 }
84
85 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
86 /// copying the contents and taking ownership of it.  This has no requirements
87 /// on EndPtr[0].
88 MemoryBuffer *MemoryBuffer::getMemBufferCopy(const char *StartPtr, 
89                                              const char *EndPtr,
90                                              const char *BufferName) {
91   return new MemoryBufferMem(StartPtr, EndPtr, BufferName, true);
92 }
93
94 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
95 /// that 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::getNewUninitMemBuffer(unsigned Size,
99                                                   const char *BufferName) {
100   char *Buf = new char[Size+1];
101   Buf[Size] = 0;
102   MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName);
103   // The memory for this buffer is owned by the MemoryBuffer.
104   SB->MustDeleteBuffer = true;
105   return SB;
106 }
107
108 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
109 /// is completely initialized to zeros.  Note that the caller should
110 /// initialize the memory allocated by this method.  The memory is owned by
111 /// the MemoryBuffer object.
112 MemoryBuffer *MemoryBuffer::getNewMemBuffer(unsigned Size,
113                                             const char *BufferName) {
114   MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
115   memset(const_cast<char*>(SB->getBufferStart()), 0, Size+1);
116   return SB;
117 }
118
119
120 //===----------------------------------------------------------------------===//
121 // MemoryBufferMMapFile implementation.
122 //===----------------------------------------------------------------------===//
123
124 namespace {
125 class MemoryBufferMMapFile : public MemoryBuffer {
126   sys::MappedFile File;
127 public:
128   MemoryBufferMMapFile() {}
129   
130   bool open(const sys::Path &Filename, std::string *ErrStr);
131   
132   virtual const char *getBufferIdentifier() const {
133     return File.path().c_str();
134   }
135     
136   ~MemoryBufferMMapFile();
137 };
138 }
139
140 bool MemoryBufferMMapFile::open(const sys::Path &Filename,
141                                 std::string *ErrStr) {
142   // FIXME: This does an extra stat syscall to figure out the size, but we
143   // already know the size!
144   bool Failure = File.open(Filename, sys::MappedFile::READ_ACCESS, ErrStr);
145   if (Failure) return true;
146   
147   if (!File.map(ErrStr))
148     return true;
149   
150   size_t Size = File.size();
151   
152   static unsigned PageSize = sys::Process::GetPageSize();
153   assert(((PageSize & (PageSize-1)) == 0) && PageSize &&
154          "Page size is not a power of 2!");
155   
156   // If this file is not an exact multiple of the system page size (common
157   // case), then the OS has zero terminated the buffer for us.
158   if ((Size & (PageSize-1))) {
159     init(File.charBase(), File.charBase()+Size);
160   } else {
161     // Otherwise, we allocate a new memory buffer and copy the data over
162     initCopyOf(File.charBase(), File.charBase()+Size);
163     
164     // No need to keep the file mapped any longer.
165     File.unmap();
166   }
167   return false;
168 }
169
170 MemoryBufferMMapFile::~MemoryBufferMMapFile() {
171   if (File.isMapped())
172     File.unmap();
173 }
174
175 //===----------------------------------------------------------------------===//
176 // MemoryBuffer::getFile implementation.
177 //===----------------------------------------------------------------------===//
178
179 MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
180                                     std::string *ErrStr, int64_t FileSize){
181   // FIXME: it would be nice if PathWithStatus didn't copy the filename into a
182   // temporary string. :(
183   sys::PathWithStatus P(FilenameStart, FnSize);
184 #if 1
185   MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
186   if (!M->open(P, ErrStr))
187     return M;
188   delete M;
189   return 0;
190 #else
191   // FIXME: We need an efficient and portable method to open a file and then use
192   // 'read' to copy the bits out.  The unix implementation is below.  This is
193   // an important optimization for clients that want to open large numbers of
194   // small files (using mmap on everything can easily exhaust address space!).
195   
196   // If the user didn't specify a filesize, do a stat to find it.
197   if (FileSize == -1) {
198     const sys::FileStatus *FS = P.getFileStatus();
199     if (FS == 0) return 0;  // Error stat'ing file.
200    
201     FileSize = FS->fileSize;
202   }
203   
204   // If the file is larger than some threshold, use mmap, otherwise use 'read'.
205   if (FileSize >= 4096*4) {
206     MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
207     if (!M->open(P, ErrStr))
208       return M;
209     delete M;
210     return 0;
211   }
212   
213   MemoryBuffer *SB = getNewUninitMemBuffer(FileSize, FilenameStart);
214   char *BufPtr = const_cast<char*>(SB->getBufferStart());
215   
216   int FD = ::open(FilenameStart, O_RDONLY);
217   if (FD == -1) {
218     delete SB;
219     return 0;
220   }
221   
222   unsigned BytesLeft = FileSize;
223   while (BytesLeft) {
224     ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
225     if (NumRead != -1) {
226       BytesLeft -= NumRead;
227       BufPtr += NumRead;
228     } else if (errno == EINTR) {
229       // try again
230     } else {
231       // error reading.
232       close(FD);
233       delete SB;
234       return 0;
235     }
236   }
237   close(FD);
238   
239   return SB;
240 #endif
241 }
242
243
244 //===----------------------------------------------------------------------===//
245 // MemoryBuffer::getSTDIN implementation.
246 //===----------------------------------------------------------------------===//
247
248 namespace {
249 class STDINBufferFile : public MemoryBuffer {
250 public:
251   virtual const char *getBufferIdentifier() const {
252     return "<stdin>";
253   }
254 };
255 }
256
257 MemoryBuffer *MemoryBuffer::getSTDIN() {
258   char Buffer[4096*4];
259   
260   std::vector<char> FileData;
261   
262   // Read in all of the data from stdin, we cannot mmap stdin.
263   sys::Program::ChangeStdinToBinary();
264   while (size_t ReadBytes = fread(Buffer, sizeof(char), 4096*4, stdin))
265     FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes);
266
267   FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end().
268   size_t Size = FileData.size();
269   if (Size <= 1)
270     return 0;
271   MemoryBuffer *B = new STDINBufferFile();
272   B->initCopyOf(&FileData[0], &FileData[Size-1]);
273   return B;
274 }