llvm.org GIT mirror llvm / d14787e
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79446 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 10 years ago
2 changed file(s) with 13 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
453453 public:
454454 /// Construct a new raw_svector_ostream.
455455 ///
456 /// \arg O - The vector to write to; this *must* have at least 128 bytes of
457 /// free space in it.
456 /// \arg O - The vector to write to; this should generally have at least 128
457 /// bytes free to avoid any extraneous memory overhead.
458458 explicit raw_svector_ostream(SmallVectorImpl &O);
459459 ~raw_svector_ostream();
460
461 /// str - Flushes the stream contents to the target vector and return a
462 /// StringRef for the vector contents.
463 StringRef str();
460464 };
461465
462466 /// raw_null_ostream - A raw_ostream that discards all output.
486486 // and we only need to set the vector size when the data is flushed.
487487
488488 raw_svector_ostream::raw_svector_ostream(SmallVectorImpl &O) : OS(O) {
489 // Set up the initial external buffer. We enforce that the buffer must have at
489 // Set up the initial external buffer. We make sure that the buffer has at
490490 // least 128 bytes free; raw_ostream itself only requires 64, but we want to
491491 // make sure that we don't grow the buffer unnecessarily on destruction (when
492492 // the data is flushed). See the FIXME below.
493 if (OS.capacity() - OS.size() < 128)
494 llvm_report_error("Invalid argument, must have at least 128 bytes free!");
493 OS.reserve(OS.size() + 128);
495494 SetBuffer(OS.end(), OS.capacity() - OS.size());
496495 }
497496
517516 }
518517
519518 uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
519
520 StringRef raw_svector_ostream::str() {
521 flush();
522 return StringRef(OS.begin(), OS.size());
523 }
520524
521525 //===----------------------------------------------------------------------===//
522526 // raw_null_ostream