[llvm-commits] [llvm] r106856 - /llvm/trunk/lib/Support/MemoryBuffer.cpp
Benjamin Kramer
benny.kra at googlemail.com
Fri Jun 25 09:07:18 PDT 2010
Author: d0k
Date: Fri Jun 25 11:07:18 2010
New Revision: 106856
URL: http://llvm.org/viewvc/llvm-project?rev=106856&view=rev
Log:
Rewrite MemoryBuffer::getSTDIN to use read(2) and a SmallVector buffer.
Modified:
llvm/trunk/lib/Support/MemoryBuffer.cpp
Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=106856&r1=106855&r2=106856&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Fri Jun 25 11:07:18 2010
@@ -272,26 +272,26 @@
//===----------------------------------------------------------------------===//
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>");
}
More information about the llvm-commits
mailing list