1 //===-- llvm/Support/circular_raw_ostream.h - Buffered streams --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains raw_ostream implementations for streams to do circular
11 // buffering of their output.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_CIRCULAR_RAW_OSTREAM_H
16 #define LLVM_SUPPORT_CIRCULAR_RAW_OSTREAM_H
18 #include "llvm/Support/raw_ostream.h"
21 /// circular_raw_ostream - A raw_ostream which *can* save its data
22 /// to a circular buffer, or can pass it through directly to an
23 /// underlying stream if specified with a buffer of zero.
25 class circular_raw_ostream : public raw_ostream {
27 /// TAKE_OWNERSHIP - Tell this stream that it owns the underlying
28 /// stream and is responsible for cleanup, memory management
31 static const bool TAKE_OWNERSHIP = true;
33 /// REFERENCE_ONLY - Tell this stream it should not manage the
36 static const bool REFERENCE_ONLY = false;
39 /// TheStream - The real stream we output to. We set it to be
40 /// unbuffered, since we're already doing our own buffering.
42 raw_ostream *TheStream;
44 /// OwnsStream - Are we responsible for managing the underlying
49 /// BufferSize - The size of the buffer in bytes.
53 /// BufferArray - The actual buffer storage.
57 /// Cur - Pointer to the current output point in BufferArray.
61 /// Filled - Indicate whether the buffer has been completely
62 /// filled. This helps avoid garbage output.
66 /// Banner - A pointer to a banner to print before dumping the
71 /// flushBuffer - Dump the contents of the buffer to Stream.
75 // Write the older portion of the buffer.
76 TheStream->write(Cur, BufferArray + BufferSize - Cur);
77 // Write the newer portion of the buffer.
78 TheStream->write(BufferArray, Cur - BufferArray);
83 void write_impl(const char *Ptr, size_t Size) override;
85 /// current_pos - Return the current position within the stream,
86 /// not counting the bytes currently in the buffer.
88 uint64_t current_pos() const override {
89 // This has the same effect as calling TheStream.current_pos(),
90 // but that interface is private.
91 return TheStream->tell() - TheStream->GetNumBytesInBuffer();
95 /// circular_raw_ostream - Construct an optionally
96 /// circular-buffered stream, handing it an underlying stream to
97 /// do the "real" output.
99 /// As a side effect, if BuffSize is nonzero, the given Stream is
100 /// set to be Unbuffered. This is because circular_raw_ostream
101 /// does its own buffering, so it doesn't want another layer of
102 /// buffering to be happening underneath it.
104 /// "Owns" tells the circular_raw_ostream whether it is
105 /// responsible for managing the held stream, doing memory
106 /// management of it, etc.
108 circular_raw_ostream(raw_ostream &Stream, const char *Header,
109 size_t BuffSize = 0, bool Owns = REFERENCE_ONLY)
110 : raw_ostream(/*unbuffered*/ true), TheStream(nullptr),
111 OwnsStream(Owns), BufferSize(BuffSize), BufferArray(nullptr),
112 Filled(false), Banner(Header) {
114 BufferArray = new char[BufferSize];
116 setStream(Stream, Owns);
119 ~circular_raw_ostream() override {
121 flushBufferWithBanner();
123 delete[] BufferArray;
126 /// setStream - Tell the circular_raw_ostream to output a
127 /// different stream. "Owns" tells circular_raw_ostream whether
128 /// it should take responsibility for managing the underlying
131 void setStream(raw_ostream &Stream, bool Owns = REFERENCE_ONLY) {
137 /// flushBufferWithBanner - Force output of the buffer along with
140 void flushBufferWithBanner();
143 /// releaseStream - Delete the held stream if needed. Otherwise,
144 /// transfer the buffer settings from this circular_raw_ostream
145 /// back to the underlying stream.
147 void releaseStream() {
154 } // end llvm namespace