LCOV - code coverage report
Current view: top level - lib/Support - MemoryBuffer.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 235 283 83.0 %
Date: 2018-09-23 13:06:45 Functions: 35 47 74.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13