llvm.org GIT mirror llvm / 0e0979e
formatted_raw_ostream both is-a raw_ostream and has-a raw_ostream. This means that two separate raw_ostreams are doing buffering before data is sent to the underlying stream. Besides the inefficiency of redundant buffering, the second level of buffering doesn't recieve flush() requests. Fix this by having formatted_raw_ostream set the underlying raw_ostream to be unbuffered. This eliminates inefficiency due to redundant buffering, and it makes the flush() disconnect harmless. This fixes PR4559. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75883 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 11 years ago
1 changed file(s) with 24 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
3232 const static bool PRESERVE_STREAM = false;
3333
3434 private:
35 /// TheStream - The real stream we output to.
35 /// TheStream - The real stream we output to. We set it to be
36 /// unbuffered, since we're already doing our own buffering.
3637 ///
3738 raw_ostream *TheStream;
3839 /// DeleteStream - Do we need to delete TheStream in the
6970 /// put into ErrorInfo, and the stream should be immediately
7071 /// destroyed; the string will be empty if no error occurred.
7172 ///
73 /// As a side effect, the given Stream is set to be Unbuffered.
74 /// This is because formatted_raw_ostream does its own buffering,
75 /// so it doesn't want another layer of buffering to be happening
76 /// underneath it.
77 ///
7278 /// \param Filename - The file to open. If this is "-" then the
7379 /// stream will use stdout instead.
7480 /// \param Binary - The file should be opened in binary mode on
7581 /// platforms that support this distinction.
7682 formatted_raw_ostream(raw_ostream &Stream, bool Delete = false)
77 : raw_ostream(), TheStream(&Stream), DeleteStream(Delete), Column(0) {}
78 explicit formatted_raw_ostream()
83 : raw_ostream(), TheStream(&Stream), DeleteStream(Delete), Column(0) {
84 // This formatted_raw_ostream inherits from raw_ostream, so it'll do its
85 // own buffering, and it doesn't need or want TheStream to do another
86 // layer of buffering underneath. Resize the buffer to what TheStream
87 // had been using, and tell TheStream not to do its own buffering.
88 TheStream->flush();
89 if (size_t BufferSize = TheStream->GetNumBytesInBuffer())
90 SetBufferSize(BufferSize);
91 TheStream->SetUnbuffered();
92 }
93 explicit formatted_raw_ostream()
7994 : raw_ostream(), TheStream(0), DeleteStream(false), Column(0) {}
8095
8196 ~formatted_raw_ostream() {
86101 void setStream(raw_ostream &Stream, bool Delete = false) {
87102 TheStream = &Stream;
88103 DeleteStream = Delete;
104
105 // Avoid double-buffering, as above.
106 TheStream->flush();
107 if (size_t BufferSize = TheStream->GetNumBytesInBuffer())
108 SetBufferSize(BufferSize);
109 TheStream->SetUnbuffered();
89110 }
90111
91112 /// PadToColumn - Align the output to some column number.