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

Daniel Dunbar daniel at zuster.org
Mon Mar 16 16:29:32 PDT 2009


Author: ddunbar
Date: Mon Mar 16 18:29:31 2009
New Revision: 67060

URL: http://llvm.org/viewvc/llvm-project?rev=67060&view=rev
Log:
raw_ostream: Replace flush_impl with write_impl, which takes data to
write as arguments.
 - Add raw_ostream::GetNumBytesInBuffer.
 - Privatize buffer pointers.
 - Get rid of slow and unnecessary code for writing out large strings.

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=67060&r1=67059&r2=67060&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
+++ llvm/trunk/include/llvm/Support/raw_ostream.h Mon Mar 16 18:29:31 2009
@@ -30,7 +30,7 @@
 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
 /// a chunk at a time.
 class raw_ostream {
-protected:
+private:
   /// \invariant { The buffer is uninitialized (OutBufStart,
   /// OutBufEnd, and OutBufCur are non-zero), or none of them are zero
   /// and there are at least 64 total bytes in the buffer. }
@@ -75,6 +75,10 @@
       flush();
   }
 
+  unsigned GetNumBytesInBuffer() const {
+    return OutBufCur - OutBufStart;
+  }
+
   //===--------------------------------------------------------------------===//
   // Data Output Interface
   //===--------------------------------------------------------------------===//
@@ -147,12 +151,12 @@
   //===--------------------------------------------------------------------===//
 
 private:
-  /// flush_impl - The is the piece of the class that is implemented by
-  /// subclasses.  This only outputs the currently buffered data.
-  ///
-  /// raw_ostream guarantees to only call this routine when there is
-  /// buffered data, i.e. OutBufStart != OutBufCur.
-  virtual void flush_impl() = 0;
+  /// write_impl - The is the piece of the class that is implemented
+  /// by subclasses.  This writes the \args Size bytes starting at
+  /// \arg Ptr to the underlying stream.
+  /// 
+  /// \invariant { Size > 0 }
+  virtual void write_impl(const char *Ptr, unsigned Size) = 0;
 
   // An out of line virtual method to provide a home for the class vtable.
   virtual void handle();
@@ -177,6 +181,9 @@
   int FD;
   bool ShouldClose;
   uint64_t pos;
+
+  /// write_impl - See raw_ostream::write_impl.
+  virtual void write_impl(const char *Ptr, unsigned Size);
 public:
   /// raw_fd_ostream - Open the specified file for writing. If an
   /// error occurs, information about the error is put into ErrorInfo,
@@ -197,20 +204,11 @@
   
   ~raw_fd_ostream();
 
-  /// flush_impl - The is the piece of the class that is implemented by
-  /// subclasses.  This only outputs the currently buffered data.
-  ///
-  /// raw_ostream guarantees to only call this routine when there is
-  /// buffered data, i.e. OutBufStart != OutBufCur.
-  virtual void flush_impl();
-
   /// close - Manually flush the stream and close the file.
   void close();
 
   /// tell - Return the current offset with the file.
-  uint64_t tell() {
-    return pos + (OutBufCur - OutBufStart);
-  }
+  uint64_t tell() { return pos + GetNumBytesInBuffer(); }
 
   /// seek - Flushes the stream and repositions the underlying file descriptor
   ///  positition to the offset specified from the beginning of the file.
@@ -252,22 +250,21 @@
 /// simple adaptor class.
 class raw_os_ostream : public raw_ostream {
   std::ostream &OS;
+
+  /// write_impl - See raw_ostream::write_impl.
+  virtual void write_impl(const char *Ptr, unsigned Size);
 public:
   raw_os_ostream(std::ostream &O) : OS(O) {}
   ~raw_os_ostream();
-
-  /// flush_impl - The is the piece of the class that is implemented by
-  /// subclasses.  This only outputs the currently buffered data.
-  ///
-  /// raw_ostream guarantees to only call this routine when there is
-  /// buffered data, i.e. OutBufStart != OutBufCur.
-  virtual void flush_impl();
 };
 
 /// raw_string_ostream - A raw_ostream that writes to an std::string.  This is a
 /// simple adaptor class.
 class raw_string_ostream : public raw_ostream {
   std::string &OS;
+
+  /// write_impl - See raw_ostream::write_impl.
+  virtual void write_impl(const char *Ptr, unsigned Size);
 public:
   raw_string_ostream(std::string &O) : OS(O) {}
   ~raw_string_ostream();
@@ -278,29 +275,18 @@
     flush();
     return OS;
   }
-
-  /// flush_impl - The is the piece of the class that is implemented by
-  /// subclasses.  This only outputs the currently buffered data.
-  ///
-  /// raw_ostream guarantees to only call this routine when there is
-  /// buffered data, i.e. OutBufStart != OutBufCur.
-  virtual void flush_impl();
 };
 
 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
 /// SmallString.  This is a simple adaptor class.
 class raw_svector_ostream : public raw_ostream {
   SmallVectorImpl<char> &OS;
+
+  /// write_impl - See raw_ostream::write_impl.
+  virtual void write_impl(const char *Ptr, unsigned Size);
 public:
   raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
   ~raw_svector_ostream();
-
-  /// flush_impl - The is the piece of the class that is implemented by
-  /// subclasses.  This only outputs the currently buffered data.
-  ///
-  /// raw_ostream guarantees to only call this routine when there is
-  /// buffered data, i.e. OutBufStart != OutBufCur.
-  virtual void flush_impl();
 };
 
 } // end llvm namespace

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

==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Mon Mar 16 18:29:31 2009
@@ -118,7 +118,7 @@
 
 void raw_ostream::flush_nonempty() {
   assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
-  flush_impl();
+  write_impl(OutBufStart, OutBufCur - OutBufStart);
   OutBufCur = OutBufStart;    
 }
 
@@ -151,20 +151,12 @@
     if (Size <= unsigned(OutBufEnd-OutBufStart)) {
       memcpy(OutBufCur, Ptr, Size);
       break;
-    }
+    } 
 
-    // If emitting a string larger than our buffer, emit in chunks.  In this
-    // case we know that we just flushed the buffer.
-    while (Size) {
-      unsigned NumToEmit = OutBufEnd-OutBufStart;
-      if (Size < NumToEmit) NumToEmit = Size;
-      assert(OutBufCur == OutBufStart);
-      memcpy(OutBufStart, Ptr, NumToEmit);
-      Ptr += NumToEmit;
-      Size -= NumToEmit;
-      OutBufCur = OutBufStart + NumToEmit;
-      flush_nonempty();
-    }
+    // Otherwise we are emitting a string larger than our buffer. We
+    // know we already flushed, so just write it out directly.
+    write_impl(Ptr, Size);
+    Size = 0;
     break;
   }
   OutBufCur += Size;
@@ -268,10 +260,10 @@
   }
 }
 
-void raw_fd_ostream::flush_impl() {
+void raw_fd_ostream::write_impl(const char *Ptr, unsigned Size) {
   assert (FD >= 0 && "File already closed.");
-  pos += (OutBufCur - OutBufStart);
-  ::write(FD, OutBufStart, OutBufCur-OutBufStart);
+  pos += Size;
+  ::write(FD, Ptr, Size);
 }
 
 void raw_fd_ostream::close() {
@@ -322,11 +314,8 @@
   flush();
 }
 
-/// flush_impl - The is the piece of the class that is implemented by
-/// subclasses.  This outputs the currently buffered data and resets the
-/// buffer to empty.
-void raw_os_ostream::flush_impl() {
-  OS.write(OutBufStart, OutBufCur-OutBufStart);
+void raw_os_ostream::write_impl(const char *Ptr, unsigned Size) {
+  OS.write(Ptr, Size);
 }
 
 //===----------------------------------------------------------------------===//
@@ -337,11 +326,8 @@
   flush();
 }
 
-/// flush_impl - The is the piece of the class that is implemented by
-/// subclasses.  This outputs the currently buffered data and resets the
-/// buffer to empty.
-void raw_string_ostream::flush_impl() {
-  OS.append(OutBufStart, OutBufCur-OutBufStart);
+void raw_string_ostream::write_impl(const char *Ptr, unsigned Size) {
+  OS.append(Ptr, Size);
 }
 
 //===----------------------------------------------------------------------===//
@@ -352,10 +338,7 @@
   flush();
 }
 
-/// flush_impl - The is the piece of the class that is implemented by
-/// subclasses.  This outputs the currently buffered data and resets the
-/// buffer to empty.
-void raw_svector_ostream::flush_impl() {
-  OS.append(OutBufStart, OutBufCur);
+void raw_svector_ostream::write_impl(const char *Ptr, unsigned Size) {
+  OS.append(Ptr, Ptr + Size);
 }
 





More information about the llvm-commits mailing list