|
|
|||
File indexing completed on 2026-05-10 08:44:28
0001 //===-- llvm/Support/circular_raw_ostream.h - Buffered streams --*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // This file contains raw_ostream implementations for streams to do circular 0010 // buffering of their output. 0011 // 0012 //===----------------------------------------------------------------------===// 0013 0014 #ifndef LLVM_SUPPORT_CIRCULAR_RAW_OSTREAM_H 0015 #define LLVM_SUPPORT_CIRCULAR_RAW_OSTREAM_H 0016 0017 #include "llvm/Support/raw_ostream.h" 0018 0019 namespace llvm { 0020 /// circular_raw_ostream - A raw_ostream which *can* save its data 0021 /// to a circular buffer, or can pass it through directly to an 0022 /// underlying stream if specified with a buffer of zero. 0023 /// 0024 class circular_raw_ostream : public raw_ostream { 0025 public: 0026 /// TAKE_OWNERSHIP - Tell this stream that it owns the underlying 0027 /// stream and is responsible for cleanup, memory management 0028 /// issues, etc. 0029 /// 0030 static constexpr bool TAKE_OWNERSHIP = true; 0031 0032 /// REFERENCE_ONLY - Tell this stream it should not manage the 0033 /// held stream. 0034 /// 0035 static constexpr bool REFERENCE_ONLY = false; 0036 0037 private: 0038 /// TheStream - The real stream we output to. We set it to be 0039 /// unbuffered, since we're already doing our own buffering. 0040 /// 0041 raw_ostream *TheStream = nullptr; 0042 0043 /// OwnsStream - Are we responsible for managing the underlying 0044 /// stream? 0045 /// 0046 bool OwnsStream; 0047 0048 /// BufferSize - The size of the buffer in bytes. 0049 /// 0050 size_t BufferSize; 0051 0052 /// BufferArray - The actual buffer storage. 0053 /// 0054 char *BufferArray = nullptr; 0055 0056 /// Cur - Pointer to the current output point in BufferArray. 0057 /// 0058 char *Cur; 0059 0060 /// Filled - Indicate whether the buffer has been completely 0061 /// filled. This helps avoid garbage output. 0062 /// 0063 bool Filled = false; 0064 0065 /// Banner - A pointer to a banner to print before dumping the 0066 /// log. 0067 /// 0068 const char *Banner; 0069 0070 /// flushBuffer - Dump the contents of the buffer to Stream. 0071 /// 0072 void flushBuffer() { 0073 if (Filled) 0074 // Write the older portion of the buffer. 0075 TheStream->write(Cur, BufferArray + BufferSize - Cur); 0076 // Write the newer portion of the buffer. 0077 TheStream->write(BufferArray, Cur - BufferArray); 0078 Cur = BufferArray; 0079 Filled = false; 0080 } 0081 0082 void write_impl(const char *Ptr, size_t Size) override; 0083 0084 /// current_pos - Return the current position within the stream, 0085 /// not counting the bytes currently in the buffer. 0086 /// 0087 uint64_t current_pos() const override { 0088 // This has the same effect as calling TheStream.current_pos(), 0089 // but that interface is private. 0090 return TheStream->tell() - TheStream->GetNumBytesInBuffer(); 0091 } 0092 0093 public: 0094 /// circular_raw_ostream - Construct an optionally 0095 /// circular-buffered stream, handing it an underlying stream to 0096 /// do the "real" output. 0097 /// 0098 /// As a side effect, if BuffSize is nonzero, the given Stream is 0099 /// set to be Unbuffered. This is because circular_raw_ostream 0100 /// does its own buffering, so it doesn't want another layer of 0101 /// buffering to be happening underneath it. 0102 /// 0103 /// "Owns" tells the circular_raw_ostream whether it is 0104 /// responsible for managing the held stream, doing memory 0105 /// management of it, etc. 0106 /// 0107 circular_raw_ostream(raw_ostream &Stream, const char *Header, 0108 size_t BuffSize = 0, bool Owns = REFERENCE_ONLY) 0109 : raw_ostream(/*unbuffered*/ true), OwnsStream(Owns), 0110 BufferSize(BuffSize), Banner(Header) { 0111 if (BufferSize != 0) 0112 BufferArray = new char[BufferSize]; 0113 Cur = BufferArray; 0114 setStream(Stream, Owns); 0115 } 0116 0117 ~circular_raw_ostream() override { 0118 flush(); 0119 flushBufferWithBanner(); 0120 releaseStream(); 0121 delete[] BufferArray; 0122 } 0123 0124 bool is_displayed() const override { 0125 return TheStream->is_displayed(); 0126 } 0127 0128 /// setStream - Tell the circular_raw_ostream to output a 0129 /// different stream. "Owns" tells circular_raw_ostream whether 0130 /// it should take responsibility for managing the underlying 0131 /// stream. 0132 /// 0133 void setStream(raw_ostream &Stream, bool Owns = REFERENCE_ONLY) { 0134 releaseStream(); 0135 TheStream = &Stream; 0136 OwnsStream = Owns; 0137 } 0138 0139 /// flushBufferWithBanner - Force output of the buffer along with 0140 /// a small header. 0141 /// 0142 void flushBufferWithBanner(); 0143 0144 private: 0145 /// releaseStream - Delete the held stream if needed. Otherwise, 0146 /// transfer the buffer settings from this circular_raw_ostream 0147 /// back to the underlying stream. 0148 /// 0149 void releaseStream() { 0150 if (!TheStream) 0151 return; 0152 if (OwnsStream) 0153 delete TheStream; 0154 } 0155 }; 0156 } // end llvm namespace 0157 0158 #endif
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|