diff options
author | Dan Gohman <gohman@apple.com> | 2009-08-13 17:27:29 +0000 |
---|---|---|
committer | Dan Gohman <gohman@apple.com> | 2009-08-13 17:27:29 +0000 |
commit | 208ec0f32eed8874074bddf97cd04f60a772198d (patch) | |
tree | 700e35731efccd9e0ccd841ba6a0df5da015d396 /lib/Support/raw_ostream.cpp | |
parent | 25a619f735fad9a2650fb8e42df0906a0fa4ba84 (diff) |
Add support to raw_ostream for sizing the buffer according to the
needs of the underlying output mechanism. raw_fd_ostream now uses
st_blksize from fstat to determine a buffer size.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78923 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/raw_ostream.cpp')
-rw-r--r-- | lib/Support/raw_ostream.cpp | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp index 2a2458d61a..c6d609bfc7 100644 --- a/lib/Support/raw_ostream.cpp +++ b/lib/Support/raw_ostream.cpp @@ -20,6 +20,8 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/ErrorHandling.h" #include <ostream> +#include <sys/stat.h> +#include <sys/types.h> #if defined(HAVE_UNISTD_H) # include <unistd.h> @@ -63,6 +65,16 @@ raw_ostream::~raw_ostream() { // An out of line virtual method to provide a home for the class vtable. void raw_ostream::handle() {} +size_t raw_ostream::preferred_buffer_size() { + // BUFSIZ is intended to be a reasonable default. + return BUFSIZ; +} + +void raw_ostream::SetBuffered() { + // Ask the subclass to determine an appropriate buffer size. + SetBufferSize(preferred_buffer_size()); +} + void raw_ostream::SetBufferSize(size_t Size) { assert(Size >= 64 && "Buffer size must be somewhat large for invariants to hold"); @@ -172,7 +184,7 @@ raw_ostream &raw_ostream::write(unsigned char C) { } if (!OutBufStart) - SetBufferSize(); + SetBuffered(); else flush_nonempty(); } @@ -190,7 +202,7 @@ raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) { return *this; } // Set up a buffer and start over. - SetBufferSize(); + SetBuffered(); return write(Ptr, Size); } // Write out the data in buffer-sized blocks until the remainder @@ -353,6 +365,17 @@ uint64_t raw_fd_ostream::seek(uint64_t off) { return pos; } +size_t raw_fd_ostream::preferred_buffer_size() { +#if !defined(_MSC_VER) // Windows reportedly doesn't have st_blksize. + assert(FD >= 0 && "File not yet open!"); + struct stat statbuf; + if (fstat(FD, &statbuf) == 0) + return statbuf.st_blksize; + error_detected(); +#endif + return raw_ostream::preferred_buffer_size(); +} + raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold, bool bg) { if (sys::Process::ColorNeedsFlush()) |