OSDN Git Service

[Support] Add WritableMemoryBuffer::getNewMemBuffer
[android-x86/external-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/SmallString.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/Errno.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/Process.h"
23 #include "llvm/Support/Program.h"
24 #include <cassert>
25 #include <cerrno>
26 #include <cstring>
27 #include <new>
28 #include <sys/types.h>
29 #include <system_error>
30 #if !defined(_MSC_VER) && !defined(__MINGW32__)
31 #include <unistd.h>
32 #else
33 #include <io.h>
34 #endif
35 using namespace llvm;
36
37 //===----------------------------------------------------------------------===//
38 // MemoryBuffer implementation itself.
39 //===----------------------------------------------------------------------===//
40
41 MemoryBuffer::~MemoryBuffer() { }
42
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                         bool RequiresNullTerminator) {
47   assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
48          "Buffer is not null terminated!");
49   BufferStart = BufStart;
50   BufferEnd = BufEnd;
51 }
52
53 //===----------------------------------------------------------------------===//
54 // MemoryBufferMem implementation.
55 //===----------------------------------------------------------------------===//
56
57 /// CopyStringRef - Copies contents of a StringRef into a block of memory and
58 /// null-terminates it.
59 static void CopyStringRef(char *Memory, StringRef Data) {
60   if (!Data.empty())
61     memcpy(Memory, Data.data(), Data.size());
62   Memory[Data.size()] = 0; // Null terminate string.
63 }
64
65 namespace {
66 struct NamedBufferAlloc {
67   const Twine &Name;
68   NamedBufferAlloc(const Twine &Name) : Name(Name) {}
69 };
70 }
71
72 void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
73   SmallString<256> NameBuf;
74   StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
75
76   char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1));
77   CopyStringRef(Mem + N, NameRef);
78   return Mem;
79 }
80
81 namespace {
82 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
83 template<typename MB>
84 class MemoryBufferMem : public MB {
85 public:
86   MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
87     MemoryBuffer::init(InputData.begin(), InputData.end(),
88                        RequiresNullTerminator);
89   }
90
91   /// Disable sized deallocation for MemoryBufferMem, because it has
92   /// tail-allocated data.
93   void operator delete(void *p) { ::operator delete(p); }
94
95   StringRef getBufferIdentifier() const override {
96     // The name is stored after the class itself.
97     return StringRef(reinterpret_cast<const char *>(this + 1));
98   }
99
100   MemoryBuffer::BufferKind getBufferKind() const override {
101     return MemoryBuffer::MemoryBuffer_Malloc;
102   }
103 };
104 }
105
106 template <typename MB>
107 static ErrorOr<std::unique_ptr<MB>>
108 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
109            uint64_t Offset, bool RequiresNullTerminator, bool IsVolatile);
110
111 std::unique_ptr<MemoryBuffer>
112 MemoryBuffer::getMemBuffer(StringRef InputData, StringRef BufferName,
113                            bool RequiresNullTerminator) {
114   auto *Ret = new (NamedBufferAlloc(BufferName))
115       MemoryBufferMem<MemoryBuffer>(InputData, RequiresNullTerminator);
116   return std::unique_ptr<MemoryBuffer>(Ret);
117 }
118
119 std::unique_ptr<MemoryBuffer>
120 MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
121   return std::unique_ptr<MemoryBuffer>(getMemBuffer(
122       Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
123 }
124
125 static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
126 getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName) {
127   auto Buf = WritableMemoryBuffer::getNewUninitMemBuffer(InputData.size(), BufferName);
128   if (!Buf)
129     return make_error_code(errc::not_enough_memory);
130   memcpy(Buf->getBufferStart(), InputData.data(), InputData.size());
131   return std::move(Buf);
132 }
133
134 std::unique_ptr<MemoryBuffer>
135 MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
136   auto Buf = getMemBufferCopyImpl(InputData, BufferName);
137   if (Buf)
138     return std::move(*Buf);
139   return nullptr;
140 }
141
142 std::unique_ptr<MemoryBuffer>
143 MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
144   return WritableMemoryBuffer::getNewMemBuffer(Size, BufferName);
145 }
146
147 ErrorOr<std::unique_ptr<MemoryBuffer>>
148 MemoryBuffer::getFileOrSTDIN(const Twine &Filename, int64_t FileSize,
149                              bool RequiresNullTerminator) {
150   SmallString<256> NameBuf;
151   StringRef NameRef = Filename.toStringRef(NameBuf);
152
153   if (NameRef == "-")
154     return getSTDIN();
155   return getFile(Filename, FileSize, RequiresNullTerminator);
156 }
157
158 ErrorOr<std::unique_ptr<MemoryBuffer>>
159 MemoryBuffer::getFileSlice(const Twine &FilePath, uint64_t MapSize, 
160                            uint64_t Offset, bool IsVolatile) {
161   return getFileAux<MemoryBuffer>(FilePath, -1, MapSize, Offset, false,
162                                   IsVolatile);
163 }
164
165 //===----------------------------------------------------------------------===//
166 // MemoryBuffer::getFile implementation.
167 //===----------------------------------------------------------------------===//
168
169 namespace {
170 /// \brief Memory maps a file descriptor using sys::fs::mapped_file_region.
171 ///
172 /// This handles converting the offset into a legal offset on the platform.
173 template<typename MB>
174 class MemoryBufferMMapFile : public MB {
175   sys::fs::mapped_file_region MFR;
176
177   static uint64_t getLegalMapOffset(uint64_t Offset) {
178     return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
179   }
180
181   static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
182     return Len + (Offset - getLegalMapOffset(Offset));
183   }
184
185   const char *getStart(uint64_t Len, uint64_t Offset) {
186     return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
187   }
188
189 public:
190   MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
191                        uint64_t Offset, std::error_code &EC)
192       : MFR(FD,
193             MB::Writable ? sys::fs::mapped_file_region::priv
194                          : sys::fs::mapped_file_region::readonly,
195             getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
196     if (!EC) {
197       const char *Start = getStart(Len, Offset);
198       MemoryBuffer::init(Start, Start + Len, RequiresNullTerminator);
199     }
200   }
201
202   /// Disable sized deallocation for MemoryBufferMMapFile, because it has
203   /// tail-allocated data.
204   void operator delete(void *p) { ::operator delete(p); }
205
206   StringRef getBufferIdentifier() const override {
207     // The name is stored after the class itself.
208     return StringRef(reinterpret_cast<const char *>(this + 1));
209   }
210
211   MemoryBuffer::BufferKind getBufferKind() const override {
212     return MemoryBuffer::MemoryBuffer_MMap;
213   }
214 };
215 }
216
217 static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
218 getMemoryBufferForStream(int FD, const Twine &BufferName) {
219   const ssize_t ChunkSize = 4096*4;
220   SmallString<ChunkSize> Buffer;
221   ssize_t ReadBytes;
222   // Read into Buffer until we hit EOF.
223   do {
224     Buffer.reserve(Buffer.size() + ChunkSize);
225     ReadBytes = sys::RetryAfterSignal(-1, read, FD, Buffer.end(), ChunkSize);
226     if (ReadBytes == -1)
227       return std::error_code(errno, std::generic_category());
228     Buffer.set_size(Buffer.size() + ReadBytes);
229   } while (ReadBytes != 0);
230
231   return getMemBufferCopyImpl(Buffer, BufferName);
232 }
233
234
235 ErrorOr<std::unique_ptr<MemoryBuffer>>
236 MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
237                       bool RequiresNullTerminator, bool IsVolatile) {
238   return getFileAux<MemoryBuffer>(Filename, FileSize, FileSize, 0,
239                                   RequiresNullTerminator, IsVolatile);
240 }
241
242 template <typename MB>
243 static ErrorOr<std::unique_ptr<MB>>
244 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
245                 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
246                 bool IsVolatile);
247
248 template <typename MB>
249 static ErrorOr<std::unique_ptr<MB>>
250 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
251            uint64_t Offset, bool RequiresNullTerminator, bool IsVolatile) {
252   int FD;
253   std::error_code EC = sys::fs::openFileForRead(Filename, FD);
254
255   if (EC)
256     return EC;
257
258   auto Ret = getOpenFileImpl<MB>(FD, Filename, FileSize, MapSize, Offset,
259                                  RequiresNullTerminator, IsVolatile);
260   close(FD);
261   return Ret;
262 }
263
264 ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
265 WritableMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
266                               bool IsVolatile) {
267   return getFileAux<WritableMemoryBuffer>(Filename, FileSize, FileSize, 0,
268                                           /*RequiresNullTerminator*/ false,
269                                           IsVolatile);
270 }
271
272 ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
273 WritableMemoryBuffer::getFileSlice(const Twine &Filename, uint64_t MapSize,
274                                    uint64_t Offset, bool IsVolatile) {
275   return getFileAux<WritableMemoryBuffer>(Filename, -1, MapSize, Offset, false,
276                                           IsVolatile);
277 }
278
279 std::unique_ptr<WritableMemoryBuffer>
280 WritableMemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName) {
281   using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
282   // Allocate space for the MemoryBuffer, the data and the name. It is important
283   // that MemoryBuffer and data are aligned so PointerIntPair works with them.
284   // TODO: Is 16-byte alignment enough?  We copy small object files with large
285   // alignment expectations into this buffer.
286   SmallString<256> NameBuf;
287   StringRef NameRef = BufferName.toStringRef(NameBuf);
288   size_t AlignedStringLen = alignTo(sizeof(MemBuffer) + NameRef.size() + 1, 16);
289   size_t RealLen = AlignedStringLen + Size + 1;
290   char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
291   if (!Mem)
292     return nullptr;
293
294   // The name is stored after the class itself.
295   CopyStringRef(Mem + sizeof(MemBuffer), NameRef);
296
297   // The buffer begins after the name and must be aligned.
298   char *Buf = Mem + AlignedStringLen;
299   Buf[Size] = 0; // Null terminate buffer.
300
301   auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true);
302   return std::unique_ptr<WritableMemoryBuffer>(Ret);
303 }
304
305 std::unique_ptr<WritableMemoryBuffer>
306 WritableMemoryBuffer::getNewMemBuffer(size_t Size, const Twine &BufferName) {
307   auto SB = WritableMemoryBuffer::getNewUninitMemBuffer(Size, BufferName);
308   if (!SB)
309     return nullptr;
310   memset(SB->getBufferStart(), 0, Size);
311   return SB;
312 }
313
314 static bool shouldUseMmap(int FD,
315                           size_t FileSize,
316                           size_t MapSize,
317                           off_t Offset,
318                           bool RequiresNullTerminator,
319                           int PageSize,
320                           bool IsVolatile) {
321   // mmap may leave the buffer without null terminator if the file size changed
322   // by the time the last page is mapped in, so avoid it if the file size is
323   // likely to change.
324   if (IsVolatile)
325     return false;
326
327   // We don't use mmap for small files because this can severely fragment our
328   // address space.
329   if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
330     return false;
331
332   if (!RequiresNullTerminator)
333     return true;
334
335   // If we don't know the file size, use fstat to find out.  fstat on an open
336   // file descriptor is cheaper than stat on a random path.
337   // FIXME: this chunk of code is duplicated, but it avoids a fstat when
338   // RequiresNullTerminator = false and MapSize != -1.
339   if (FileSize == size_t(-1)) {
340     sys::fs::file_status Status;
341     if (sys::fs::status(FD, Status))
342       return false;
343     FileSize = Status.getSize();
344   }
345
346   // If we need a null terminator and the end of the map is inside the file,
347   // we cannot use mmap.
348   size_t End = Offset + MapSize;
349   assert(End <= FileSize);
350   if (End != FileSize)
351     return false;
352
353   // Don't try to map files that are exactly a multiple of the system page size
354   // if we need a null terminator.
355   if ((FileSize & (PageSize -1)) == 0)
356     return false;
357
358 #if defined(__CYGWIN__)
359   // Don't try to map files that are exactly a multiple of the physical page size
360   // if we need a null terminator.
361   // FIXME: We should reorganize again getPageSize() on Win32.
362   if ((FileSize & (4096 - 1)) == 0)
363     return false;
364 #endif
365
366   return true;
367 }
368
369 template <typename MB>
370 static ErrorOr<std::unique_ptr<MB>>
371 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
372                 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
373                 bool IsVolatile) {
374   static int PageSize = sys::Process::getPageSize();
375
376   // Default is to map the full file.
377   if (MapSize == uint64_t(-1)) {
378     // If we don't know the file size, use fstat to find out.  fstat on an open
379     // file descriptor is cheaper than stat on a random path.
380     if (FileSize == uint64_t(-1)) {
381       sys::fs::file_status Status;
382       std::error_code EC = sys::fs::status(FD, Status);
383       if (EC)
384         return EC;
385
386       // If this not a file or a block device (e.g. it's a named pipe
387       // or character device), we can't trust the size. Create the memory
388       // buffer by copying off the stream.
389       sys::fs::file_type Type = Status.type();
390       if (Type != sys::fs::file_type::regular_file &&
391           Type != sys::fs::file_type::block_file)
392         return getMemoryBufferForStream(FD, Filename);
393
394       FileSize = Status.getSize();
395     }
396     MapSize = FileSize;
397   }
398
399   if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
400                     PageSize, IsVolatile)) {
401     std::error_code EC;
402     std::unique_ptr<MB> Result(
403         new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
404             RequiresNullTerminator, FD, MapSize, Offset, EC));
405     if (!EC)
406       return std::move(Result);
407   }
408
409   auto Buf = WritableMemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
410   if (!Buf) {
411     // Failed to create a buffer. The only way it can fail is if
412     // new(std::nothrow) returns 0.
413     return make_error_code(errc::not_enough_memory);
414   }
415
416   char *BufPtr = Buf.get()->getBufferStart();
417
418   size_t BytesLeft = MapSize;
419 #ifndef HAVE_PREAD
420   if (lseek(FD, Offset, SEEK_SET) == -1)
421     return std::error_code(errno, std::generic_category());
422 #endif
423
424   while (BytesLeft) {
425 #ifdef HAVE_PREAD
426     ssize_t NumRead = sys::RetryAfterSignal(-1, ::pread, FD, BufPtr, BytesLeft,
427                                             MapSize - BytesLeft + Offset);
428 #else
429     ssize_t NumRead = sys::RetryAfterSignal(-1, ::read, FD, BufPtr, BytesLeft);
430 #endif
431     if (NumRead == -1) {
432       // Error while reading.
433       return std::error_code(errno, std::generic_category());
434     }
435     if (NumRead == 0) {
436       memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
437       break;
438     }
439     BytesLeft -= NumRead;
440     BufPtr += NumRead;
441   }
442
443   return std::move(Buf);
444 }
445
446 ErrorOr<std::unique_ptr<MemoryBuffer>>
447 MemoryBuffer::getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
448                           bool RequiresNullTerminator, bool IsVolatile) {
449   return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0,
450                          RequiresNullTerminator, IsVolatile);
451 }
452
453 ErrorOr<std::unique_ptr<MemoryBuffer>>
454 MemoryBuffer::getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
455                                int64_t Offset, bool IsVolatile) {
456   assert(MapSize != uint64_t(-1));
457   return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize, Offset, false,
458                                        IsVolatile);
459 }
460
461 ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() {
462   // Read in all of the data from stdin, we cannot mmap stdin.
463   //
464   // FIXME: That isn't necessarily true, we should try to mmap stdin and
465   // fallback if it fails.
466   sys::ChangeStdinToBinary();
467
468   return getMemoryBufferForStream(0, "<stdin>");
469 }
470
471 ErrorOr<std::unique_ptr<MemoryBuffer>>
472 MemoryBuffer::getFileAsStream(const Twine &Filename) {
473   int FD;
474   std::error_code EC = sys::fs::openFileForRead(Filename, FD);
475   if (EC)
476     return EC;
477   ErrorOr<std::unique_ptr<MemoryBuffer>> Ret =
478       getMemoryBufferForStream(FD, Filename);
479   close(FD);
480   return Ret;
481 }
482
483 MemoryBufferRef MemoryBuffer::getMemBufferRef() const {
484   StringRef Data = getBuffer();
485   StringRef Identifier = getBufferIdentifier();
486   return MemoryBufferRef(Data, Identifier);
487 }