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"
22 /// circular_raw_ostream - A raw_ostream which *can* save its data
23 /// to a circular buffer, or can pass it through directly to an
24 /// underlying stream if specified with a buffer of zero.
26 class circular_raw_ostream : public raw_ostream {
28 /// TAKE_OWNERSHIP - Tell this stream that it owns the underlying
29 /// stream and is responsible for cleanup, memory management
32 static const bool TAKE_OWNERSHIP = true;
34 /// REFERENCE_ONLY - Tell this stream it should not manage the
37 static const bool REFERENCE_ONLY = false;
40 /// TheStream - The real stream we output to. We set it to be
41 /// unbuffered, since we're already doing our own buffering.
43 raw_ostream *TheStream;
45 /// OwnsStream - Are we responsible for managing the underlying
50 /// BufferSize - The size of the buffer in bytes.
54 /// BufferArray - The actual buffer storage.
58 /// Cur - Pointer to the current output point in BufferArray.
62 /// Filled - Indicate whether the buffer has been completely
63 /// filled. This helps avoid garbage output.
67 /// Banner - A pointer to a banner to print before dumping the
72 /// flushBuffer - Dump the contents of the buffer to Stream.
76 // Write the older portion of the buffer.
77 TheStream->write(Cur, BufferArray + BufferSize - Cur);
78 // Write the newer portion of the buffer.
79 TheStream->write(BufferArray, Cur - BufferArray);
84 void write_impl(const char *Ptr, size_t Size) override;
86 /// current_pos - Return the current position within the stream,
87 /// not counting the bytes currently in the buffer.
89 uint64_t current_pos() const override {
90 // This has the same effect as calling TheStream.current_pos(),
91 // but that interface is private.
92 return TheStream->tell() - TheStream->GetNumBytesInBuffer();
96 /// circular_raw_ostream - Construct an optionally
97 /// circular-buffered stream, handing it an underlying stream to
98 /// do the "real" output.
100 /// As a side effect, if BuffSize is nonzero, the given Stream is
101 /// set to be Unbuffered. This is because circular_raw_ostream
102 /// does its own buffering, so it doesn't want another layer of
103 /// buffering to be happening underneath it.
105 /// "Owns" tells the circular_raw_ostream whether it is
106 /// responsible for managing the held stream, doing memory
107 /// management of it, etc.
109 circular_raw_ostream(raw_ostream &Stream, const char *Header,
110 size_t BuffSize = 0, bool Owns = REFERENCE_ONLY)
111 : raw_ostream(/*unbuffered*/ true), TheStream(nullptr),
112 OwnsStream(Owns), BufferSize(BuffSize), BufferArray(nullptr),
113 Filled(false), Banner(Header) {
115 BufferArray = new char[BufferSize];
117 setStream(Stream, Owns);
120 ~circular_raw_ostream() override {
122 flushBufferWithBanner();
124 delete[] BufferArray;
127 /// setStream - Tell the circular_raw_ostream to output a
128 /// different stream. "Owns" tells circular_raw_ostream whether
129 /// it should take responsibility for managing the underlying
132 void setStream(raw_ostream &Stream, bool Owns = REFERENCE_ONLY) {
138 /// flushBufferWithBanner - Force output of the buffer along with
141 void flushBufferWithBanner();
144 /// releaseStream - Delete the held stream if needed. Otherwise,
145 /// transfer the buffer settings from this circular_raw_ostream
146 /// back to the underlying stream.
148 void releaseStream() {
155 } // end llvm namespace