[llvm-commits] [llvm] r78923 - in /llvm/trunk: include/llvm/Support/raw_ostream.h lib/Support/raw_ostream.cpp

Dan Gohman gohman at apple.com
Thu Aug 13 10:27:29 PDT 2009


Author: djg
Date: Thu Aug 13 12:27:29 2009
New Revision: 78923

URL: http://llvm.org/viewvc/llvm-project?rev=78923&view=rev
Log:
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.

Modified:
    llvm/trunk/include/llvm/Support/raw_ostream.h
    llvm/trunk/lib/Support/raw_ostream.cpp

Modified: llvm/trunk/include/llvm/Support/raw_ostream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/raw_ostream.h?rev=78923&r1=78922&r2=78923&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
+++ llvm/trunk/include/llvm/Support/raw_ostream.h Thu Aug 13 12:27:29 2009
@@ -96,16 +96,26 @@
   // Configuration Interface
   //===--------------------------------------------------------------------===//
 
-  /// SetBufferSize - Set the internal buffer size to the specified amount
-  /// instead of the default.
-  void SetBufferSize(size_t Size=4096);
+  /// SetBuffered - Set the stream to be buffered, with an automatically
+  /// determined buffer size.
+  void SetBuffered();
+
+  /// SetBufferrSize - Set the stream to be buffered, using the
+  /// specified buffer size.
+  void SetBufferSize(size_t Size);
+
+  size_t GetBufferSize() {
+    // If we're supposed to be buffered but haven't actually gotten around
+    // to allocating the buffer yet, return the value that would be used.
+    if (!Unbuffered && !OutBufStart)
+      return preferred_buffer_size();
 
-  size_t GetBufferSize() const {
+    // Otherwise just return the size of the allocated buffer.
     return OutBufEnd - OutBufStart;
   }
 
-  /// SetUnbuffered - Set the streams buffering status. When
-  /// unbuffered the stream will flush after every write. This routine
+  /// SetUnbuffered - Set the stream to be unbuffered. When
+  /// unbuffered, the stream will flush after every write. This routine
   /// will also flush the buffer immediately when the stream is being
   /// set to unbuffered.
   void SetUnbuffered();
@@ -233,6 +243,10 @@
   virtual uint64_t current_pos() = 0;
 
 protected:
+  /// preferred_buffer_size - Return an efficient buffer size for the
+  /// underlying output mechanism.
+  virtual size_t preferred_buffer_size();
+
   /// error_detected - Set the flag indicating that an output error has
   /// been encountered.
   void error_detected() { Error = true; }
@@ -273,6 +287,9 @@
   /// counting the bytes currently in the buffer.
   virtual uint64_t current_pos() { return pos; }
 
+  /// preferred_buffer_size - Determine an efficient buffer size.
+  virtual size_t preferred_buffer_size();
+
 public:
   /// raw_fd_ostream - Open the specified file for writing. If an
   /// error occurs, information about the error is put into ErrorInfo,

Modified: llvm/trunk/lib/Support/raw_ostream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=78923&r1=78922&r2=78923&view=diff

==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Thu Aug 13 12:27:29 2009
@@ -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 @@
 // 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 @@
     }
     
     if (!OutBufStart)
-      SetBufferSize();
+      SetBuffered();
     else
       flush_nonempty();
   }
@@ -190,7 +202,7 @@
         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 @@
   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())





More information about the llvm-commits mailing list