On Friday 18 December 2009 20:20, David Greene wrote:
> I wanted something to signify in the output where the debug dump starts.
> That's for that case where some stuff might still get output through
> errs(). I anticipate that while I'll change most uses of errs() to
dbgs(),
> we probably don't want to change all of them.
>
> I agree the naming here is poor. I'll fix that.
Here's the next iteration.
-Dave
Index: include/llvm/Support/circular_raw_ostream.h
==================================================================---
include/llvm/Support/circular_raw_ostream.h (revision 0)
+++ include/llvm/Support/circular_raw_ostream.h (revision 0)
@@ -0,0 +1,166 @@
+//===-- llvm/Support/circular_raw_ostream.h - Buffered streams --*- C++
-*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains raw_ostream implementations for streams to do circular
+// buffering of their output.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_CIRCULAR_RAW_OSTREAM_H
+#define LLVM_SUPPORT_CIRCULAR_RAW_OSTREAM_H
+
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm
+{
+ /// circular_raw_ostream - A raw_ostream which *can* save its data
+ /// to a circular buffer, or can pass it through directly to an
+ /// underlying stream if specified with a buffer of zero.
+ ///
+ class circular_raw_ostream : public raw_ostream {
+ public:
+ /// TAKE_OWNERSHIP - Tell this stream that it owns the underlying
+ /// stream and is responsible for cleanup, memory management
+ /// issues, etc.
+ ///
+ static const bool TAKE_OWNERSHIP = true;
+
+ /// REFERENCE_ONLY - Tell this stream it should not manage the
+ /// held stream.
+ ///
+ static const bool REFERENCE_ONLY = false;
+
+ private:
+ /// TheStream - The real stream we output to. We set it to be
+ /// unbuffered, since we're already doing our own buffering.
+ ///
+ raw_ostream *TheStream;
+
+ /// OwnsStream - Are we responsible for managing the underlying
+ /// stream?
+ ///
+ bool OwnsStream;
+
+ /// BufferSize - The size of the buffer in bytes.
+ ///
+ size_t BufferSize;
+
+ /// BufferArray - The actual buffer storage.
+ ///
+ char *BufferArray;
+
+ /// Cur - Pointer to the current output point in BufferArray.
+ ///
+ char *Cur;
+
+ /// flushBuffer - Dump the contents of the buffer to Stream.
+ ///
+ void flushBuffer(void) {
+ // Write the older portion of the buffer.
+ TheStream->write(Cur, BufferArray + BufferSize - Cur);
+ // Write the newer portion of the buffer.
+ TheStream->write(BufferArray, Cur - BufferArray);
+ Cur = BufferArray;
+ }
+
+ virtual void write_impl(const char *Ptr, size_t Size);
+
+ /// current_pos - Return the current position within the stream,
+ /// not counting the bytes currently in the buffer.
+ virtual uint64_t current_pos() const {
+ // This has the same effect as calling TheStream.current_pos(),
+ // but that interface is private.
+ return TheStream->tell() - TheStream->GetNumBytesInBuffer();
+ }
+
+ public:
+ /// circular_raw_ostream - Construct an optionally
+ /// circular-buffered stream, handing it an underlying stream to
+ /// do the "real" output.
+ ///
+ /// As a side effect, if BuffSize is nonzero, the given Stream is
+ /// set to be Unbuffered. This is because circular_raw_ostream
+ /// does its own buffering, so it doesn't want another layer of
+ /// buffering to be happening underneath it.
+ ///
+ /// "Owns" tells the circular_raw_ostream whether it is
+ /// responsible for managing the held stream, doing memory
+ /// management of it, etc.
+ ///
+ circular_raw_ostream(raw_ostream &Stream, size_t BuffSize = 0,
+ bool Owns = REFERENCE_ONLY)
+ : raw_ostream(/*unbuffered*/true),
+ TheStream(0),
+ OwnsStream(Owns),
+ BufferSize(BuffSize),
+ BufferArray(0) {
+ if (BufferSize != 0)
+ BufferArray = new char[BufferSize];
+ Cur = BufferArray;
+ setStream(Stream, Owns);
+ }
+ explicit circular_raw_ostream()
+ : raw_ostream(/*unbuffered*/true),
+ TheStream(0),
+ OwnsStream(REFERENCE_ONLY),
+ BufferArray(0) {
+ Cur = BufferArray;
+ }
+
+ ~circular_raw_ostream() {
+ flush();
+ flushBufferWithBanner();
+ releaseStream();
+ delete[] BufferArray;
+ }
+
+ /// setStream - Tell the circular_raw_ostream to output a
+ /// different stream. "Owns" tells circular_raw_ostream whether
+ /// it should take responsibility for managing the underlying
+ /// stream.
+ ///
+ void setStream(raw_ostream &Stream, bool Owns = REFERENCE_ONLY) {
+ releaseStream();
+
+ TheStream = &Stream;
+ OwnsStream = Owns;
+
+ if (BufferSize != 0) {
+ // This circular_raw_ostream will do its own buffering and it
+ // doesn't need or want TheStream to do another layer of
+ // buffering underneath. Tell TheStream not to do its own
+ // buffering.
+ TheStream->SetUnbuffered();
+ }
+ }
+
+ /// flushBufferWithBanner - Force output of the buffer along with
+ /// a small header.
+ ///
+ void flushBufferWithBanner(void);
+
+ private:
+ /// releaseStream - Delete the held stream if needed. Otherwise,
+ /// transfer the buffer settings from this circular_raw_ostream
+ /// back to the underlying stream.
+ void releaseStream() {
+ if (!TheStream)
+ return;
+ if (OwnsStream)
+ delete TheStream;
+ else if (BufferSize != 0)
+ TheStream->SetBufferSize(BufferSize);
+ else
+ TheStream->SetUnbuffered();
+ }
+ };
+} // end llvm namespace
+
+
+#endif
Index: lib/Support/circular_raw_ostream.cpp
==================================================================---
lib/Support/circular_raw_ostream.cpp (revision 0)
+++ lib/Support/circular_raw_ostream.cpp (revision 0)
@@ -0,0 +1,47 @@
+//===- circulat_raw_ostream.cpp - Implement the circular_raw_ostream class
-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This implements support for circular buffered streams.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/circular_raw_ostream.h"
+
+#include <algorithm>
+
+using namespace llvm;
+
+void circular_raw_ostream::write_impl(const char *Ptr, size_t Size) {
+ if (BufferSize == 0) {
+ TheStream->write(Ptr, Size);
+ return;
+ }
+
+ // Write into the buffer, wrapping if necessary.
+ while (Size != 0) {
+ unsigned Bytes = std::min(Size, BufferSize - (Cur - BufferArray));
+ memcpy(Cur, Ptr, Bytes);
+ Size -= Bytes;
+ Cur += Bytes;
+ if (Cur == BufferArray + BufferSize)
+ // Reset the output pointer to the start of the buffer.
+ Cur = BufferArray;
+ }
+}
+
+void circular_raw_ostream::flushBufferWithBanner(void) {
+ if (BufferSize != 0) {
+ // Write out the buffer
+ const char *msg = "*** Log Output ***\n";
+ int num = std::strlen(msg);
+
+ TheStream->write(msg, num);
+ flushBuffer();
+ }
+}