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

Daniel Dunbar daniel at zuster.org
Wed Aug 19 11:40:58 PDT 2009


Author: ddunbar
Date: Wed Aug 19 13:40:58 2009
New Revision: 79446

URL: http://llvm.org/viewvc/llvm-project?rev=79446&view=rev
Log:
Change raw_svector_ostream to reserve the input buffer if necessary, Ted was
right.
 - This class turns out to be much more convenient to use if we do this; clients
   can make sure the buffer is always big enough if they care (since our current
   idiom tends to be to use a SmallString<256> for the input to this we should
   generally be avoiding an unnecessary malloc).

Also, add a convenience raw_svector_ostream::str method which flushes the buffer
and returns a StringRef for the vector contents.

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=79446&r1=79445&r2=79446&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
+++ llvm/trunk/include/llvm/Support/raw_ostream.h Wed Aug 19 13:40:58 2009
@@ -454,10 +454,14 @@
 public:
   /// Construct a new raw_svector_ostream.
   ///
-  /// \arg O - The vector to write to; this *must* have at least 128 bytes of
-  /// free space in it.
+  /// \arg O - The vector to write to; this should generally have at least 128
+  /// bytes free to avoid any extraneous memory overhead.
   explicit raw_svector_ostream(SmallVectorImpl<char> &O);
   ~raw_svector_ostream();
+
+  /// str - Flushes the stream contents to the target vector and return a
+  /// StringRef for the vector contents.
+  StringRef str();
 };
 
 /// raw_null_ostream - A raw_ostream that discards all output.

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

==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Wed Aug 19 13:40:58 2009
@@ -487,12 +487,11 @@
 // and we only need to set the vector size when the data is flushed.
 
 raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
-  // Set up the initial external buffer. We enforce that the buffer must have at
+  // Set up the initial external buffer. We make sure that the buffer has at
   // least 128 bytes free; raw_ostream itself only requires 64, but we want to
   // make sure that we don't grow the buffer unnecessarily on destruction (when
   // the data is flushed). See the FIXME below.
-  if (OS.capacity() - OS.size() < 128)
-    llvm_report_error("Invalid argument, must have at least 128 bytes free!");
+  OS.reserve(OS.size() + 128);
   SetBuffer(OS.end(), OS.capacity() - OS.size());
 }
 
@@ -519,6 +518,11 @@
 
 uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
 
+StringRef raw_svector_ostream::str() {
+  flush();
+  return StringRef(OS.begin(), OS.size());
+}
+
 //===----------------------------------------------------------------------===//
 //  raw_null_ostream
 //===----------------------------------------------------------------------===//





More information about the llvm-commits mailing list