1 //===-- llvm/Support/circular_raw_ostream.h - Buffered streams --*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains raw_ostream implementations for streams to do circular
10 // buffering of their output.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_CIRCULAR_RAW_OSTREAM_H
15 #define LLVM_SUPPORT_CIRCULAR_RAW_OSTREAM_H
17 #include "llvm/Support/raw_ostream.h"
20 /// circular_raw_ostream - A raw_ostream which *can* save its data
21 /// to a circular buffer, or can pass it through directly to an
22 /// underlying stream if specified with a buffer of zero.
24 class circular_raw_ostream
: public raw_ostream
{
26 /// TAKE_OWNERSHIP - Tell this stream that it owns the underlying
27 /// stream and is responsible for cleanup, memory management
30 static const bool TAKE_OWNERSHIP
= true;
32 /// REFERENCE_ONLY - Tell this stream it should not manage the
35 static const bool REFERENCE_ONLY
= false;
38 /// TheStream - The real stream we output to. We set it to be
39 /// unbuffered, since we're already doing our own buffering.
41 raw_ostream
*TheStream
;
43 /// OwnsStream - Are we responsible for managing the underlying
48 /// BufferSize - The size of the buffer in bytes.
52 /// BufferArray - The actual buffer storage.
56 /// Cur - Pointer to the current output point in BufferArray.
60 /// Filled - Indicate whether the buffer has been completely
61 /// filled. This helps avoid garbage output.
65 /// Banner - A pointer to a banner to print before dumping the
70 /// flushBuffer - Dump the contents of the buffer to Stream.
74 // Write the older portion of the buffer.
75 TheStream
->write(Cur
, BufferArray
+ BufferSize
- Cur
);
76 // Write the newer portion of the buffer.
77 TheStream
->write(BufferArray
, Cur
- BufferArray
);
82 void write_impl(const char *Ptr
, size_t Size
) override
;
84 /// current_pos - Return the current position within the stream,
85 /// not counting the bytes currently in the buffer.
87 uint64_t current_pos() const override
{
88 // This has the same effect as calling TheStream.current_pos(),
89 // but that interface is private.
90 return TheStream
->tell() - TheStream
->GetNumBytesInBuffer();
94 /// circular_raw_ostream - Construct an optionally
95 /// circular-buffered stream, handing it an underlying stream to
96 /// do the "real" output.
98 /// As a side effect, if BuffSize is nonzero, the given Stream is
99 /// set to be Unbuffered. This is because circular_raw_ostream
100 /// does its own buffering, so it doesn't want another layer of
101 /// buffering to be happening underneath it.
103 /// "Owns" tells the circular_raw_ostream whether it is
104 /// responsible for managing the held stream, doing memory
105 /// management of it, etc.
107 circular_raw_ostream(raw_ostream
&Stream
, const char *Header
,
108 size_t BuffSize
= 0, bool Owns
= REFERENCE_ONLY
)
109 : raw_ostream(/*unbuffered*/ true), TheStream(nullptr),
110 OwnsStream(Owns
), BufferSize(BuffSize
), BufferArray(nullptr),
111 Filled(false), Banner(Header
) {
113 BufferArray
= new char[BufferSize
];
115 setStream(Stream
, Owns
);
118 ~circular_raw_ostream() override
{
120 flushBufferWithBanner();
122 delete[] BufferArray
;
125 bool is_displayed() const override
{
126 return TheStream
->is_displayed();
129 /// setStream - Tell the circular_raw_ostream to output a
130 /// different stream. "Owns" tells circular_raw_ostream whether
131 /// it should take responsibility for managing the underlying
134 void setStream(raw_ostream
&Stream
, bool Owns
= REFERENCE_ONLY
) {
140 /// flushBufferWithBanner - Force output of the buffer along with
143 void flushBufferWithBanner();
146 /// releaseStream - Delete the held stream if needed. Otherwise,
147 /// transfer the buffer settings from this circular_raw_ostream
148 /// back to the underlying stream.
150 void releaseStream() {
157 } // end llvm namespace