Fix issue with bitwise and precedence.
[oota-llvm.git] / lib / Support / MemoryBuffer.cpp
1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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/ADT/OwningPtr.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Config/config.h"
18 #include "llvm/Support/MathExtras.h"
19 #include "llvm/Support/Errno.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/Process.h"
22 #include "llvm/Support/Program.h"
23 #include "llvm/Support/system_error.h"
24 #include <cassert>
25 #include <cstdio>
26 #include <cstring>
27 #include <cerrno>
28 #include <new>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #if !defined(_MSC_VER) && !defined(__MINGW32__)
32 #include <unistd.h>
33 #else
34 #include <io.h>
35 #endif
36 #include <fcntl.h>
37 using namespace llvm;
38
39 //===----------------------------------------------------------------------===//
40 // MemoryBuffer implementation itself.
41 //===----------------------------------------------------------------------===//
42
43 MemoryBuffer::~MemoryBuffer() { }
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                         bool RequiresNullTerminator) {
49   assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
50          "Buffer is not null terminated!");
51   BufferStart = BufStart;
52   BufferEnd = BufEnd;
53 }
54
55 //===----------------------------------------------------------------------===//
56 // MemoryBufferMem implementation.
57 //===----------------------------------------------------------------------===//
58
59 /// CopyStringRef - Copies contents of a StringRef into a block of memory and
60 /// null-terminates it.
61 static void CopyStringRef(char *Memory, StringRef Data) {
62   memcpy(Memory, Data.data(), Data.size());
63   Memory[Data.size()] = 0; // Null terminate string.
64 }
65
66 /// GetNamedBuffer - Allocates a new MemoryBuffer with Name copied after it.
67 template <typename T>
68 static T *GetNamedBuffer(StringRef Buffer, StringRef Name,
69                          bool RequiresNullTerminator) {
70   char *Mem = static_cast<char*>(operator new(sizeof(T) + Name.size() + 1));
71   CopyStringRef(Mem + sizeof(T), Name);
72   return new (Mem) T(Buffer, RequiresNullTerminator);
73 }
74
75 namespace {
76 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
77 class MemoryBufferMem : public MemoryBuffer {
78 public:
79   MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
80     init(InputData.begin(), InputData.end(), RequiresNullTerminator);
81   }
82
83   virtual const char *getBufferIdentifier() const {
84      // The name is stored after the class itself.
85     return reinterpret_cast<const char*>(this + 1);
86   }
87   
88   virtual BufferKind getBufferKind() const {
89     return MemoryBuffer_Malloc;
90   }
91 };
92 }
93
94 /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
95 /// that InputData must be a null terminated if RequiresNullTerminator is true!
96 MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData,
97                                          StringRef BufferName,
98                                          bool RequiresNullTerminator) {
99   return GetNamedBuffer<MemoryBufferMem>(InputData, BufferName,
100                                          RequiresNullTerminator);
101 }
102
103 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
104 /// copying the contents and taking ownership of it.  This has no requirements
105 /// on EndPtr[0].
106 MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData,
107                                              StringRef BufferName) {
108   MemoryBuffer *Buf = getNewUninitMemBuffer(InputData.size(), BufferName);
109   if (!Buf) return 0;
110   memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
111          InputData.size());
112   return Buf;
113 }
114
115 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
116 /// that is not initialized.  Note that the caller should initialize the
117 /// memory allocated by this method.  The memory is owned by the MemoryBuffer
118 /// object.
119 MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
120                                                   StringRef BufferName) {
121   // Allocate space for the MemoryBuffer, the data and the name. It is important
122   // that MemoryBuffer and data are aligned so PointerIntPair works with them.
123   size_t AlignedStringLen =
124     RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1,
125                        sizeof(void*)); // TODO: Is sizeof(void*) enough?
126   size_t RealLen = AlignedStringLen + Size + 1;
127   char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
128   if (!Mem) return 0;
129
130   // The name is stored after the class itself.
131   CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName);
132
133   // The buffer begins after the name and must be aligned.
134   char *Buf = Mem + AlignedStringLen;
135   Buf[Size] = 0; // Null terminate buffer.
136
137   return new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
138 }
139
140 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
141 /// is completely initialized to zeros.  Note that the caller should
142 /// initialize the memory allocated by this method.  The memory is owned by
143 /// the MemoryBuffer object.
144 MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
145   MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
146   if (!SB) return 0;
147   memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
148   return SB;
149 }
150
151
152 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
153 /// if the Filename is "-".  If an error occurs, this returns null and fills
154 /// in *ErrStr with a reason.  If stdin is empty, this API (unlike getSTDIN)
155 /// returns an empty buffer.
156 error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
157                                         OwningPtr<MemoryBuffer> &result,
158                                         int64_t FileSize) {
159   if (Filename == "-")
160     return getSTDIN(result);
161   return getFile(Filename, result, FileSize);
162 }
163
164 error_code MemoryBuffer::getFileOrSTDIN(const char *Filename,
165                                         OwningPtr<MemoryBuffer> &result,
166                                         int64_t FileSize) {
167   if (strcmp(Filename, "-") == 0)
168     return getSTDIN(result);
169   return getFile(Filename, result, FileSize);
170 }
171
172 //===----------------------------------------------------------------------===//
173 // MemoryBuffer::getFile implementation.
174 //===----------------------------------------------------------------------===//
175
176 namespace {
177 /// MemoryBufferMMapFile - This represents a file that was mapped in with the
178 /// sys::Path::MapInFilePages method.  When destroyed, it calls the
179 /// sys::Path::UnMapFilePages method.
180 class MemoryBufferMMapFile : public MemoryBufferMem {
181 public:
182   MemoryBufferMMapFile(StringRef Buffer, bool RequiresNullTerminator)
183     : MemoryBufferMem(Buffer, RequiresNullTerminator) { }
184
185   ~MemoryBufferMMapFile() {
186     static int PageSize = sys::Process::GetPageSize();
187
188     uintptr_t Start = reinterpret_cast<uintptr_t>(getBufferStart());
189     size_t Size = getBufferSize();
190     uintptr_t RealStart = Start & ~(PageSize - 1);
191     size_t RealSize = Size + (Start - RealStart);
192
193     sys::Path::UnMapFilePages(reinterpret_cast<const char*>(RealStart),
194                               RealSize);
195   }
196   
197   virtual BufferKind getBufferKind() const {
198     return MemoryBuffer_MMap;
199   }
200 };
201 }
202
203 error_code MemoryBuffer::getFile(StringRef Filename,
204                                  OwningPtr<MemoryBuffer> &result,
205                                  int64_t FileSize,
206                                  bool RequiresNullTerminator) {
207   // Ensure the path is null terminated.
208   SmallString<256> PathBuf(Filename.begin(), Filename.end());
209   return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize,
210                                RequiresNullTerminator);
211 }
212
213 error_code MemoryBuffer::getFile(const char *Filename,
214                                  OwningPtr<MemoryBuffer> &result,
215                                  int64_t FileSize,
216                                  bool RequiresNullTerminator) {
217   int OpenFlags = O_RDONLY;
218 #ifdef O_BINARY
219   OpenFlags |= O_BINARY;  // Open input file in binary mode on win32.
220 #endif
221   int FD = ::open(Filename, OpenFlags);
222   if (FD == -1)
223     return error_code(errno, posix_category());
224
225   error_code ret = getOpenFile(FD, Filename, result, FileSize, FileSize,
226                                0, RequiresNullTerminator);
227   close(FD);
228   return ret;
229 }
230
231 static bool shouldUseMmap(int FD,
232                           size_t FileSize,
233                           size_t MapSize,
234                           off_t Offset,
235                           bool RequiresNullTerminator,
236                           int PageSize) {
237   // We don't use mmap for small files because this can severely fragment our
238   // address space.
239   if (MapSize < 4096*4)
240     return false;
241
242   if (!RequiresNullTerminator)
243     return true;
244
245
246   // If we don't know the file size, use fstat to find out.  fstat on an open
247   // file descriptor is cheaper than stat on a random path.
248   // FIXME: this chunk of code is duplicated, but it avoids a fstat when
249   // RequiresNullTerminator = false and MapSize != -1.
250   if (FileSize == size_t(-1)) {
251     struct stat FileInfo;
252     // TODO: This should use fstat64 when available.
253     if (fstat(FD, &FileInfo) == -1) {
254       return error_code(errno, posix_category());
255     }
256     FileSize = FileInfo.st_size;
257   }
258
259   // If we need a null terminator and the end of the map is inside the file,
260   // we cannot use mmap.
261   size_t End = Offset + MapSize;
262   assert(End <= FileSize);
263   if (End != FileSize)
264     return false;
265
266   // Don't try to map files that are exactly a multiple of the system page size
267   // if we need a null terminator.
268   if ((FileSize & (PageSize -1)) == 0)
269     return false;
270
271   return true;
272 }
273
274 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
275                                      OwningPtr<MemoryBuffer> &result,
276                                      uint64_t FileSize, uint64_t MapSize,
277                                      int64_t Offset,
278                                      bool RequiresNullTerminator) {
279   static int PageSize = sys::Process::GetPageSize();
280
281   // Default is to map the full file.
282   if (MapSize == uint64_t(-1)) {
283     // If we don't know the file size, use fstat to find out.  fstat on an open
284     // file descriptor is cheaper than stat on a random path.
285     if (FileSize == uint64_t(-1)) {
286       struct stat FileInfo;
287       // TODO: This should use fstat64 when available.
288       if (fstat(FD, &FileInfo) == -1) {
289         return error_code(errno, posix_category());
290       }
291       FileSize = FileInfo.st_size;
292     }
293     MapSize = FileSize;
294   }
295
296   if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
297                     PageSize)) {
298     off_t RealMapOffset = Offset & ~(PageSize - 1);
299     off_t Delta = Offset - RealMapOffset;
300     size_t RealMapSize = MapSize + Delta;
301
302     if (const char *Pages = sys::Path::MapInFilePages(FD,
303                                                       RealMapSize,
304                                                       RealMapOffset)) {
305       result.reset(GetNamedBuffer<MemoryBufferMMapFile>(
306           StringRef(Pages + Delta, MapSize), Filename, RequiresNullTerminator));
307       return error_code::success();
308     }
309   }
310
311   MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
312   if (!Buf) {
313     // Failed to create a buffer. The only way it can fail is if
314     // new(std::nothrow) returns 0.
315     return make_error_code(errc::not_enough_memory);
316   }
317
318   OwningPtr<MemoryBuffer> SB(Buf);
319   char *BufPtr = const_cast<char*>(SB->getBufferStart());
320
321   size_t BytesLeft = MapSize;
322 #ifndef HAVE_PREAD
323   if (lseek(FD, Offset, SEEK_SET) == -1)
324     return error_code(errno, posix_category());
325 #endif
326
327   while (BytesLeft) {
328 #ifdef HAVE_PREAD
329     ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
330 #else
331     ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
332 #endif
333     if (NumRead == -1) {
334       if (errno == EINTR)
335         continue;
336       // Error while reading.
337       return error_code(errno, posix_category());
338     }
339     assert(NumRead != 0 && "fstat reported an invalid file size.");
340     BytesLeft -= NumRead;
341     BufPtr += NumRead;
342   }
343
344   result.swap(SB);
345   return error_code::success();
346 }
347
348 //===----------------------------------------------------------------------===//
349 // MemoryBuffer::getSTDIN implementation.
350 //===----------------------------------------------------------------------===//
351
352 error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &result) {
353   // Read in all of the data from stdin, we cannot mmap stdin.
354   //
355   // FIXME: That isn't necessarily true, we should try to mmap stdin and
356   // fallback if it fails.
357   sys::Program::ChangeStdinToBinary();
358
359   const ssize_t ChunkSize = 4096*4;
360   SmallString<ChunkSize> Buffer;
361   ssize_t ReadBytes;
362   // Read into Buffer until we hit EOF.
363   do {
364     Buffer.reserve(Buffer.size() + ChunkSize);
365     ReadBytes = read(0, Buffer.end(), ChunkSize);
366     if (ReadBytes == -1) {
367       if (errno == EINTR) continue;
368       return error_code(errno, posix_category());
369     }
370     Buffer.set_size(Buffer.size() + ReadBytes);
371   } while (ReadBytes != 0);
372
373   result.reset(getMemBufferCopy(Buffer, "<stdin>"));
374   return error_code::success();
375 }