diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2010-06-25 16:07:18 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2010-06-25 16:07:18 +0000 |
commit | 831338b320d9059b59564aa82f0703c3aff8c74f (patch) | |
tree | 65d449b0b303613edf1c2c29870ee553ef61fbf7 /lib/Support | |
parent | b447c4e65b5f6d39db16cb8fc338133965291972 (diff) |
Rewrite MemoryBuffer::getSTDIN to use read(2) and a SmallVector buffer.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@106856 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r-- | lib/Support/MemoryBuffer.cpp | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index 22e12e9d34..542162d513 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -272,26 +272,26 @@ MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr, //===----------------------------------------------------------------------===// MemoryBuffer *MemoryBuffer::getSTDIN(std::string *ErrStr) { - char Buffer[4096*4]; - - std::vector<char> FileData; - // Read in all of the data from stdin, we cannot mmap stdin. // // FIXME: That isn't necessarily true, we should try to mmap stdin and // fallback if it fails. sys::Program::ChangeStdinToBinary(); - size_t ReadBytes; - do { - ReadBytes = fread(Buffer, sizeof(char), sizeof(Buffer), stdin); - FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes); - } while (ReadBytes == sizeof(Buffer)); - if (!feof(stdin)) { - if (ErrStr) *ErrStr = "error reading from stdin"; - return 0; - } + const ssize_t ChunkSize = 4096*4; + SmallString<ChunkSize> Buffer; + ssize_t ReadBytes; + // Read into Buffer until we hit EOF. + do { + Buffer.reserve(Buffer.size() + ChunkSize); + ReadBytes = read(0, Buffer.end(), ChunkSize); + if (ReadBytes == -1) { + if (errno == EINTR) continue; + if (ErrStr) *ErrStr = sys::StrError(); + return 0; + } + Buffer.set_size(Buffer.size() + ReadBytes); + } while (ReadBytes != 0); - FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end(). - return getMemBufferCopy(StringRef(&FileData[0],FileData.size()-1), "<stdin>"); + return getMemBufferCopy(Buffer, "<stdin>"); } |